xref: /openbsd-src/regress/lib/libpthread/preemption_float/preemption_float.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: preemption_float.c,v 1.1.1.1 2001/08/15 14:37:12 fgsch 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 void *log_loop (void *x) {
51   int i;
52   double d, d1, d2;
53   /* sleep (1); */
54   for (i = 0; i < limit; i++) {
55     d = 42.0;
56     d = log (exp (d));
57     d = (d + 39.0) / d;
58     if (i == 0)
59       d1 = d;
60     else {
61 		d2 = d;
62 		d = sin(d);
63 		/* if (d2 != d1) { */
64 		if (memcmp (&d2, &d1, 8)) {
65 			pthread_exit(&float_failed);
66 		}
67 	}
68   }
69   pthread_exit(&float_passed);
70 }
71 
72 void *trig_loop (void *x) {
73   int i;
74   double d, d1, d2;
75   /* sleep (1);  */
76   for (i = 0; i < limit; i++) {
77     d = 35.0;
78     d *= M_PI;
79     d /= M_LN2;
80     d = sin (d);
81     d = cos (1 / d);
82     if (i == 0)
83       d1 = d;
84     else {
85 		d2 = d;
86 		d = sin(d);
87 		/* if (d2 != d1) { */
88 		if (memcmp (&d2, &d1, 8)) {
89   			pthread_exit(&float_failed);
90 		}
91 	}
92   }
93   pthread_exit(&float_passed);
94 }
95 
96 int
97 floatloop(pthread_attr_t *attrp)
98 {
99 	pthread_t thread[2];
100 	int *x, *y;
101 
102 	CHECKr(pthread_create (&thread[0], attrp, trig_loop, 0));
103 	CHECKr(pthread_create (&thread[1], attrp, log_loop, 0));
104 	CHECKr(pthread_join(thread[0], (void **) &x));
105 	CHECKr(pthread_join(thread[1], (void **) &y));
106 
107 	/* Return 0 for success */
108 	return ((*y == float_failed)?2:0) |
109 	       ((*x == float_failed)?1:0);
110 }
111 
112 #define N 10
113 int
114 main()
115 {
116 	pthread_attr_t attr;
117 	int i;
118 
119 	/* Try with float point state not preserved */
120 
121 	CHECKr(pthread_attr_init(&attr));
122 	CHECKr(pthread_attr_setfloatstate(&attr, PTHREAD_NOFLOAT));
123 
124 	for(limit = 2; limit < 100000; limit *=4)
125 		if (floatloop(&attr) != 0)
126 			break;
127 
128 	if (limit >= 100000) {
129 		printf("results are INDETERMINATE\n");
130 		SUCCEED; /* XXX */
131 	}
132 
133 	limit *= 4;  /* just to make sure */
134 
135 	printf("using limit = %d\n", limit);
136 
137 	for (i = 0; i < 32; i++) {
138 		/* Try the failure mode one more time. */
139 		if (floatloop(&attr) == 0) {
140 			printf("%d ", i);
141 			fflush(stdout);
142 		}
143 		/* Now see if saving float state will get rid of failure. */
144 		ASSERT(floatloop(NULL) == 0);
145 	}
146 
147 	SUCCEED;
148 }
149