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