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
27 /*
28 * Tests for casin[h](), cacos[h](), and catan[h]().
29 */
30
31 #include <sys/param.h>
32 #include <complex.h>
33 #include <fenv.h>
34 #include <float.h>
35 #include <math.h>
36 #include <stdio.h>
37
38 #include "test-utils.h"
39
40 #pragma STDC FENV_ACCESS ON
41 #pragma STDC CX_LIMITED_RANGE OFF
42
43 /*
44 * Test that a function returns the correct value and sets the
45 * exception flags correctly. The exceptmask specifies which
46 * exceptions we should check. We need to be lenient for several
47 * reasons, but mainly because on some architectures it's impossible
48 * to raise FE_OVERFLOW without raising FE_INEXACT.
49 *
50 * These are macros instead of functions so that assert provides more
51 * meaningful error messages.
52 *
53 * XXX The volatile here is to avoid gcc's bogus constant folding and work
54 * around the lack of support for the FENV_ACCESS pragma.
55 */
56 #define test_p(func, z, result, exceptmask, excepts, checksign) do { \
57 volatile long double complex _d = z; \
58 debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \
59 creall(_d), cimagl(_d), creall(result), cimagl(result)); \
60 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
61 CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign)); \
62 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
63 #func, #z); \
64 } while (0)
65
66 /*
67 * Test within a given tolerance. The tolerance indicates relative error
68 * in ulps.
69 */
70 #define test_p_tol(func, z, result, tol) do { \
71 debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \
72 creall(z), cimagl(z), creall(result), cimagl(result)); \
73 CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), CS_BOTH); \
74 } while (0)
75
76 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
77 #define test(func, z, result, exceptmask, excepts, checksign) do { \
78 test_p(func, z, result, exceptmask, excepts, checksign); \
79 test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
80 } while (0)
81 #define test_tol(func, z, result, tol) do { \
82 test_p_tol(func, z, result, tol); \
83 test_p_tol(func, conjl(z), conjl(result), tol); \
84 } while (0)
85
86 /* Test the given function in all precisions. */
87 #define testall(func, x, result, exceptmask, excepts, checksign) do { \
88 test(func, x, result, exceptmask, excepts, checksign); \
89 test(func##f, x, result, exceptmask, excepts, checksign); \
90 } while (0)
91 #define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
92 testall(func, x, result, exceptmask, excepts, checksign); \
93 testall(func, -(x), -result, exceptmask, excepts, checksign); \
94 } while (0)
95 #define testall_even(func, x, result, exceptmask, excepts, checksign) do { \
96 testall(func, x, result, exceptmask, excepts, checksign); \
97 testall(func, -(x), result, exceptmask, excepts, checksign); \
98 } while (0)
99
100 /*
101 * Test the given function in all precisions, within a given tolerance.
102 * The tolerance is specified in ulps.
103 */
104 #define testall_tol(func, x, result, tol) do { \
105 test_tol(func, x, result, (tol) * DBL_ULP()); \
106 test_tol(func##f, x, result, (tol) * FLT_ULP()); \
107 } while (0)
108 #define testall_odd_tol(func, x, result, tol) do { \
109 testall_tol(func, x, result, tol); \
110 testall_tol(func, -(x), -result, tol); \
111 } while (0)
112 #define testall_even_tol(func, x, result, tol) do { \
113 testall_tol(func, x, result, tol); \
114 testall_tol(func, -(x), result, tol); \
115 } while (0)
116
117 static const long double
118 pi = 3.14159265358979323846264338327950280L,
119 c3pi = 9.42477796076937971538793014983850839L;
120
121
122 /* Tests for 0 */
123 ATF_TC_WITHOUT_HEAD(zero);
ATF_TC_BODY(zero,tc)124 ATF_TC_BODY(zero, tc)
125 {
126 #if defined(__riscv)
127 atf_tc_expect_death("https://bugs.freebsd.org/290099");
128 #endif
129 long double complex zero = CMPLXL(0.0, 0.0);
130
131 testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
132 testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
133 testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
134 testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
135
136 testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
137 testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
138
139 testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
140 testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
141 }
142
143 /*
144 * Tests for NaN inputs.
145 */
146 ATF_TC_WITHOUT_HEAD(nan);
ATF_TC_BODY(nan,tc)147 ATF_TC_BODY(nan, tc)
148 {
149 #if defined(__riscv)
150 atf_tc_expect_death("https://bugs.freebsd.org/290099");
151 #endif
152 long double complex nan_nan = CMPLXL(NAN, NAN);
153 long double complex z;
154
155 /*
156 * IN CACOSH CACOS CASINH CATANH
157 * NaN,NaN NaN,NaN NaN,NaN NaN,NaN NaN,NaN
158 * finite,NaN NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN*
159 * NaN,finite NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN*
160 * NaN,Inf Inf,NaN NaN,-Inf ?Inf,NaN ?0,pi/2
161 * +-Inf,NaN Inf,NaN NaN,?Inf +-Inf,NaN +-0,NaN
162 * +-0,NaN NaN,NaN* pi/2,NaN NaN,NaN* +-0,NaN
163 * NaN,0 NaN,NaN* NaN,NaN* NaN,0 NaN,NaN*
164 *
165 * * = raise invalid
166 */
167 z = nan_nan;
168 testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
169 testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
170 testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
171 testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
172 testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
173 testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
174
175 z = CMPLXL(0.5, NAN);
176 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
177 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
178 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
179 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
180 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
181 testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
182
183 z = CMPLXL(NAN, 0.5);
184 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
185 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
186 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
187 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
188 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
189 testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
190
191 z = CMPLXL(NAN, INFINITY);
192 testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
193 testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
194 testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
195 testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
196 testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
197 testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
198 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
199
200 z = CMPLXL(INFINITY, NAN);
201 testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
202 CS_REAL);
203 testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
204 testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
205 CS_REAL);
206 testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
207 testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
208 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
209
210 z = CMPLXL(0.0, NAN);
211 /* XXX We allow a spurious inexact exception here. */
212 testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
213 testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
214 testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
215 testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
216 testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
217 testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
218
219 z = CMPLXL(NAN, 0.0);
220 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
221 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
222 testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
223 testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
224 testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
225 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
226 }
227
228 ATF_TC_WITHOUT_HEAD(inf);
ATF_TC_BODY(inf,tc)229 ATF_TC_BODY(inf, tc)
230 {
231 #if defined(__riscv)
232 atf_tc_expect_death("https://bugs.freebsd.org/290099");
233 #endif
234 long double complex z;
235
236 /*
237 * IN CACOSH CACOS CASINH CATANH
238 * Inf,Inf Inf,pi/4 pi/4,-Inf Inf,pi/4 0,pi/2
239 * -Inf,Inf Inf,3pi/4 3pi/4,-Inf --- ---
240 * Inf,finite Inf,0 0,-Inf Inf,0 0,pi/2
241 * -Inf,finite Inf,pi pi,-Inf --- ---
242 * finite,Inf Inf,pi/2 pi/2,-Inf Inf,pi/2 0,pi/2
243 */
244 z = CMPLXL(INFINITY, INFINITY);
245 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
246 testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
247 testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
248 testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
249 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
250 testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
251 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
252 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
253
254 z = CMPLXL(INFINITY, 0.5);
255 /* XXX We allow a spurious inexact exception here. */
256 testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
257 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
258 testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
259 testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
260 testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
261 testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
262 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
263 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
264
265 z = CMPLXL(0.5, INFINITY);
266 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
267 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
268 testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
269 testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
270 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
271 /* XXX We allow a spurious inexact exception here. */
272 testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
273 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
274 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
275 }
276
277 /* Tests along the real and imaginary axes. */
278 ATF_TC_WITHOUT_HEAD(axes);
ATF_TC_BODY(axes,tc)279 ATF_TC_BODY(axes, tc)
280 {
281 #if defined(__riscv)
282 atf_tc_expect_death("https://bugs.freebsd.org/290099");
283 #endif
284 static const long double nums[] = {
285 -2, -1, -0.5, 0.5, 1, 2
286 };
287 long double complex z;
288 unsigned i;
289
290 for (i = 0; i < nitems(nums); i++) {
291 /* Real axis */
292 z = CMPLXL(nums[i], 0.0);
293 if (fabsl(nums[i]) <= 1) {
294 testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
295 testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
296 testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
297 testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
298 } else {
299 testall_tol(cacosh, z,
300 CMPLXL(acosh(fabsl(nums[i])),
301 (nums[i] < 0) ? pi : 0), 1);
302 testall_tol(cacos, z,
303 CMPLXL((nums[i] < 0) ? pi : 0,
304 -acosh(fabsl(nums[i]))), 1);
305 testall_tol(casin, z,
306 CMPLXL(copysign(pi / 2, nums[i]),
307 acosh(fabsl(nums[i]))), 1);
308 testall_tol(catanh, z,
309 CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
310 }
311 testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
312 testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
313
314 /* TODO: Test the imaginary axis. */
315 }
316 }
317
318 ATF_TC_WITHOUT_HEAD(small);
ATF_TC_BODY(small,tc)319 ATF_TC_BODY(small, tc)
320 {
321 #if defined(__riscv)
322 atf_tc_expect_death("https://bugs.freebsd.org/290099");
323 #endif
324 /*
325 * z = 0.75 + i 0.25
326 * acos(z) = Pi/4 - i ln(2)/2
327 * asin(z) = Pi/4 + i ln(2)/2
328 * atan(z) = atan(4)/2 + i ln(17/9)/4
329 */
330 complex long double z;
331 complex long double acos_z;
332 complex long double asin_z;
333 complex long double atan_z;
334
335 z = CMPLXL(0.75L, 0.25L);
336 acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L);
337 asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L);
338 atan_z = CMPLXL(0.66290883183401623252961960521423782L,
339 0.15899719167999917436476103600701878L);
340
341 testall_tol(cacos, z, acos_z, 2);
342 testall_odd_tol(casin, z, asin_z, 2);
343 testall_odd_tol(catan, z, atan_z, 2);
344 }
345
346 /* Test inputs that might cause overflow in a sloppy implementation. */
347 ATF_TC_WITHOUT_HEAD(large);
ATF_TC_BODY(large,tc)348 ATF_TC_BODY(large, tc)
349 {
350 /* TODO: Write these tests */
351 }
352
ATF_TP_ADD_TCS(tp)353 ATF_TP_ADD_TCS(tp)
354 {
355 ATF_TP_ADD_TC(tp, zero);
356 ATF_TP_ADD_TC(tp, nan);
357 ATF_TP_ADD_TC(tp, inf);
358 ATF_TP_ADD_TC(tp, axes);
359 ATF_TP_ADD_TC(tp, small);
360 ATF_TP_ADD_TC(tp, large);
361
362 return (atf_no_error());
363 }
364