xref: /freebsd/lib/msun/tests/ctrig_test.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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 
31abe427afSEnji Cooper #include <sys/param.h>
328a7d0e8cSEnji Cooper #include <complex.h>
338a7d0e8cSEnji Cooper #include <fenv.h>
348a7d0e8cSEnji Cooper #include <float.h>
358a7d0e8cSEnji Cooper #include <math.h>
368a7d0e8cSEnji Cooper #include <stdio.h>
378a7d0e8cSEnji Cooper 
388a7d0e8cSEnji Cooper #include "test-utils.h"
398a7d0e8cSEnji Cooper 
408a7d0e8cSEnji Cooper #pragma STDC FENV_ACCESS	ON
418a7d0e8cSEnji Cooper #pragma	STDC CX_LIMITED_RANGE	OFF
428a7d0e8cSEnji Cooper 
438a7d0e8cSEnji Cooper /*
448a7d0e8cSEnji Cooper  * Test that a function returns the correct value and sets the
458a7d0e8cSEnji Cooper  * exception flags correctly. The exceptmask specifies which
468a7d0e8cSEnji Cooper  * exceptions we should check. We need to be lenient for several
478a7d0e8cSEnji Cooper  * reasons, but mainly because on some architectures it's impossible
488a7d0e8cSEnji Cooper  * to raise FE_OVERFLOW without raising FE_INEXACT.
498a7d0e8cSEnji Cooper  *
508a7d0e8cSEnji Cooper  * These are macros instead of functions so that assert provides more
518a7d0e8cSEnji Cooper  * meaningful error messages.
528a7d0e8cSEnji Cooper  *
538a7d0e8cSEnji Cooper  * XXX The volatile here is to avoid gcc's bogus constant folding and work
548a7d0e8cSEnji Cooper  *     around the lack of support for the FENV_ACCESS pragma.
558a7d0e8cSEnji Cooper  */
56f3f7b0dcSAlex Richardson #define test_p(func, z, result, exceptmask, excepts, checksign)			\
57f3f7b0dcSAlex Richardson 	do {									\
588a7d0e8cSEnji Cooper 		volatile long double complex _d = z;				\
598a7d0e8cSEnji Cooper 		debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
608a7d0e8cSEnji Cooper 		    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
615c472818SEnji Cooper 		ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
62*b424e003SAlex Richardson 		CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
63f3f7b0dcSAlex Richardson 		volatile int _e = fetestexcept(exceptmask);			\
64f3f7b0dcSAlex Richardson 		ATF_CHECK_MSG(_e == (excepts),					\
65f3f7b0dcSAlex Richardson 		    "%s fetestexcept(%s) (%#x) != %#x",	__XSTRING(func),	\
66f3f7b0dcSAlex Richardson 		    __XSTRING(exceptmask), _e, (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 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
76*b424e003SAlex Richardson 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
77*b424e003SAlex Richardson 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), FPE_ABS_ZERO); \
788a7d0e8cSEnji Cooper } while (0)
798a7d0e8cSEnji Cooper 
808a7d0e8cSEnji Cooper /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
818a7d0e8cSEnji Cooper #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
828a7d0e8cSEnji Cooper 	test_p(func, z, result, exceptmask, excepts, checksign);	\
838a7d0e8cSEnji Cooper 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
848a7d0e8cSEnji Cooper } while (0)
858a7d0e8cSEnji Cooper #define	test_tol(func, z, result, tol)				do {	\
868a7d0e8cSEnji Cooper 	test_p_tol(func, z, result, tol);				\
878a7d0e8cSEnji Cooper 	test_p_tol(func, conjl(z), conjl(result), tol);			\
888a7d0e8cSEnji Cooper } while (0)
898a7d0e8cSEnji Cooper #define	test_odd_tol(func, z, result, tol)			do {	\
908a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
918a7d0e8cSEnji Cooper 	test_tol(func, -(z), -(result), tol);				\
928a7d0e8cSEnji Cooper } while (0)
938a7d0e8cSEnji Cooper #define	test_even_tol(func, z, result, tol)			do {	\
948a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
958a7d0e8cSEnji Cooper 	test_tol(func, -(z), result, tol);				\
968a7d0e8cSEnji Cooper } while (0)
978a7d0e8cSEnji Cooper 
988a7d0e8cSEnji Cooper /* Test the given function in all precisions. */
998a7d0e8cSEnji Cooper #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
1008a7d0e8cSEnji Cooper 	test(func, x, result, exceptmask, excepts, checksign);		\
1018a7d0e8cSEnji Cooper 	test(func##f, x, result, exceptmask, excepts, checksign);	\
1028a7d0e8cSEnji Cooper } while (0)
1038a7d0e8cSEnji Cooper #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
1048a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1058a7d0e8cSEnji Cooper 	testall(func, -x, -result, exceptmask, excepts, checksign);	\
1068a7d0e8cSEnji Cooper } while (0)
1078a7d0e8cSEnji Cooper #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
1088a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1098a7d0e8cSEnji Cooper 	testall(func, -x, result, exceptmask, excepts, checksign);	\
1108a7d0e8cSEnji Cooper } while (0)
1118a7d0e8cSEnji Cooper 
1128a7d0e8cSEnji Cooper /*
1138a7d0e8cSEnji Cooper  * Test the given function in all precisions, within a given tolerance.
1148a7d0e8cSEnji Cooper  * The tolerance is specified in ulps.
1158a7d0e8cSEnji Cooper  */
1168a7d0e8cSEnji Cooper #define	testall_tol(func, x, result, tol)	       		   do { \
1178a7d0e8cSEnji Cooper 	test_tol(func, x, result, tol * DBL_ULP());			\
1188a7d0e8cSEnji Cooper 	test_tol(func##f, x, result, tol * FLT_ULP());			\
1198a7d0e8cSEnji Cooper } while (0)
1208a7d0e8cSEnji Cooper #define	testall_odd_tol(func, x, result, tol)	       		   do { \
1218a7d0e8cSEnji Cooper 	test_odd_tol(func, x, result, tol * DBL_ULP());			\
1228a7d0e8cSEnji Cooper 	test_odd_tol(func##f, x, result, tol * FLT_ULP());		\
1238a7d0e8cSEnji Cooper } while (0)
1248a7d0e8cSEnji Cooper #define	testall_even_tol(func, x, result, tol)	       		   do { \
1258a7d0e8cSEnji Cooper 	test_even_tol(func, x, result, tol * DBL_ULP());		\
1268a7d0e8cSEnji Cooper 	test_even_tol(func##f, x, result, tol * FLT_ULP());		\
1278a7d0e8cSEnji Cooper } while (0)
1288a7d0e8cSEnji Cooper 
1298a7d0e8cSEnji Cooper 
1305c472818SEnji Cooper ATF_TC(test_zero_input);
ATF_TC_HEAD(test_zero_input,tc)1315c472818SEnji Cooper ATF_TC_HEAD(test_zero_input, tc)
1325c472818SEnji Cooper {
1335c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test 0 input");
1345c472818SEnji Cooper }
ATF_TC_BODY(test_zero_input,tc)1355c472818SEnji Cooper ATF_TC_BODY(test_zero_input, tc)
1368a7d0e8cSEnji Cooper {
1378a7d0e8cSEnji Cooper 	long double complex zero = CMPLXL(0.0, 0.0);
1388a7d0e8cSEnji Cooper 
1398a7d0e8cSEnji Cooper 	/* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
1408a7d0e8cSEnji Cooper 	testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1418a7d0e8cSEnji Cooper 	testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1428a7d0e8cSEnji Cooper 	testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
1438a7d0e8cSEnji Cooper 	testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
1448a7d0e8cSEnji Cooper 	testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1458a7d0e8cSEnji Cooper 	testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1468a7d0e8cSEnji Cooper }
1478a7d0e8cSEnji Cooper 
1485c472818SEnji Cooper ATF_TC(test_nan_inputs);
ATF_TC_HEAD(test_nan_inputs,tc)1495c472818SEnji Cooper ATF_TC_HEAD(test_nan_inputs, tc)
1505c472818SEnji Cooper {
1515c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test NaN inputs");
1525c472818SEnji Cooper }
ATF_TC_BODY(test_nan_inputs,tc)1535c472818SEnji Cooper ATF_TC_BODY(test_nan_inputs, tc)
1548a7d0e8cSEnji Cooper {
1558a7d0e8cSEnji Cooper 	long double complex nan_nan = CMPLXL(NAN, NAN);
1568a7d0e8cSEnji Cooper 	long double complex z;
1578a7d0e8cSEnji Cooper 
1588a7d0e8cSEnji Cooper 	/*
1598a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
1608a7d0e8cSEnji Cooper 	 * NaN,NaN	NaN,NaN		NaN,NaN		NaN,NaN
1618a7d0e8cSEnji Cooper 	 * finite,NaN	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1628a7d0e8cSEnji Cooper 	 * NaN,finite	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1638a7d0e8cSEnji Cooper 	 * NaN,Inf	NaN,NaN [inval]	NaN,NaN	[inval]	NaN,NaN [inval]
1648a7d0e8cSEnji Cooper 	 * Inf,NaN	+-Inf,NaN	Inf,NaN		1,+-0
165a7b42c4bSAlex Richardson 	 * 0,NaN	+-0,NaN		NaN,+-0		+-0,NaN
166a7b42c4bSAlex Richardson 	 * NaN,0	NaN,0		NaN,+-0		NaN,+-0
1678a7d0e8cSEnji Cooper 	 */
1688a7d0e8cSEnji Cooper 	z = nan_nan;
1698a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1708a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1718a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1728a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1738a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1748a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1758a7d0e8cSEnji Cooper 
1768a7d0e8cSEnji Cooper 	z = CMPLXL(42, NAN);
1778a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1788a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1798a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1808a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1818a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1828a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1838a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
1848a7d0e8cSEnji Cooper 
1858a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 42);
1868a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1878a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1888a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
1898a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
1908a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
1918a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
1928a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
1938a7d0e8cSEnji Cooper 
1948a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, INFINITY);
1958a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
1968a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
1978a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
1988a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
1998a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2008a7d0e8cSEnji Cooper 	    CS_IMAG);
2018a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
2028a7d0e8cSEnji Cooper 
2038a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, NAN);
2048a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
2058a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2068a7d0e8cSEnji Cooper 		     CS_REAL);
2078a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2088a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
2098a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
2108a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
2118a7d0e8cSEnji Cooper 
2128a7d0e8cSEnji Cooper 	z = CMPLXL(0, NAN);
213a7b42c4bSAlex Richardson 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2148a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
215a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), OPT_INVALID, 0, CS_REAL);
2168a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2178a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2188a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2198a7d0e8cSEnji Cooper 
2208a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 0);
2218a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2228a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2238a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2248a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2258a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
226a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2278a7d0e8cSEnji Cooper }
2288a7d0e8cSEnji Cooper 
2295c472818SEnji Cooper ATF_TC(test_inf_inputs);
ATF_TC_HEAD(test_inf_inputs,tc)2305c472818SEnji Cooper ATF_TC_HEAD(test_inf_inputs, tc)
2315c472818SEnji Cooper {
2325c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test infinity inputs");
2335c472818SEnji Cooper }
ATF_TC_BODY(test_inf_inputs,tc)2345c472818SEnji Cooper ATF_TC_BODY(test_inf_inputs, tc)
2358a7d0e8cSEnji Cooper {
2368a7d0e8cSEnji Cooper 	static const long double finites[] = {
2378a7d0e8cSEnji Cooper 	    0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
2388a7d0e8cSEnji Cooper 	};
2398a7d0e8cSEnji Cooper 	long double complex z, c, s;
240abe427afSEnji Cooper 	unsigned i;
2418a7d0e8cSEnji Cooper 
2428a7d0e8cSEnji Cooper 	/*
2438a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
2448a7d0e8cSEnji Cooper 	 * Inf,Inf	+-Inf,NaN inval	+-Inf,NaN inval	1,+-0
2458a7d0e8cSEnji Cooper 	 * Inf,finite	Inf cis(finite)	Inf cis(finite)	1,0 sin(2 finite)
246a7b42c4bSAlex Richardson 	 * 0,Inf	+-0,NaN	inval	NaN,+-0 inval	+-0,NaN
2478a7d0e8cSEnji Cooper 	 * finite,Inf	NaN,NaN inval	NaN,NaN inval	NaN,NaN inval
2488a7d0e8cSEnji Cooper 	 */
2498a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, INFINITY);
2508a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN),
2518a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2528a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN),
2538a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2548a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2558a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY),
2568a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2578a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN),
2588a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2598a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
2608a7d0e8cSEnji Cooper 
2618a7d0e8cSEnji Cooper 	/* XXX We allow spurious inexact exceptions here (hard to avoid). */
262abe427afSEnji Cooper 	for (i = 0; i < nitems(finites); i++) {
2638a7d0e8cSEnji Cooper 		z = CMPLXL(INFINITY, finites[i]);
2648a7d0e8cSEnji Cooper 		c = INFINITY * cosl(finites[i]);
2658a7d0e8cSEnji Cooper 		s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
2668a7d0e8cSEnji Cooper 		testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2678a7d0e8cSEnji Cooper 		testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2688a7d0e8cSEnji Cooper 		testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)),
2698a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2708a7d0e8cSEnji Cooper 		z = CMPLXL(finites[i], INFINITY);
2718a7d0e8cSEnji Cooper 		testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH);
2728a7d0e8cSEnji Cooper 		testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH);
2738a7d0e8cSEnji Cooper 		testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1),
2748a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2758a7d0e8cSEnji Cooper 	}
2768a7d0e8cSEnji Cooper 
2778a7d0e8cSEnji Cooper 	z = CMPLXL(0, INFINITY);
2788a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2798a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
280a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, CS_REAL);
2818a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 0);
2828a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2838a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
284a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, CS_IMAG);
2858a7d0e8cSEnji Cooper 
2868a7d0e8cSEnji Cooper 	z = CMPLXL(42, INFINITY);
2878a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2888a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2898a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2908a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2918a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 42);
2928a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2938a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2948a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2958a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
2968a7d0e8cSEnji Cooper }
2978a7d0e8cSEnji Cooper 
2985c472818SEnji Cooper ATF_TC(test_axes);
ATF_TC_HEAD(test_axes,tc)2995c472818SEnji Cooper ATF_TC_HEAD(test_axes, tc)
3005c472818SEnji Cooper {
3015c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test along the real/imaginary axes");
3025c472818SEnji Cooper }
ATF_TC_BODY(test_axes,tc)3035c472818SEnji Cooper ATF_TC_BODY(test_axes, tc)
3048a7d0e8cSEnji Cooper {
3058a7d0e8cSEnji Cooper 	static const long double nums[] = {
3068a7d0e8cSEnji Cooper 	    M_PI / 4, M_PI / 2, 3 * M_PI / 4,
3078a7d0e8cSEnji Cooper 	    5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
3088a7d0e8cSEnji Cooper 	};
3098a7d0e8cSEnji Cooper 	long double complex z;
310abe427afSEnji Cooper 	unsigned i;
3118a7d0e8cSEnji Cooper 
312abe427afSEnji Cooper 	for (i = 0; i < nitems(nums); i++) {
3138a7d0e8cSEnji Cooper 		/* Real axis */
3148a7d0e8cSEnji Cooper 		z = CMPLXL(nums[i], 0.0);
3158a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP());
3168a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP());
3178a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP());
3188a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(sin(nums[i]),
3198a7d0e8cSEnji Cooper 		    copysign(0, cos(nums[i]))), DBL_ULP());
3208a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cos(nums[i]),
3218a7d0e8cSEnji Cooper 		    -copysign(0, sin(nums[i]))), DBL_ULP());
3228a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP());
3238a7d0e8cSEnji Cooper 
3248a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP());
3258a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP());
3268a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(z), cimag(z));
3278a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z)));
3288a7d0e8cSEnji Cooper 		printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY));
3298a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0),
3308a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3318a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]),
3328a7d0e8cSEnji Cooper 		    copysign(0, cosf(nums[i]))), FLT_ULP());
3338a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]),
3348a7d0e8cSEnji Cooper 		    -copysign(0, sinf(nums[i]))), 2 * FLT_ULP());
3358a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP());
3368a7d0e8cSEnji Cooper 
3378a7d0e8cSEnji Cooper 		/* Imaginary axis */
3388a7d0e8cSEnji Cooper 		z = CMPLXL(0.0, nums[i]);
3398a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])),
3408a7d0e8cSEnji Cooper 						 sin(nums[i])), DBL_ULP());
3418a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cos(nums[i]),
3428a7d0e8cSEnji Cooper 		    copysign(0, sin(nums[i]))), DBL_ULP());
3438a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP());
3448a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP());
3458a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP());
3468a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP());
3478a7d0e8cSEnji Cooper 
3488a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])),
3498a7d0e8cSEnji Cooper 						 sinf(nums[i])), FLT_ULP());
3508a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]),
3518a7d0e8cSEnji Cooper 		    copysign(0, sinf(nums[i]))), FLT_ULP());
3528a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP());
3538a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP());
3548a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0),
3558a7d0e8cSEnji Cooper 			      FLT_ULP());
3568a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])),
3578a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3588a7d0e8cSEnji Cooper 	}
3598a7d0e8cSEnji Cooper }
3608a7d0e8cSEnji Cooper 
3615c472818SEnji Cooper ATF_TC(test_small_inputs);
ATF_TC_HEAD(test_small_inputs,tc)3625c472818SEnji Cooper ATF_TC_HEAD(test_small_inputs, tc)
3635c472818SEnji Cooper {
3645c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test underflow inputs");
3655c472818SEnji Cooper }
ATF_TC_BODY(test_small_inputs,tc)3665c472818SEnji Cooper ATF_TC_BODY(test_small_inputs, tc)
3678a7d0e8cSEnji Cooper {
3688a7d0e8cSEnji Cooper 	/*
3698a7d0e8cSEnji Cooper 	 * z =  0.5 + i Pi/4
3708a7d0e8cSEnji Cooper 	 *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
3718a7d0e8cSEnji Cooper 	 *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
3728a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
3738a7d0e8cSEnji Cooper 	 * z = -0.5 + i Pi/2
3748a7d0e8cSEnji Cooper 	 *     sinh(z) = cosh(0.5)
3758a7d0e8cSEnji Cooper 	 *     cosh(z) = -i sinh(0.5)
3768a7d0e8cSEnji Cooper 	 *     tanh(z) = -coth(0.5)
3778a7d0e8cSEnji Cooper 	 * z =  1.0 + i 3Pi/4
3788a7d0e8cSEnji Cooper 	 *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
3798a7d0e8cSEnji Cooper 	 *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
3808a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
3818a7d0e8cSEnji Cooper 	 */
3828a7d0e8cSEnji Cooper 	static const struct {
3838a7d0e8cSEnji Cooper 		long double a, b;
3848a7d0e8cSEnji Cooper 		long double sinh_a, sinh_b;
3858a7d0e8cSEnji Cooper 		long double cosh_a, cosh_b;
3868a7d0e8cSEnji Cooper 		long double tanh_a, tanh_b;
3878a7d0e8cSEnji Cooper 	} tests[] = {
3888a7d0e8cSEnji Cooper 		{  0.5L,
3898a7d0e8cSEnji Cooper 		   0.78539816339744830961566084581987572L,
3908a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3918a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3928a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
3938a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
3948a7d0e8cSEnji Cooper 		   0.76159415595576488811945828260479359L,
3958a7d0e8cSEnji Cooper 		   0.64805427366388539957497735322615032L },
3968a7d0e8cSEnji Cooper 		{ -0.5L,
3978a7d0e8cSEnji Cooper 		   1.57079632679489661923132169163975144L,
3988a7d0e8cSEnji Cooper 		   0.0L,
3998a7d0e8cSEnji Cooper 		   1.12762596520638078522622516140267201L,
4008a7d0e8cSEnji Cooper 		   0.0L,
4018a7d0e8cSEnji Cooper 		  -0.52109530549374736162242562641149156L,
4028a7d0e8cSEnji Cooper 		  -2.16395341373865284877000401021802312L,
4038a7d0e8cSEnji Cooper 		   0.0L },
4048a7d0e8cSEnji Cooper 		{  1.0L,
4058a7d0e8cSEnji Cooper 		   2.35619449019234492884698253745962716L,
4068a7d0e8cSEnji Cooper 		  -0.83099273328405698212637979852748608L,
4078a7d0e8cSEnji Cooper 		   1.09112278079550143030545602018565236L,
4088a7d0e8cSEnji Cooper 		  -1.09112278079550143030545602018565236L,
4098a7d0e8cSEnji Cooper 		   0.83099273328405698212637979852748609L,
4108a7d0e8cSEnji Cooper 		   0.96402758007581688394641372410092315L,
4118a7d0e8cSEnji Cooper 		  -0.26580222883407969212086273981988897L }
4128a7d0e8cSEnji Cooper 	};
4138a7d0e8cSEnji Cooper 	long double complex z;
414abe427afSEnji Cooper 	unsigned i;
4158a7d0e8cSEnji Cooper 
416abe427afSEnji Cooper 	for (i = 0; i < nitems(tests); i++) {
4178a7d0e8cSEnji Cooper 		z = CMPLXL(tests[i].a, tests[i].b);
4188a7d0e8cSEnji Cooper 		testall_odd_tol(csinh, z,
4198a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1);
4208a7d0e8cSEnji Cooper 		testall_even_tol(ccosh, z,
4218a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1);
4228a7d0e8cSEnji Cooper 		testall_odd_tol(ctanh, z,
4238a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4);
4248a7d0e8cSEnji Cooper         }
4258a7d0e8cSEnji Cooper }
4268a7d0e8cSEnji Cooper 
4275c472818SEnji Cooper ATF_TC(test_large_inputs);
ATF_TC_HEAD(test_large_inputs,tc)4285c472818SEnji Cooper ATF_TC_HEAD(test_large_inputs, tc)
4295c472818SEnji Cooper {
4305c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
4315c472818SEnji Cooper 	    "Test inputs that might cause overflow in a sloppy implementation");
4325c472818SEnji Cooper }
ATF_TC_BODY(test_large_inputs,tc)4335c472818SEnji Cooper ATF_TC_BODY(test_large_inputs, tc)
4348a7d0e8cSEnji Cooper {
4358a7d0e8cSEnji Cooper 	long double complex z;
4368a7d0e8cSEnji Cooper 
4378a7d0e8cSEnji Cooper 	/* tanh() uses a threshold around x=22, so check both sides. */
4388a7d0e8cSEnji Cooper 	z = CMPLXL(21, 0.78539816339744830961566084581987572L);
4398a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4408a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2);
4418a7d0e8cSEnji Cooper 	z++;
4428a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4438a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1);
4448a7d0e8cSEnji Cooper 
4458a7d0e8cSEnji Cooper 	z = CMPLXL(355, 0.78539816339744830961566084581987572L);
4468a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4478a7d0e8cSEnji Cooper 		     CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L),
4488a7d0e8cSEnji Cooper 		     DBL_ULP());
4498a7d0e8cSEnji Cooper 	z = CMPLXL(30, 0x1p1023L);
4508a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4518a7d0e8cSEnji Cooper 		     CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L),
4528a7d0e8cSEnji Cooper 		     DBL_ULP());
4538a7d0e8cSEnji Cooper 	z = CMPLXL(1, 0x1p1023L);
4548a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4558a7d0e8cSEnji Cooper 		     CMPLXL(0.878606311888306869546254022621986509L,
4568a7d0e8cSEnji Cooper 			    -0.225462792499754505792678258169527424L),
4578a7d0e8cSEnji Cooper 		     DBL_ULP());
4588a7d0e8cSEnji Cooper 
4598a7d0e8cSEnji Cooper 	z = CMPLXL(710.6, 0.78539816339744830961566084581987572L);
4608a7d0e8cSEnji Cooper 	test_odd_tol(csinh, z,
4618a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4628a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4638a7d0e8cSEnji Cooper 	test_even_tol(ccosh, z,
4648a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4658a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4668a7d0e8cSEnji Cooper 
4678a7d0e8cSEnji Cooper 	z = CMPLXL(1500, 0.78539816339744830961566084581987572L);
4688a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4698a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4708a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4718a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4728a7d0e8cSEnji Cooper }
4738a7d0e8cSEnji Cooper 
ATF_TP_ADD_TCS(tp)4745c472818SEnji Cooper ATF_TP_ADD_TCS(tp)
4758a7d0e8cSEnji Cooper {
4768a7d0e8cSEnji Cooper 
4775c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_zero_input);
4785c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_nan_inputs);
4795c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_inf_inputs);
4805c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_axes);
4815c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_small_inputs);
4825c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_large_inputs);
4838a7d0e8cSEnji Cooper 
4845c472818SEnji Cooper 	return (atf_no_error());
4858a7d0e8cSEnji Cooper }
486