1 /*-
2 * Copyright (c) 2008-2013 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-invctrig.c,v 1.1 2013/05/31 00:27:55 svnexp Exp $
27 */
28
29 /*
30 * Tests for casin[h](), cacos[h](), and catan[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 /* Flags that determine whether to check the signs of the result. */
52 #define CS_REAL 1
53 #define CS_IMAG 2
54 #define CS_BOTH (CS_REAL | CS_IMAG)
55
56 #ifdef DEBUG
57 #define debug(...) printf(__VA_ARGS__)
58 #else
59 #define debug(...) (void)0
60 #endif
61
62 /*
63 * Test that a function returns the correct value and sets the
64 * exception flags correctly. The exceptmask specifies which
65 * exceptions we should check. We need to be lenient for several
66 * reasons, but mainly because on some architectures it's impossible
67 * to raise FE_OVERFLOW without raising FE_INEXACT.
68 *
69 * These are macros instead of functions so that assert provides more
70 * meaningful error messages.
71 *
72 * XXX The volatile here is to avoid gcc's bogus constant folding and work
73 * around the lack of support for the FENV_ACCESS pragma.
74 */
75 #define test_p(func, z, result, exceptmask, excepts, checksign) do { \
76 volatile long double complex _d = z; \
77 debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \
78 creall(_d), cimagl(_d), creall(result), cimagl(result)); \
79 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
80 assert(cfpequal((func)(_d), (result), (checksign))); \
81 assert(((func), fetestexcept(exceptmask) == (excepts))); \
82 } while (0)
83
84 /*
85 * Test within a given tolerance. The tolerance indicates relative error
86 * in ulps.
87 */
88 #define test_p_tol(func, z, result, tol) do { \
89 volatile long double complex _d = z; \
90 debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \
91 creall(_d), cimagl(_d), creall(result), cimagl(result)); \
92 assert(cfpequal_tol((func)(_d), (result), (tol))); \
93 } while (0)
94
95 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
96 #define test(func, z, result, exceptmask, excepts, checksign) do { \
97 test_p(func, z, result, exceptmask, excepts, checksign); \
98 test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
99 } while (0)
100 #define test_tol(func, z, result, tol) do { \
101 test_p_tol(func, z, result, tol); \
102 test_p_tol(func, conjl(z), conjl(result), tol); \
103 } while (0)
104
105 /* Test the given function in all precisions. */
106 #define testall(func, x, result, exceptmask, excepts, checksign) do { \
107 test(func, x, result, exceptmask, excepts, checksign); \
108 test(func##f, x, result, exceptmask, excepts, checksign); \
109 } while (0)
110 #define testall_odd(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 #define testall_even(func, x, result, exceptmask, excepts, checksign) do { \
115 testall(func, x, result, exceptmask, excepts, checksign); \
116 testall(func, -(x), result, exceptmask, excepts, checksign); \
117 } while (0)
118
119 /*
120 * Test the given function in all precisions, within a given tolerance.
121 * The tolerance is specified in ulps.
122 */
123 #define testall_tol(func, x, result, tol) do { \
124 test_tol(func, x, result, (tol) * DBL_ULP()); \
125 test_tol(func##f, x, result, (tol) * FLT_ULP()); \
126 } while (0)
127 #define testall_odd_tol(func, x, result, tol) do { \
128 testall_tol(func, x, result, tol); \
129 testall_tol(func, -(x), -result, tol); \
130 } while (0)
131 #define testall_even_tol(func, x, result, tol) do { \
132 testall_tol(func, x, result, tol); \
133 testall_tol(func, -(x), result, tol); \
134 } while (0)
135
136 /* XXX static const long double */
137 #define pi 3.14159265358979323846264338327950280L
138 #define c3pi 9.42477796076937971538793014983850839L
139
140 /*
141 * Determine whether x and y are equal, with two special rules:
142 * +0.0 != -0.0
143 * NaN == NaN
144 * If checksign is 0, we compare the absolute values instead.
145 */
146 static int
fpequal(long double x,long double y,int checksign)147 fpequal(long double x, long double y, int checksign)
148 {
149 if (isnan(x) && isnan(y))
150 return (1);
151 if (checksign)
152 return (x == y && !signbit(x) == !signbit(y));
153 else
154 return (fabsl(x) == fabsl(y));
155 }
156
157 static int
fpequal_tol(long double x,long double y,long double tol)158 fpequal_tol(long double x, long double y, long double tol)
159 {
160 fenv_t env;
161 int ret;
162
163 if (isnan(x) && isnan(y))
164 return (1);
165 if (!signbit(x) != !signbit(y))
166 return (0);
167 if (x == y)
168 return (1);
169 if (tol == 0 || y == 0.0)
170 return (0);
171
172 /* Hard case: need to check the tolerance. */
173 feholdexcept(&env);
174 ret = fabsl(x - y) <= fabsl(y * tol);
175 fesetenv(&env);
176 return (ret);
177 }
178
179 static int
cfpequal(long double complex x,long double complex y,int checksign)180 cfpequal(long double complex x, long double complex y, int checksign)
181 {
182 return (fpequal(creal(x), creal(y), checksign & CS_REAL)
183 && fpequal(cimag(x), cimag(y), checksign & CS_IMAG));
184 }
185
186 static int
cfpequal_tol(long double complex x,long double complex y,long double tol)187 cfpequal_tol(long double complex x, long double complex y, long double tol)
188 {
189 return (fpequal_tol(creal(x), creal(y), tol)
190 && fpequal_tol(cimag(x), cimag(y), tol));
191 }
192
193
194 /* Tests for 0 */
195 void
test_zero(void)196 test_zero(void)
197 {
198 long double complex zero = CMPLXL(0.0, 0.0);
199
200 testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
201 testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
202 testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
203 testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
204
205 testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
206 testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
207
208 testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
209 testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
210 }
211
212 /*
213 * Tests for NaN inputs.
214 */
215 void
test_nan()216 test_nan()
217 {
218 long double complex nan_nan = CMPLXL(NAN, NAN);
219 long double complex z;
220
221 /*
222 * IN CACOSH CACOS CASINH CATANH
223 * NaN,NaN NaN,NaN NaN,NaN NaN,NaN NaN,NaN
224 * finite,NaN NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN*
225 * NaN,finite NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN*
226 * NaN,Inf Inf,NaN NaN,-Inf ?Inf,NaN ?0,pi/2
227 * +-Inf,NaN Inf,NaN NaN,?Inf +-Inf,NaN +-0,NaN
228 * +-0,NaN NaN,NaN* pi/2,NaN NaN,NaN* +-0,NaN
229 * NaN,0 NaN,NaN* NaN,NaN* NaN,0 NaN,NaN*
230 *
231 * * = raise invalid
232 */
233 z = nan_nan;
234 testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
235 testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
236 testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
237 testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
238 testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
239 testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
240
241 z = CMPLXL(0.5, NAN);
242 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
243 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
244 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
245 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
246 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
247 testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
248
249 z = CMPLXL(NAN, 0.5);
250 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
251 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
252 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
253 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
254 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
255 testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
256
257 z = CMPLXL(NAN, INFINITY);
258 testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
259 testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
260 testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
261 testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
262 testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
263 testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
264 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
265
266 z = CMPLXL(INFINITY, NAN);
267 testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
268 CS_REAL);
269 testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
270 testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
271 CS_REAL);
272 testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
273 testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
274 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
275
276 z = CMPLXL(0.0, NAN);
277 /* XXX We allow a spurious inexact exception here. */
278 testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
279 testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
280 testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
281 testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
282 testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
283 testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
284
285 z = CMPLXL(NAN, 0.0);
286 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
287 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
288 testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
289 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
290 testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
291 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
292 }
293
294 void
test_inf(void)295 test_inf(void)
296 {
297 long double complex z;
298
299 /*
300 * IN CACOSH CACOS CASINH CATANH
301 * Inf,Inf Inf,pi/4 pi/4,-Inf Inf,pi/4 0,pi/2
302 * -Inf,Inf Inf,3pi/4 3pi/4,-Inf --- ---
303 * Inf,finite Inf,0 0,-Inf Inf,0 0,pi/2
304 * -Inf,finite Inf,pi pi,-Inf --- ---
305 * finite,Inf Inf,pi/2 pi/2,-Inf Inf,pi/2 0,pi/2
306 */
307 z = CMPLXL(INFINITY, INFINITY);
308 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
309 testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
310 testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
311 testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
312 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
313 testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
314 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
315 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
316
317 z = CMPLXL(INFINITY, 0.5);
318 /* XXX We allow a spurious inexact exception here. */
319 testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
320 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
321 testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
322 testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
323 testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
324 testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
325 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
326 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
327
328 z = CMPLXL(0.5, INFINITY);
329 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
330 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
331 testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
332 testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
333 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
334 /* XXX We allow a spurious inexact exception here. */
335 testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
336 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
337 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
338 }
339
340 /* Tests along the real and imaginary axes. */
341 void
test_axes(void)342 test_axes(void)
343 {
344 static const long double nums[] = {
345 -2, -1, -0.5, 0.5, 1, 2
346 };
347 long double complex z;
348 int i;
349
350 for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
351 /* Real axis */
352 z = CMPLXL(nums[i], 0.0);
353 if (fabs(nums[i]) <= 1) {
354 testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
355 testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
356 testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
357 testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
358 } else {
359 testall_tol(cacosh, z,
360 CMPLXL(acosh(fabs(nums[i])),
361 (nums[i] < 0) ? pi : 0), 1);
362 testall_tol(cacos, z,
363 CMPLXL((nums[i] < 0) ? pi : 0,
364 -acosh(fabs(nums[i]))), 1);
365 testall_tol(casin, z,
366 CMPLXL(copysign(pi / 2, nums[i]),
367 acosh(fabs(nums[i]))), 1);
368 testall_tol(catanh, z,
369 CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
370 }
371 testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
372 testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
373
374 /* TODO: Test the imaginary axis. */
375 }
376 }
377
378 void
test_small(void)379 test_small(void)
380 {
381 /*
382 * z = 0.75 + i 0.25
383 * acos(z) = Pi/4 - i ln(2)/2
384 * asin(z) = Pi/4 + i ln(2)/2
385 * atan(z) = atan(4)/2 + i ln(17/9)/4
386 */
387 static const struct {
388 complex long double z;
389 complex long double acos_z;
390 complex long double asin_z;
391 complex long double atan_z;
392 } tests[] = {
393 { CMPLXL(0.75L, 0.25L),
394 CMPLXL(pi / 4, -0.34657359027997265470861606072908828L),
395 CMPLXL(pi / 4, 0.34657359027997265470861606072908828L),
396 CMPLXL(0.66290883183401623252961960521423782L,
397 0.15899719167999917436476103600701878L) },
398 };
399 int i;
400
401 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
402 testall_tol(cacos, tests[i].z, tests[i].acos_z, 2);
403 testall_odd_tol(casin, tests[i].z, tests[i].asin_z, 2);
404 testall_odd_tol(catan, tests[i].z, tests[i].atan_z, 2);
405 }
406 }
407
408 /* Test inputs that might cause overflow in a sloppy implementation. */
409 void
test_large(void)410 test_large(void)
411 {
412
413 /* TODO: Write these tests */
414 }
415
416 int
main(int argc,char * argv[])417 main(int argc, char *argv[])
418 {
419
420 printf("1..6\n");
421
422 test_zero();
423 printf("ok 1 - invctrig zero\n");
424
425 test_nan();
426 printf("ok 2 - invctrig nan\n");
427
428 test_inf();
429 printf("ok 3 - invctrig inf\n");
430
431 test_axes();
432 printf("ok 4 - invctrig axes\n");
433
434 test_small();
435 printf("ok 5 - invctrig small\n");
436
437 test_large();
438 printf("ok 6 - invctrig large\n");
439
440 return (0);
441 }
442