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