1 /* $OpenBSD: preemption_float.c,v 1.5 2010/12/26 13:50:20 miod Exp $ */
2 /*
3 * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4 * proven@mit.edu All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Chris Provenzano,
17 * the University of California, Berkeley, and contributors.
18 * 4. Neither the name of Chris Provenzano, the University, nor the names of
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* Test to see if floating point state is being properly maintained
36 for each thread. Different threads doing floating point operations
37 simultaneously should not interfere with one another. This
38 includes operations that might change some FPU flags, such as
39 rounding modes, at least implicitly. */
40
41 #include <pthread.h>
42 #include <math.h>
43 #include <stdio.h>
44 #include "test.h"
45
46 int limit = 2;
47 int float_passed = 0;
48 int float_failed = 1;
49
50 static void *
log_loop(void * x)51 log_loop (void *x) {
52 int i;
53 double d, d1, d2;
54 /* sleep (1); */
55 for (i = 0; i < limit; i++) {
56 d = 42.0;
57 d = log (exp (d));
58 d = (d + 39.0) / d;
59 if (i == 0)
60 d1 = d;
61 else {
62 d2 = d;
63 d = sin(d);
64 /* if (d2 != d1) { */
65 if (memcmp (&d2, &d1, sizeof(double))) {
66 printf("log loop: %f != %f\n", d1, d2);
67 pthread_exit(&float_failed);
68 }
69 }
70 }
71 pthread_exit(&float_passed);
72 }
73
74 static void *
trig_loop(void * x)75 trig_loop (void *x) {
76 int i;
77 double d, d1, d2;
78 /* sleep (1); */
79 for (i = 0; i < limit; i++) {
80 d = 35.0;
81 d *= M_PI;
82 d /= M_LN2;
83 d = sin (d);
84 d = cos (1 / d);
85 if (i == 0)
86 d1 = d;
87 else {
88 d2 = d;
89 d = sin(d);
90 /* if (d2 != d1) { */
91 if (memcmp (&d2, &d1, sizeof(double))) {
92 printf("trig loop: %f != %f\n", d1, d2);
93 pthread_exit(&float_failed);
94 }
95 }
96 }
97 pthread_exit(&float_passed);
98 }
99
100 static int
floatloop(void)101 floatloop(void)
102 {
103 pthread_t thread[2];
104 int *x, *y;
105
106 CHECKr(pthread_create (&thread[0], NULL, trig_loop, NULL));
107 CHECKr(pthread_create (&thread[1], NULL, log_loop, NULL));
108 CHECKr(pthread_join(thread[0], (void **) &x));
109 CHECKr(pthread_join(thread[1], (void **) &y));
110
111 /* Return 0 for success */
112 return ((*y == float_failed)?2:0) |
113 ((*x == float_failed)?1:0);
114 }
115
116 int
main(int argc,char * argv[])117 main(int argc, char *argv[])
118 {
119 pthread_t thread;
120 int *result;
121
122 /* single active thread, trig test */
123 for(limit = 2; limit < 100000; limit *=4) {
124 CHECKr(pthread_create (&thread, NULL, trig_loop, NULL));
125 CHECKr(pthread_join(thread, (void **) &result));
126 ASSERT(*result == 0);
127 }
128
129 /* single active thread, log test */
130 for(limit = 2; limit < 100000; limit *=4) {
131 CHECKr(pthread_create (&thread, NULL, log_loop, NULL));
132 CHECKr(pthread_join(thread, (void **) &result));
133 ASSERT(*result == 0);
134 }
135
136 /* run both threads concurrently using a higher limit */
137 limit *= 4;
138 ASSERT(floatloop() == 0);
139 SUCCEED;
140 }
141