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