invctrig_test.c revision 251119
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: head/tools/regression/lib/msun/test-invctrig.c 251119 2013-05-30 04:46:36Z das $");
33
34#include <assert.h>
35#include <complex.h>
36#include <fenv.h>
37#include <float.h>
38#include <math.h>
39#include <stdio.h>
40
41#define	ALL_STD_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
42			 FE_OVERFLOW | FE_UNDERFLOW)
43#define	OPT_INVALID	(ALL_STD_EXCEPT & ~FE_INVALID)
44#define	OPT_INEXACT	(ALL_STD_EXCEPT & ~FE_INEXACT)
45#define	FLT_ULP()	ldexpl(1.0, 1 - FLT_MANT_DIG)
46#define	DBL_ULP()	ldexpl(1.0, 1 - DBL_MANT_DIG)
47#define	LDBL_ULP()	ldexpl(1.0, 1 - LDBL_MANT_DIG)
48
49#pragma	STDC FENV_ACCESS	ON
50#pragma	STDC CX_LIMITED_RANGE	OFF
51
52/* Flags that determine whether to check the signs of the result. */
53#define	CS_REAL	1
54#define	CS_IMAG	2
55#define	CS_BOTH	(CS_REAL | CS_IMAG)
56
57#ifdef	DEBUG
58#define	debug(...)	printf(__VA_ARGS__)
59#else
60#define	debug(...)	(void)0
61#endif
62
63/*
64 * Test that a function returns the correct value and sets the
65 * exception flags correctly. The exceptmask specifies which
66 * exceptions we should check. We need to be lenient for several
67 * reasons, but mainly because on some architectures it's impossible
68 * to raise FE_OVERFLOW without raising FE_INEXACT.
69 *
70 * These are macros instead of functions so that assert provides more
71 * meaningful error messages.
72 *
73 * XXX The volatile here is to avoid gcc's bogus constant folding and work
74 *     around the lack of support for the FENV_ACCESS pragma.
75 */
76#define	test_p(func, z, result, exceptmask, excepts, checksign)	do {	\
77	volatile long double complex _d = z;				\
78	debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
79	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
80	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
81	assert(cfpequal((func)(_d), (result), (checksign)));		\
82	assert(((func), fetestexcept(exceptmask) == (excepts)));	\
83} while (0)
84
85/*
86 * Test within a given tolerance.  The tolerance indicates relative error
87 * in ulps.
88 */
89#define	test_p_tol(func, z, result, tol)			do {	\
90	volatile long double complex _d = z;				\
91	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
92	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
93	assert(cfpequal_tol((func)(_d), (result), (tol)));		\
94} while (0)
95
96/* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
97#define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
98	test_p(func, z, result, exceptmask, excepts, checksign);	\
99	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
100} while (0)
101#define	test_tol(func, z, result, tol)				do {	\
102	test_p_tol(func, z, result, tol);				\
103	test_p_tol(func, conjl(z), conjl(result), tol);			\
104} while (0)
105
106/* Test the given function in all precisions. */
107#define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
108	test(func, x, result, exceptmask, excepts, checksign);		\
109	test(func##f, x, result, exceptmask, excepts, checksign);	\
110} while (0)
111#define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
112	testall(func, x, result, exceptmask, excepts, checksign);	\
113	testall(func, -(x), -result, exceptmask, excepts, checksign);	\
114} while (0)
115#define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
116	testall(func, x, result, exceptmask, excepts, checksign);	\
117	testall(func, -(x), result, exceptmask, excepts, checksign);	\
118} while (0)
119
120/*
121 * Test the given function in all precisions, within a given tolerance.
122 * The tolerance is specified in ulps.
123 */
124#define	testall_tol(func, x, result, tol)	       		   do { \
125	test_tol(func, x, result, (tol) * DBL_ULP());			\
126	test_tol(func##f, x, result, (tol) * FLT_ULP());		\
127} while (0)
128#define	testall_odd_tol(func, x, result, tol)	       		   do { \
129	testall_tol(func, x, result, tol);				\
130	testall_tol(func, -(x), -result, tol);				\
131} while (0)
132#define	testall_even_tol(func, x, result, tol)	       		   do { \
133	testall_tol(func, x, result, tol);				\
134	testall_tol(func, -(x), result, tol);				\
135} while (0)
136
137static const long double
138pi = 3.14159265358979323846264338327950280L,
139c3pi = 9.42477796076937971538793014983850839L;
140
141/*
142 * Determine whether x and y are equal, with two special rules:
143 *	+0.0 != -0.0
144 *	 NaN == NaN
145 * If checksign is 0, we compare the absolute values instead.
146 */
147static int
148fpequal(long double x, long double y, int checksign)
149{
150	if (isnan(x) && isnan(y))
151		return (1);
152	if (checksign)
153		return (x == y && !signbit(x) == !signbit(y));
154	else
155		return (fabsl(x) == fabsl(y));
156}
157
158static int
159fpequal_tol(long double x, long double y, long double tol)
160{
161	fenv_t env;
162	int ret;
163
164	if (isnan(x) && isnan(y))
165		return (1);
166	if (!signbit(x) != !signbit(y))
167		return (0);
168	if (x == y)
169		return (1);
170	if (tol == 0 || y == 0.0)
171		return (0);
172
173	/* Hard case: need to check the tolerance. */
174	feholdexcept(&env);
175	ret = fabsl(x - y) <= fabsl(y * tol);
176	fesetenv(&env);
177	return (ret);
178}
179
180static int
181cfpequal(long double complex x, long double complex y, int checksign)
182{
183	return (fpequal(creal(x), creal(y), checksign & CS_REAL)
184		&& fpequal(cimag(x), cimag(y), checksign & CS_IMAG));
185}
186
187static int
188cfpequal_tol(long double complex x, long double complex y, long double tol)
189{
190	return (fpequal_tol(creal(x), creal(y), tol)
191		&& fpequal_tol(cimag(x), cimag(y), tol));
192}
193
194
195/* Tests for 0 */
196void
197test_zero(void)
198{
199	long double complex zero = CMPLXL(0.0, 0.0);
200
201	testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
202	testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
203	testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
204	testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
205
206	testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
207	testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
208
209	testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
210	testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
211}
212
213/*
214 * Tests for NaN inputs.
215 */
216void
217test_nan()
218{
219	long double complex nan_nan = CMPLXL(NAN, NAN);
220	long double complex z;
221
222	/*
223	 * IN		CACOSH	    CACOS	CASINH	    CATANH
224	 * NaN,NaN	NaN,NaN	    NaN,NaN	NaN,NaN	    NaN,NaN
225	 * finite,NaN	NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
226	 * NaN,finite   NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
227	 * NaN,Inf	Inf,NaN     NaN,-Inf	?Inf,NaN    ?0,pi/2
228	 * +-Inf,NaN	Inf,NaN     NaN,?Inf	+-Inf,NaN   +-0,NaN
229	 * +-0,NaN	NaN,NaN*    pi/2,NaN	NaN,NaN*    +-0,NaN
230	 * NaN,0	NaN,NaN*    NaN,NaN*	NaN,0	    NaN,NaN*
231	 *
232	 *  * = raise invalid
233	 */
234	z = nan_nan;
235	testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
236	testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
237	testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
238	testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
239	testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
240	testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
241
242	z = CMPLXL(0.5, NAN);
243	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
244	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
245	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
246	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
247	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
248	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
249
250	z = CMPLXL(NAN, 0.5);
251	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
252	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
253	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
254	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
255	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
256	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
257
258	z = CMPLXL(NAN, INFINITY);
259	testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
260	testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
261	testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
262	testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
263	testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
264	testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
265	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
266
267	z = CMPLXL(INFINITY, NAN);
268	testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
269		     CS_REAL);
270	testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
271	testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
272		    CS_REAL);
273	testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
274	testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
275	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
276
277	z = CMPLXL(0.0, NAN);
278        /* XXX We allow a spurious inexact exception here. */
279	testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
280	testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
281	testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
282	testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
283	testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
284	testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
285
286	z = CMPLXL(NAN, 0.0);
287	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
288	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
289	testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
290	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
291	testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
292	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
293}
294
295void
296test_inf(void)
297{
298	long double complex z;
299
300	/*
301	 * IN		CACOSH	    CACOS	CASINH	    CATANH
302	 * Inf,Inf	Inf,pi/4    pi/4,-Inf	Inf,pi/4    0,pi/2
303	 * -Inf,Inf	Inf,3pi/4   3pi/4,-Inf	---	    ---
304	 * Inf,finite	Inf,0	    0,-Inf	Inf,0	    0,pi/2
305	 * -Inf,finite	Inf,pi      pi,-Inf	---	    ---
306	 * finite,Inf	Inf,pi/2    pi/2,-Inf	Inf,pi/2    0,pi/2
307	 */
308	z = CMPLXL(INFINITY, INFINITY);
309	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
310	testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
311	testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
312	testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
313	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
314	testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
315	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
316	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
317
318	z = CMPLXL(INFINITY, 0.5);
319	/* XXX We allow a spurious inexact exception here. */
320	testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
321	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
322	testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
323	testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
324	testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
325	testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
326	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
327	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
328
329	z = CMPLXL(0.5, INFINITY);
330	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
331	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
332	testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
333	testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
334	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
335	/* XXX We allow a spurious inexact exception here. */
336	testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
337	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
338	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
339}
340
341/* Tests along the real and imaginary axes. */
342void
343test_axes(void)
344{
345	static const long double nums[] = {
346		-2, -1, -0.5, 0.5, 1, 2
347	};
348	long double complex z;
349	int i;
350
351	for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
352		/* Real axis */
353		z = CMPLXL(nums[i], 0.0);
354		if (fabs(nums[i]) <= 1) {
355			testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
356			testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
357			testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
358			testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
359		} else {
360			testall_tol(cacosh, z,
361				    CMPLXL(acosh(fabs(nums[i])),
362					   (nums[i] < 0) ? pi : 0), 1);
363			testall_tol(cacos, z,
364				    CMPLXL((nums[i] < 0) ? pi : 0,
365					   -acosh(fabs(nums[i]))), 1);
366			testall_tol(casin, z,
367				    CMPLXL(copysign(pi / 2, nums[i]),
368					   acosh(fabs(nums[i]))), 1);
369			testall_tol(catanh, z,
370				    CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
371		}
372		testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
373		testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
374
375		/* TODO: Test the imaginary axis. */
376	}
377}
378
379void
380test_small(void)
381{
382	/*
383	 * z =  0.75 + i 0.25
384	 *     acos(z) = Pi/4 - i ln(2)/2
385	 *     asin(z) = Pi/4 + i ln(2)/2
386	 *     atan(z) = atan(4)/2 + i ln(17/9)/4
387	 */
388	static const struct {
389		complex long double z;
390		complex long double acos_z;
391		complex long double asin_z;
392		complex long double atan_z;
393	} tests[] = {
394		{ CMPLXL(0.75L, 0.25L),
395		  CMPLXL(pi / 4, -0.34657359027997265470861606072908828L),
396		  CMPLXL(pi / 4, 0.34657359027997265470861606072908828L),
397		  CMPLXL(0.66290883183401623252961960521423782L,
398			 0.15899719167999917436476103600701878L) },
399	};
400	int i;
401
402	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
403		testall_tol(cacos, tests[i].z, tests[i].acos_z, 2);
404		testall_odd_tol(casin, tests[i].z, tests[i].asin_z, 2);
405		testall_odd_tol(catan, tests[i].z, tests[i].atan_z, 2);
406        }
407}
408
409/* Test inputs that might cause overflow in a sloppy implementation. */
410void
411test_large(void)
412{
413
414	/* TODO: Write these tests */
415}
416
417int
418main(int argc, char *argv[])
419{
420
421	printf("1..6\n");
422
423	test_zero();
424	printf("ok 1 - invctrig zero\n");
425
426	test_nan();
427	printf("ok 2 - invctrig nan\n");
428
429	test_inf();
430	printf("ok 3 - invctrig inf\n");
431
432	test_axes();
433	printf("ok 4 - invctrig axes\n");
434
435	test_small();
436	printf("ok 5 - invctrig small\n");
437
438	test_large();
439	printf("ok 6 - invctrig large\n");
440
441	return (0);
442}
443