xref: /openbsd/regress/lib/libm/msun/ctrig_test.c (revision 498c7b5e)
1*498c7b5eSderaadt /*	$OpenBSD: ctrig_test.c,v 1.3 2021/12/13 18:04:28 deraadt Exp $	*/
2c36e572eSmbuhl /*-
3c36e572eSmbuhl  * Copyright (c) 2008-2011 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl  * All rights reserved.
5c36e572eSmbuhl  *
6c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8c36e572eSmbuhl  * are met:
9c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl  *
15c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl  * SUCH DAMAGE.
26c36e572eSmbuhl  */
27c36e572eSmbuhl 
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl 
30c36e572eSmbuhl /*
31c36e572eSmbuhl  * Tests for csin[h](), ccos[h](), and ctan[h]().
32c36e572eSmbuhl  */
33c36e572eSmbuhl 
3449a6e16fSderaadt #include <sys/types.h>
35c36e572eSmbuhl #include <complex.h>
36c36e572eSmbuhl #include <fenv.h>
37c36e572eSmbuhl #include <float.h>
38c36e572eSmbuhl #include <math.h>
39c36e572eSmbuhl #include <stdio.h>
40c36e572eSmbuhl 
41c36e572eSmbuhl #include "test-utils.h"
42c36e572eSmbuhl 
43c36e572eSmbuhl #pragma STDC FENV_ACCESS	ON
44c36e572eSmbuhl #pragma	STDC CX_LIMITED_RANGE	OFF
45c36e572eSmbuhl 
46c36e572eSmbuhl /*
47c36e572eSmbuhl  * Test that a function returns the correct value and sets the
48c36e572eSmbuhl  * exception flags correctly. The exceptmask specifies which
49c36e572eSmbuhl  * exceptions we should check. We need to be lenient for several
50c36e572eSmbuhl  * reasons, but mainly because on some architectures it's impossible
51c36e572eSmbuhl  * to raise FE_OVERFLOW without raising FE_INEXACT.
52c36e572eSmbuhl  *
53c36e572eSmbuhl  * These are macros instead of functions so that assert provides more
54c36e572eSmbuhl  * meaningful error messages.
55c36e572eSmbuhl  *
56c36e572eSmbuhl  * XXX The volatile here is to avoid gcc's bogus constant folding and work
57c36e572eSmbuhl  *     around the lack of support for the FENV_ACCESS pragma.
58c36e572eSmbuhl  */
59c36e572eSmbuhl #define test_p(func, z, result, exceptmask, excepts, checksign)			\
60c36e572eSmbuhl 	do {									\
61c36e572eSmbuhl 		volatile long double complex _d = z;				\
62c36e572eSmbuhl 		debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
63c36e572eSmbuhl 		    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
64c36e572eSmbuhl 		ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0);			\
65c36e572eSmbuhl 		CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
66c36e572eSmbuhl 		volatile int _e = fetestexcept(exceptmask);			\
67c36e572eSmbuhl 		ATF_CHECK_MSG(_e == (excepts),					\
68c36e572eSmbuhl 		    "%s fetestexcept(%s) (%#x) != %#x",	__XSTRING(func),	\
69c36e572eSmbuhl 		    __XSTRING(exceptmask), _e, (excepts));			\
70c36e572eSmbuhl 	} while (0)
71c36e572eSmbuhl 
72c36e572eSmbuhl /*
73c36e572eSmbuhl  * Test within a given tolerance.  The tolerance indicates relative error
74c36e572eSmbuhl  * in ulps.  If result is 0, however, it measures absolute error in units
75c36e572eSmbuhl  * of <format>_EPSILON.
76c36e572eSmbuhl  */
77c36e572eSmbuhl #define	test_p_tol(func, z, result, tol)			do {	\
78c36e572eSmbuhl 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
79c36e572eSmbuhl 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
80c36e572eSmbuhl 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), FPE_ABS_ZERO); \
81c36e572eSmbuhl } while (0)
82c36e572eSmbuhl 
83c36e572eSmbuhl /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
84c36e572eSmbuhl #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
85c36e572eSmbuhl 	test_p(func, z, result, exceptmask, excepts, checksign);	\
86c36e572eSmbuhl 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
87c36e572eSmbuhl } while (0)
88c36e572eSmbuhl #define	test_tol(func, z, result, tol)				do {	\
89c36e572eSmbuhl 	test_p_tol(func, z, result, tol);				\
90c36e572eSmbuhl 	test_p_tol(func, conjl(z), conjl(result), tol);			\
91c36e572eSmbuhl } while (0)
92c36e572eSmbuhl #define	test_odd_tol(func, z, result, tol)			do {	\
93c36e572eSmbuhl 	test_tol(func, z, result, tol);					\
94c36e572eSmbuhl 	test_tol(func, -(z), -(result), tol);				\
95c36e572eSmbuhl } while (0)
96c36e572eSmbuhl #define	test_even_tol(func, z, result, tol)			do {	\
97c36e572eSmbuhl 	test_tol(func, z, result, tol);					\
98c36e572eSmbuhl 	test_tol(func, -(z), result, tol);				\
99c36e572eSmbuhl } while (0)
100c36e572eSmbuhl 
101c36e572eSmbuhl /* Test the given function in all precisions. */
102c36e572eSmbuhl #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
103c36e572eSmbuhl 	test(func, x, result, exceptmask, excepts, checksign);		\
104c36e572eSmbuhl 	test(func##f, x, result, exceptmask, excepts, checksign);	\
105c36e572eSmbuhl } while (0)
106c36e572eSmbuhl #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
107c36e572eSmbuhl 	testall(func, x, result, exceptmask, excepts, checksign);	\
108c36e572eSmbuhl 	testall(func, -x, -result, exceptmask, excepts, checksign);	\
109c36e572eSmbuhl } while (0)
110c36e572eSmbuhl #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
111c36e572eSmbuhl 	testall(func, x, result, exceptmask, excepts, checksign);	\
112c36e572eSmbuhl 	testall(func, -x, result, exceptmask, excepts, checksign);	\
113c36e572eSmbuhl } while (0)
114c36e572eSmbuhl 
115c36e572eSmbuhl /*
116c36e572eSmbuhl  * Test the given function in all precisions, within a given tolerance.
117c36e572eSmbuhl  * The tolerance is specified in ulps.
118c36e572eSmbuhl  */
119c36e572eSmbuhl #define	testall_tol(func, x, result, tol)	       		   do { \
120c36e572eSmbuhl 	test_tol(func, x, result, tol * DBL_ULP());			\
121c36e572eSmbuhl 	test_tol(func##f, x, result, tol * FLT_ULP());			\
122c36e572eSmbuhl } while (0)
123c36e572eSmbuhl #define	testall_odd_tol(func, x, result, tol)	       		   do { \
124c36e572eSmbuhl 	test_odd_tol(func, x, result, tol * DBL_ULP());			\
125c36e572eSmbuhl 	test_odd_tol(func##f, x, result, tol * FLT_ULP());		\
126c36e572eSmbuhl } while (0)
127c36e572eSmbuhl #define	testall_even_tol(func, x, result, tol)	       		   do { \
128c36e572eSmbuhl 	test_even_tol(func, x, result, tol * DBL_ULP());		\
129c36e572eSmbuhl 	test_even_tol(func##f, x, result, tol * FLT_ULP());		\
130c36e572eSmbuhl } while (0)
131c36e572eSmbuhl 
132c36e572eSmbuhl 
133c36e572eSmbuhl ATF_TC(test_zero_input);
ATF_TC_HEAD(test_zero_input,tc)134c36e572eSmbuhl ATF_TC_HEAD(test_zero_input, tc)
135c36e572eSmbuhl {
136c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr", "test 0 input");
137c36e572eSmbuhl }
ATF_TC_BODY(test_zero_input,tc)138c36e572eSmbuhl ATF_TC_BODY(test_zero_input, tc)
139c36e572eSmbuhl {
140c36e572eSmbuhl 	long double complex zero = CMPLXL(0.0, 0.0);
141c36e572eSmbuhl 
142c36e572eSmbuhl 	/* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
143c36e572eSmbuhl 	testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
144c36e572eSmbuhl 	testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
145c36e572eSmbuhl 	testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
146c36e572eSmbuhl 	testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
147c36e572eSmbuhl 	testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
148c36e572eSmbuhl 	testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
149c36e572eSmbuhl }
150c36e572eSmbuhl 
151c36e572eSmbuhl ATF_TC(test_nan_inputs);
ATF_TC_HEAD(test_nan_inputs,tc)152c36e572eSmbuhl ATF_TC_HEAD(test_nan_inputs, tc)
153c36e572eSmbuhl {
154c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr", "test NaN inputs");
155c36e572eSmbuhl }
ATF_TC_BODY(test_nan_inputs,tc)156c36e572eSmbuhl ATF_TC_BODY(test_nan_inputs, tc)
157c36e572eSmbuhl {
158c36e572eSmbuhl 	long double complex nan_nan = CMPLXL(NAN, NAN);
159c36e572eSmbuhl 	long double complex z;
160c36e572eSmbuhl 
161c36e572eSmbuhl 	/*
162c36e572eSmbuhl 	 * IN		CSINH		CCOSH		CTANH
163c36e572eSmbuhl 	 * NaN,NaN	NaN,NaN		NaN,NaN		NaN,NaN
164c36e572eSmbuhl 	 * finite,NaN	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
165c36e572eSmbuhl 	 * NaN,finite	NaN,NaN [inval]	NaN,NaN [inval]	NaN,NaN [inval]
166c36e572eSmbuhl 	 * NaN,Inf	NaN,NaN [inval]	NaN,NaN	[inval]	NaN,NaN [inval]
167c36e572eSmbuhl 	 * Inf,NaN	+-Inf,NaN	Inf,NaN		1,+-0
168c36e572eSmbuhl 	 * 0,NaN	+-0,NaN		NaN,+-0		+-0,NaN
169c36e572eSmbuhl 	 * NaN,0	NaN,0		NaN,+-0		NaN,+-0
170c36e572eSmbuhl 	 */
171c36e572eSmbuhl 	z = nan_nan;
172c36e572eSmbuhl 	testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
173c36e572eSmbuhl 	testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
174c36e572eSmbuhl 	testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
175c36e572eSmbuhl 	testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
176c36e572eSmbuhl 	testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
177c36e572eSmbuhl 	testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
178c36e572eSmbuhl 
179c36e572eSmbuhl 	z = CMPLXL(42, NAN);
180c36e572eSmbuhl 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
181c36e572eSmbuhl 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
182c36e572eSmbuhl 	/* XXX We allow a spurious inexact exception here. */
183c36e572eSmbuhl 	testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
184c36e572eSmbuhl 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
185c36e572eSmbuhl 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
186c36e572eSmbuhl 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
187c36e572eSmbuhl 
188c36e572eSmbuhl 	z = CMPLXL(NAN, 42);
189c36e572eSmbuhl 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
190c36e572eSmbuhl 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
191c36e572eSmbuhl 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
192c36e572eSmbuhl 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
193c36e572eSmbuhl 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
194c36e572eSmbuhl 	/* XXX We allow a spurious inexact exception here. */
195c36e572eSmbuhl 	testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
196c36e572eSmbuhl 
197c36e572eSmbuhl 	z = CMPLXL(NAN, INFINITY);
198c36e572eSmbuhl 	testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
199c36e572eSmbuhl 	testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
200c36e572eSmbuhl 	testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
201c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
202c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
203c36e572eSmbuhl 	    CS_IMAG);
204c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
205c36e572eSmbuhl 
206c36e572eSmbuhl 	z = CMPLXL(INFINITY, NAN);
207c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
208c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
209c36e572eSmbuhl 		     CS_REAL);
210c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
211c36e572eSmbuhl 	testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
212c36e572eSmbuhl 	testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
213c36e572eSmbuhl 	testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
214c36e572eSmbuhl 
215c36e572eSmbuhl 	z = CMPLXL(0, NAN);
216c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
217c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
218c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(0, NAN), OPT_INVALID, 0, CS_REAL);
219c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
220c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
221c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
222c36e572eSmbuhl 
223c36e572eSmbuhl 	z = CMPLXL(NAN, 0);
224c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
225c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
226c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
227c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
228c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0);
229c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
230c36e572eSmbuhl }
231c36e572eSmbuhl 
232c36e572eSmbuhl ATF_TC(test_inf_inputs);
ATF_TC_HEAD(test_inf_inputs,tc)233c36e572eSmbuhl ATF_TC_HEAD(test_inf_inputs, tc)
234c36e572eSmbuhl {
235c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr", "test infinity inputs");
236c36e572eSmbuhl }
ATF_TC_BODY(test_inf_inputs,tc)237c36e572eSmbuhl ATF_TC_BODY(test_inf_inputs, tc)
238c36e572eSmbuhl {
239c36e572eSmbuhl 	static const long double finites[] = {
240c36e572eSmbuhl 	    0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
241c36e572eSmbuhl 	};
242c36e572eSmbuhl 	long double complex z, c, s;
243c36e572eSmbuhl 	unsigned i;
244c36e572eSmbuhl 
245c36e572eSmbuhl 	/*
246c36e572eSmbuhl 	 * IN		CSINH		CCOSH		CTANH
247c36e572eSmbuhl 	 * Inf,Inf	+-Inf,NaN inval	+-Inf,NaN inval	1,+-0
248c36e572eSmbuhl 	 * Inf,finite	Inf cis(finite)	Inf cis(finite)	1,0 sin(2 finite)
249c36e572eSmbuhl 	 * 0,Inf	+-0,NaN	inval	NaN,+-0 inval	+-0,NaN
250c36e572eSmbuhl 	 * finite,Inf	NaN,NaN inval	NaN,NaN inval	NaN,NaN inval
251c36e572eSmbuhl 	 */
252c36e572eSmbuhl 	z = CMPLXL(INFINITY, INFINITY);
253c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(INFINITY, NAN),
254c36e572eSmbuhl 		    ALL_STD_EXCEPT, FE_INVALID, 0);
255c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(INFINITY, NAN),
256c36e572eSmbuhl 		     ALL_STD_EXCEPT, FE_INVALID, 0);
257c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
258c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(NAN, INFINITY),
259c36e572eSmbuhl 		    ALL_STD_EXCEPT, FE_INVALID, 0);
260c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(INFINITY, NAN),
261c36e572eSmbuhl 		     ALL_STD_EXCEPT, FE_INVALID, 0);
262c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
263c36e572eSmbuhl 
264c36e572eSmbuhl 	/* XXX We allow spurious inexact exceptions here (hard to avoid). */
265c36e572eSmbuhl 	for (i = 0; i < nitems(finites); i++) {
266c36e572eSmbuhl 		z = CMPLXL(INFINITY, finites[i]);
267c36e572eSmbuhl 		c = INFINITY * cosl(finites[i]);
268c36e572eSmbuhl 		s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
269c36e572eSmbuhl 		testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
270c36e572eSmbuhl 		testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH);
271c36e572eSmbuhl 		testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)),
272c36e572eSmbuhl 			    OPT_INEXACT, 0, CS_BOTH);
273c36e572eSmbuhl 		z = CMPLXL(finites[i], INFINITY);
274c36e572eSmbuhl 		testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH);
275c36e572eSmbuhl 		testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH);
276c36e572eSmbuhl 		testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1),
277c36e572eSmbuhl 			    OPT_INEXACT, 0, CS_BOTH);
278c36e572eSmbuhl 	}
279c36e572eSmbuhl 
280c36e572eSmbuhl 	z = CMPLXL(0, INFINITY);
281c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
282c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
283c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, CS_REAL);
284c36e572eSmbuhl 	z = CMPLXL(INFINITY, 0);
285c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
286c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
287c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, CS_IMAG);
288c36e572eSmbuhl 
289c36e572eSmbuhl 	z = CMPLXL(42, INFINITY);
290c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
291c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
292c36e572eSmbuhl 	/* XXX We allow a spurious inexact exception here. */
293c36e572eSmbuhl 	testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
294c36e572eSmbuhl 	z = CMPLXL(INFINITY, 42);
295c36e572eSmbuhl 	testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
296c36e572eSmbuhl 	testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
297c36e572eSmbuhl 	/* XXX We allow a spurious inexact exception here. */
298c36e572eSmbuhl 	testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
299c36e572eSmbuhl }
300c36e572eSmbuhl 
301c36e572eSmbuhl ATF_TC(test_axes);
ATF_TC_HEAD(test_axes,tc)302c36e572eSmbuhl ATF_TC_HEAD(test_axes, tc)
303c36e572eSmbuhl {
304c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr", "test along the real/imaginary axes");
305c36e572eSmbuhl }
ATF_TC_BODY(test_axes,tc)306c36e572eSmbuhl ATF_TC_BODY(test_axes, tc)
307c36e572eSmbuhl {
308c36e572eSmbuhl 	static const long double nums[] = {
309c36e572eSmbuhl 	    M_PI / 4, M_PI / 2, 3 * M_PI / 4,
310c36e572eSmbuhl 	    5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
311c36e572eSmbuhl 	};
312c36e572eSmbuhl 	long double complex z;
313c36e572eSmbuhl 	unsigned i;
314c36e572eSmbuhl 
315c36e572eSmbuhl 	for (i = 0; i < nitems(nums); i++) {
316c36e572eSmbuhl 		/* Real axis */
317c36e572eSmbuhl 		z = CMPLXL(nums[i], 0.0);
318c36e572eSmbuhl 		test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP());
319c36e572eSmbuhl 		test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP());
320c36e572eSmbuhl 		test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP());
321c36e572eSmbuhl 		test_odd_tol(csin, z, CMPLXL(sin(nums[i]),
322c36e572eSmbuhl 		    copysign(0, cos(nums[i]))), DBL_ULP());
323c36e572eSmbuhl 		test_even_tol(ccos, z, CMPLXL(cos(nums[i]),
324c36e572eSmbuhl 		    -copysign(0, sin(nums[i]))), DBL_ULP());
325c36e572eSmbuhl 		test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP());
326c36e572eSmbuhl 
327c36e572eSmbuhl 		test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP());
328c36e572eSmbuhl 		test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP());
329c36e572eSmbuhl 		printf("%a %a\n", creal(z), cimag(z));
330c36e572eSmbuhl 		printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z)));
331c36e572eSmbuhl 		printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY));
332c36e572eSmbuhl 		test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0),
333c36e572eSmbuhl 			     1.3 * FLT_ULP());
334c36e572eSmbuhl 		test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]),
335c36e572eSmbuhl 		    copysign(0, cosf(nums[i]))), FLT_ULP());
336c36e572eSmbuhl 		test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]),
337c36e572eSmbuhl 		    -copysign(0, sinf(nums[i]))), 2 * FLT_ULP());
338c36e572eSmbuhl 		test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP());
339c36e572eSmbuhl 
340c36e572eSmbuhl 		/* Imaginary axis */
341c36e572eSmbuhl 		z = CMPLXL(0.0, nums[i]);
342c36e572eSmbuhl 		test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])),
343c36e572eSmbuhl 						 sin(nums[i])), DBL_ULP());
344c36e572eSmbuhl 		test_even_tol(ccosh, z, CMPLXL(cos(nums[i]),
345c36e572eSmbuhl 		    copysign(0, sin(nums[i]))), DBL_ULP());
346c36e572eSmbuhl 		test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP());
347c36e572eSmbuhl 		test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP());
348c36e572eSmbuhl 		test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP());
349c36e572eSmbuhl 		test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP());
350c36e572eSmbuhl 
351c36e572eSmbuhl 		test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])),
352c36e572eSmbuhl 						 sinf(nums[i])), FLT_ULP());
353c36e572eSmbuhl 		test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]),
354c36e572eSmbuhl 		    copysign(0, sinf(nums[i]))), FLT_ULP());
355c36e572eSmbuhl 		test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP());
356c36e572eSmbuhl 		test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP());
357c36e572eSmbuhl 		test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0),
358c36e572eSmbuhl 			      FLT_ULP());
359c36e572eSmbuhl 		test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])),
360c36e572eSmbuhl 			     1.3 * FLT_ULP());
361c36e572eSmbuhl 	}
362c36e572eSmbuhl }
363c36e572eSmbuhl 
364c36e572eSmbuhl ATF_TC(test_small_inputs);
ATF_TC_HEAD(test_small_inputs,tc)365c36e572eSmbuhl ATF_TC_HEAD(test_small_inputs, tc)
366c36e572eSmbuhl {
367c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr", "test underflow inputs");
368c36e572eSmbuhl }
ATF_TC_BODY(test_small_inputs,tc)369c36e572eSmbuhl ATF_TC_BODY(test_small_inputs, tc)
370c36e572eSmbuhl {
371c36e572eSmbuhl 	/*
372c36e572eSmbuhl 	 * z =  0.5 + i Pi/4
373c36e572eSmbuhl 	 *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
374c36e572eSmbuhl 	 *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
375c36e572eSmbuhl 	 *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
376c36e572eSmbuhl 	 * z = -0.5 + i Pi/2
377c36e572eSmbuhl 	 *     sinh(z) = cosh(0.5)
378c36e572eSmbuhl 	 *     cosh(z) = -i sinh(0.5)
379c36e572eSmbuhl 	 *     tanh(z) = -coth(0.5)
380c36e572eSmbuhl 	 * z =  1.0 + i 3Pi/4
381c36e572eSmbuhl 	 *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
382c36e572eSmbuhl 	 *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
383c36e572eSmbuhl 	 *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
384c36e572eSmbuhl 	 */
385c36e572eSmbuhl 	static const struct {
386c36e572eSmbuhl 		long double a, b;
387c36e572eSmbuhl 		long double sinh_a, sinh_b;
388c36e572eSmbuhl 		long double cosh_a, cosh_b;
389c36e572eSmbuhl 		long double tanh_a, tanh_b;
390c36e572eSmbuhl 	} tests[] = {
391c36e572eSmbuhl 		{  0.5L,
392c36e572eSmbuhl 		   0.78539816339744830961566084581987572L,
393c36e572eSmbuhl 		   0.36847002415910435172083660522240710L,
394c36e572eSmbuhl 		   0.79735196663945774996093142586179334L,
395c36e572eSmbuhl 		   0.79735196663945774996093142586179334L,
396c36e572eSmbuhl 		   0.36847002415910435172083660522240710L,
397c36e572eSmbuhl 		   0.76159415595576488811945828260479359L,
398c36e572eSmbuhl 		   0.64805427366388539957497735322615032L },
399c36e572eSmbuhl 		{ -0.5L,
400c36e572eSmbuhl 		   1.57079632679489661923132169163975144L,
401c36e572eSmbuhl 		   0.0L,
402c36e572eSmbuhl 		   1.12762596520638078522622516140267201L,
403c36e572eSmbuhl 		   0.0L,
404c36e572eSmbuhl 		  -0.52109530549374736162242562641149156L,
405c36e572eSmbuhl 		  -2.16395341373865284877000401021802312L,
406c36e572eSmbuhl 		   0.0L },
407c36e572eSmbuhl 		{  1.0L,
408c36e572eSmbuhl 		   2.35619449019234492884698253745962716L,
409c36e572eSmbuhl 		  -0.83099273328405698212637979852748608L,
410c36e572eSmbuhl 		   1.09112278079550143030545602018565236L,
411c36e572eSmbuhl 		  -1.09112278079550143030545602018565236L,
412c36e572eSmbuhl 		   0.83099273328405698212637979852748609L,
413c36e572eSmbuhl 		   0.96402758007581688394641372410092315L,
414c36e572eSmbuhl 		  -0.26580222883407969212086273981988897L }
415c36e572eSmbuhl 	};
416c36e572eSmbuhl 	long double complex z;
417c36e572eSmbuhl 	unsigned i;
418c36e572eSmbuhl 
419c36e572eSmbuhl 	for (i = 0; i < nitems(tests); i++) {
420c36e572eSmbuhl 		z = CMPLXL(tests[i].a, tests[i].b);
421c36e572eSmbuhl 		testall_odd_tol(csinh, z,
422c36e572eSmbuhl 		    CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1);
423c36e572eSmbuhl 		testall_even_tol(ccosh, z,
424c36e572eSmbuhl 		    CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1);
425c36e572eSmbuhl 		testall_odd_tol(ctanh, z,
426c36e572eSmbuhl 		    CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4);
427c36e572eSmbuhl         }
428c36e572eSmbuhl }
429c36e572eSmbuhl 
430c36e572eSmbuhl ATF_TC(test_large_inputs);
ATF_TC_HEAD(test_large_inputs,tc)431c36e572eSmbuhl ATF_TC_HEAD(test_large_inputs, tc)
432c36e572eSmbuhl {
433c36e572eSmbuhl 	atf_tc_set_md_var(tc, "descr",
434c36e572eSmbuhl 	    "Test inputs that might cause overflow in a sloppy implementation");
435c36e572eSmbuhl }
ATF_TC_BODY(test_large_inputs,tc)436c36e572eSmbuhl ATF_TC_BODY(test_large_inputs, tc)
437c36e572eSmbuhl {
438c36e572eSmbuhl 	long double complex z;
439c36e572eSmbuhl 
440c36e572eSmbuhl 	/* tanh() uses a threshold around x=22, so check both sides. */
441c36e572eSmbuhl 	z = CMPLXL(21, 0.78539816339744830961566084581987572L);
442c36e572eSmbuhl 	testall_odd_tol(ctanh, z,
443c36e572eSmbuhl 	    CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2);
444c36e572eSmbuhl 	z++;
445c36e572eSmbuhl 	testall_odd_tol(ctanh, z,
446c36e572eSmbuhl 	    CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1);
447c36e572eSmbuhl 
448c36e572eSmbuhl 	z = CMPLXL(355, 0.78539816339744830961566084581987572L);
449c36e572eSmbuhl 	test_odd_tol(ctanh, z,
450c36e572eSmbuhl 		     CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L),
451c36e572eSmbuhl 		     DBL_ULP());
452c36e572eSmbuhl 	z = CMPLXL(30, 0x1p1023L);
453c36e572eSmbuhl 	test_odd_tol(ctanh, z,
454c36e572eSmbuhl 		     CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L),
455c36e572eSmbuhl 		     DBL_ULP());
456c36e572eSmbuhl 	z = CMPLXL(1, 0x1p1023L);
457c36e572eSmbuhl 	test_odd_tol(ctanh, z,
458c36e572eSmbuhl 		     CMPLXL(0.878606311888306869546254022621986509L,
459c36e572eSmbuhl 			    -0.225462792499754505792678258169527424L),
460c36e572eSmbuhl 		     DBL_ULP());
461c36e572eSmbuhl 
462c36e572eSmbuhl 	z = CMPLXL(710.6, 0.78539816339744830961566084581987572L);
463c36e572eSmbuhl 	test_odd_tol(csinh, z,
464c36e572eSmbuhl 	    CMPLXL(1.43917579766621073533185387499658944e308L,
465c36e572eSmbuhl 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
466c36e572eSmbuhl 	test_even_tol(ccosh, z,
467c36e572eSmbuhl 	    CMPLXL(1.43917579766621073533185387499658944e308L,
468c36e572eSmbuhl 		   1.43917579766621073533185387499658944e308L), DBL_ULP());
469c36e572eSmbuhl 
470c36e572eSmbuhl 	z = CMPLXL(1500, 0.78539816339744830961566084581987572L);
471c36e572eSmbuhl 	testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
472c36e572eSmbuhl 	    FE_OVERFLOW, CS_BOTH);
473c36e572eSmbuhl 	testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT,
474c36e572eSmbuhl 	    FE_OVERFLOW, CS_BOTH);
475c36e572eSmbuhl }
476c36e572eSmbuhl 
ATF_TP_ADD_TCS(tp)477c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
478c36e572eSmbuhl {
479c36e572eSmbuhl 
480c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_zero_input);
481c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_nan_inputs);
482c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_inf_inputs);
483c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_axes);
484c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_small_inputs);
485c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test_large_inputs);
486c36e572eSmbuhl 
487c36e572eSmbuhl 	return (atf_no_error());
488c36e572eSmbuhl }
489