xref: /dragonfly/tools/regression/lib/libm/test-ctrig.c (revision 7f8c68295613ce24cc71827cf210cb3d1e3bc69b)
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  * $FreeBSD: src/tools/regression/lib/msun/test-ctrig.c,v 1.1 2011/10/21 06:34:38 das Exp $
27  */
28 
29 /*
30  * Tests for csin[h](), ccos[h](), and ctan[h]().
31  */
32 
33 #include <assert.h>
34 #include <complex.h>
35 #include <fenv.h>
36 #include <float.h>
37 #include <math.h>
38 #include <stdio.h>
39 
40 #define   ALL_STD_EXCEPT      (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
41                                FE_OVERFLOW | FE_UNDERFLOW)
42 #define   OPT_INVALID         (ALL_STD_EXCEPT & ~FE_INVALID)
43 #define   OPT_INEXACT         (ALL_STD_EXCEPT & ~FE_INEXACT)
44 #define   FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
45 #define   DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
46 #define   LDBL_ULP()          ldexpl(1.0, 1 - LDBL_MANT_DIG)
47 
48 #pragma STDC FENV_ACCESS      ON
49 #pragma   STDC CX_LIMITED_RANGE         OFF
50 
51 /*
52  * XXX gcc implements complex multiplication incorrectly. In
53  * particular, it implements it as if the CX_LIMITED_RANGE pragma
54  * were ON. Consequently, we need this function to form numbers
55  * such as x + INFINITY * I, since gcc evalutes INFINITY * I as
56  * NaN + INFINITY * I.
57  */
58 static inline long double complex
cpackl(long double x,long double y)59 cpackl(long double x, long double y)
60 {
61           long double complex z;
62 
63           __real__ z = x;
64           __imag__ z = y;
65           return (z);
66 }
67 
68 /* Flags that determine whether to check the signs of the result. */
69 #define   CS_REAL   1
70 #define   CS_IMAG   2
71 #define   CS_BOTH   (CS_REAL | CS_IMAG)
72 
73 #ifdef    DEBUG
74 #define   debug(...)          printf(__VA_ARGS__)
75 #else
76 #define   debug(...)          (void)0
77 #endif
78 
79 /*
80  * Test that a function returns the correct value and sets the
81  * exception flags correctly. The exceptmask specifies which
82  * exceptions we should check. We need to be lenient for several
83  * reasons, but mainly because on some architectures it's impossible
84  * to raise FE_OVERFLOW without raising FE_INEXACT.
85  *
86  * These are macros instead of functions so that assert provides more
87  * meaningful error messages.
88  *
89  * XXX The volatile here is to avoid gcc's bogus constant folding and work
90  *     around the lack of support for the FENV_ACCESS pragma.
91  */
92 #define   test_p(func, z, result, exceptmask, excepts, checksign)     do {      \
93           volatile long double complex _d = z;                                  \
94           debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,  \
95               creall(_d), cimagl(_d), creall(result), cimagl(result));          \
96           assert(feclearexcept(FE_ALL_EXCEPT) == 0);                            \
97           assert(cfpequal((func)(_d), (result), (checksign)));                  \
98           assert(((func), fetestexcept(exceptmask) == (excepts)));    \
99 } while (0)
100 
101 /*
102  * Test within a given tolerance.  The tolerance indicates relative error
103  * in ulps.  If result is 0, however, it measures absolute error in units
104  * of <format>_EPSILON.
105  */
106 #define   test_p_tol(func, z, result, tol)                            do {      \
107           volatile long double complex _d = z;                                  \
108           debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,  \
109               creall(_d), cimagl(_d), creall(result), cimagl(result));          \
110           assert(cfpequal_tol((func)(_d), (result), (tol)));                    \
111 } while (0)
112 
113 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
114 #define   test(func, z, result, exceptmask, excepts, checksign)       do {      \
115           test_p(func, z, result, exceptmask, excepts, checksign);    \
116           test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
117 } while (0)
118 #define   test_tol(func, z, result, tol)                                        do {      \
119           test_p_tol(func, z, result, tol);                                     \
120           test_p_tol(func, conjl(z), conjl(result), tol);                       \
121 } while (0)
122 
123 /* Test the given function in all precisions. */
124 #define   testall(func, x, result, exceptmask, excepts, checksign) do {         \
125           test(func, x, result, exceptmask, excepts, checksign);                \
126           test(func##f, x, result, exceptmask, excepts, checksign);   \
127 } while (0)
128 #define   testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
129           testall(func, x, result, exceptmask, excepts, checksign);   \
130           testall(func, -x, -result, exceptmask, excepts, checksign); \
131 } while (0)
132 #define   testall_even(func, x, result, exceptmask, excepts, checksign) do { \
133           testall(func, x, result, exceptmask, excepts, checksign);   \
134           testall(func, -x, result, exceptmask, excepts, checksign);  \
135 } while (0)
136 
137 /*
138  * Test the given function in all precisions, within a given tolerance.
139  * The tolerance is specified in ulps.
140  */
141 #define   testall_tol(func, x, result, tol)                              do { \
142           test_tol(func, x, result, tol * DBL_ULP());                           \
143           test_tol(func##f, x, result, tol * FLT_ULP());                        \
144 } while (0)
145 #define   testall_odd_tol(func, x, result, tol)                          do { \
146           test_tol(func, x, result, tol * DBL_ULP());                           \
147           test_tol(func, -x, -result, tol * DBL_ULP());                         \
148 } while (0)
149 #define   testall_even_tol(func, x, result, tol)                         do { \
150           test_tol(func, x, result, tol * DBL_ULP());                           \
151           test_tol(func, -x, result, tol * DBL_ULP());                          \
152 } while (0)
153 
154 /*
155  * Determine whether x and y are equal, with two special rules:
156  *        +0.0 != -0.0
157  *         NaN == NaN
158  * If checksign is 0, we compare the absolute values instead.
159  */
160 static int
fpequal(long double x,long double y,int checksign)161 fpequal(long double x, long double y, int checksign)
162 {
163           if (isnan(x) && isnan(y))
164                     return (1);
165           if (checksign)
166                     return (x == y && !signbit(x) == !signbit(y));
167           else
168                     return (fabsl(x) == fabsl(y));
169 }
170 
171 static int
fpequal_tol(long double x,long double y,long double tol)172 fpequal_tol(long double x, long double y, long double tol)
173 {
174           fenv_t env;
175           int ret;
176 
177           if (isnan(x) && isnan(y))
178                     return (1);
179           if (!signbit(x) != !signbit(y) && tol == 0)
180                     return (0);
181           if (x == y)
182                     return (1);
183           if (tol == 0)
184                     return (0);
185 
186           /* Hard case: need to check the tolerance. */
187           feholdexcept(&env);
188           /*
189            * For our purposes here, if y=0, we interpret tol as an absolute
190            * tolerance. This is to account for roundoff in the input, e.g.,
191            * cos(Pi/2) ~= 0.
192            */
193           if (y == 0.0)
194                     ret = fabsl(x - y) <= fabsl(tol);
195           else
196                     ret = fabsl(x - y) <= fabsl(y * tol);
197           fesetenv(&env);
198           return (ret);
199 }
200 
201 static int
cfpequal(long double complex x,long double complex y,int checksign)202 cfpequal(long double complex x, long double complex y, int checksign)
203 {
204           return (fpequal(creal(x), creal(y), checksign & CS_REAL)
205                     && fpequal(cimag(x), cimag(y), checksign & CS_IMAG));
206 }
207 
208 static int
cfpequal_tol(long double complex x,long double complex y,long double tol)209 cfpequal_tol(long double complex x, long double complex y, long double tol)
210 {
211           return (fpequal_tol(creal(x), creal(y), tol)
212                     && fpequal_tol(cimag(x), cimag(y), tol));
213 }
214 
215 
216 /* Tests for 0 */
217 void
test_zero(void)218 test_zero(void)
219 {
220           long double complex zero = cpackl(0.0, 0.0);
221 
222           /* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
223           testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
224           testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
225           testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
226           testall_even(ccos, zero, cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
227           testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
228           testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
229 }
230 
231 /*
232  * Tests for NaN inputs.
233  */
234 void
test_nan()235 test_nan()
236 {
237           long double complex nan_nan = cpackl(NAN, NAN);
238           long double complex z;
239 
240           /*
241            * IN               CSINH               CCOSH               CTANH
242            * NaN,NaN          NaN,NaN             NaN,NaN             NaN,NaN
243            * finite,NaN       NaN,NaN [inval]     NaN,NaN [inval]     NaN,NaN [inval]
244            * NaN,finite       NaN,NaN [inval]     NaN,NaN [inval]     NaN,NaN [inval]
245            * NaN,Inf          NaN,NaN [inval]     NaN,NaN   [inval]   NaN,NaN [inval]
246            * Inf,NaN          +-Inf,NaN Inf,NaN             1,+-0
247            * 0,NaN  +-0,NaN             NaN,+-0             NaN,NaN   [inval]
248            * NaN,0  NaN,0               NaN,+-0             NaN,0
249            */
250           z = nan_nan;
251           testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
252           testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
253           testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
254           testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
255           testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
256           testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
257 
258           z = cpackl(42, NAN);
259           testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
260           testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
261           /* XXX We allow a spurious inexact exception here. */
262           testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
263           testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
264           testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
265           testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
266 
267           z = cpackl(NAN, 42);
268           testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
269           testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
270           testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
271           testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
272           testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
273           /* XXX We allow a spurious inexact exception here. */
274           testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
275 
276           z = cpackl(NAN, INFINITY);
277           testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
278           testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
279           testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
280           testall_odd(csin, z, cpackl(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
281           testall_even(ccos, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0,
282               CS_IMAG);
283           testall_odd(ctan, z, cpackl(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
284 
285           z = cpackl(INFINITY, NAN);
286           testall_odd(csinh, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
287           testall_even(ccosh, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0,
288                          CS_REAL);
289           testall_odd(ctanh, z, cpackl(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
290           testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
291           testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
292           testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
293 
294           z = cpackl(0, NAN);
295           testall_odd(csinh, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, 0);
296           testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
297           testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
298           testall_odd(csin, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
299           testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
300           testall_odd(ctan, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
301 
302           z = cpackl(NAN, 0);
303           testall_odd(csinh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
304           testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
305           testall_odd(ctanh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
306           testall_odd(csin, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
307           testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
308           testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
309 }
310 
311 void
test_inf(void)312 test_inf(void)
313 {
314           static const long double finites[] = {
315               0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
316           };
317           long double complex z, c, s;
318           int i;
319 
320           /*
321            * IN               CSINH               CCOSH               CTANH
322            * Inf,Inf          +-Inf,NaN inval     +-Inf,NaN inval     1,+-0
323            * Inf,finite       Inf cis(finite)     Inf cis(finite)     1,0 sin(2 finite)
324            * 0,Inf  +-0,NaN   inval     NaN,+-0 inval       NaN,NaN   inval
325            * finite,Inf       NaN,NaN inval       NaN,NaN inval       NaN,NaN inval
326            */
327           z = cpackl(INFINITY, INFINITY);
328           testall_odd(csinh, z, cpackl(INFINITY, NAN),
329                         ALL_STD_EXCEPT, FE_INVALID, 0);
330           testall_even(ccosh, z, cpackl(INFINITY, NAN),
331                          ALL_STD_EXCEPT, FE_INVALID, 0);
332           testall_odd(ctanh, z, cpackl(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
333           testall_odd(csin, z, cpackl(NAN, INFINITY),
334                         ALL_STD_EXCEPT, FE_INVALID, 0);
335           testall_even(ccos, z, cpackl(INFINITY, NAN),
336                          ALL_STD_EXCEPT, FE_INVALID, 0);
337           testall_odd(ctan, z, cpackl(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
338 
339           /* XXX We allow spurious inexact exceptions here (hard to avoid). */
340           for (i = 0; i < sizeof(finites) / sizeof(finites[0]); i++) {
341                     z = cpackl(INFINITY, finites[i]);
342                     c = INFINITY * cosl(finites[i]);
343                     s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
344                     testall_odd(csinh, z, cpackl(c, s), OPT_INEXACT, 0, CS_BOTH);
345                     testall_even(ccosh, z, cpackl(c, s), OPT_INEXACT, 0, CS_BOTH);
346                     testall_odd(ctanh, z, cpackl(1, 0 * sin(finites[i] * 2)),
347                                   OPT_INEXACT, 0, CS_BOTH);
348                     z = cpackl(finites[i], INFINITY);
349                     testall_odd(csin, z, cpackl(s, c), OPT_INEXACT, 0, CS_BOTH);
350                     testall_even(ccos, z, cpackl(c, -s), OPT_INEXACT, 0, CS_BOTH);
351                     testall_odd(ctan, z, cpackl(0 * sin(finites[i] * 2), 1),
352                                   OPT_INEXACT, 0, CS_BOTH);
353           }
354 
355           z = cpackl(0, INFINITY);
356           testall_odd(csinh, z, cpackl(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
357           testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
358           testall_odd(ctanh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
359           z = cpackl(INFINITY, 0);
360           testall_odd(csin, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
361           testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
362           testall_odd(ctan, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
363 
364           z = cpackl(42, INFINITY);
365           testall_odd(csinh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
366           testall_even(ccosh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
367           /* XXX We allow a spurious inexact exception here. */
368           testall_odd(ctanh, z, cpackl(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
369           z = cpackl(INFINITY, 42);
370           testall_odd(csin, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
371           testall_even(ccos, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
372           /* XXX We allow a spurious inexact exception here. */
373           testall_odd(ctan, z, cpackl(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
374 }
375 
376 /* Tests along the real and imaginary axes. */
377 void
test_axes(void)378 test_axes(void)
379 {
380           static const long double nums[] = {
381               M_PI / 4, M_PI / 2, 3 * M_PI / 4,
382               5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
383           };
384           long double complex z;
385           int i;
386 
387           for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
388                     /* Real axis */
389                     z = cpackl(nums[i], 0.0);
390                     testall_odd_tol(csinh, z, cpackl(sinh(nums[i]), 0), 0);
391                     testall_even_tol(ccosh, z, cpackl(cosh(nums[i]), 0), 0);
392                     testall_odd_tol(ctanh, z, cpackl(tanh(nums[i]), 0), 1);
393                     testall_odd_tol(csin, z, cpackl(sin(nums[i]),
394                                                       copysign(0, cos(nums[i]))), 0);
395                     testall_even_tol(ccos, z, cpackl(cos(nums[i]),
396                         -copysign(0, sin(nums[i]))), 0);
397                     testall_odd_tol(ctan, z, cpackl(tan(nums[i]), 0), 1);
398 
399                     /* Imaginary axis */
400                     z = cpackl(0.0, nums[i]);
401                     testall_odd_tol(csinh, z, cpackl(copysign(0, cos(nums[i])),
402                                                              sin(nums[i])), 0);
403                     testall_even_tol(ccosh, z, cpackl(cos(nums[i]),
404                         copysign(0, sin(nums[i]))), 0);
405                     testall_odd_tol(ctanh, z, cpackl(0, tan(nums[i])), 1);
406                     testall_odd_tol(csin, z, cpackl(0, sinh(nums[i])), 0);
407                     testall_even_tol(ccos, z, cpackl(cosh(nums[i]), -0.0), 0);
408                     testall_odd_tol(ctan, z, cpackl(0, tanh(nums[i])), 1);
409           }
410 }
411 
412 void
test_small(void)413 test_small(void)
414 {
415           /*
416            * z =  0.5 + i Pi/4
417            *     sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
418            *     cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
419            *     tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
420            * z = -0.5 + i Pi/2
421            *     sinh(z) = cosh(0.5)
422            *     cosh(z) = -i sinh(0.5)
423            *     tanh(z) = -coth(0.5)
424            * z =  1.0 + i 3Pi/4
425            *     sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
426            *     cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
427            *     tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
428            */
429           static const struct {
430                     long double a, b;
431                     long double sinh_a, sinh_b;
432                     long double cosh_a, cosh_b;
433                     long double tanh_a, tanh_b;
434           } tests[] = {
435                     {  0.5L,
436                        0.78539816339744830961566084581987572L,
437                        0.36847002415910435172083660522240710L,
438                        0.79735196663945774996093142586179334L,
439                        0.79735196663945774996093142586179334L,
440                        0.36847002415910435172083660522240710L,
441                        0.76159415595576488811945828260479359L,
442                        0.64805427366388539957497735322615032L },
443                     { -0.5L,
444                        1.57079632679489661923132169163975144L,
445                        0.0L,
446                        1.12762596520638078522622516140267201L,
447                        0.0L,
448                       -0.52109530549374736162242562641149156L,
449                       -2.16395341373865284877000401021802312L,
450                        0.0L },
451                     {  1.0L,
452                        2.35619449019234492884698253745962716L,
453                       -0.83099273328405698212637979852748608L,
454                        1.09112278079550143030545602018565236L,
455                       -1.09112278079550143030545602018565236L,
456                        0.83099273328405698212637979852748609L,
457                        0.96402758007581688394641372410092315L,
458                       -0.26580222883407969212086273981988897L }
459           };
460           long double complex z;
461           int i;
462 
463           for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
464                     z = cpackl(tests[i].a, tests[i].b);
465                     testall_odd_tol(csinh, z,
466                         cpackl(tests[i].sinh_a, tests[i].sinh_b), 1.1);
467                     testall_even_tol(ccosh, z,
468                         cpackl(tests[i].cosh_a, tests[i].cosh_b), 1.1);
469                     testall_odd_tol(ctanh, z,
470                         cpackl(tests[i].tanh_a, tests[i].tanh_b), 1.1);
471         }
472 }
473 
474 /* Test inputs that might cause overflow in a sloppy implementation. */
475 void
test_large(void)476 test_large(void)
477 {
478           long double complex z;
479 
480           /* tanh() uses a threshold around x=22, so check both sides. */
481           z = cpackl(21, 0.78539816339744830961566084581987572L);
482           testall_odd_tol(ctanh, z,
483               cpackl(1.0, 1.14990445285871196133287617611468468e-18L), 1);
484           z++;
485           testall_odd_tol(ctanh, z,
486               cpackl(1.0, 1.55622644822675930314266334585597964e-19L), 1);
487 
488           z = cpackl(355, 0.78539816339744830961566084581987572L);
489           testall_odd_tol(ctanh, z,
490               cpackl(1.0, 8.95257245135025991216632140458264468e-309L), 1);
491           z = cpackl(30, 0x1p1023L);
492           testall_odd_tol(ctanh, z,
493               cpackl(1.0, -1.62994325413993477997492170229268382e-26L), 1);
494           z = cpackl(1, 0x1p1023L);
495           testall_odd_tol(ctanh, z,
496               cpackl(0.878606311888306869546254022621986509L,
497                        -0.225462792499754505792678258169527424L), 1);
498 
499           z = cpackl(710.6, 0.78539816339744830961566084581987572L);
500           testall_odd_tol(csinh, z,
501               cpackl(1.43917579766621073533185387499658944e308L,
502                        1.43917579766621073533185387499658944e308L), 1);
503           testall_even_tol(ccosh, z,
504               cpackl(1.43917579766621073533185387499658944e308L,
505                        1.43917579766621073533185387499658944e308L), 1);
506 
507           z = cpackl(1500, 0.78539816339744830961566084581987572L);
508           testall_odd(csinh, z, cpackl(INFINITY, INFINITY), OPT_INEXACT,
509               FE_OVERFLOW, CS_BOTH);
510           testall_even(ccosh, z, cpackl(INFINITY, INFINITY), OPT_INEXACT,
511               FE_OVERFLOW, CS_BOTH);
512 }
513 
514 int
main(int argc,char * argv[])515 main(int argc, char *argv[])
516 {
517 
518           printf("1..6\n");
519 
520           test_zero();
521           printf("ok 1 - ctrig zero\n");
522 
523           test_nan();
524           printf("ok 2 - ctrig nan\n");
525 
526           test_inf();
527           printf("ok 3 - ctrig inf\n");
528 
529           test_axes();
530           printf("ok 4 - ctrig axes\n");
531 
532           test_small();
533           printf("ok 5 - ctrig small\n");
534 
535           test_large();
536           printf("ok 6 - ctrig large\n");
537 
538           return (0);
539 }
540