xref: /freebsd/lib/msun/tests/invctrig_test.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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/param.h>
32 #include <complex.h>
33 #include <fenv.h>
34 #include <float.h>
35 #include <math.h>
36 #include <stdio.h>
37 
38 #include "test-utils.h"
39 
40 #pragma	STDC FENV_ACCESS	ON
41 #pragma	STDC CX_LIMITED_RANGE	OFF
42 
43 /*
44  * Test that a function returns the correct value and sets the
45  * exception flags correctly. The exceptmask specifies which
46  * exceptions we should check. We need to be lenient for several
47  * reasons, but mainly because on some architectures it's impossible
48  * to raise FE_OVERFLOW without raising FE_INEXACT.
49  *
50  * These are macros instead of functions so that assert provides more
51  * meaningful error messages.
52  *
53  * XXX The volatile here is to avoid gcc's bogus constant folding and work
54  *     around the lack of support for the FENV_ACCESS pragma.
55  */
56 #define	test_p(func, z, result, exceptmask, excepts, checksign)	do {	\
57 	volatile long double complex _d = z;				\
58 	debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
59 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
60 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
61 	CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
62 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
63 	    #func, #z);							\
64 } while (0)
65 
66 /*
67  * Test within a given tolerance.  The tolerance indicates relative error
68  * in ulps.
69  */
70 #define	test_p_tol(func, z, result, tol)			do {	\
71 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
72 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
73 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), CS_BOTH);	\
74 } while (0)
75 
76 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
77 #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
78 	test_p(func, z, result, exceptmask, excepts, checksign);	\
79 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
80 } while (0)
81 #define	test_tol(func, z, result, tol)				do {	\
82 	test_p_tol(func, z, result, tol);				\
83 	test_p_tol(func, conjl(z), conjl(result), tol);			\
84 } while (0)
85 
86 /* Test the given function in all precisions. */
87 #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
88 	test(func, x, result, exceptmask, excepts, checksign);		\
89 	test(func##f, x, result, exceptmask, excepts, checksign);	\
90 } while (0)
91 #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
92 	testall(func, x, result, exceptmask, excepts, checksign);	\
93 	testall(func, -(x), -result, exceptmask, excepts, checksign);	\
94 } while (0)
95 #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
96 	testall(func, x, result, exceptmask, excepts, checksign);	\
97 	testall(func, -(x), result, exceptmask, excepts, checksign);	\
98 } while (0)
99 
100 /*
101  * Test the given function in all precisions, within a given tolerance.
102  * The tolerance is specified in ulps.
103  */
104 #define	testall_tol(func, x, result, tol)	       		   do { \
105 	test_tol(func, x, result, (tol) * DBL_ULP());			\
106 	test_tol(func##f, x, result, (tol) * FLT_ULP());		\
107 } while (0)
108 #define	testall_odd_tol(func, x, result, tol)	       		   do { \
109 	testall_tol(func, x, result, tol);				\
110 	testall_tol(func, -(x), -result, tol);				\
111 } while (0)
112 #define	testall_even_tol(func, x, result, tol)	       		   do { \
113 	testall_tol(func, x, result, tol);				\
114 	testall_tol(func, -(x), result, tol);				\
115 } while (0)
116 
117 static const long double
118 pi = 3.14159265358979323846264338327950280L,
119 c3pi = 9.42477796076937971538793014983850839L;
120 
121 
122 /* Tests for 0 */
123 ATF_TC_WITHOUT_HEAD(zero);
124 ATF_TC_BODY(zero, tc)
125 {
126 	long double complex zero = CMPLXL(0.0, 0.0);
127 
128 	testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
129 	testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
130 	testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
131 	testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
132 
133 	testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
134 	testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
135 
136 	testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
137 	testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
138 }
139 
140 /*
141  * Tests for NaN inputs.
142  */
143 ATF_TC_WITHOUT_HEAD(nan);
144 ATF_TC_BODY(nan, tc)
145 {
146 	long double complex nan_nan = CMPLXL(NAN, NAN);
147 	long double complex z;
148 
149 	/*
150 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
151 	 * NaN,NaN	NaN,NaN	    NaN,NaN	NaN,NaN	    NaN,NaN
152 	 * finite,NaN	NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
153 	 * NaN,finite   NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
154 	 * NaN,Inf	Inf,NaN     NaN,-Inf	?Inf,NaN    ?0,pi/2
155 	 * +-Inf,NaN	Inf,NaN     NaN,?Inf	+-Inf,NaN   +-0,NaN
156 	 * +-0,NaN	NaN,NaN*    pi/2,NaN	NaN,NaN*    +-0,NaN
157 	 * NaN,0	NaN,NaN*    NaN,NaN*	NaN,0	    NaN,NaN*
158 	 *
159 	 *  * = raise invalid
160 	 */
161 	z = nan_nan;
162 	testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
163 	testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
164 	testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
165 	testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
166 	testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
167 	testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
168 
169 	z = CMPLXL(0.5, NAN);
170 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
171 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
172 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
173 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
174 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
175 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
176 
177 	z = CMPLXL(NAN, 0.5);
178 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
179 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
180 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
181 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
182 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
183 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
184 
185 	z = CMPLXL(NAN, INFINITY);
186 	testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
187 	testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
188 	testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
189 	testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
190 	testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
191 	testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
192 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
193 
194 	z = CMPLXL(INFINITY, NAN);
195 	testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
196 		     CS_REAL);
197 	testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
198 	testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
199 		    CS_REAL);
200 	testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
201 	testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
202 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
203 
204 	z = CMPLXL(0.0, NAN);
205         /* XXX We allow a spurious inexact exception here. */
206 	testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
207 	testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
208 	testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
209 	testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
210 	testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
211 	testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
212 
213 	z = CMPLXL(NAN, 0.0);
214 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
215 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
216 	testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
217 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
218 	testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
219 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
220 }
221 
222 ATF_TC_WITHOUT_HEAD(inf);
223 ATF_TC_BODY(inf, tc)
224 {
225 	long double complex z;
226 
227 	/*
228 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
229 	 * Inf,Inf	Inf,pi/4    pi/4,-Inf	Inf,pi/4    0,pi/2
230 	 * -Inf,Inf	Inf,3pi/4   3pi/4,-Inf	---	    ---
231 	 * Inf,finite	Inf,0	    0,-Inf	Inf,0	    0,pi/2
232 	 * -Inf,finite	Inf,pi      pi,-Inf	---	    ---
233 	 * finite,Inf	Inf,pi/2    pi/2,-Inf	Inf,pi/2    0,pi/2
234 	 */
235 	z = CMPLXL(INFINITY, INFINITY);
236 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
237 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
238 	testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
239 	testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
240 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
241 	testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
242 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
243 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
244 
245 	z = CMPLXL(INFINITY, 0.5);
246 	/* XXX We allow a spurious inexact exception here. */
247 	testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
248 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
249 	testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
250 	testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
251 	testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
252 	testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
253 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
254 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
255 
256 	z = CMPLXL(0.5, INFINITY);
257 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
258 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
259 	testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
260 	testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
261 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
262 	/* XXX We allow a spurious inexact exception here. */
263 	testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
264 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
265 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
266 }
267 
268 /* Tests along the real and imaginary axes. */
269 ATF_TC_WITHOUT_HEAD(axes);
270 ATF_TC_BODY(axes, tc)
271 {
272 	static const long double nums[] = {
273 		-2, -1, -0.5, 0.5, 1, 2
274 	};
275 	long double complex z;
276 	unsigned i;
277 
278 	for (i = 0; i < nitems(nums); i++) {
279 		/* Real axis */
280 		z = CMPLXL(nums[i], 0.0);
281 		if (fabsl(nums[i]) <= 1) {
282 			testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
283 			testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
284 			testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
285 			testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
286 		} else {
287 			testall_tol(cacosh, z,
288 				    CMPLXL(acosh(fabsl(nums[i])),
289 					   (nums[i] < 0) ? pi : 0), 1);
290 			testall_tol(cacos, z,
291 				    CMPLXL((nums[i] < 0) ? pi : 0,
292 					   -acosh(fabsl(nums[i]))), 1);
293 			testall_tol(casin, z,
294 				    CMPLXL(copysign(pi / 2, nums[i]),
295 					   acosh(fabsl(nums[i]))), 1);
296 			testall_tol(catanh, z,
297 				    CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
298 		}
299 		testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
300 		testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
301 
302 		/* TODO: Test the imaginary axis. */
303 	}
304 }
305 
306 ATF_TC_WITHOUT_HEAD(small);
307 ATF_TC_BODY(small, tc)
308 {
309 	/*
310 	 * z =  0.75 + i 0.25
311 	 *     acos(z) = Pi/4 - i ln(2)/2
312 	 *     asin(z) = Pi/4 + i ln(2)/2
313 	 *     atan(z) = atan(4)/2 + i ln(17/9)/4
314 	 */
315 	complex long double z;
316 	complex long double acos_z;
317 	complex long double asin_z;
318 	complex long double atan_z;
319 
320 	z = CMPLXL(0.75L, 0.25L);
321 	acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L);
322 	asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L);
323 	atan_z = CMPLXL(0.66290883183401623252961960521423782L,
324 			 0.15899719167999917436476103600701878L);
325 
326 	testall_tol(cacos, z, acos_z, 2);
327 	testall_odd_tol(casin, z, asin_z, 2);
328 	testall_odd_tol(catan, z, atan_z, 2);
329 }
330 
331 /* Test inputs that might cause overflow in a sloppy implementation. */
332 ATF_TC_WITHOUT_HEAD(large);
333 ATF_TC_BODY(large, tc)
334 {
335 	/* TODO: Write these tests */
336 }
337 
338 ATF_TP_ADD_TCS(tp)
339 {
340 	ATF_TP_ADD_TC(tp, zero);
341 	ATF_TP_ADD_TC(tp, nan);
342 	ATF_TP_ADD_TC(tp, inf);
343 	ATF_TP_ADD_TC(tp, axes);
344 	ATF_TP_ADD_TC(tp, small);
345 	ATF_TP_ADD_TC(tp, large);
346 
347 	return (atf_no_error());
348 }
349