xref: /freebsd/lib/msun/tests/ctrig_test.c (revision 5c4728189373a2c4da449cf742c0bf3ad7369984)
18a7d0e8cSEnji Cooper /*-
28a7d0e8cSEnji Cooper  * Copyright (c) 2008-2011 David Schultz <das@FreeBSD.org>
38a7d0e8cSEnji Cooper  * All rights reserved.
48a7d0e8cSEnji Cooper  *
58a7d0e8cSEnji Cooper  * Redistribution and use in source and binary forms, with or without
68a7d0e8cSEnji Cooper  * modification, are permitted provided that the following conditions
78a7d0e8cSEnji Cooper  * are met:
88a7d0e8cSEnji Cooper  * 1. Redistributions of source code must retain the above copyright
98a7d0e8cSEnji Cooper  *    notice, this list of conditions and the following disclaimer.
108a7d0e8cSEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
118a7d0e8cSEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
128a7d0e8cSEnji Cooper  *    documentation and/or other materials provided with the distribution.
138a7d0e8cSEnji Cooper  *
148a7d0e8cSEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158a7d0e8cSEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168a7d0e8cSEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178a7d0e8cSEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188a7d0e8cSEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198a7d0e8cSEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208a7d0e8cSEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218a7d0e8cSEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228a7d0e8cSEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238a7d0e8cSEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248a7d0e8cSEnji Cooper  * SUCH DAMAGE.
258a7d0e8cSEnji Cooper  */
268a7d0e8cSEnji Cooper 
278a7d0e8cSEnji Cooper /*
288a7d0e8cSEnji Cooper  * Tests for csin[h](), ccos[h](), and ctan[h]().
298a7d0e8cSEnji Cooper  */
308a7d0e8cSEnji Cooper 
318a7d0e8cSEnji Cooper #include <sys/cdefs.h>
328a7d0e8cSEnji Cooper __FBSDID("$FreeBSD$");
338a7d0e8cSEnji Cooper 
348a7d0e8cSEnji Cooper #include <complex.h>
358a7d0e8cSEnji Cooper #include <fenv.h>
368a7d0e8cSEnji Cooper #include <float.h>
378a7d0e8cSEnji Cooper #include <math.h>
388a7d0e8cSEnji Cooper #include <stdio.h>
398a7d0e8cSEnji Cooper 
40*5c472818SEnji Cooper #include <atf-c.h>
41*5c472818SEnji Cooper 
428a7d0e8cSEnji Cooper #include "test-utils.h"
438a7d0e8cSEnji Cooper 
448a7d0e8cSEnji Cooper #pragma STDC FENV_ACCESS	ON
458a7d0e8cSEnji Cooper #pragma	STDC CX_LIMITED_RANGE	OFF
468a7d0e8cSEnji Cooper 
478a7d0e8cSEnji Cooper /*
488a7d0e8cSEnji Cooper  * Test that a function returns the correct value and sets the
498a7d0e8cSEnji Cooper  * exception flags correctly. The exceptmask specifies which
508a7d0e8cSEnji Cooper  * exceptions we should check. We need to be lenient for several
518a7d0e8cSEnji Cooper  * reasons, but mainly because on some architectures it's impossible
528a7d0e8cSEnji Cooper  * to raise FE_OVERFLOW without raising FE_INEXACT.
538a7d0e8cSEnji Cooper  *
548a7d0e8cSEnji Cooper  * These are macros instead of functions so that assert provides more
558a7d0e8cSEnji Cooper  * meaningful error messages.
568a7d0e8cSEnji Cooper  *
578a7d0e8cSEnji Cooper  * XXX The volatile here is to avoid gcc's bogus constant folding and work
588a7d0e8cSEnji Cooper  *     around the lack of support for the FENV_ACCESS pragma.
598a7d0e8cSEnji Cooper  */
608a7d0e8cSEnji Cooper #define	test_p(func, z, result, exceptmask, excepts, checksign)	do {	\
618a7d0e8cSEnji Cooper 	volatile long double complex _d = z;				\
628a7d0e8cSEnji Cooper 	debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
638a7d0e8cSEnji Cooper 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
64*5c472818SEnji Cooper 	ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
65*5c472818SEnji Cooper 	ATF_CHECK(cfpequal_cs((func)(_d), (result), (checksign)));		\
66*5c472818SEnji Cooper 	ATF_CHECK(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
678a7d0e8cSEnji Cooper } while (0)
688a7d0e8cSEnji Cooper 
698a7d0e8cSEnji Cooper /*
708a7d0e8cSEnji Cooper  * Test within a given tolerance.  The tolerance indicates relative error
718a7d0e8cSEnji Cooper  * in ulps.  If result is 0, however, it measures absolute error in units
728a7d0e8cSEnji Cooper  * of <format>_EPSILON.
738a7d0e8cSEnji Cooper  */
748a7d0e8cSEnji Cooper #define	test_p_tol(func, z, result, tol)			do {	\
758a7d0e8cSEnji Cooper 	volatile long double complex _d = z;				\
768a7d0e8cSEnji Cooper 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
778a7d0e8cSEnji Cooper 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
78*5c472818SEnji Cooper 	ATF_CHECK(cfpequal_tol((func)(_d), (result), (tol), FPE_ABS_ZERO)); \
798a7d0e8cSEnji Cooper } while (0)
808a7d0e8cSEnji Cooper 
818a7d0e8cSEnji Cooper /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
828a7d0e8cSEnji Cooper #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
838a7d0e8cSEnji Cooper 	test_p(func, z, result, exceptmask, excepts, checksign);	\
848a7d0e8cSEnji Cooper 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
858a7d0e8cSEnji Cooper } while (0)
868a7d0e8cSEnji Cooper #define	test_tol(func, z, result, tol)				do {	\
878a7d0e8cSEnji Cooper 	test_p_tol(func, z, result, tol);				\
888a7d0e8cSEnji Cooper 	test_p_tol(func, conjl(z), conjl(result), tol);			\
898a7d0e8cSEnji Cooper } while (0)
908a7d0e8cSEnji Cooper #define	test_odd_tol(func, z, result, tol)			do {	\
918a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
928a7d0e8cSEnji Cooper 	test_tol(func, -(z), -(result), tol);				\
938a7d0e8cSEnji Cooper } while (0)
948a7d0e8cSEnji Cooper #define	test_even_tol(func, z, result, tol)			do {	\
958a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
968a7d0e8cSEnji Cooper 	test_tol(func, -(z), result, tol);				\
978a7d0e8cSEnji Cooper } while (0)
988a7d0e8cSEnji Cooper 
998a7d0e8cSEnji Cooper /* Test the given function in all precisions. */
1008a7d0e8cSEnji Cooper #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
1018a7d0e8cSEnji Cooper 	test(func, x, result, exceptmask, excepts, checksign);		\
1028a7d0e8cSEnji Cooper 	test(func##f, x, result, exceptmask, excepts, checksign);	\
1038a7d0e8cSEnji Cooper } while (0)
1048a7d0e8cSEnji Cooper #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
1058a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1068a7d0e8cSEnji Cooper 	testall(func, -x, -result, exceptmask, excepts, checksign);	\
1078a7d0e8cSEnji Cooper } while (0)
1088a7d0e8cSEnji Cooper #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
1098a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1108a7d0e8cSEnji Cooper 	testall(func, -x, result, exceptmask, excepts, checksign);	\
1118a7d0e8cSEnji Cooper } while (0)
1128a7d0e8cSEnji Cooper 
1138a7d0e8cSEnji Cooper /*
1148a7d0e8cSEnji Cooper  * Test the given function in all precisions, within a given tolerance.
1158a7d0e8cSEnji Cooper  * The tolerance is specified in ulps.
1168a7d0e8cSEnji Cooper  */
1178a7d0e8cSEnji Cooper #define	testall_tol(func, x, result, tol)	       		   do { \
1188a7d0e8cSEnji Cooper 	test_tol(func, x, result, tol * DBL_ULP());			\
1198a7d0e8cSEnji Cooper 	test_tol(func##f, x, result, tol * FLT_ULP());			\
1208a7d0e8cSEnji Cooper } while (0)
1218a7d0e8cSEnji Cooper #define	testall_odd_tol(func, x, result, tol)	       		   do { \
1228a7d0e8cSEnji Cooper 	test_odd_tol(func, x, result, tol * DBL_ULP());			\
1238a7d0e8cSEnji Cooper 	test_odd_tol(func##f, x, result, tol * FLT_ULP());		\
1248a7d0e8cSEnji Cooper } while (0)
1258a7d0e8cSEnji Cooper #define	testall_even_tol(func, x, result, tol)	       		   do { \
1268a7d0e8cSEnji Cooper 	test_even_tol(func, x, result, tol * DBL_ULP());		\
1278a7d0e8cSEnji Cooper 	test_even_tol(func##f, x, result, tol * FLT_ULP());		\
1288a7d0e8cSEnji Cooper } while (0)
1298a7d0e8cSEnji Cooper 
1308a7d0e8cSEnji Cooper 
131*5c472818SEnji Cooper ATF_TC(test_zero_input);
132*5c472818SEnji Cooper ATF_TC_HEAD(test_zero_input, tc)
133*5c472818SEnji Cooper {
134*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test 0 input");
135*5c472818SEnji Cooper }
136*5c472818SEnji Cooper ATF_TC_BODY(test_zero_input, tc)
1378a7d0e8cSEnji Cooper {
1388a7d0e8cSEnji Cooper 	long double complex zero = CMPLXL(0.0, 0.0);
1398a7d0e8cSEnji Cooper 
1408a7d0e8cSEnji Cooper 	/* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
1418a7d0e8cSEnji Cooper 	testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1428a7d0e8cSEnji Cooper 	testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1438a7d0e8cSEnji Cooper 	testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
1448a7d0e8cSEnji Cooper 	testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
1458a7d0e8cSEnji Cooper 	testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1468a7d0e8cSEnji Cooper 	testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1478a7d0e8cSEnji Cooper }
1488a7d0e8cSEnji Cooper 
149*5c472818SEnji Cooper ATF_TC(test_nan_inputs);
150*5c472818SEnji Cooper ATF_TC_HEAD(test_nan_inputs, tc)
151*5c472818SEnji Cooper {
152*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test NaN inputs");
153*5c472818SEnji Cooper }
154*5c472818SEnji Cooper ATF_TC_BODY(test_nan_inputs, tc)
1558a7d0e8cSEnji Cooper {
1568a7d0e8cSEnji Cooper 	long double complex nan_nan = CMPLXL(NAN, NAN);
1578a7d0e8cSEnji Cooper 	long double complex z;
1588a7d0e8cSEnji Cooper 
1598a7d0e8cSEnji Cooper 	/*
1608a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
1618a7d0e8cSEnji Cooper 	 * NaN,NaN	NaN,NaN		NaN,NaN		NaN,NaN
1628a7d0e8cSEnji Cooper 	 * finite,NaN	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1638a7d0e8cSEnji Cooper 	 * NaN,finite	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1648a7d0e8cSEnji Cooper 	 * NaN,Inf	NaN,NaN [inval]	NaN,NaN	[inval]	NaN,NaN [inval]
1658a7d0e8cSEnji Cooper 	 * Inf,NaN	+-Inf,NaN	Inf,NaN		1,+-0
1668a7d0e8cSEnji Cooper 	 * 0,NaN	+-0,NaN		NaN,+-0		NaN,NaN	[inval]
1678a7d0e8cSEnji Cooper 	 * NaN,0	NaN,0		NaN,+-0		NaN,0
1688a7d0e8cSEnji Cooper 	 */
1698a7d0e8cSEnji Cooper 	z = nan_nan;
1708a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1718a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1728a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1738a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1748a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1758a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1768a7d0e8cSEnji Cooper 
1778a7d0e8cSEnji Cooper 	z = CMPLXL(42, NAN);
1788a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1798a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1808a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1818a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1828a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1838a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1848a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
1858a7d0e8cSEnji Cooper 
1868a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 42);
1878a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1888a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1898a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
1908a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1918a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1928a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1938a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1948a7d0e8cSEnji Cooper 
1958a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, INFINITY);
1968a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1978a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1988a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
1998a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
2008a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2018a7d0e8cSEnji Cooper 	    CS_IMAG);
2028a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
2038a7d0e8cSEnji Cooper 
2048a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, NAN);
2058a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
2068a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2078a7d0e8cSEnji Cooper 		     CS_REAL);
2088a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2098a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
2108a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
2118a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
2128a7d0e8cSEnji Cooper 
2138a7d0e8cSEnji Cooper 	z = CMPLXL(0, NAN);
2148a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, 0);
2158a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2168a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
2178a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2188a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2198a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2208a7d0e8cSEnji Cooper 
2218a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 0);
2228a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2238a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2248a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2258a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2268a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2278a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
2288a7d0e8cSEnji Cooper }
2298a7d0e8cSEnji Cooper 
230*5c472818SEnji Cooper ATF_TC(test_inf_inputs);
231*5c472818SEnji Cooper ATF_TC_HEAD(test_inf_inputs, tc)
232*5c472818SEnji Cooper {
233*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test infinity inputs");
234*5c472818SEnji Cooper }
235*5c472818SEnji Cooper ATF_TC_BODY(test_inf_inputs, tc)
2368a7d0e8cSEnji Cooper {
2378a7d0e8cSEnji Cooper 	static const long double finites[] = {
2388a7d0e8cSEnji Cooper 	    0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
2398a7d0e8cSEnji Cooper 	};
2408a7d0e8cSEnji Cooper 	long double complex z, c, s;
2418a7d0e8cSEnji Cooper 	int i;
2428a7d0e8cSEnji Cooper 
2438a7d0e8cSEnji Cooper 	/*
2448a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
2458a7d0e8cSEnji Cooper 	 * Inf,Inf	+-Inf,NaN inval	+-Inf,NaN inval	1,+-0
2468a7d0e8cSEnji Cooper 	 * Inf,finite	Inf cis(finite)	Inf cis(finite)	1,0 sin(2 finite)
2478a7d0e8cSEnji Cooper 	 * 0,Inf	+-0,NaN	inval	NaN,+-0 inval	NaN,NaN	inval
2488a7d0e8cSEnji Cooper 	 * finite,Inf	NaN,NaN inval	NaN,NaN inval	NaN,NaN inval
2498a7d0e8cSEnji Cooper 	 */
2508a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, INFINITY);
2518a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN),
2528a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2538a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN),
2548a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2558a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2568a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY),
2578a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2588a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN),
2598a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2608a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
2618a7d0e8cSEnji Cooper 
2628a7d0e8cSEnji Cooper 	/* XXX We allow spurious inexact exceptions here (hard to avoid). */
2638a7d0e8cSEnji Cooper 	for (i = 0; i < sizeof(finites) / sizeof(finites[0]); i++) {
2648a7d0e8cSEnji Cooper 		z = CMPLXL(INFINITY, finites[i]);
2658a7d0e8cSEnji Cooper 		c = INFINITY * cosl(finites[i]);
2668a7d0e8cSEnji Cooper 		s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
2678a7d0e8cSEnji Cooper 		testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2688a7d0e8cSEnji Cooper 		testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2698a7d0e8cSEnji Cooper 		testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)),
2708a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2718a7d0e8cSEnji Cooper 		z = CMPLXL(finites[i], INFINITY);
2728a7d0e8cSEnji Cooper 		testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH);
2738a7d0e8cSEnji Cooper 		testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH);
2748a7d0e8cSEnji Cooper 		testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1),
2758a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2768a7d0e8cSEnji Cooper 	}
2778a7d0e8cSEnji Cooper 
2788a7d0e8cSEnji Cooper 	z = CMPLXL(0, INFINITY);
2798a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2808a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2818a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2828a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 0);
2838a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2848a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2858a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2868a7d0e8cSEnji Cooper 
2878a7d0e8cSEnji Cooper 	z = CMPLXL(42, INFINITY);
2888a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2898a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2908a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2918a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2928a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 42);
2938a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2948a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2958a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2968a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2978a7d0e8cSEnji Cooper }
2988a7d0e8cSEnji Cooper 
299*5c472818SEnji Cooper ATF_TC(test_axes);
300*5c472818SEnji Cooper ATF_TC_HEAD(test_axes, tc)
301*5c472818SEnji Cooper {
302*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test along the real/imaginary axes");
303*5c472818SEnji Cooper }
304*5c472818SEnji Cooper ATF_TC_BODY(test_axes, tc)
3058a7d0e8cSEnji Cooper {
3068a7d0e8cSEnji Cooper 	static const long double nums[] = {
3078a7d0e8cSEnji Cooper 	    M_PI / 4, M_PI / 2, 3 * M_PI / 4,
3088a7d0e8cSEnji Cooper 	    5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
3098a7d0e8cSEnji Cooper 	};
3108a7d0e8cSEnji Cooper 	long double complex z;
3118a7d0e8cSEnji Cooper 	int i;
3128a7d0e8cSEnji Cooper 
3138a7d0e8cSEnji Cooper 	for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
3148a7d0e8cSEnji Cooper 		/* Real axis */
3158a7d0e8cSEnji Cooper 		z = CMPLXL(nums[i], 0.0);
3168a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP());
3178a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP());
3188a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP());
3198a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(sin(nums[i]),
3208a7d0e8cSEnji Cooper 		    copysign(0, cos(nums[i]))), DBL_ULP());
3218a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cos(nums[i]),
3228a7d0e8cSEnji Cooper 		    -copysign(0, sin(nums[i]))), DBL_ULP());
3238a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP());
3248a7d0e8cSEnji Cooper 
3258a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP());
3268a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP());
3278a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(z), cimag(z));
3288a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z)));
3298a7d0e8cSEnji Cooper 		printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY));
3308a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0),
3318a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3328a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]),
3338a7d0e8cSEnji Cooper 		    copysign(0, cosf(nums[i]))), FLT_ULP());
3348a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]),
3358a7d0e8cSEnji Cooper 		    -copysign(0, sinf(nums[i]))), 2 * FLT_ULP());
3368a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP());
3378a7d0e8cSEnji Cooper 
3388a7d0e8cSEnji Cooper 		/* Imaginary axis */
3398a7d0e8cSEnji Cooper 		z = CMPLXL(0.0, nums[i]);
3408a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])),
3418a7d0e8cSEnji Cooper 						 sin(nums[i])), DBL_ULP());
3428a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cos(nums[i]),
3438a7d0e8cSEnji Cooper 		    copysign(0, sin(nums[i]))), DBL_ULP());
3448a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP());
3458a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP());
3468a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP());
3478a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP());
3488a7d0e8cSEnji Cooper 
3498a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])),
3508a7d0e8cSEnji Cooper 						 sinf(nums[i])), FLT_ULP());
3518a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]),
3528a7d0e8cSEnji Cooper 		    copysign(0, sinf(nums[i]))), FLT_ULP());
3538a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP());
3548a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP());
3558a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0),
3568a7d0e8cSEnji Cooper 			      FLT_ULP());
3578a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])),
3588a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3598a7d0e8cSEnji Cooper 	}
3608a7d0e8cSEnji Cooper }
3618a7d0e8cSEnji Cooper 
362*5c472818SEnji Cooper ATF_TC(test_small_inputs);
363*5c472818SEnji Cooper ATF_TC_HEAD(test_small_inputs, tc)
364*5c472818SEnji Cooper {
365*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test underflow inputs");
366*5c472818SEnji Cooper }
367*5c472818SEnji Cooper ATF_TC_BODY(test_small_inputs, tc)
3688a7d0e8cSEnji Cooper {
3698a7d0e8cSEnji Cooper 	/*
3708a7d0e8cSEnji Cooper 	 * z =  0.5 + i Pi/4
3718a7d0e8cSEnji Cooper 	 *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
3728a7d0e8cSEnji Cooper 	 *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
3738a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
3748a7d0e8cSEnji Cooper 	 * z = -0.5 + i Pi/2
3758a7d0e8cSEnji Cooper 	 *     sinh(z) = cosh(0.5)
3768a7d0e8cSEnji Cooper 	 *     cosh(z) = -i sinh(0.5)
3778a7d0e8cSEnji Cooper 	 *     tanh(z) = -coth(0.5)
3788a7d0e8cSEnji Cooper 	 * z =  1.0 + i 3Pi/4
3798a7d0e8cSEnji Cooper 	 *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
3808a7d0e8cSEnji Cooper 	 *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
3818a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
3828a7d0e8cSEnji Cooper 	 */
3838a7d0e8cSEnji Cooper 	static const struct {
3848a7d0e8cSEnji Cooper 		long double a, b;
3858a7d0e8cSEnji Cooper 		long double sinh_a, sinh_b;
3868a7d0e8cSEnji Cooper 		long double cosh_a, cosh_b;
3878a7d0e8cSEnji Cooper 		long double tanh_a, tanh_b;
3888a7d0e8cSEnji Cooper 	} tests[] = {
3898a7d0e8cSEnji Cooper 		{  0.5L,
3908a7d0e8cSEnji Cooper 		   0.78539816339744830961566084581987572L,
3918a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3928a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3938a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3948a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3958a7d0e8cSEnji Cooper 		   0.76159415595576488811945828260479359L,
3968a7d0e8cSEnji Cooper 		   0.64805427366388539957497735322615032L },
3978a7d0e8cSEnji Cooper 		{ -0.5L,
3988a7d0e8cSEnji Cooper 		   1.57079632679489661923132169163975144L,
3998a7d0e8cSEnji Cooper 		   0.0L,
4008a7d0e8cSEnji Cooper 		   1.12762596520638078522622516140267201L,
4018a7d0e8cSEnji Cooper 		   0.0L,
4028a7d0e8cSEnji Cooper 		  -0.52109530549374736162242562641149156L,
4038a7d0e8cSEnji Cooper 		  -2.16395341373865284877000401021802312L,
4048a7d0e8cSEnji Cooper 		   0.0L },
4058a7d0e8cSEnji Cooper 		{  1.0L,
4068a7d0e8cSEnji Cooper 		   2.35619449019234492884698253745962716L,
4078a7d0e8cSEnji Cooper 		  -0.83099273328405698212637979852748608L,
4088a7d0e8cSEnji Cooper 		   1.09112278079550143030545602018565236L,
4098a7d0e8cSEnji Cooper 		  -1.09112278079550143030545602018565236L,
4108a7d0e8cSEnji Cooper 		   0.83099273328405698212637979852748609L,
4118a7d0e8cSEnji Cooper 		   0.96402758007581688394641372410092315L,
4128a7d0e8cSEnji Cooper 		  -0.26580222883407969212086273981988897L }
4138a7d0e8cSEnji Cooper 	};
4148a7d0e8cSEnji Cooper 	long double complex z;
4158a7d0e8cSEnji Cooper 	int i;
4168a7d0e8cSEnji Cooper 
4178a7d0e8cSEnji Cooper 	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
4188a7d0e8cSEnji Cooper 		z = CMPLXL(tests[i].a, tests[i].b);
4198a7d0e8cSEnji Cooper 		testall_odd_tol(csinh, z,
4208a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1);
4218a7d0e8cSEnji Cooper 		testall_even_tol(ccosh, z,
4228a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1);
4238a7d0e8cSEnji Cooper 		testall_odd_tol(ctanh, z,
4248a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4);
4258a7d0e8cSEnji Cooper         }
4268a7d0e8cSEnji Cooper }
4278a7d0e8cSEnji Cooper 
428*5c472818SEnji Cooper ATF_TC(test_large_inputs);
429*5c472818SEnji Cooper ATF_TC_HEAD(test_large_inputs, tc)
430*5c472818SEnji Cooper {
431*5c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
432*5c472818SEnji Cooper 	    "Test inputs that might cause overflow in a sloppy implementation");
433*5c472818SEnji Cooper }
434*5c472818SEnji Cooper ATF_TC_BODY(test_large_inputs, tc)
4358a7d0e8cSEnji Cooper {
4368a7d0e8cSEnji Cooper 	long double complex z;
4378a7d0e8cSEnji Cooper 
438*5c472818SEnji Cooper #ifdef	__i386__
439*5c472818SEnji Cooper 	atf_tc_expect_fail("test fails on i386 - bug 205446");
440*5c472818SEnji Cooper #endif
441*5c472818SEnji Cooper 
4428a7d0e8cSEnji Cooper 	/* tanh() uses a threshold around x=22, so check both sides. */
4438a7d0e8cSEnji Cooper 	z = CMPLXL(21, 0.78539816339744830961566084581987572L);
4448a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4458a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2);
4468a7d0e8cSEnji Cooper 	z++;
4478a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4488a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1);
4498a7d0e8cSEnji Cooper 
4508a7d0e8cSEnji Cooper 	z = CMPLXL(355, 0.78539816339744830961566084581987572L);
4518a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4528a7d0e8cSEnji Cooper 		     CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L),
4538a7d0e8cSEnji Cooper 		     DBL_ULP());
4548a7d0e8cSEnji Cooper 	z = CMPLXL(30, 0x1p1023L);
4558a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4568a7d0e8cSEnji Cooper 		     CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L),
4578a7d0e8cSEnji Cooper 		     DBL_ULP());
4588a7d0e8cSEnji Cooper 	z = CMPLXL(1, 0x1p1023L);
4598a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4608a7d0e8cSEnji Cooper 		     CMPLXL(0.878606311888306869546254022621986509L,
4618a7d0e8cSEnji Cooper 			    -0.225462792499754505792678258169527424L),
4628a7d0e8cSEnji Cooper 		     DBL_ULP());
4638a7d0e8cSEnji Cooper 
4648a7d0e8cSEnji Cooper 	z = CMPLXL(710.6, 0.78539816339744830961566084581987572L);
4658a7d0e8cSEnji Cooper 	test_odd_tol(csinh, z,
4668a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4678a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4688a7d0e8cSEnji Cooper 	test_even_tol(ccosh, z,
4698a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4708a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4718a7d0e8cSEnji Cooper 
4728a7d0e8cSEnji Cooper 	z = CMPLXL(1500, 0.78539816339744830961566084581987572L);
4738a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4748a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4758a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4768a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4778a7d0e8cSEnji Cooper }
4788a7d0e8cSEnji Cooper 
479*5c472818SEnji Cooper ATF_TP_ADD_TCS(tp)
4808a7d0e8cSEnji Cooper {
4818a7d0e8cSEnji Cooper 
482*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_zero_input);
483*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_nan_inputs);
484*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_inf_inputs);
485*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_axes);
486*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_small_inputs);
487*5c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_large_inputs);
4888a7d0e8cSEnji Cooper 
489*5c472818SEnji Cooper 	return (atf_no_error());
4908a7d0e8cSEnji Cooper }
491