xref: /freebsd/lib/msun/tests/ctrig_test.c (revision b424e0038a00dffbec800a6f0778db0ffdabe9d6)
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 
34abe427afSEnji Cooper #include <sys/param.h>
358a7d0e8cSEnji Cooper #include <complex.h>
368a7d0e8cSEnji Cooper #include <fenv.h>
378a7d0e8cSEnji Cooper #include <float.h>
388a7d0e8cSEnji Cooper #include <math.h>
398a7d0e8cSEnji Cooper #include <stdio.h>
408a7d0e8cSEnji Cooper 
418a7d0e8cSEnji Cooper #include "test-utils.h"
428a7d0e8cSEnji Cooper 
438a7d0e8cSEnji Cooper #pragma STDC FENV_ACCESS	ON
448a7d0e8cSEnji Cooper #pragma	STDC CX_LIMITED_RANGE	OFF
458a7d0e8cSEnji Cooper 
468a7d0e8cSEnji Cooper /*
478a7d0e8cSEnji Cooper  * Test that a function returns the correct value and sets the
488a7d0e8cSEnji Cooper  * exception flags correctly. The exceptmask specifies which
498a7d0e8cSEnji Cooper  * exceptions we should check. We need to be lenient for several
508a7d0e8cSEnji Cooper  * reasons, but mainly because on some architectures it's impossible
518a7d0e8cSEnji Cooper  * to raise FE_OVERFLOW without raising FE_INEXACT.
528a7d0e8cSEnji Cooper  *
538a7d0e8cSEnji Cooper  * These are macros instead of functions so that assert provides more
548a7d0e8cSEnji Cooper  * meaningful error messages.
558a7d0e8cSEnji Cooper  *
568a7d0e8cSEnji Cooper  * XXX The volatile here is to avoid gcc's bogus constant folding and work
578a7d0e8cSEnji Cooper  *     around the lack of support for the FENV_ACCESS pragma.
588a7d0e8cSEnji Cooper  */
59f3f7b0dcSAlex Richardson #define test_p(func, z, result, exceptmask, excepts, checksign)			\
60f3f7b0dcSAlex Richardson 	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));	\
645c472818SEnji Cooper 		ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
65*b424e003SAlex Richardson 		CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
66f3f7b0dcSAlex Richardson 		volatile int _e = fetestexcept(exceptmask);			\
67f3f7b0dcSAlex Richardson 		ATF_CHECK_MSG(_e == (excepts),					\
68f3f7b0dcSAlex Richardson 		    "%s fetestexcept(%s) (%#x) != %#x",	__XSTRING(func),	\
69f3f7b0dcSAlex Richardson 		    __XSTRING(exceptmask), _e, (excepts));			\
708a7d0e8cSEnji Cooper 	} while (0)
718a7d0e8cSEnji Cooper 
728a7d0e8cSEnji Cooper /*
738a7d0e8cSEnji Cooper  * Test within a given tolerance.  The tolerance indicates relative error
748a7d0e8cSEnji Cooper  * in ulps.  If result is 0, however, it measures absolute error in units
758a7d0e8cSEnji Cooper  * of <format>_EPSILON.
768a7d0e8cSEnji Cooper  */
778a7d0e8cSEnji Cooper #define	test_p_tol(func, z, result, tol)			do {	\
788a7d0e8cSEnji Cooper 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
79*b424e003SAlex Richardson 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
80*b424e003SAlex Richardson 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), FPE_ABS_ZERO); \
818a7d0e8cSEnji Cooper } while (0)
828a7d0e8cSEnji Cooper 
838a7d0e8cSEnji Cooper /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
848a7d0e8cSEnji Cooper #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
858a7d0e8cSEnji Cooper 	test_p(func, z, result, exceptmask, excepts, checksign);	\
868a7d0e8cSEnji Cooper 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
878a7d0e8cSEnji Cooper } while (0)
888a7d0e8cSEnji Cooper #define	test_tol(func, z, result, tol)				do {	\
898a7d0e8cSEnji Cooper 	test_p_tol(func, z, result, tol);				\
908a7d0e8cSEnji Cooper 	test_p_tol(func, conjl(z), conjl(result), tol);			\
918a7d0e8cSEnji Cooper } while (0)
928a7d0e8cSEnji Cooper #define	test_odd_tol(func, z, result, tol)			do {	\
938a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
948a7d0e8cSEnji Cooper 	test_tol(func, -(z), -(result), tol);				\
958a7d0e8cSEnji Cooper } while (0)
968a7d0e8cSEnji Cooper #define	test_even_tol(func, z, result, tol)			do {	\
978a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
988a7d0e8cSEnji Cooper 	test_tol(func, -(z), result, tol);				\
998a7d0e8cSEnji Cooper } while (0)
1008a7d0e8cSEnji Cooper 
1018a7d0e8cSEnji Cooper /* Test the given function in all precisions. */
1028a7d0e8cSEnji Cooper #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
1038a7d0e8cSEnji Cooper 	test(func, x, result, exceptmask, excepts, checksign);		\
1048a7d0e8cSEnji Cooper 	test(func##f, x, result, exceptmask, excepts, checksign);	\
1058a7d0e8cSEnji Cooper } while (0)
1068a7d0e8cSEnji Cooper #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
1078a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1088a7d0e8cSEnji Cooper 	testall(func, -x, -result, exceptmask, excepts, checksign);	\
1098a7d0e8cSEnji Cooper } while (0)
1108a7d0e8cSEnji Cooper #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
1118a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1128a7d0e8cSEnji Cooper 	testall(func, -x, result, exceptmask, excepts, checksign);	\
1138a7d0e8cSEnji Cooper } while (0)
1148a7d0e8cSEnji Cooper 
1158a7d0e8cSEnji Cooper /*
1168a7d0e8cSEnji Cooper  * Test the given function in all precisions, within a given tolerance.
1178a7d0e8cSEnji Cooper  * The tolerance is specified in ulps.
1188a7d0e8cSEnji Cooper  */
1198a7d0e8cSEnji Cooper #define	testall_tol(func, x, result, tol)	       		   do { \
1208a7d0e8cSEnji Cooper 	test_tol(func, x, result, tol * DBL_ULP());			\
1218a7d0e8cSEnji Cooper 	test_tol(func##f, x, result, tol * FLT_ULP());			\
1228a7d0e8cSEnji Cooper } while (0)
1238a7d0e8cSEnji Cooper #define	testall_odd_tol(func, x, result, tol)	       		   do { \
1248a7d0e8cSEnji Cooper 	test_odd_tol(func, x, result, tol * DBL_ULP());			\
1258a7d0e8cSEnji Cooper 	test_odd_tol(func##f, x, result, tol * FLT_ULP());		\
1268a7d0e8cSEnji Cooper } while (0)
1278a7d0e8cSEnji Cooper #define	testall_even_tol(func, x, result, tol)	       		   do { \
1288a7d0e8cSEnji Cooper 	test_even_tol(func, x, result, tol * DBL_ULP());		\
1298a7d0e8cSEnji Cooper 	test_even_tol(func##f, x, result, tol * FLT_ULP());		\
1308a7d0e8cSEnji Cooper } while (0)
1318a7d0e8cSEnji Cooper 
1328a7d0e8cSEnji Cooper 
1335c472818SEnji Cooper ATF_TC(test_zero_input);
1345c472818SEnji Cooper ATF_TC_HEAD(test_zero_input, tc)
1355c472818SEnji Cooper {
1365c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test 0 input");
1375c472818SEnji Cooper }
1385c472818SEnji Cooper ATF_TC_BODY(test_zero_input, tc)
1398a7d0e8cSEnji Cooper {
1408a7d0e8cSEnji Cooper 	long double complex zero = CMPLXL(0.0, 0.0);
1418a7d0e8cSEnji Cooper 
1428a7d0e8cSEnji Cooper 	/* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
1438a7d0e8cSEnji Cooper 	testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1448a7d0e8cSEnji Cooper 	testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1458a7d0e8cSEnji Cooper 	testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
1468a7d0e8cSEnji Cooper 	testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
1478a7d0e8cSEnji Cooper 	testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1488a7d0e8cSEnji Cooper 	testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1498a7d0e8cSEnji Cooper }
1508a7d0e8cSEnji Cooper 
1515c472818SEnji Cooper ATF_TC(test_nan_inputs);
1525c472818SEnji Cooper ATF_TC_HEAD(test_nan_inputs, tc)
1535c472818SEnji Cooper {
1545c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test NaN inputs");
1555c472818SEnji Cooper }
1565c472818SEnji Cooper ATF_TC_BODY(test_nan_inputs, tc)
1578a7d0e8cSEnji Cooper {
1588a7d0e8cSEnji Cooper 	long double complex nan_nan = CMPLXL(NAN, NAN);
1598a7d0e8cSEnji Cooper 	long double complex z;
1608a7d0e8cSEnji Cooper 
1618a7d0e8cSEnji Cooper 	/*
1628a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
1638a7d0e8cSEnji Cooper 	 * NaN,NaN	NaN,NaN		NaN,NaN		NaN,NaN
1648a7d0e8cSEnji Cooper 	 * finite,NaN	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1658a7d0e8cSEnji Cooper 	 * NaN,finite	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1668a7d0e8cSEnji Cooper 	 * NaN,Inf	NaN,NaN [inval]	NaN,NaN	[inval]	NaN,NaN [inval]
1678a7d0e8cSEnji Cooper 	 * Inf,NaN	+-Inf,NaN	Inf,NaN		1,+-0
168a7b42c4bSAlex Richardson 	 * 0,NaN	+-0,NaN		NaN,+-0		+-0,NaN
169a7b42c4bSAlex Richardson 	 * NaN,0	NaN,0		NaN,+-0		NaN,+-0
1708a7d0e8cSEnji Cooper 	 */
1718a7d0e8cSEnji Cooper 	z = nan_nan;
1728a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1738a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1748a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1758a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1768a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1778a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1788a7d0e8cSEnji Cooper 
1798a7d0e8cSEnji Cooper 	z = CMPLXL(42, NAN);
1808a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1818a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1828a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1838a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1848a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1858a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1868a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
1878a7d0e8cSEnji Cooper 
1888a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 42);
1898a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1908a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1918a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
1928a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1938a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1948a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1958a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1968a7d0e8cSEnji Cooper 
1978a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, INFINITY);
1988a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1998a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
2008a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
2018a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
2028a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2038a7d0e8cSEnji Cooper 	    CS_IMAG);
2048a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
2058a7d0e8cSEnji Cooper 
2068a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, NAN);
2078a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
2088a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2098a7d0e8cSEnji Cooper 		     CS_REAL);
2108a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2118a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
2128a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
2138a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
2148a7d0e8cSEnji Cooper 
2158a7d0e8cSEnji Cooper 	z = CMPLXL(0, NAN);
216a7b42c4bSAlex Richardson 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2178a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
218a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), OPT_INVALID, 0, CS_REAL);
2198a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2208a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2218a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2228a7d0e8cSEnji Cooper 
2238a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 0);
2248a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2258a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2268a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2278a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2288a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
229a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2308a7d0e8cSEnji Cooper }
2318a7d0e8cSEnji Cooper 
2325c472818SEnji Cooper ATF_TC(test_inf_inputs);
2335c472818SEnji Cooper ATF_TC_HEAD(test_inf_inputs, tc)
2345c472818SEnji Cooper {
2355c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test infinity inputs");
2365c472818SEnji Cooper }
2375c472818SEnji Cooper ATF_TC_BODY(test_inf_inputs, tc)
2388a7d0e8cSEnji Cooper {
2398a7d0e8cSEnji Cooper 	static const long double finites[] = {
2408a7d0e8cSEnji Cooper 	    0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
2418a7d0e8cSEnji Cooper 	};
2428a7d0e8cSEnji Cooper 	long double complex z, c, s;
243abe427afSEnji Cooper 	unsigned i;
2448a7d0e8cSEnji Cooper 
2458a7d0e8cSEnji Cooper 	/*
2468a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
2478a7d0e8cSEnji Cooper 	 * Inf,Inf	+-Inf,NaN inval	+-Inf,NaN inval	1,+-0
2488a7d0e8cSEnji Cooper 	 * Inf,finite	Inf cis(finite)	Inf cis(finite)	1,0 sin(2 finite)
249a7b42c4bSAlex Richardson 	 * 0,Inf	+-0,NaN	inval	NaN,+-0 inval	+-0,NaN
2508a7d0e8cSEnji Cooper 	 * finite,Inf	NaN,NaN inval	NaN,NaN inval	NaN,NaN inval
2518a7d0e8cSEnji Cooper 	 */
2528a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, INFINITY);
2538a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN),
2548a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2558a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN),
2568a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2578a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2588a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY),
2598a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2608a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN),
2618a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2628a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
2638a7d0e8cSEnji Cooper 
2648a7d0e8cSEnji Cooper 	/* XXX We allow spurious inexact exceptions here (hard to avoid). */
265abe427afSEnji Cooper 	for (i = 0; i < nitems(finites); i++) {
2668a7d0e8cSEnji Cooper 		z = CMPLXL(INFINITY, finites[i]);
2678a7d0e8cSEnji Cooper 		c = INFINITY * cosl(finites[i]);
2688a7d0e8cSEnji Cooper 		s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
2698a7d0e8cSEnji Cooper 		testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2708a7d0e8cSEnji Cooper 		testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2718a7d0e8cSEnji Cooper 		testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)),
2728a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2738a7d0e8cSEnji Cooper 		z = CMPLXL(finites[i], INFINITY);
2748a7d0e8cSEnji Cooper 		testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH);
2758a7d0e8cSEnji Cooper 		testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH);
2768a7d0e8cSEnji Cooper 		testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1),
2778a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2788a7d0e8cSEnji Cooper 	}
2798a7d0e8cSEnji Cooper 
2808a7d0e8cSEnji Cooper 	z = CMPLXL(0, INFINITY);
2818a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2828a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
283a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, CS_REAL);
2848a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 0);
2858a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2868a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
287a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, CS_IMAG);
2888a7d0e8cSEnji Cooper 
2898a7d0e8cSEnji Cooper 	z = CMPLXL(42, INFINITY);
2908a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2918a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2928a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2938a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2948a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 42);
2958a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2968a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2978a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2988a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2998a7d0e8cSEnji Cooper }
3008a7d0e8cSEnji Cooper 
3015c472818SEnji Cooper ATF_TC(test_axes);
3025c472818SEnji Cooper ATF_TC_HEAD(test_axes, tc)
3035c472818SEnji Cooper {
3045c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test along the real/imaginary axes");
3055c472818SEnji Cooper }
3065c472818SEnji Cooper ATF_TC_BODY(test_axes, tc)
3078a7d0e8cSEnji Cooper {
3088a7d0e8cSEnji Cooper 	static const long double nums[] = {
3098a7d0e8cSEnji Cooper 	    M_PI / 4, M_PI / 2, 3 * M_PI / 4,
3108a7d0e8cSEnji Cooper 	    5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
3118a7d0e8cSEnji Cooper 	};
3128a7d0e8cSEnji Cooper 	long double complex z;
313abe427afSEnji Cooper 	unsigned i;
3148a7d0e8cSEnji Cooper 
315abe427afSEnji Cooper 	for (i = 0; i < nitems(nums); i++) {
3168a7d0e8cSEnji Cooper 		/* Real axis */
3178a7d0e8cSEnji Cooper 		z = CMPLXL(nums[i], 0.0);
3188a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP());
3198a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP());
3208a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP());
3218a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(sin(nums[i]),
3228a7d0e8cSEnji Cooper 		    copysign(0, cos(nums[i]))), DBL_ULP());
3238a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cos(nums[i]),
3248a7d0e8cSEnji Cooper 		    -copysign(0, sin(nums[i]))), DBL_ULP());
3258a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP());
3268a7d0e8cSEnji Cooper 
3278a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP());
3288a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP());
3298a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(z), cimag(z));
3308a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z)));
3318a7d0e8cSEnji Cooper 		printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY));
3328a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0),
3338a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3348a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]),
3358a7d0e8cSEnji Cooper 		    copysign(0, cosf(nums[i]))), FLT_ULP());
3368a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]),
3378a7d0e8cSEnji Cooper 		    -copysign(0, sinf(nums[i]))), 2 * FLT_ULP());
3388a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP());
3398a7d0e8cSEnji Cooper 
3408a7d0e8cSEnji Cooper 		/* Imaginary axis */
3418a7d0e8cSEnji Cooper 		z = CMPLXL(0.0, nums[i]);
3428a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])),
3438a7d0e8cSEnji Cooper 						 sin(nums[i])), DBL_ULP());
3448a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cos(nums[i]),
3458a7d0e8cSEnji Cooper 		    copysign(0, sin(nums[i]))), DBL_ULP());
3468a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP());
3478a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP());
3488a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP());
3498a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP());
3508a7d0e8cSEnji Cooper 
3518a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])),
3528a7d0e8cSEnji Cooper 						 sinf(nums[i])), FLT_ULP());
3538a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]),
3548a7d0e8cSEnji Cooper 		    copysign(0, sinf(nums[i]))), FLT_ULP());
3558a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP());
3568a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP());
3578a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0),
3588a7d0e8cSEnji Cooper 			      FLT_ULP());
3598a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])),
3608a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3618a7d0e8cSEnji Cooper 	}
3628a7d0e8cSEnji Cooper }
3638a7d0e8cSEnji Cooper 
3645c472818SEnji Cooper ATF_TC(test_small_inputs);
3655c472818SEnji Cooper ATF_TC_HEAD(test_small_inputs, tc)
3665c472818SEnji Cooper {
3675c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test underflow inputs");
3685c472818SEnji Cooper }
3695c472818SEnji Cooper ATF_TC_BODY(test_small_inputs, tc)
3708a7d0e8cSEnji Cooper {
3718a7d0e8cSEnji Cooper 	/*
3728a7d0e8cSEnji Cooper 	 * z =  0.5 + i Pi/4
3738a7d0e8cSEnji Cooper 	 *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
3748a7d0e8cSEnji Cooper 	 *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
3758a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
3768a7d0e8cSEnji Cooper 	 * z = -0.5 + i Pi/2
3778a7d0e8cSEnji Cooper 	 *     sinh(z) = cosh(0.5)
3788a7d0e8cSEnji Cooper 	 *     cosh(z) = -i sinh(0.5)
3798a7d0e8cSEnji Cooper 	 *     tanh(z) = -coth(0.5)
3808a7d0e8cSEnji Cooper 	 * z =  1.0 + i 3Pi/4
3818a7d0e8cSEnji Cooper 	 *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
3828a7d0e8cSEnji Cooper 	 *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
3838a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
3848a7d0e8cSEnji Cooper 	 */
3858a7d0e8cSEnji Cooper 	static const struct {
3868a7d0e8cSEnji Cooper 		long double a, b;
3878a7d0e8cSEnji Cooper 		long double sinh_a, sinh_b;
3888a7d0e8cSEnji Cooper 		long double cosh_a, cosh_b;
3898a7d0e8cSEnji Cooper 		long double tanh_a, tanh_b;
3908a7d0e8cSEnji Cooper 	} tests[] = {
3918a7d0e8cSEnji Cooper 		{  0.5L,
3928a7d0e8cSEnji Cooper 		   0.78539816339744830961566084581987572L,
3938a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3948a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3958a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3968a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3978a7d0e8cSEnji Cooper 		   0.76159415595576488811945828260479359L,
3988a7d0e8cSEnji Cooper 		   0.64805427366388539957497735322615032L },
3998a7d0e8cSEnji Cooper 		{ -0.5L,
4008a7d0e8cSEnji Cooper 		   1.57079632679489661923132169163975144L,
4018a7d0e8cSEnji Cooper 		   0.0L,
4028a7d0e8cSEnji Cooper 		   1.12762596520638078522622516140267201L,
4038a7d0e8cSEnji Cooper 		   0.0L,
4048a7d0e8cSEnji Cooper 		  -0.52109530549374736162242562641149156L,
4058a7d0e8cSEnji Cooper 		  -2.16395341373865284877000401021802312L,
4068a7d0e8cSEnji Cooper 		   0.0L },
4078a7d0e8cSEnji Cooper 		{  1.0L,
4088a7d0e8cSEnji Cooper 		   2.35619449019234492884698253745962716L,
4098a7d0e8cSEnji Cooper 		  -0.83099273328405698212637979852748608L,
4108a7d0e8cSEnji Cooper 		   1.09112278079550143030545602018565236L,
4118a7d0e8cSEnji Cooper 		  -1.09112278079550143030545602018565236L,
4128a7d0e8cSEnji Cooper 		   0.83099273328405698212637979852748609L,
4138a7d0e8cSEnji Cooper 		   0.96402758007581688394641372410092315L,
4148a7d0e8cSEnji Cooper 		  -0.26580222883407969212086273981988897L }
4158a7d0e8cSEnji Cooper 	};
4168a7d0e8cSEnji Cooper 	long double complex z;
417abe427afSEnji Cooper 	unsigned i;
4188a7d0e8cSEnji Cooper 
419abe427afSEnji Cooper 	for (i = 0; i < nitems(tests); i++) {
4208a7d0e8cSEnji Cooper 		z = CMPLXL(tests[i].a, tests[i].b);
4218a7d0e8cSEnji Cooper 		testall_odd_tol(csinh, z,
4228a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1);
4238a7d0e8cSEnji Cooper 		testall_even_tol(ccosh, z,
4248a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1);
4258a7d0e8cSEnji Cooper 		testall_odd_tol(ctanh, z,
4268a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4);
4278a7d0e8cSEnji Cooper         }
4288a7d0e8cSEnji Cooper }
4298a7d0e8cSEnji Cooper 
4305c472818SEnji Cooper ATF_TC(test_large_inputs);
4315c472818SEnji Cooper ATF_TC_HEAD(test_large_inputs, tc)
4325c472818SEnji Cooper {
4335c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
4345c472818SEnji Cooper 	    "Test inputs that might cause overflow in a sloppy implementation");
4355c472818SEnji Cooper }
4365c472818SEnji Cooper ATF_TC_BODY(test_large_inputs, tc)
4378a7d0e8cSEnji Cooper {
4388a7d0e8cSEnji Cooper 	long double complex z;
4398a7d0e8cSEnji Cooper 
4408a7d0e8cSEnji Cooper 	/* tanh() uses a threshold around x=22, so check both sides. */
4418a7d0e8cSEnji Cooper 	z = CMPLXL(21, 0.78539816339744830961566084581987572L);
4428a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4438a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2);
4448a7d0e8cSEnji Cooper 	z++;
4458a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4468a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1);
4478a7d0e8cSEnji Cooper 
4488a7d0e8cSEnji Cooper 	z = CMPLXL(355, 0.78539816339744830961566084581987572L);
4498a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4508a7d0e8cSEnji Cooper 		     CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L),
4518a7d0e8cSEnji Cooper 		     DBL_ULP());
4528a7d0e8cSEnji Cooper 	z = CMPLXL(30, 0x1p1023L);
4538a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4548a7d0e8cSEnji Cooper 		     CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L),
4558a7d0e8cSEnji Cooper 		     DBL_ULP());
4568a7d0e8cSEnji Cooper 	z = CMPLXL(1, 0x1p1023L);
4578a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4588a7d0e8cSEnji Cooper 		     CMPLXL(0.878606311888306869546254022621986509L,
4598a7d0e8cSEnji Cooper 			    -0.225462792499754505792678258169527424L),
4608a7d0e8cSEnji Cooper 		     DBL_ULP());
4618a7d0e8cSEnji Cooper 
4628a7d0e8cSEnji Cooper 	z = CMPLXL(710.6, 0.78539816339744830961566084581987572L);
4638a7d0e8cSEnji Cooper 	test_odd_tol(csinh, z,
4648a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4658a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4668a7d0e8cSEnji Cooper 	test_even_tol(ccosh, z,
4678a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4688a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4698a7d0e8cSEnji Cooper 
4708a7d0e8cSEnji Cooper 	z = CMPLXL(1500, 0.78539816339744830961566084581987572L);
4718a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4728a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4738a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4748a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4758a7d0e8cSEnji Cooper }
4768a7d0e8cSEnji Cooper 
4775c472818SEnji Cooper ATF_TP_ADD_TCS(tp)
4788a7d0e8cSEnji Cooper {
4798a7d0e8cSEnji Cooper 
4805c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_zero_input);
4815c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_nan_inputs);
4825c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_inf_inputs);
4835c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_axes);
4845c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_small_inputs);
4855c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_large_inputs);
4868a7d0e8cSEnji Cooper 
4875c472818SEnji Cooper 	return (atf_no_error());
4888a7d0e8cSEnji Cooper }
489