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