xref: /freebsd/contrib/netbsd-tests/lib/libpthread/t_fpu.c (revision 4acf8d706e36dba0a51abf45def6c31c3fe7bac5)
1 /* $NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $");
33 
34 /*
35  * This is adapted from part of csw/cstest of the MPD implementation by
36  * the University of Arizona CS department (http://www.cs.arizona.edu/sr/)
37  * which is in the public domain:
38  *
39  * "The MPD system is in the public domain and you may use and distribute it
40  *  as you wish.  We ask that you retain credits referencing the University
41  *  of Arizona and that you identify any changes you make.
42  *
43  *  We can't provide a warranty with MPD; it's up to you to determine its
44  *  suitability and reliability for your needs.  We would like to hear of
45  *  any problems you encounter but we cannot promise a timely correction."
46  *
47  * It was changed to use pthread_create() and sched_yield() instead of
48  * the internal MPD context switching primitives by Ignatios Souvatzis
49  * <is@netbsd.org>.
50  */
51 
52 #include <errno.h>
53 #include <math.h>
54 #include <pthread.h>
55 #include <sched.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 
61 #include <atf-c.h>
62 
63 #ifdef	__FreeBSD__
64 #include <errno.h>
65 #include <string.h>
66 #endif
67 
68 #include "h_common.h"
69 
70 #define N_RECURSE 10
71 
72 static void recurse(void);
73 
74 int recursion_depth = 0;
75 pthread_mutex_t recursion_depth_lock;
76 
77 static void *
78 stir(void *p)
79 {
80 	double *q = (double *)p;
81 	double x = *q++;
82 	double y = *q++;
83 	double z = *q++;
84 
85 	for (;;) {
86 		x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6)));
87 		ATF_REQUIRE_MSG(sched_yield() == 0,
88 		    "sched_yield failed: %s", strerror(errno));
89 	}
90 }
91 
92 static double
93 mul3(double x, double y, double z)
94 {
95 	ATF_REQUIRE_MSG(sched_yield() == 0,
96 	    "sched_yield failed: %s", strerror(errno));
97 
98 	return x * y * z;
99 }
100 
101 static void *
102 bar(void *p)
103 {
104 	double d;
105 	int rc;
106 
107 	d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.));
108 	ATF_REQUIRE_EQ(d, 223092870.);
109 
110 	PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock));
111 	rc = recursion_depth++;
112 	PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock));
113 
114 	if (rc < N_RECURSE)
115 		recurse();
116 	else
117 		atf_tc_pass();
118 
119 	/* NOTREACHED */
120 	return NULL;
121 }
122 
123 static void
124 recurse(void) {
125 	pthread_t s2;
126 	PTHREAD_REQUIRE(pthread_create(&s2, 0, bar, 0));
127 	sleep(20); /* XXX must be long enough for our slowest machine */
128 }
129 
130 ATF_TC(fpu);
131 ATF_TC_HEAD(fpu, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr",
134 		"Checks that thread context switches will leave the "
135 		"floating point computations unharmed");
136 }
137 ATF_TC_BODY(fpu, tc)
138 {
139 	double stirseed[] = { 1.7, 3.2, 2.4 };
140 	pthread_t s5;
141 
142 	printf("Testing threaded floating point computations...\n");
143 
144 	PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0));
145 
146 	PTHREAD_REQUIRE(pthread_create(&s5, 0, stir, stirseed));
147 	recurse();
148 
149 	atf_tc_fail("exiting from main");
150 }
151 
152 ATF_TP_ADD_TCS(tp)
153 {
154 	ATF_TP_ADD_TC(tp, fpu);
155 
156 	return atf_no_error();
157 }
158