xref: /freebsd/lib/msun/tests/invctrig_test.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * Copyright (c) 2008-2013 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Tests for casin[h](), cacos[h](), and catan[h]().
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <complex.h>
36 #include <fenv.h>
37 #include <float.h>
38 #include <math.h>
39 #include <stdio.h>
40 
41 #include "test-utils.h"
42 
43 #pragma	STDC FENV_ACCESS	ON
44 #pragma	STDC CX_LIMITED_RANGE	OFF
45 
46 /*
47  * Test that a function returns the correct value and sets the
48  * exception flags correctly. The exceptmask specifies which
49  * exceptions we should check. We need to be lenient for several
50  * reasons, but mainly because on some architectures it's impossible
51  * to raise FE_OVERFLOW without raising FE_INEXACT.
52  *
53  * These are macros instead of functions so that assert provides more
54  * meaningful error messages.
55  *
56  * XXX The volatile here is to avoid gcc's bogus constant folding and work
57  *     around the lack of support for the FENV_ACCESS pragma.
58  */
59 #define	test_p(func, z, result, exceptmask, excepts, checksign)	do {	\
60 	volatile long double complex _d = z;				\
61 	debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
62 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
63 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
64 	CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
65 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
66 	    #func, #z);							\
67 } while (0)
68 
69 /*
70  * Test within a given tolerance.  The tolerance indicates relative error
71  * in ulps.
72  */
73 #define	test_p_tol(func, z, result, tol)			do {	\
74 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
75 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
76 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), CS_BOTH);	\
77 } while (0)
78 
79 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
80 #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
81 	test_p(func, z, result, exceptmask, excepts, checksign);	\
82 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
83 } while (0)
84 #define	test_tol(func, z, result, tol)				do {	\
85 	test_p_tol(func, z, result, tol);				\
86 	test_p_tol(func, conjl(z), conjl(result), tol);			\
87 } while (0)
88 
89 /* Test the given function in all precisions. */
90 #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
91 	test(func, x, result, exceptmask, excepts, checksign);		\
92 	test(func##f, x, result, exceptmask, excepts, checksign);	\
93 } while (0)
94 #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
95 	testall(func, x, result, exceptmask, excepts, checksign);	\
96 	testall(func, -(x), -result, exceptmask, excepts, checksign);	\
97 } while (0)
98 #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
99 	testall(func, x, result, exceptmask, excepts, checksign);	\
100 	testall(func, -(x), result, exceptmask, excepts, checksign);	\
101 } while (0)
102 
103 /*
104  * Test the given function in all precisions, within a given tolerance.
105  * The tolerance is specified in ulps.
106  */
107 #define	testall_tol(func, x, result, tol)	       		   do { \
108 	test_tol(func, x, result, (tol) * DBL_ULP());			\
109 	test_tol(func##f, x, result, (tol) * FLT_ULP());		\
110 } while (0)
111 #define	testall_odd_tol(func, x, result, tol)	       		   do { \
112 	testall_tol(func, x, result, tol);				\
113 	testall_tol(func, -(x), -result, tol);				\
114 } while (0)
115 #define	testall_even_tol(func, x, result, tol)	       		   do { \
116 	testall_tol(func, x, result, tol);				\
117 	testall_tol(func, -(x), result, tol);				\
118 } while (0)
119 
120 static const long double
121 pi = 3.14159265358979323846264338327950280L,
122 c3pi = 9.42477796076937971538793014983850839L;
123 
124 
125 /* Tests for 0 */
126 ATF_TC_WITHOUT_HEAD(zero);
127 ATF_TC_BODY(zero, tc)
128 {
129 	long double complex zero = CMPLXL(0.0, 0.0);
130 
131 	testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
132 	testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
133 	testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
134 	testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
135 
136 	testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
137 	testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
138 
139 	testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
140 	testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
141 }
142 
143 /*
144  * Tests for NaN inputs.
145  */
146 ATF_TC_WITHOUT_HEAD(nan);
147 ATF_TC_BODY(nan, tc)
148 {
149 	long double complex nan_nan = CMPLXL(NAN, NAN);
150 	long double complex z;
151 
152 	/*
153 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
154 	 * NaN,NaN	NaN,NaN	    NaN,NaN	NaN,NaN	    NaN,NaN
155 	 * finite,NaN	NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
156 	 * NaN,finite   NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
157 	 * NaN,Inf	Inf,NaN     NaN,-Inf	?Inf,NaN    ?0,pi/2
158 	 * +-Inf,NaN	Inf,NaN     NaN,?Inf	+-Inf,NaN   +-0,NaN
159 	 * +-0,NaN	NaN,NaN*    pi/2,NaN	NaN,NaN*    +-0,NaN
160 	 * NaN,0	NaN,NaN*    NaN,NaN*	NaN,0	    NaN,NaN*
161 	 *
162 	 *  * = raise invalid
163 	 */
164 	z = nan_nan;
165 	testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
166 	testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
167 	testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
168 	testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
169 	testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
170 	testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
171 
172 	z = CMPLXL(0.5, NAN);
173 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
174 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
175 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
176 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
177 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
178 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
179 
180 	z = CMPLXL(NAN, 0.5);
181 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
182 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
183 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
184 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
185 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
186 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
187 
188 	z = CMPLXL(NAN, INFINITY);
189 	testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
190 	testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
191 	testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
192 	testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
193 	testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
194 	testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
195 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
196 
197 	z = CMPLXL(INFINITY, NAN);
198 	testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
199 		     CS_REAL);
200 	testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
201 	testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
202 		    CS_REAL);
203 	testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
204 	testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
205 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
206 
207 	z = CMPLXL(0.0, NAN);
208         /* XXX We allow a spurious inexact exception here. */
209 	testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
210 	testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
211 	testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
212 	testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
213 	testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
214 	testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
215 
216 	z = CMPLXL(NAN, 0.0);
217 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
218 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
219 	testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
220 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
221 	testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
222 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
223 }
224 
225 ATF_TC_WITHOUT_HEAD(inf);
226 ATF_TC_BODY(inf, tc)
227 {
228 	long double complex z;
229 
230 	/*
231 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
232 	 * Inf,Inf	Inf,pi/4    pi/4,-Inf	Inf,pi/4    0,pi/2
233 	 * -Inf,Inf	Inf,3pi/4   3pi/4,-Inf	---	    ---
234 	 * Inf,finite	Inf,0	    0,-Inf	Inf,0	    0,pi/2
235 	 * -Inf,finite	Inf,pi      pi,-Inf	---	    ---
236 	 * finite,Inf	Inf,pi/2    pi/2,-Inf	Inf,pi/2    0,pi/2
237 	 */
238 	z = CMPLXL(INFINITY, INFINITY);
239 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
240 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
241 	testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
242 	testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
243 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
244 	testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
245 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
246 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
247 
248 	z = CMPLXL(INFINITY, 0.5);
249 	/* XXX We allow a spurious inexact exception here. */
250 	testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
251 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
252 	testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
253 	testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
254 	testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
255 	testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
256 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
257 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
258 
259 	z = CMPLXL(0.5, INFINITY);
260 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
261 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
262 	testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
263 	testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
264 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
265 	/* XXX We allow a spurious inexact exception here. */
266 	testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
267 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
268 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
269 }
270 
271 /* Tests along the real and imaginary axes. */
272 ATF_TC_WITHOUT_HEAD(axes);
273 ATF_TC_BODY(axes, tc)
274 {
275 	static const long double nums[] = {
276 		-2, -1, -0.5, 0.5, 1, 2
277 	};
278 	long double complex z;
279 	unsigned i;
280 
281 	for (i = 0; i < nitems(nums); i++) {
282 		/* Real axis */
283 		z = CMPLXL(nums[i], 0.0);
284 		if (fabsl(nums[i]) <= 1) {
285 			testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
286 			testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
287 			testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
288 			testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
289 		} else {
290 			testall_tol(cacosh, z,
291 				    CMPLXL(acosh(fabsl(nums[i])),
292 					   (nums[i] < 0) ? pi : 0), 1);
293 			testall_tol(cacos, z,
294 				    CMPLXL((nums[i] < 0) ? pi : 0,
295 					   -acosh(fabsl(nums[i]))), 1);
296 			testall_tol(casin, z,
297 				    CMPLXL(copysign(pi / 2, nums[i]),
298 					   acosh(fabsl(nums[i]))), 1);
299 			testall_tol(catanh, z,
300 				    CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
301 		}
302 		testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
303 		testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
304 
305 		/* TODO: Test the imaginary axis. */
306 	}
307 }
308 
309 ATF_TC_WITHOUT_HEAD(small);
310 ATF_TC_BODY(small, tc)
311 {
312 	/*
313 	 * z =  0.75 + i 0.25
314 	 *     acos(z) = Pi/4 - i ln(2)/2
315 	 *     asin(z) = Pi/4 + i ln(2)/2
316 	 *     atan(z) = atan(4)/2 + i ln(17/9)/4
317 	 */
318 	complex long double z;
319 	complex long double acos_z;
320 	complex long double asin_z;
321 	complex long double atan_z;
322 
323 	z = CMPLXL(0.75L, 0.25L);
324 	acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L);
325 	asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L);
326 	atan_z = CMPLXL(0.66290883183401623252961960521423782L,
327 			 0.15899719167999917436476103600701878L);
328 
329 	testall_tol(cacos, z, acos_z, 2);
330 	testall_odd_tol(casin, z, asin_z, 2);
331 	testall_odd_tol(catan, z, atan_z, 2);
332 }
333 
334 /* Test inputs that might cause overflow in a sloppy implementation. */
335 ATF_TC_WITHOUT_HEAD(large);
336 ATF_TC_BODY(large, tc)
337 {
338 	/* TODO: Write these tests */
339 }
340 
341 ATF_TP_ADD_TCS(tp)
342 {
343 	ATF_TP_ADD_TC(tp, zero);
344 	ATF_TP_ADD_TC(tp, nan);
345 	ATF_TP_ADD_TC(tp, inf);
346 	ATF_TP_ADD_TC(tp, axes);
347 	ATF_TP_ADD_TC(tp, small);
348 	ATF_TP_ADD_TC(tp, large);
349 
350 	return (atf_no_error());
351 }
352