xref: /openbsd-src/regress/lib/libpthread/preemption_float/preemption_float.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: preemption_float.c,v 1.2 2002/06/23 20:21:22 marc 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(void)
98 {
99 	pthread_t thread[2];
100 	int *x, *y;
101 
102 	CHECKr(pthread_create (&thread[0], NULL, trig_loop, NULL));
103 	CHECKr(pthread_create (&thread[1], NULL, log_loop, NULL));
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 int
113 main()
114 {
115 	pthread_t thread;
116 	int *result;
117 
118 	/* single active thread, trig test */
119 	for(limit = 2; limit < 100000; limit *=4) {
120 		CHECKr(pthread_create (&thread, NULL, trig_loop, NULL));
121 		CHECKr(pthread_join(thread, (void **) &result));
122 		ASSERT(*result == 0);
123 	}
124 
125 	/* single active thread, log test */
126 	for(limit = 2; limit < 100000; limit *=4) {
127 		CHECKr(pthread_create (&thread, NULL, log_loop, NULL));
128 		CHECKr(pthread_join(thread, (void **) &result));
129 		ASSERT(*result == 0);
130 	}
131 
132 	/* run both threads concurrently using a higher limit */
133 	limit *= 4;
134 	ASSERT(floatloop() == 0);
135 	SUCCEED;
136 }
137