xref: /openbsd-src/regress/lib/libm/msun/invtrig_test.c (revision c36e572ec6e4c7a6aca6564610c2c45de467ead4)
1*c36e572eSmbuhl /*	$OpenBSD: invtrig_test.c,v 1.1 2021/10/22 18:00:22 mbuhl Exp $	*/
2*c36e572eSmbuhl /*-
3*c36e572eSmbuhl  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
4*c36e572eSmbuhl  * All rights reserved.
5*c36e572eSmbuhl  *
6*c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7*c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8*c36e572eSmbuhl  * are met:
9*c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10*c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11*c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12*c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13*c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14*c36e572eSmbuhl  *
15*c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*c36e572eSmbuhl  * SUCH DAMAGE.
26*c36e572eSmbuhl  */
27*c36e572eSmbuhl 
28*c36e572eSmbuhl #include "macros.h"
29*c36e572eSmbuhl 
30*c36e572eSmbuhl /*
31*c36e572eSmbuhl  * Tests for corner cases in the inverse trigonometric functions. Some
32*c36e572eSmbuhl  * accuracy tests are included as well, but these are very basic
33*c36e572eSmbuhl  * sanity checks, not intended to be comprehensive.
34*c36e572eSmbuhl  */
35*c36e572eSmbuhl 
36*c36e572eSmbuhl #include <sys/cdefs.h>
37*c36e572eSmbuhl __FBSDID("$FreeBSD$");
38*c36e572eSmbuhl 
39*c36e572eSmbuhl #include <fenv.h>
40*c36e572eSmbuhl #include <float.h>
41*c36e572eSmbuhl #include <math.h>
42*c36e572eSmbuhl #include <stdio.h>
43*c36e572eSmbuhl 
44*c36e572eSmbuhl #include "test-utils.h"
45*c36e572eSmbuhl 
46*c36e572eSmbuhl #pragma STDC FENV_ACCESS ON
47*c36e572eSmbuhl 
48*c36e572eSmbuhl /*
49*c36e572eSmbuhl  * Test that a function returns the correct value and sets the
50*c36e572eSmbuhl  * exception flags correctly. A tolerance specifying the maximum
51*c36e572eSmbuhl  * relative error allowed may be specified. For the 'testall'
52*c36e572eSmbuhl  * functions, the tolerance is specified in ulps.
53*c36e572eSmbuhl  *
54*c36e572eSmbuhl  * These are macros instead of functions so that assert provides more
55*c36e572eSmbuhl  * meaningful error messages.
56*c36e572eSmbuhl  */
57*c36e572eSmbuhl #define	test_tol(func, x, result, tol, excepts) do {			\
58*c36e572eSmbuhl 	volatile long double _in = (x), _out = (result);		\
59*c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
60*c36e572eSmbuhl 	CHECK_FPEQUAL_TOL(func(_in), _out, (tol), CS_BOTH);		\
61*c36e572eSmbuhl 	CHECK_FP_EXCEPTIONS_MSG(excepts, ALL_STD_EXCEPT, "for %s(%s)",	\
62*c36e572eSmbuhl 	    #func, #x);							\
63*c36e572eSmbuhl } while (0)
64*c36e572eSmbuhl #define test(func, x, result, excepts)					\
65*c36e572eSmbuhl 	test_tol(func, (x), (result), 0, (excepts))
66*c36e572eSmbuhl 
67*c36e572eSmbuhl #define	_testall_tol(prefix, x, result, tol, excepts) do {		\
68*c36e572eSmbuhl 	test_tol(prefix, (double)(x), (double)(result),			\
69*c36e572eSmbuhl 		 (tol) * ldexp(1.0, 1 - DBL_MANT_DIG), (excepts));	\
70*c36e572eSmbuhl 	test_tol(prefix##f, (float)(x), (float)(result),		\
71*c36e572eSmbuhl 		 (tol) * ldexpf(1.0, 1 - FLT_MANT_DIG), (excepts));	\
72*c36e572eSmbuhl } while (0)
73*c36e572eSmbuhl 
74*c36e572eSmbuhl #if LDBL_PREC == 53
75*c36e572eSmbuhl #define	testall_tol	_testall_tol
76*c36e572eSmbuhl #else
77*c36e572eSmbuhl #define	testall_tol(prefix, x, result, tol, excepts) do {		\
78*c36e572eSmbuhl 	_testall_tol(prefix, x, result, tol, excepts);			\
79*c36e572eSmbuhl 	test_tol(prefix##l, (x), (result),				\
80*c36e572eSmbuhl 		 (tol) * ldexpl(1.0, 1 - LDBL_MANT_DIG), (excepts));	\
81*c36e572eSmbuhl } while (0)
82*c36e572eSmbuhl #endif
83*c36e572eSmbuhl 
84*c36e572eSmbuhl #define testall(prefix, x, result, excepts)				\
85*c36e572eSmbuhl 	testall_tol(prefix, (x), (result), 0, (excepts))
86*c36e572eSmbuhl 
87*c36e572eSmbuhl #define	test2_tol(func, y, x, result, tol, excepts) do {		\
88*c36e572eSmbuhl 	volatile long double _iny = (y), _inx = (x), _out = (result);	\
89*c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
90*c36e572eSmbuhl 	CHECK_FPEQUAL_TOL(func(_iny, _inx), _out, (tol), CS_BOTH);	\
91*c36e572eSmbuhl 	CHECK_FP_EXCEPTIONS_MSG(excepts, ALL_STD_EXCEPT, "for %s(%s)",	\
92*c36e572eSmbuhl 	    #func, #x);							\
93*c36e572eSmbuhl } while (0)
94*c36e572eSmbuhl #define test2(func, y, x, result, excepts)				\
95*c36e572eSmbuhl 	test2_tol(func, (y), (x), (result), 0, (excepts))
96*c36e572eSmbuhl 
97*c36e572eSmbuhl #define	_testall2_tol(prefix, y, x, result, tol, excepts) do {		\
98*c36e572eSmbuhl 	test2_tol(prefix, (double)(y), (double)(x), (double)(result),	\
99*c36e572eSmbuhl 		  (tol) * ldexp(1.0, 1 - DBL_MANT_DIG), (excepts));	\
100*c36e572eSmbuhl 	test2_tol(prefix##f, (float)(y), (float)(x), (float)(result),	\
101*c36e572eSmbuhl 		  (tol) * ldexpf(1.0, 1 - FLT_MANT_DIG), (excepts));	\
102*c36e572eSmbuhl } while (0)
103*c36e572eSmbuhl 
104*c36e572eSmbuhl #if LDBL_PREC == 53
105*c36e572eSmbuhl #define	testall2_tol	_testall2_tol
106*c36e572eSmbuhl #else
107*c36e572eSmbuhl #define	testall2_tol(prefix, y, x, result, tol, excepts) do {		\
108*c36e572eSmbuhl 	_testall2_tol(prefix, y, x, result, tol, excepts);		\
109*c36e572eSmbuhl 	test2_tol(prefix##l, (y), (x), (result),			\
110*c36e572eSmbuhl 		  (tol) * ldexpl(1.0, 1 - LDBL_MANT_DIG), (excepts));	\
111*c36e572eSmbuhl } while (0)
112*c36e572eSmbuhl #endif
113*c36e572eSmbuhl 
114*c36e572eSmbuhl #define testall2(prefix, y, x, result, excepts)				\
115*c36e572eSmbuhl 	testall2_tol(prefix, (y), (x), (result), 0, (excepts))
116*c36e572eSmbuhl 
117*c36e572eSmbuhl static long double
118*c36e572eSmbuhl pi =   3.14159265358979323846264338327950280e+00L,
119*c36e572eSmbuhl pio3 = 1.04719755119659774615421446109316766e+00L,
120*c36e572eSmbuhl c3pi = 9.42477796076937971538793014983850839e+00L,
121*c36e572eSmbuhl c7pi = 2.19911485751285526692385036829565196e+01L,
122*c36e572eSmbuhl c5pio3 = 5.23598775598298873077107230546583851e+00L,
123*c36e572eSmbuhl sqrt2m1 = 4.14213562373095048801688724209698081e-01L;
124*c36e572eSmbuhl 
125*c36e572eSmbuhl 
126*c36e572eSmbuhl /*
127*c36e572eSmbuhl  * Test special case inputs in asin(), acos() and atan(): signed
128*c36e572eSmbuhl  * zeroes, infinities, and NaNs.
129*c36e572eSmbuhl  */
130*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(special);
131*c36e572eSmbuhl ATF_TC_BODY(special, tc)
132*c36e572eSmbuhl {
133*c36e572eSmbuhl 
134*c36e572eSmbuhl 	testall(asin, 0.0, 0.0, 0);
135*c36e572eSmbuhl 	testall(acos, 0.0, pi / 2, FE_INEXACT);
136*c36e572eSmbuhl 	testall(atan, 0.0, 0.0, 0);
137*c36e572eSmbuhl 	testall(asin, -0.0, -0.0, 0);
138*c36e572eSmbuhl 	testall(acos, -0.0, pi / 2, FE_INEXACT);
139*c36e572eSmbuhl 	testall(atan, -0.0, -0.0, 0);
140*c36e572eSmbuhl 
141*c36e572eSmbuhl 	testall(asin, INFINITY, NAN, FE_INVALID);
142*c36e572eSmbuhl 	testall(acos, INFINITY, NAN, FE_INVALID);
143*c36e572eSmbuhl 	testall(atan, INFINITY, pi / 2, FE_INEXACT);
144*c36e572eSmbuhl 	testall(asin, -INFINITY, NAN, FE_INVALID);
145*c36e572eSmbuhl 	testall(acos, -INFINITY, NAN, FE_INVALID);
146*c36e572eSmbuhl 	testall(atan, -INFINITY, -pi / 2, FE_INEXACT);
147*c36e572eSmbuhl 
148*c36e572eSmbuhl 	testall(asin, NAN, NAN, 0);
149*c36e572eSmbuhl 	testall(acos, NAN, NAN, 0);
150*c36e572eSmbuhl 	testall(atan, NAN, NAN, 0);
151*c36e572eSmbuhl }
152*c36e572eSmbuhl 
153*c36e572eSmbuhl /*
154*c36e572eSmbuhl  * Test special case inputs in atan2(), where the exact value of y/x is
155*c36e572eSmbuhl  * zero or non-finite.
156*c36e572eSmbuhl  */
157*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(special_atan2);
158*c36e572eSmbuhl ATF_TC_BODY(special_atan2, tc)
159*c36e572eSmbuhl {
160*c36e572eSmbuhl 	long double z;
161*c36e572eSmbuhl 	int e;
162*c36e572eSmbuhl 
163*c36e572eSmbuhl 	testall2(atan2, 0.0, -0.0, pi, FE_INEXACT);
164*c36e572eSmbuhl 	testall2(atan2, -0.0, -0.0, -pi, FE_INEXACT);
165*c36e572eSmbuhl 	testall2(atan2, 0.0, 0.0, 0.0, 0);
166*c36e572eSmbuhl 	testall2(atan2, -0.0, 0.0, -0.0, 0);
167*c36e572eSmbuhl 
168*c36e572eSmbuhl 	testall2(atan2, INFINITY, -INFINITY, c3pi / 4, FE_INEXACT);
169*c36e572eSmbuhl 	testall2(atan2, -INFINITY, -INFINITY, -c3pi / 4, FE_INEXACT);
170*c36e572eSmbuhl 	testall2(atan2, INFINITY, INFINITY, pi / 4, FE_INEXACT);
171*c36e572eSmbuhl 	testall2(atan2, -INFINITY, INFINITY, -pi / 4, FE_INEXACT);
172*c36e572eSmbuhl 
173*c36e572eSmbuhl 	/* Tests with one input in the range (0, Inf]. */
174*c36e572eSmbuhl 	z = 1.23456789L;
175*c36e572eSmbuhl 	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e <= FLT_MAX_EXP; e++) {
176*c36e572eSmbuhl 		test2(atan2f, 0.0, ldexpf(z, e), 0.0, 0);
177*c36e572eSmbuhl 		test2(atan2f, -0.0, ldexpf(z, e), -0.0, 0);
178*c36e572eSmbuhl 		test2(atan2f, 0.0, ldexpf(-z, e), (float)pi, FE_INEXACT);
179*c36e572eSmbuhl 		test2(atan2f, -0.0, ldexpf(-z, e), (float)-pi, FE_INEXACT);
180*c36e572eSmbuhl 		test2(atan2f, ldexpf(z, e), 0.0, (float)pi / 2, FE_INEXACT);
181*c36e572eSmbuhl 		test2(atan2f, ldexpf(z, e), -0.0, (float)pi / 2, FE_INEXACT);
182*c36e572eSmbuhl 		test2(atan2f, ldexpf(-z, e), 0.0, (float)-pi / 2, FE_INEXACT);
183*c36e572eSmbuhl 		test2(atan2f, ldexpf(-z, e), -0.0, (float)-pi / 2, FE_INEXACT);
184*c36e572eSmbuhl 	}
185*c36e572eSmbuhl 	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e <= DBL_MAX_EXP; e++) {
186*c36e572eSmbuhl 		test2(atan2, 0.0, ldexp(z, e), 0.0, 0);
187*c36e572eSmbuhl 		test2(atan2, -0.0, ldexp(z, e), -0.0, 0);
188*c36e572eSmbuhl 		test2(atan2, 0.0, ldexp(-z, e), (double)pi, FE_INEXACT);
189*c36e572eSmbuhl 		test2(atan2, -0.0, ldexp(-z, e), (double)-pi, FE_INEXACT);
190*c36e572eSmbuhl 		test2(atan2, ldexp(z, e), 0.0, (double)pi / 2, FE_INEXACT);
191*c36e572eSmbuhl 		test2(atan2, ldexp(z, e), -0.0, (double)pi / 2, FE_INEXACT);
192*c36e572eSmbuhl 		test2(atan2, ldexp(-z, e), 0.0, (double)-pi / 2, FE_INEXACT);
193*c36e572eSmbuhl 		test2(atan2, ldexp(-z, e), -0.0, (double)-pi / 2, FE_INEXACT);
194*c36e572eSmbuhl 	}
195*c36e572eSmbuhl 	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e <= LDBL_MAX_EXP; e++) {
196*c36e572eSmbuhl 		test2(atan2l, 0.0, ldexpl(z, e), 0.0, 0);
197*c36e572eSmbuhl 		test2(atan2l, -0.0, ldexpl(z, e), -0.0, 0);
198*c36e572eSmbuhl 		test2(atan2l, 0.0, ldexpl(-z, e), pi, FE_INEXACT);
199*c36e572eSmbuhl 		test2(atan2l, -0.0, ldexpl(-z, e), -pi, FE_INEXACT);
200*c36e572eSmbuhl 		test2(atan2l, ldexpl(z, e), 0.0, pi / 2, FE_INEXACT);
201*c36e572eSmbuhl 		test2(atan2l, ldexpl(z, e), -0.0, pi / 2, FE_INEXACT);
202*c36e572eSmbuhl 		test2(atan2l, ldexpl(-z, e), 0.0, -pi / 2, FE_INEXACT);
203*c36e572eSmbuhl 		test2(atan2l, ldexpl(-z, e), -0.0, -pi / 2, FE_INEXACT);
204*c36e572eSmbuhl 	}
205*c36e572eSmbuhl 
206*c36e572eSmbuhl 	/* Tests with one input in the range (0, Inf). */
207*c36e572eSmbuhl 	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e <= FLT_MAX_EXP - 1; e++) {
208*c36e572eSmbuhl 		test2(atan2f, ldexpf(z, e), INFINITY, 0.0, 0);
209*c36e572eSmbuhl 		test2(atan2f, ldexpf(-z,e), INFINITY, -0.0, 0);
210*c36e572eSmbuhl 		test2(atan2f, ldexpf(z, e), -INFINITY, (float)pi, FE_INEXACT);
211*c36e572eSmbuhl 		test2(atan2f, ldexpf(-z,e), -INFINITY, (float)-pi, FE_INEXACT);
212*c36e572eSmbuhl 		test2(atan2f, INFINITY, ldexpf(z,e), (float)pi/2, FE_INEXACT);
213*c36e572eSmbuhl 		test2(atan2f, INFINITY, ldexpf(-z,e), (float)pi/2, FE_INEXACT);
214*c36e572eSmbuhl 		test2(atan2f, -INFINITY, ldexpf(z,e), (float)-pi/2,FE_INEXACT);
215*c36e572eSmbuhl 		test2(atan2f, -INFINITY, ldexpf(-z,e),(float)-pi/2,FE_INEXACT);
216*c36e572eSmbuhl 	}
217*c36e572eSmbuhl 	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e <= DBL_MAX_EXP - 1; e++) {
218*c36e572eSmbuhl 		test2(atan2, ldexp(z, e), INFINITY, 0.0, 0);
219*c36e572eSmbuhl 		test2(atan2, ldexp(-z,e), INFINITY, -0.0, 0);
220*c36e572eSmbuhl 		test2(atan2, ldexp(z, e), -INFINITY, (double)pi, FE_INEXACT);
221*c36e572eSmbuhl 		test2(atan2, ldexp(-z,e), -INFINITY, (double)-pi, FE_INEXACT);
222*c36e572eSmbuhl 		test2(atan2, INFINITY, ldexp(z,e), (double)pi/2, FE_INEXACT);
223*c36e572eSmbuhl 		test2(atan2, INFINITY, ldexp(-z,e), (double)pi/2, FE_INEXACT);
224*c36e572eSmbuhl 		test2(atan2, -INFINITY, ldexp(z,e), (double)-pi/2,FE_INEXACT);
225*c36e572eSmbuhl 		test2(atan2, -INFINITY, ldexp(-z,e),(double)-pi/2,FE_INEXACT);
226*c36e572eSmbuhl 	}
227*c36e572eSmbuhl 	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e <= LDBL_MAX_EXP - 1; e++) {
228*c36e572eSmbuhl 		test2(atan2l, ldexpl(z, e), INFINITY, 0.0, 0);
229*c36e572eSmbuhl 		test2(atan2l, ldexpl(-z,e), INFINITY, -0.0, 0);
230*c36e572eSmbuhl 		test2(atan2l, ldexpl(z, e), -INFINITY, pi, FE_INEXACT);
231*c36e572eSmbuhl 		test2(atan2l, ldexpl(-z,e), -INFINITY, -pi, FE_INEXACT);
232*c36e572eSmbuhl 		test2(atan2l, INFINITY, ldexpl(z, e), pi / 2, FE_INEXACT);
233*c36e572eSmbuhl 		test2(atan2l, INFINITY, ldexpl(-z, e), pi / 2, FE_INEXACT);
234*c36e572eSmbuhl 		test2(atan2l, -INFINITY, ldexpl(z, e), -pi / 2, FE_INEXACT);
235*c36e572eSmbuhl 		test2(atan2l, -INFINITY, ldexpl(-z, e), -pi / 2, FE_INEXACT);
236*c36e572eSmbuhl 	}
237*c36e572eSmbuhl }
238*c36e572eSmbuhl 
239*c36e572eSmbuhl /*
240*c36e572eSmbuhl  * Test various inputs to asin(), acos() and atan() and verify that the
241*c36e572eSmbuhl  * results are accurate to within 1 ulp.
242*c36e572eSmbuhl  */
243*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(accuracy);
244*c36e572eSmbuhl ATF_TC_BODY(accuracy, tc)
245*c36e572eSmbuhl {
246*c36e572eSmbuhl 
247*c36e572eSmbuhl 	/* We expect correctly rounded results for these basic cases. */
248*c36e572eSmbuhl 	testall(asin, 1.0, pi / 2, FE_INEXACT);
249*c36e572eSmbuhl 	testall(acos, 1.0, 0, 0);
250*c36e572eSmbuhl 	testall(atan, 1.0, pi / 4, FE_INEXACT);
251*c36e572eSmbuhl 	testall(asin, -1.0, -pi / 2, FE_INEXACT);
252*c36e572eSmbuhl 	testall(acos, -1.0, pi, FE_INEXACT);
253*c36e572eSmbuhl 	testall(atan, -1.0, -pi / 4, FE_INEXACT);
254*c36e572eSmbuhl 
255*c36e572eSmbuhl 	/*
256*c36e572eSmbuhl 	 * Here we expect answers to be within 1 ulp, although inexactness
257*c36e572eSmbuhl 	 * in the input, combined with double rounding, could cause larger
258*c36e572eSmbuhl 	 * errors.
259*c36e572eSmbuhl 	 */
260*c36e572eSmbuhl 
261*c36e572eSmbuhl 	testall_tol(asin, sqrtl(2) / 2, pi / 4, 1, FE_INEXACT);
262*c36e572eSmbuhl 	testall_tol(acos, sqrtl(2) / 2, pi / 4, 1, FE_INEXACT);
263*c36e572eSmbuhl 	testall_tol(asin, -sqrtl(2) / 2, -pi / 4, 1, FE_INEXACT);
264*c36e572eSmbuhl 	testall_tol(acos, -sqrtl(2) / 2, c3pi / 4, 1, FE_INEXACT);
265*c36e572eSmbuhl 
266*c36e572eSmbuhl 	testall_tol(asin, sqrtl(3) / 2, pio3, 1, FE_INEXACT);
267*c36e572eSmbuhl 	testall_tol(acos, sqrtl(3) / 2, pio3 / 2, 1, FE_INEXACT);
268*c36e572eSmbuhl 	testall_tol(atan, sqrtl(3), pio3, 1, FE_INEXACT);
269*c36e572eSmbuhl 	testall_tol(asin, -sqrtl(3) / 2, -pio3, 1, FE_INEXACT);
270*c36e572eSmbuhl 	testall_tol(acos, -sqrtl(3) / 2, c5pio3 / 2, 1, FE_INEXACT);
271*c36e572eSmbuhl 	testall_tol(atan, -sqrtl(3), -pio3, 1, FE_INEXACT);
272*c36e572eSmbuhl 
273*c36e572eSmbuhl 	testall_tol(atan, sqrt2m1, pi / 8, 1, FE_INEXACT);
274*c36e572eSmbuhl 	testall_tol(atan, -sqrt2m1, -pi / 8, 1, FE_INEXACT);
275*c36e572eSmbuhl }
276*c36e572eSmbuhl 
277*c36e572eSmbuhl /*
278*c36e572eSmbuhl  * Test inputs to atan2() where x is a power of 2. These are easy cases
279*c36e572eSmbuhl  * because y/x is exact.
280*c36e572eSmbuhl  */
281*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(p2x_atan2);
282*c36e572eSmbuhl ATF_TC_BODY(p2x_atan2, tc)
283*c36e572eSmbuhl {
284*c36e572eSmbuhl 
285*c36e572eSmbuhl 	testall2(atan2, 1.0, 1.0, pi / 4, FE_INEXACT);
286*c36e572eSmbuhl 	testall2(atan2, 1.0, -1.0, c3pi / 4, FE_INEXACT);
287*c36e572eSmbuhl 	testall2(atan2, -1.0, 1.0, -pi / 4, FE_INEXACT);
288*c36e572eSmbuhl 	testall2(atan2, -1.0, -1.0, -c3pi / 4, FE_INEXACT);
289*c36e572eSmbuhl 
290*c36e572eSmbuhl 	testall2_tol(atan2, sqrt2m1 * 2, 2.0, pi / 8, 1, FE_INEXACT);
291*c36e572eSmbuhl 	testall2_tol(atan2, sqrt2m1 * 2, -2.0, c7pi / 8, 1, FE_INEXACT);
292*c36e572eSmbuhl 	testall2_tol(atan2, -sqrt2m1 * 2, 2.0, -pi / 8, 1, FE_INEXACT);
293*c36e572eSmbuhl 	testall2_tol(atan2, -sqrt2m1 * 2, -2.0, -c7pi / 8, 1, FE_INEXACT);
294*c36e572eSmbuhl 
295*c36e572eSmbuhl 	testall2_tol(atan2, sqrtl(3) * 0.5, 0.5, pio3, 1, FE_INEXACT);
296*c36e572eSmbuhl 	testall2_tol(atan2, sqrtl(3) * 0.5, -0.5, pio3 * 2, 1, FE_INEXACT);
297*c36e572eSmbuhl 	testall2_tol(atan2, -sqrtl(3) * 0.5, 0.5, -pio3, 1, FE_INEXACT);
298*c36e572eSmbuhl 	testall2_tol(atan2, -sqrtl(3) * 0.5, -0.5, -pio3 * 2, 1, FE_INEXACT);
299*c36e572eSmbuhl }
300*c36e572eSmbuhl 
301*c36e572eSmbuhl /*
302*c36e572eSmbuhl  * Test inputs very close to 0.
303*c36e572eSmbuhl  */
304*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(tiny);
305*c36e572eSmbuhl ATF_TC_BODY(tiny, tc)
306*c36e572eSmbuhl {
307*c36e572eSmbuhl 	float tiny = 0x1.23456p-120f;
308*c36e572eSmbuhl 
309*c36e572eSmbuhl 	testall(asin, tiny, tiny, FE_INEXACT);
310*c36e572eSmbuhl 	testall(acos, tiny, pi / 2, FE_INEXACT);
311*c36e572eSmbuhl 	testall(atan, tiny, tiny, FE_INEXACT);
312*c36e572eSmbuhl 
313*c36e572eSmbuhl 	testall(asin, -tiny, -tiny, FE_INEXACT);
314*c36e572eSmbuhl 	testall(acos, -tiny, pi / 2, FE_INEXACT);
315*c36e572eSmbuhl 	testall(atan, -tiny, -tiny, FE_INEXACT);
316*c36e572eSmbuhl 
317*c36e572eSmbuhl 	/* Test inputs to atan2() that would cause y/x to underflow. */
318*c36e572eSmbuhl 	test2(atan2f, 0x1.0p-100, 0x1.0p100, 0.0, FE_INEXACT | FE_UNDERFLOW);
319*c36e572eSmbuhl 	test2(atan2, 0x1.0p-1000, 0x1.0p1000, 0.0, FE_INEXACT | FE_UNDERFLOW);
320*c36e572eSmbuhl 	test2(atan2l, ldexpl(1.0, 100 - LDBL_MAX_EXP),
321*c36e572eSmbuhl 	      ldexpl(1.0, LDBL_MAX_EXP - 100), 0.0, FE_INEXACT | FE_UNDERFLOW);
322*c36e572eSmbuhl 	test2(atan2f, -0x1.0p-100, 0x1.0p100, -0.0, FE_INEXACT | FE_UNDERFLOW);
323*c36e572eSmbuhl 	test2(atan2, -0x1.0p-1000, 0x1.0p1000, -0.0, FE_INEXACT | FE_UNDERFLOW);
324*c36e572eSmbuhl 	test2(atan2l, -ldexpl(1.0, 100 - LDBL_MAX_EXP),
325*c36e572eSmbuhl 	      ldexpl(1.0, LDBL_MAX_EXP - 100), -0.0, FE_INEXACT | FE_UNDERFLOW);
326*c36e572eSmbuhl 	test2(atan2f, 0x1.0p-100, -0x1.0p100, (float)pi, FE_INEXACT);
327*c36e572eSmbuhl 	test2(atan2, 0x1.0p-1000, -0x1.0p1000, (double)pi, FE_INEXACT);
328*c36e572eSmbuhl 	test2(atan2l, ldexpl(1.0, 100 - LDBL_MAX_EXP),
329*c36e572eSmbuhl 	      -ldexpl(1.0, LDBL_MAX_EXP - 100), pi, FE_INEXACT);
330*c36e572eSmbuhl 	test2(atan2f, -0x1.0p-100, -0x1.0p100, (float)-pi, FE_INEXACT);
331*c36e572eSmbuhl 	test2(atan2, -0x1.0p-1000, -0x1.0p1000, (double)-pi, FE_INEXACT);
332*c36e572eSmbuhl 	test2(atan2l, -ldexpl(1.0, 100 - LDBL_MAX_EXP),
333*c36e572eSmbuhl 	      -ldexpl(1.0, LDBL_MAX_EXP - 100), -pi, FE_INEXACT);
334*c36e572eSmbuhl }
335*c36e572eSmbuhl 
336*c36e572eSmbuhl /*
337*c36e572eSmbuhl  * Test very large inputs to atan().
338*c36e572eSmbuhl  */
339*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(atan_huge);
340*c36e572eSmbuhl ATF_TC_BODY(atan_huge, tc)
341*c36e572eSmbuhl {
342*c36e572eSmbuhl 	float huge = 0x1.23456p120;
343*c36e572eSmbuhl 
344*c36e572eSmbuhl 	testall(atan, huge, pi / 2, FE_INEXACT);
345*c36e572eSmbuhl 	testall(atan, -huge, -pi / 2, FE_INEXACT);
346*c36e572eSmbuhl 
347*c36e572eSmbuhl 	/* Test inputs to atan2() that would cause y/x to overflow. */
348*c36e572eSmbuhl 	test2(atan2f, 0x1.0p100, 0x1.0p-100, (float)pi / 2, FE_INEXACT);
349*c36e572eSmbuhl 	test2(atan2, 0x1.0p1000, 0x1.0p-1000, (double)pi / 2, FE_INEXACT);
350*c36e572eSmbuhl 	test2(atan2l, ldexpl(1.0, LDBL_MAX_EXP - 100),
351*c36e572eSmbuhl 	      ldexpl(1.0, 100 - LDBL_MAX_EXP), pi / 2, FE_INEXACT);
352*c36e572eSmbuhl 	test2(atan2f, -0x1.0p100, 0x1.0p-100, (float)-pi / 2, FE_INEXACT);
353*c36e572eSmbuhl 	test2(atan2, -0x1.0p1000, 0x1.0p-1000, (double)-pi / 2, FE_INEXACT);
354*c36e572eSmbuhl 	test2(atan2l, -ldexpl(1.0, LDBL_MAX_EXP - 100),
355*c36e572eSmbuhl 	      ldexpl(1.0, 100 - LDBL_MAX_EXP), -pi / 2, FE_INEXACT);
356*c36e572eSmbuhl 
357*c36e572eSmbuhl 	test2(atan2f, 0x1.0p100, -0x1.0p-100, (float)pi / 2, FE_INEXACT);
358*c36e572eSmbuhl 	test2(atan2, 0x1.0p1000, -0x1.0p-1000, (double)pi / 2, FE_INEXACT);
359*c36e572eSmbuhl 	test2(atan2l, ldexpl(1.0, LDBL_MAX_EXP - 100),
360*c36e572eSmbuhl 	      -ldexpl(1.0, 100 - LDBL_MAX_EXP), pi / 2, FE_INEXACT);
361*c36e572eSmbuhl 	test2(atan2f, -0x1.0p100, -0x1.0p-100, (float)-pi / 2, FE_INEXACT);
362*c36e572eSmbuhl 	test2(atan2, -0x1.0p1000, -0x1.0p-1000, (double)-pi / 2, FE_INEXACT);
363*c36e572eSmbuhl 	test2(atan2l, -ldexpl(1.0, LDBL_MAX_EXP - 100),
364*c36e572eSmbuhl 	      -ldexpl(1.0, 100 - LDBL_MAX_EXP), -pi / 2, FE_INEXACT);
365*c36e572eSmbuhl }
366*c36e572eSmbuhl 
367*c36e572eSmbuhl /*
368*c36e572eSmbuhl  * Test that sin(asin(x)) == x, and similarly for acos() and atan().
369*c36e572eSmbuhl  * You need to have a working sinl(), cosl(), and tanl() for these
370*c36e572eSmbuhl  * tests to pass.
371*c36e572eSmbuhl  */
372*c36e572eSmbuhl static long double
373*c36e572eSmbuhl sinasinf(float x)
374*c36e572eSmbuhl {
375*c36e572eSmbuhl 
376*c36e572eSmbuhl 	return (sinl(asinf(x)));
377*c36e572eSmbuhl }
378*c36e572eSmbuhl 
379*c36e572eSmbuhl static long double
380*c36e572eSmbuhl sinasin(double x)
381*c36e572eSmbuhl {
382*c36e572eSmbuhl 
383*c36e572eSmbuhl 	return (sinl(asin(x)));
384*c36e572eSmbuhl }
385*c36e572eSmbuhl 
386*c36e572eSmbuhl static long double
387*c36e572eSmbuhl sinasinl(long double x)
388*c36e572eSmbuhl {
389*c36e572eSmbuhl 
390*c36e572eSmbuhl 	return (sinl(asinl(x)));
391*c36e572eSmbuhl }
392*c36e572eSmbuhl 
393*c36e572eSmbuhl static long double
394*c36e572eSmbuhl cosacosf(float x)
395*c36e572eSmbuhl {
396*c36e572eSmbuhl 
397*c36e572eSmbuhl 	return (cosl(acosf(x)));
398*c36e572eSmbuhl }
399*c36e572eSmbuhl 
400*c36e572eSmbuhl static long double
401*c36e572eSmbuhl cosacos(double x)
402*c36e572eSmbuhl {
403*c36e572eSmbuhl 
404*c36e572eSmbuhl 	return (cosl(acos(x)));
405*c36e572eSmbuhl }
406*c36e572eSmbuhl 
407*c36e572eSmbuhl static long double
408*c36e572eSmbuhl cosacosl(long double x)
409*c36e572eSmbuhl {
410*c36e572eSmbuhl 
411*c36e572eSmbuhl 	return (cosl(acosl(x)));
412*c36e572eSmbuhl }
413*c36e572eSmbuhl 
414*c36e572eSmbuhl static long double
415*c36e572eSmbuhl tanatanf(float x)
416*c36e572eSmbuhl {
417*c36e572eSmbuhl 
418*c36e572eSmbuhl 	return (tanl(atanf(x)));
419*c36e572eSmbuhl }
420*c36e572eSmbuhl 
421*c36e572eSmbuhl static long double
422*c36e572eSmbuhl tanatan(double x)
423*c36e572eSmbuhl {
424*c36e572eSmbuhl 
425*c36e572eSmbuhl 	return (tanl(atan(x)));
426*c36e572eSmbuhl }
427*c36e572eSmbuhl 
428*c36e572eSmbuhl static long double
429*c36e572eSmbuhl tanatanl(long double x)
430*c36e572eSmbuhl {
431*c36e572eSmbuhl 
432*c36e572eSmbuhl 	return (tanl(atanl(x)));
433*c36e572eSmbuhl }
434*c36e572eSmbuhl 
435*c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(inverse);
436*c36e572eSmbuhl ATF_TC_BODY(inverse, tc)
437*c36e572eSmbuhl {
438*c36e572eSmbuhl 	float i;
439*c36e572eSmbuhl 
440*c36e572eSmbuhl 	for (i = -1; i <= 1; i += 0x1.0p-12f) {
441*c36e572eSmbuhl 		testall_tol(sinasin, i, i, 2, i == 0 ? 0 : FE_INEXACT);
442*c36e572eSmbuhl 		/* The relative error for cosacos is very large near x=0. */
443*c36e572eSmbuhl 		if (fabsf(i) > 0x1.0p-4f)
444*c36e572eSmbuhl 			testall_tol(cosacos, i, i, 16, i == 1 ? 0 : FE_INEXACT);
445*c36e572eSmbuhl 		testall_tol(tanatan, i, i, 2, i == 0 ? 0 : FE_INEXACT);
446*c36e572eSmbuhl 	}
447*c36e572eSmbuhl }
448*c36e572eSmbuhl 
449*c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
450*c36e572eSmbuhl {
451*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, special);
452*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, special_atan2);
453*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, accuracy);
454*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, p2x_atan2);
455*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, tiny);
456*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, atan_huge);
457*c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, inverse);
458*c36e572eSmbuhl 
459*c36e572eSmbuhl 	return (atf_no_error());
460*c36e572eSmbuhl }
461