xref: /freebsd/lib/msun/tests/ctrig_test.c (revision f3f7b0dc065ce30f29b221788c58079d78931a77)
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  */
61*f3f7b0dcSAlex Richardson #define test_p(func, z, result, exceptmask, excepts, checksign)			\
62*f3f7b0dcSAlex Richardson 	do {									\
638a7d0e8cSEnji Cooper 		volatile long double complex _d = z;				\
648a7d0e8cSEnji Cooper 		debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
658a7d0e8cSEnji Cooper 		    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
665c472818SEnji Cooper 		ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
67*f3f7b0dcSAlex Richardson 		volatile long double complex _r = (func)(_d);			\
68*f3f7b0dcSAlex Richardson 		ATF_CHECK_MSG(cfpequal_cs(_r, (result), (checksign)),		\
69*f3f7b0dcSAlex Richardson 		    "%s (%Lg + %Lg I) != expected (%Lg + %Lg I)",		\
70*f3f7b0dcSAlex Richardson 		    __XSTRING((func)(_d)), creall(_r), cimagl(_r),		\
71*f3f7b0dcSAlex Richardson 		    creall(result), cimagl(result));				\
72*f3f7b0dcSAlex Richardson 		volatile int _e = fetestexcept(exceptmask);			\
73*f3f7b0dcSAlex Richardson 		ATF_CHECK_MSG(_e == (excepts),					\
74*f3f7b0dcSAlex Richardson 		    "%s fetestexcept(%s) (%#x) != %#x",	__XSTRING(func),	\
75*f3f7b0dcSAlex Richardson 		    __XSTRING(exceptmask), _e, (excepts));			\
768a7d0e8cSEnji Cooper 	} while (0)
778a7d0e8cSEnji Cooper 
788a7d0e8cSEnji Cooper /*
798a7d0e8cSEnji Cooper  * Test within a given tolerance.  The tolerance indicates relative error
808a7d0e8cSEnji Cooper  * in ulps.  If result is 0, however, it measures absolute error in units
818a7d0e8cSEnji Cooper  * of <format>_EPSILON.
828a7d0e8cSEnji Cooper  */
838a7d0e8cSEnji Cooper #define	test_p_tol(func, z, result, tol)			do {	\
848a7d0e8cSEnji Cooper 	volatile long double complex _d = z;				\
858a7d0e8cSEnji Cooper 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
868a7d0e8cSEnji Cooper 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
875c472818SEnji Cooper 	ATF_CHECK(cfpequal_tol((func)(_d), (result), (tol), FPE_ABS_ZERO)); \
888a7d0e8cSEnji Cooper } while (0)
898a7d0e8cSEnji Cooper 
908a7d0e8cSEnji Cooper /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
918a7d0e8cSEnji Cooper #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
928a7d0e8cSEnji Cooper 	test_p(func, z, result, exceptmask, excepts, checksign);	\
938a7d0e8cSEnji Cooper 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
948a7d0e8cSEnji Cooper } while (0)
958a7d0e8cSEnji Cooper #define	test_tol(func, z, result, tol)				do {	\
968a7d0e8cSEnji Cooper 	test_p_tol(func, z, result, tol);				\
978a7d0e8cSEnji Cooper 	test_p_tol(func, conjl(z), conjl(result), tol);			\
988a7d0e8cSEnji Cooper } while (0)
998a7d0e8cSEnji Cooper #define	test_odd_tol(func, z, result, tol)			do {	\
1008a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
1018a7d0e8cSEnji Cooper 	test_tol(func, -(z), -(result), tol);				\
1028a7d0e8cSEnji Cooper } while (0)
1038a7d0e8cSEnji Cooper #define	test_even_tol(func, z, result, tol)			do {	\
1048a7d0e8cSEnji Cooper 	test_tol(func, z, result, tol);					\
1058a7d0e8cSEnji Cooper 	test_tol(func, -(z), result, tol);				\
1068a7d0e8cSEnji Cooper } while (0)
1078a7d0e8cSEnji Cooper 
1088a7d0e8cSEnji Cooper /* Test the given function in all precisions. */
1098a7d0e8cSEnji Cooper #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
1108a7d0e8cSEnji Cooper 	test(func, x, result, exceptmask, excepts, checksign);		\
1118a7d0e8cSEnji Cooper 	test(func##f, x, result, exceptmask, excepts, checksign);	\
1128a7d0e8cSEnji Cooper } while (0)
1138a7d0e8cSEnji Cooper #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
1148a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1158a7d0e8cSEnji Cooper 	testall(func, -x, -result, exceptmask, excepts, checksign);	\
1168a7d0e8cSEnji Cooper } while (0)
1178a7d0e8cSEnji Cooper #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
1188a7d0e8cSEnji Cooper 	testall(func, x, result, exceptmask, excepts, checksign);	\
1198a7d0e8cSEnji Cooper 	testall(func, -x, result, exceptmask, excepts, checksign);	\
1208a7d0e8cSEnji Cooper } while (0)
1218a7d0e8cSEnji Cooper 
1228a7d0e8cSEnji Cooper /*
1238a7d0e8cSEnji Cooper  * Test the given function in all precisions, within a given tolerance.
1248a7d0e8cSEnji Cooper  * The tolerance is specified in ulps.
1258a7d0e8cSEnji Cooper  */
1268a7d0e8cSEnji Cooper #define	testall_tol(func, x, result, tol)	       		   do { \
1278a7d0e8cSEnji Cooper 	test_tol(func, x, result, tol * DBL_ULP());			\
1288a7d0e8cSEnji Cooper 	test_tol(func##f, x, result, tol * FLT_ULP());			\
1298a7d0e8cSEnji Cooper } while (0)
1308a7d0e8cSEnji Cooper #define	testall_odd_tol(func, x, result, tol)	       		   do { \
1318a7d0e8cSEnji Cooper 	test_odd_tol(func, x, result, tol * DBL_ULP());			\
1328a7d0e8cSEnji Cooper 	test_odd_tol(func##f, x, result, tol * FLT_ULP());		\
1338a7d0e8cSEnji Cooper } while (0)
1348a7d0e8cSEnji Cooper #define	testall_even_tol(func, x, result, tol)	       		   do { \
1358a7d0e8cSEnji Cooper 	test_even_tol(func, x, result, tol * DBL_ULP());		\
1368a7d0e8cSEnji Cooper 	test_even_tol(func##f, x, result, tol * FLT_ULP());		\
1378a7d0e8cSEnji Cooper } while (0)
1388a7d0e8cSEnji Cooper 
1398a7d0e8cSEnji Cooper 
1405c472818SEnji Cooper ATF_TC(test_zero_input);
1415c472818SEnji Cooper ATF_TC_HEAD(test_zero_input, tc)
1425c472818SEnji Cooper {
1435c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test 0 input");
1445c472818SEnji Cooper }
1455c472818SEnji Cooper ATF_TC_BODY(test_zero_input, tc)
1468a7d0e8cSEnji Cooper {
1478a7d0e8cSEnji Cooper 	long double complex zero = CMPLXL(0.0, 0.0);
1488a7d0e8cSEnji Cooper 
1498a7d0e8cSEnji Cooper 	/* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
1508a7d0e8cSEnji Cooper 	testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1518a7d0e8cSEnji Cooper 	testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1528a7d0e8cSEnji Cooper 	testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
1538a7d0e8cSEnji Cooper 	testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
1548a7d0e8cSEnji Cooper 	testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1558a7d0e8cSEnji Cooper 	testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
1568a7d0e8cSEnji Cooper }
1578a7d0e8cSEnji Cooper 
1585c472818SEnji Cooper ATF_TC(test_nan_inputs);
1595c472818SEnji Cooper ATF_TC_HEAD(test_nan_inputs, tc)
1605c472818SEnji Cooper {
1615c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test NaN inputs");
1625c472818SEnji Cooper }
1635c472818SEnji Cooper ATF_TC_BODY(test_nan_inputs, tc)
1648a7d0e8cSEnji Cooper {
1658a7d0e8cSEnji Cooper 	long double complex nan_nan = CMPLXL(NAN, NAN);
1668a7d0e8cSEnji Cooper 	long double complex z;
1678a7d0e8cSEnji Cooper 
1688a7d0e8cSEnji Cooper 	/*
1698a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
1708a7d0e8cSEnji Cooper 	 * NaN,NaN	NaN,NaN		NaN,NaN		NaN,NaN
1718a7d0e8cSEnji Cooper 	 * finite,NaN	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1728a7d0e8cSEnji Cooper 	 * NaN,finite	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
1738a7d0e8cSEnji Cooper 	 * NaN,Inf	NaN,NaN [inval]	NaN,NaN	[inval]	NaN,NaN [inval]
1748a7d0e8cSEnji Cooper 	 * Inf,NaN	+-Inf,NaN	Inf,NaN		1,+-0
175a7b42c4bSAlex Richardson 	 * 0,NaN	+-0,NaN		NaN,+-0		+-0,NaN
176a7b42c4bSAlex Richardson 	 * NaN,0	NaN,0		NaN,+-0		NaN,+-0
1778a7d0e8cSEnji Cooper 	 */
1788a7d0e8cSEnji Cooper 	z = nan_nan;
1798a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1808a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1818a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1828a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1838a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1848a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
1858a7d0e8cSEnji Cooper 
1868a7d0e8cSEnji Cooper 	z = CMPLXL(42, NAN);
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 	/* XXX We allow a spurious inexact exception here. */
1908a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 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 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
1948a7d0e8cSEnji Cooper 
1958a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 42);
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, nan_nan, OPT_INVALID, 0, 0);
2008a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
2018a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
2028a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
2038a7d0e8cSEnji Cooper 
2048a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, INFINITY);
2058a7d0e8cSEnji Cooper 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
2068a7d0e8cSEnji Cooper 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
2078a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
2088a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
2098a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2108a7d0e8cSEnji Cooper 	    CS_IMAG);
2118a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
2128a7d0e8cSEnji Cooper 
2138a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, NAN);
2148a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
2158a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
2168a7d0e8cSEnji Cooper 		     CS_REAL);
2178a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2188a7d0e8cSEnji Cooper 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
2198a7d0e8cSEnji Cooper 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
2208a7d0e8cSEnji Cooper 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
2218a7d0e8cSEnji Cooper 
2228a7d0e8cSEnji Cooper 	z = CMPLXL(0, NAN);
223a7b42c4bSAlex Richardson 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2248a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
225a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), OPT_INVALID, 0, CS_REAL);
2268a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2278a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2288a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
2298a7d0e8cSEnji Cooper 
2308a7d0e8cSEnji Cooper 	z = CMPLXL(NAN, 0);
2318a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2328a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2338a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2348a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
2358a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
236a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
2378a7d0e8cSEnji Cooper }
2388a7d0e8cSEnji Cooper 
2395c472818SEnji Cooper ATF_TC(test_inf_inputs);
2405c472818SEnji Cooper ATF_TC_HEAD(test_inf_inputs, tc)
2415c472818SEnji Cooper {
2425c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test infinity inputs");
2435c472818SEnji Cooper }
2445c472818SEnji Cooper ATF_TC_BODY(test_inf_inputs, tc)
2458a7d0e8cSEnji Cooper {
2468a7d0e8cSEnji Cooper 	static const long double finites[] = {
2478a7d0e8cSEnji Cooper 	    0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
2488a7d0e8cSEnji Cooper 	};
2498a7d0e8cSEnji Cooper 	long double complex z, c, s;
250abe427afSEnji Cooper 	unsigned i;
2518a7d0e8cSEnji Cooper 
2528a7d0e8cSEnji Cooper 	/*
2538a7d0e8cSEnji Cooper 	 * IN		CSINH		CCOSH		CTANH
2548a7d0e8cSEnji Cooper 	 * Inf,Inf	+-Inf,NaN inval	+-Inf,NaN inval	1,+-0
2558a7d0e8cSEnji Cooper 	 * Inf,finite	Inf cis(finite)	Inf cis(finite)	1,0 sin(2 finite)
256a7b42c4bSAlex Richardson 	 * 0,Inf	+-0,NaN	inval	NaN,+-0 inval	+-0,NaN
2578a7d0e8cSEnji Cooper 	 * finite,Inf	NaN,NaN inval	NaN,NaN inval	NaN,NaN inval
2588a7d0e8cSEnji Cooper 	 */
2598a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, INFINITY);
2608a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN),
2618a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2628a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN),
2638a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2648a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
2658a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, INFINITY),
2668a7d0e8cSEnji Cooper 		    ALL_STD_EXCEPT, FE_INVALID, 0);
2678a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(INFINITY, NAN),
2688a7d0e8cSEnji Cooper 		     ALL_STD_EXCEPT, FE_INVALID, 0);
2698a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
2708a7d0e8cSEnji Cooper 
2718a7d0e8cSEnji Cooper 	/* XXX We allow spurious inexact exceptions here (hard to avoid). */
272abe427afSEnji Cooper 	for (i = 0; i < nitems(finites); i++) {
2738a7d0e8cSEnji Cooper 		z = CMPLXL(INFINITY, finites[i]);
2748a7d0e8cSEnji Cooper 		c = INFINITY * cosl(finites[i]);
2758a7d0e8cSEnji Cooper 		s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
2768a7d0e8cSEnji Cooper 		testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2778a7d0e8cSEnji Cooper 		testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
2788a7d0e8cSEnji Cooper 		testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)),
2798a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2808a7d0e8cSEnji Cooper 		z = CMPLXL(finites[i], INFINITY);
2818a7d0e8cSEnji Cooper 		testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH);
2828a7d0e8cSEnji Cooper 		testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH);
2838a7d0e8cSEnji Cooper 		testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1),
2848a7d0e8cSEnji Cooper 			    OPT_INEXACT, 0, CS_BOTH);
2858a7d0e8cSEnji Cooper 	}
2868a7d0e8cSEnji Cooper 
2878a7d0e8cSEnji Cooper 	z = CMPLXL(0, INFINITY);
2888a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2898a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
290a7b42c4bSAlex Richardson 	testall_odd(ctanh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, CS_REAL);
2918a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 0);
2928a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
2938a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
294a7b42c4bSAlex Richardson 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, CS_IMAG);
2958a7d0e8cSEnji Cooper 
2968a7d0e8cSEnji Cooper 	z = CMPLXL(42, INFINITY);
2978a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2988a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
2998a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
3008a7d0e8cSEnji Cooper 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
3018a7d0e8cSEnji Cooper 	z = CMPLXL(INFINITY, 42);
3028a7d0e8cSEnji Cooper 	testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
3038a7d0e8cSEnji Cooper 	testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
3048a7d0e8cSEnji Cooper 	/* XXX We allow a spurious inexact exception here. */
3058a7d0e8cSEnji Cooper 	testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
3068a7d0e8cSEnji Cooper }
3078a7d0e8cSEnji Cooper 
3085c472818SEnji Cooper ATF_TC(test_axes);
3095c472818SEnji Cooper ATF_TC_HEAD(test_axes, tc)
3105c472818SEnji Cooper {
3115c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test along the real/imaginary axes");
3125c472818SEnji Cooper }
3135c472818SEnji Cooper ATF_TC_BODY(test_axes, tc)
3148a7d0e8cSEnji Cooper {
3158a7d0e8cSEnji Cooper 	static const long double nums[] = {
3168a7d0e8cSEnji Cooper 	    M_PI / 4, M_PI / 2, 3 * M_PI / 4,
3178a7d0e8cSEnji Cooper 	    5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
3188a7d0e8cSEnji Cooper 	};
3198a7d0e8cSEnji Cooper 	long double complex z;
320abe427afSEnji Cooper 	unsigned i;
3218a7d0e8cSEnji Cooper 
322abe427afSEnji Cooper 	for (i = 0; i < nitems(nums); i++) {
3238a7d0e8cSEnji Cooper 		/* Real axis */
3248a7d0e8cSEnji Cooper 		z = CMPLXL(nums[i], 0.0);
3258a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP());
3268a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP());
3278a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP());
3288a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(sin(nums[i]),
3298a7d0e8cSEnji Cooper 		    copysign(0, cos(nums[i]))), DBL_ULP());
3308a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cos(nums[i]),
3318a7d0e8cSEnji Cooper 		    -copysign(0, sin(nums[i]))), DBL_ULP());
3328a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP());
3338a7d0e8cSEnji Cooper 
3348a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP());
3358a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP());
3368a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(z), cimag(z));
3378a7d0e8cSEnji Cooper 		printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z)));
3388a7d0e8cSEnji Cooper 		printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY));
3398a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0),
3408a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3418a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]),
3428a7d0e8cSEnji Cooper 		    copysign(0, cosf(nums[i]))), FLT_ULP());
3438a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]),
3448a7d0e8cSEnji Cooper 		    -copysign(0, sinf(nums[i]))), 2 * FLT_ULP());
3458a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP());
3468a7d0e8cSEnji Cooper 
3478a7d0e8cSEnji Cooper 		/* Imaginary axis */
3488a7d0e8cSEnji Cooper 		z = CMPLXL(0.0, nums[i]);
3498a7d0e8cSEnji Cooper 		test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])),
3508a7d0e8cSEnji Cooper 						 sin(nums[i])), DBL_ULP());
3518a7d0e8cSEnji Cooper 		test_even_tol(ccosh, z, CMPLXL(cos(nums[i]),
3528a7d0e8cSEnji Cooper 		    copysign(0, sin(nums[i]))), DBL_ULP());
3538a7d0e8cSEnji Cooper 		test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP());
3548a7d0e8cSEnji Cooper 		test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP());
3558a7d0e8cSEnji Cooper 		test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP());
3568a7d0e8cSEnji Cooper 		test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP());
3578a7d0e8cSEnji Cooper 
3588a7d0e8cSEnji Cooper 		test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])),
3598a7d0e8cSEnji Cooper 						 sinf(nums[i])), FLT_ULP());
3608a7d0e8cSEnji Cooper 		test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]),
3618a7d0e8cSEnji Cooper 		    copysign(0, sinf(nums[i]))), FLT_ULP());
3628a7d0e8cSEnji Cooper 		test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP());
3638a7d0e8cSEnji Cooper 		test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP());
3648a7d0e8cSEnji Cooper 		test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0),
3658a7d0e8cSEnji Cooper 			      FLT_ULP());
3668a7d0e8cSEnji Cooper 		test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])),
3678a7d0e8cSEnji Cooper 			     1.3 * FLT_ULP());
3688a7d0e8cSEnji Cooper 	}
3698a7d0e8cSEnji Cooper }
3708a7d0e8cSEnji Cooper 
3715c472818SEnji Cooper ATF_TC(test_small_inputs);
3725c472818SEnji Cooper ATF_TC_HEAD(test_small_inputs, tc)
3735c472818SEnji Cooper {
3745c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "test underflow inputs");
3755c472818SEnji Cooper }
3765c472818SEnji Cooper ATF_TC_BODY(test_small_inputs, tc)
3778a7d0e8cSEnji Cooper {
3788a7d0e8cSEnji Cooper 	/*
3798a7d0e8cSEnji Cooper 	 * z =  0.5 + i Pi/4
3808a7d0e8cSEnji Cooper 	 *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
3818a7d0e8cSEnji Cooper 	 *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
3828a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
3838a7d0e8cSEnji Cooper 	 * z = -0.5 + i Pi/2
3848a7d0e8cSEnji Cooper 	 *     sinh(z) = cosh(0.5)
3858a7d0e8cSEnji Cooper 	 *     cosh(z) = -i sinh(0.5)
3868a7d0e8cSEnji Cooper 	 *     tanh(z) = -coth(0.5)
3878a7d0e8cSEnji Cooper 	 * z =  1.0 + i 3Pi/4
3888a7d0e8cSEnji Cooper 	 *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
3898a7d0e8cSEnji Cooper 	 *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
3908a7d0e8cSEnji Cooper 	 *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
3918a7d0e8cSEnji Cooper 	 */
3928a7d0e8cSEnji Cooper 	static const struct {
3938a7d0e8cSEnji Cooper 		long double a, b;
3948a7d0e8cSEnji Cooper 		long double sinh_a, sinh_b;
3958a7d0e8cSEnji Cooper 		long double cosh_a, cosh_b;
3968a7d0e8cSEnji Cooper 		long double tanh_a, tanh_b;
3978a7d0e8cSEnji Cooper 	} tests[] = {
3988a7d0e8cSEnji Cooper 		{  0.5L,
3998a7d0e8cSEnji Cooper 		   0.78539816339744830961566084581987572L,
4008a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
4018a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
4028a7d0e8cSEnji Cooper 		   0.79735196663945774996093142586179334L,
4038a7d0e8cSEnji Cooper 		   0.36847002415910435172083660522240710L,
4048a7d0e8cSEnji Cooper 		   0.76159415595576488811945828260479359L,
4058a7d0e8cSEnji Cooper 		   0.64805427366388539957497735322615032L },
4068a7d0e8cSEnji Cooper 		{ -0.5L,
4078a7d0e8cSEnji Cooper 		   1.57079632679489661923132169163975144L,
4088a7d0e8cSEnji Cooper 		   0.0L,
4098a7d0e8cSEnji Cooper 		   1.12762596520638078522622516140267201L,
4108a7d0e8cSEnji Cooper 		   0.0L,
4118a7d0e8cSEnji Cooper 		  -0.52109530549374736162242562641149156L,
4128a7d0e8cSEnji Cooper 		  -2.16395341373865284877000401021802312L,
4138a7d0e8cSEnji Cooper 		   0.0L },
4148a7d0e8cSEnji Cooper 		{  1.0L,
4158a7d0e8cSEnji Cooper 		   2.35619449019234492884698253745962716L,
4168a7d0e8cSEnji Cooper 		  -0.83099273328405698212637979852748608L,
4178a7d0e8cSEnji Cooper 		   1.09112278079550143030545602018565236L,
4188a7d0e8cSEnji Cooper 		  -1.09112278079550143030545602018565236L,
4198a7d0e8cSEnji Cooper 		   0.83099273328405698212637979852748609L,
4208a7d0e8cSEnji Cooper 		   0.96402758007581688394641372410092315L,
4218a7d0e8cSEnji Cooper 		  -0.26580222883407969212086273981988897L }
4228a7d0e8cSEnji Cooper 	};
4238a7d0e8cSEnji Cooper 	long double complex z;
424abe427afSEnji Cooper 	unsigned i;
4258a7d0e8cSEnji Cooper 
426abe427afSEnji Cooper 	for (i = 0; i < nitems(tests); i++) {
4278a7d0e8cSEnji Cooper 		z = CMPLXL(tests[i].a, tests[i].b);
4288a7d0e8cSEnji Cooper 		testall_odd_tol(csinh, z,
4298a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1);
4308a7d0e8cSEnji Cooper 		testall_even_tol(ccosh, z,
4318a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1);
4328a7d0e8cSEnji Cooper 		testall_odd_tol(ctanh, z,
4338a7d0e8cSEnji Cooper 		    CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4);
4348a7d0e8cSEnji Cooper         }
4358a7d0e8cSEnji Cooper }
4368a7d0e8cSEnji Cooper 
4375c472818SEnji Cooper ATF_TC(test_large_inputs);
4385c472818SEnji Cooper ATF_TC_HEAD(test_large_inputs, tc)
4395c472818SEnji Cooper {
4405c472818SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
4415c472818SEnji Cooper 	    "Test inputs that might cause overflow in a sloppy implementation");
4425c472818SEnji Cooper }
4435c472818SEnji Cooper ATF_TC_BODY(test_large_inputs, tc)
4448a7d0e8cSEnji Cooper {
4458a7d0e8cSEnji Cooper 	long double complex z;
4468a7d0e8cSEnji Cooper 
4478a7d0e8cSEnji Cooper 	/* tanh() uses a threshold around x=22, so check both sides. */
4488a7d0e8cSEnji Cooper 	z = CMPLXL(21, 0.78539816339744830961566084581987572L);
4498a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4508a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2);
4518a7d0e8cSEnji Cooper 	z++;
4528a7d0e8cSEnji Cooper 	testall_odd_tol(ctanh, z,
4538a7d0e8cSEnji Cooper 	    CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1);
4548a7d0e8cSEnji Cooper 
4558a7d0e8cSEnji Cooper 	z = CMPLXL(355, 0.78539816339744830961566084581987572L);
4568a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4578a7d0e8cSEnji Cooper 		     CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L),
4588a7d0e8cSEnji Cooper 		     DBL_ULP());
4598a7d0e8cSEnji Cooper 	z = CMPLXL(30, 0x1p1023L);
4608a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4618a7d0e8cSEnji Cooper 		     CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L),
4628a7d0e8cSEnji Cooper 		     DBL_ULP());
4638a7d0e8cSEnji Cooper 	z = CMPLXL(1, 0x1p1023L);
4648a7d0e8cSEnji Cooper 	test_odd_tol(ctanh, z,
4658a7d0e8cSEnji Cooper 		     CMPLXL(0.878606311888306869546254022621986509L,
4668a7d0e8cSEnji Cooper 			    -0.225462792499754505792678258169527424L),
4678a7d0e8cSEnji Cooper 		     DBL_ULP());
4688a7d0e8cSEnji Cooper 
4698a7d0e8cSEnji Cooper 	z = CMPLXL(710.6, 0.78539816339744830961566084581987572L);
4708a7d0e8cSEnji Cooper 	test_odd_tol(csinh, z,
4718a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4728a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4738a7d0e8cSEnji Cooper 	test_even_tol(ccosh, z,
4748a7d0e8cSEnji Cooper 	    CMPLXL(1.43917579766621073533185387499658944e308L,
4758a7d0e8cSEnji Cooper 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
4768a7d0e8cSEnji Cooper 
4778a7d0e8cSEnji Cooper 	z = CMPLXL(1500, 0.78539816339744830961566084581987572L);
4788a7d0e8cSEnji Cooper 	testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4798a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4808a7d0e8cSEnji Cooper 	testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
4818a7d0e8cSEnji Cooper 	    FE_OVERFLOW, CS_BOTH);
4828a7d0e8cSEnji Cooper }
4838a7d0e8cSEnji Cooper 
4845c472818SEnji Cooper ATF_TP_ADD_TCS(tp)
4858a7d0e8cSEnji Cooper {
4868a7d0e8cSEnji Cooper 
4875c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_zero_input);
4885c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_nan_inputs);
4895c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_inf_inputs);
4905c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_axes);
4915c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_small_inputs);
4925c472818SEnji Cooper 	ATF_TP_ADD_TC(tp, test_large_inputs);
4938a7d0e8cSEnji Cooper 
4945c472818SEnji Cooper 	return (atf_no_error());
4958a7d0e8cSEnji Cooper }
496