xref: /openbsd-src/regress/lib/libm/msun/trig_test.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: trig_test.c,v 1.1 2021/10/22 18:00:23 mbuhl Exp $	*/
2 /*-
3  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
4  * 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "macros.h"
29 
30 /*
31  * Tests for corner cases in trigonometric functions. Some accuracy tests
32  * are included as well, but these are very basic sanity checks, not
33  * intended to be comprehensive.
34  *
35  * The program for generating representable numbers near multiples of pi is
36  * available at http://www.cs.berkeley.edu/~wkahan/testpi/ .
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 
44 #include <fenv.h>
45 #include <float.h>
46 #include <math.h>
47 #include <stdio.h>
48 
49 #include "test-utils.h"
50 
51 #pragma STDC FENV_ACCESS ON
52 
53 /*
54  * Test that a function returns the correct value and sets the
55  * exception flags correctly. The exceptmask specifies which
56  * exceptions we should check. We need to be lenient for several
57  * reasons, but mainly because on some architectures it's impossible
58  * to raise FE_OVERFLOW without raising FE_INEXACT.
59  *
60  * These are macros instead of functions so that assert provides more
61  * meaningful error messages.
62  *
63  * XXX The volatile here is to avoid gcc's bogus constant folding and work
64  *     around the lack of support for the FENV_ACCESS pragma.
65  */
66 #define	test(func, x, result, exceptmask, excepts)	do {		\
67 	volatile long double _d = x;					\
68 	ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
69 	CHECK_FPEQUAL((func)(_d), (result));			\
70 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
71 	    #func, #x);							\
72 } while (0)
73 
74 #define	testall(prefix, x, result, exceptmask, excepts)	do {		\
75 	test(prefix, x, (double)result, exceptmask, excepts);		\
76 	test(prefix##f, x, (float)result, exceptmask, excepts);		\
77 	test(prefix##l, x, result, exceptmask, excepts);		\
78 } while (0)
79 
80 #define	testdf(prefix, x, result, exceptmask, excepts)	do {		\
81 	test(prefix, x, (double)result, exceptmask, excepts);		\
82 	test(prefix##f, x, (float)result, exceptmask, excepts);		\
83 } while (0)
84 
85 ATF_TC(special);
86 ATF_TC_HEAD(special, tc)
87 {
88 
89 	atf_tc_set_md_var(tc, "descr",
90  	    "test special cases in sin(), cos(), and tan()");
91 }
92 ATF_TC_BODY(special, tc)
93 {
94 
95 	/* Values at 0 should be exact. */
96 	testall(tan, 0.0, 0.0, ALL_STD_EXCEPT, 0);
97 	testall(tan, -0.0, -0.0, ALL_STD_EXCEPT, 0);
98 	testall(cos, 0.0, 1.0, ALL_STD_EXCEPT, 0);
99 	testall(cos, -0.0, 1.0, ALL_STD_EXCEPT, 0);
100 	testall(sin, 0.0, 0.0, ALL_STD_EXCEPT, 0);
101 	testall(sin, -0.0, -0.0, ALL_STD_EXCEPT, 0);
102 
103 	/* func(+-Inf) == NaN */
104 	testall(tan, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
105 	testall(sin, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
106 	testall(cos, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
107 	testall(tan, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
108 	testall(sin, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
109 	testall(cos, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
110 
111 	/* func(NaN) == NaN */
112 	testall(tan, NAN, NAN, ALL_STD_EXCEPT, 0);
113 	testall(sin, NAN, NAN, ALL_STD_EXCEPT, 0);
114 	testall(cos, NAN, NAN, ALL_STD_EXCEPT, 0);
115 }
116 
117 #ifndef __i386__
118 ATF_TC(reduction);
119 ATF_TC_HEAD(reduction, tc)
120 {
121 
122 	atf_tc_set_md_var(tc, "descr",
123  	    "tests to ensure argument reduction for large arguments is accurate");
124 }
125 ATF_TC_BODY(reduction, tc)
126 {
127 	/* floats very close to odd multiples of pi */
128 	static const float f_pi_odd[] = {
129 		85563208.0f,
130 		43998769152.0f,
131 		9.2763667655669323e+25f,
132 		1.5458357838905804e+29f,
133 	};
134 	/* doubles very close to odd multiples of pi */
135 	static const double d_pi_odd[] = {
136 		3.1415926535897931,
137 		91.106186954104004,
138 		642615.9188844458,
139 		3397346.5699258847,
140 		6134899525417045.0,
141 		3.0213551960457761e+43,
142 		1.2646209897993783e+295,
143 		6.2083625380677099e+307,
144 	};
145 	/* long doubles very close to odd multiples of pi */
146 #if LDBL_MANT_DIG == 64
147 	static const long double ld_pi_odd[] = {
148 		1.1891886960373841596e+101L,
149 		1.07999475322710967206e+2087L,
150 		6.522151627890431836e+2147L,
151 		8.9368974898260328229e+2484L,
152 		9.2961044110572205863e+2555L,
153 		4.90208421886578286e+3189L,
154 		1.5275546401232615884e+3317L,
155 		1.7227465626338900093e+3565L,
156 		2.4160090594000745334e+3808L,
157 		9.8477555741888350649e+4314L,
158 		1.6061597222105160737e+4326L,
159 	};
160 #endif
161 
162 	unsigned i;
163 
164 #if defined(__amd64__) && defined(__clang__) && __clang_major__ >= 7 && \
165     __clang_major__ < 10 && __FreeBSD_cc_version < 1300002
166 	atf_tc_expect_fail("test fails with clang 7-9 - bug 234040");
167 #endif
168 
169 	for (i = 0; i < nitems(f_pi_odd); i++) {
170 		ATF_CHECK(fabs(sinf(f_pi_odd[i])) < FLT_EPSILON);
171 		ATF_CHECK(cosf(f_pi_odd[i]) == -1.0);
172 		ATF_CHECK(fabs(tan(f_pi_odd[i])) < FLT_EPSILON);
173 
174 		ATF_CHECK(fabs(sinf(-f_pi_odd[i])) < FLT_EPSILON);
175 		ATF_CHECK(cosf(-f_pi_odd[i]) == -1.0);
176 		ATF_CHECK(fabs(tanf(-f_pi_odd[i])) < FLT_EPSILON);
177 
178 		ATF_CHECK(fabs(sinf(f_pi_odd[i] * 2)) < FLT_EPSILON);
179 		ATF_CHECK(cosf(f_pi_odd[i] * 2) == 1.0);
180 		ATF_CHECK(fabs(tanf(f_pi_odd[i] * 2)) < FLT_EPSILON);
181 
182 		ATF_CHECK(fabs(sinf(-f_pi_odd[i] * 2)) < FLT_EPSILON);
183 		ATF_CHECK(cosf(-f_pi_odd[i] * 2) == 1.0);
184 		ATF_CHECK(fabs(tanf(-f_pi_odd[i] * 2)) < FLT_EPSILON);
185 	}
186 
187 	for (i = 0; i < nitems(d_pi_odd); i++) {
188 		ATF_CHECK(fabs(sin(d_pi_odd[i])) < 2 * DBL_EPSILON);
189 		ATF_CHECK(cos(d_pi_odd[i]) == -1.0);
190 		ATF_CHECK(fabs(tan(d_pi_odd[i])) < 2 * DBL_EPSILON);
191 
192 		ATF_CHECK(fabs(sin(-d_pi_odd[i])) < 2 * DBL_EPSILON);
193 		ATF_CHECK(cos(-d_pi_odd[i]) == -1.0);
194 		ATF_CHECK(fabs(tan(-d_pi_odd[i])) < 2 * DBL_EPSILON);
195 
196 		ATF_CHECK(fabs(sin(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
197 		ATF_CHECK(cos(d_pi_odd[i] * 2) == 1.0);
198 		ATF_CHECK(fabs(tan(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
199 
200 		ATF_CHECK(fabs(sin(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
201 		ATF_CHECK(cos(-d_pi_odd[i] * 2) == 1.0);
202 		ATF_CHECK(fabs(tan(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
203 	}
204 
205 #if LDBL_MANT_DIG == 64 /* XXX: || LDBL_MANT_DIG == 113 */
206 	for (i = 0; i < nitems(ld_pi_odd); i++) {
207 		ATF_CHECK(fabsl(sinl(ld_pi_odd[i])) < LDBL_EPSILON);
208 		ATF_CHECK(cosl(ld_pi_odd[i]) == -1.0);
209 		ATF_CHECK(fabsl(tanl(ld_pi_odd[i])) < LDBL_EPSILON);
210 
211 		ATF_CHECK(fabsl(sinl(-ld_pi_odd[i])) < LDBL_EPSILON);
212 		ATF_CHECK(cosl(-ld_pi_odd[i]) == -1.0);
213 		ATF_CHECK(fabsl(tanl(-ld_pi_odd[i])) < LDBL_EPSILON);
214 
215 		ATF_CHECK(fabsl(sinl(ld_pi_odd[i] * 2)) < LDBL_EPSILON);
216 		ATF_CHECK(cosl(ld_pi_odd[i] * 2) == 1.0);
217 		ATF_CHECK(fabsl(tanl(ld_pi_odd[i] * 2)) < LDBL_EPSILON);
218 
219 		ATF_CHECK(fabsl(sinl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON);
220 		ATF_CHECK(cosl(-ld_pi_odd[i] * 2) == 1.0);
221 		ATF_CHECK(fabsl(tanl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON);
222 	}
223 #endif
224 }
225 
226 ATF_TC(accuracy);
227 ATF_TC_HEAD(accuracy, tc)
228 {
229 
230 	atf_tc_set_md_var(tc, "descr",
231 	    "tests the accuracy of these functions over the primary range");
232 }
233 ATF_TC_BODY(accuracy, tc)
234 {
235 
236 	/* For small args, sin(x) = tan(x) = x, and cos(x) = 1. */
237 	testall(sin, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L,
238 	     ALL_STD_EXCEPT, FE_INEXACT);
239 	testall(tan, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L,
240 	     ALL_STD_EXCEPT, FE_INEXACT);
241 	testall(cos, 0xd.50ee515fe4aea16p-114L, 1.0,
242 		ALL_STD_EXCEPT, FE_INEXACT);
243 
244 	/*
245 	 * These tests should pass for f32, d64, and ld80 as long as
246 	 * the error is <= 0.75 ulp (round to nearest)
247 	 */
248 #if LDBL_MANT_DIG <= 64
249 #define	testacc	testall
250 #else
251 #define	testacc	testdf
252 #endif
253 	testacc(sin, 0.17255452780841205174L, 0.17169949801444412683L,
254 		ALL_STD_EXCEPT, FE_INEXACT);
255 	testacc(sin, -0.75431944555904520893L, -0.68479288156557286353L,
256 		ALL_STD_EXCEPT, FE_INEXACT);
257 	testacc(cos, 0.70556358769838947292L, 0.76124620693117771850L,
258 		ALL_STD_EXCEPT, FE_INEXACT);
259 	testacc(cos, -0.34061437849088045332L, 0.94254960031831729956L,
260 		ALL_STD_EXCEPT, FE_INEXACT);
261 	testacc(tan, -0.15862817413325692897L, -0.15997221861309522115L,
262 		ALL_STD_EXCEPT, FE_INEXACT);
263 	testacc(tan, 0.38374784931303813530L, 0.40376500259976759951L,
264 		ALL_STD_EXCEPT, FE_INEXACT);
265 
266 	/*
267 	 * XXX missing:
268 	 * - tests for ld128
269 	 * - tests for other rounding modes (probably won't pass for now)
270 	 * - tests for large numbers that get reduced to hi+lo with lo!=0
271 	 */
272 }
273 #endif
274 
275 ATF_TP_ADD_TCS(tp)
276 {
277 
278 	ATF_TP_ADD_TC(tp, special);
279 
280 #ifndef __i386__
281 	ATF_TP_ADD_TC(tp, accuracy);
282 	ATF_TP_ADD_TC(tp, reduction);
283 #endif
284 
285 	return (atf_no_error());
286 }
287