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