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