2 * Copyright (c) 2008-2011 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Tests for corner cases in cexp*().
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
42 FE_OVERFLOW | FE_UNDERFLOW)
43 #define FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
44 #define DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
45 #define LDBL_ULP() ldexpl(1.0, 1 - LDBL_MANT_DIG)
47 #define N(i) (sizeof(i) / sizeof((i)[0]))
49 #pragma STDC FENV_ACCESS ON
50 #pragma STDC CX_LIMITED_RANGE OFF
53 * XXX gcc implements complex multiplication incorrectly. In
54 * particular, it implements it as if the CX_LIMITED_RANGE pragma
55 * were ON. Consequently, we need this function to form numbers
56 * such as x + INFINITY * I, since gcc evalutes INFINITY * I as
59 static inline long double complex
60 cpackl(long double x, long double y)
62 long double complex z;
70 * Test that a function returns the correct value and sets the
71 * exception flags correctly. The exceptmask specifies which
72 * exceptions we should check. We need to be lenient for several
73 * reasons, but mainly because on some architectures it's impossible
74 * to raise FE_OVERFLOW without raising FE_INEXACT. In some cases,
75 * whether cexp() raises an invalid exception is unspecified.
77 * These are macros instead of functions so that assert provides more
78 * meaningful error messages.
80 * XXX The volatile here is to avoid gcc's bogus constant folding and work
81 * around the lack of support for the FENV_ACCESS pragma.
83 #define test(func, z, result, exceptmask, excepts, checksign) do { \
84 volatile long double complex _d = z; \
85 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
86 assert(cfpequal((func)(_d), (result), (checksign))); \
87 assert(((func), fetestexcept(exceptmask) == (excepts))); \
90 /* Test within a given tolerance. */
91 #define test_tol(func, z, result, tol) do { \
92 volatile long double complex _d = z; \
93 assert(cfpequal_tol((func)(_d), (result), (tol))); \
96 /* Test all the functions that compute cexp(x). */
97 #define testall(x, result, exceptmask, excepts, checksign) do { \
98 test(cexp, x, result, exceptmask, excepts, checksign); \
99 test(cexpf, x, result, exceptmask, excepts, checksign); \
103 * Test all the functions that compute cexp(x), within a given tolerance.
104 * The tolerance is specified in ulps.
106 #define testall_tol(x, result, tol) do { \
107 test_tol(cexp, x, result, tol * DBL_ULP()); \
108 test_tol(cexpf, x, result, tol * FLT_ULP()); \
111 /* Various finite non-zero numbers to test. */
112 static const float finites[] =
113 { -42.0e20, -1.0 -1.0e-10, -0.0, 0.0, 1.0e-10, 1.0, 42.0e20 };
116 * Determine whether x and y are equal, with two special rules:
119 * If checksign is 0, we compare the absolute values instead.
122 fpequal(long double x, long double y, int checksign)
124 if (isnan(x) || isnan(y))
127 return (x == y && !signbit(x) == !signbit(y));
129 return (fabsl(x) == fabsl(y));
133 fpequal_tol(long double x, long double y, long double tol)
138 if (isnan(x) && isnan(y))
140 if (!signbit(x) != !signbit(y))
147 /* Hard case: need to check the tolerance. */
150 * For our purposes here, if y=0, we interpret tol as an absolute
151 * tolerance. This is to account for roundoff in the input, e.g.,
155 ret = fabsl(x - y) <= fabsl(tol);
157 ret = fabsl(x - y) <= fabsl(y * tol);
163 cfpequal(long double complex x, long double complex y, int checksign)
165 return (fpequal(creal(x), creal(y), checksign)
166 && fpequal(cimag(x), cimag(y), checksign));
170 cfpequal_tol(long double complex x, long double complex y, long double tol)
172 return (fpequal_tol(creal(x), creal(y), tol)
173 && fpequal_tol(cimag(x), cimag(y), tol));
182 /* cexp(0) = 1, no exceptions raised */
183 testall(0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
184 testall(-0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
185 testall(cpackl(0.0, -0.0), cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
186 testall(cpackl(-0.0, -0.0), cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
190 * Tests for NaN. The signs of the results are indeterminate unless the
191 * imaginary part is 0.
198 /* cexp(x + NaNi) = NaN + NaNi and optionally raises invalid */
199 /* cexp(NaN + yi) = NaN + NaNi and optionally raises invalid (|y|>0) */
200 for (i = 0; i < N(finites); i++) {
201 testall(cpackl(finites[i], NAN), cpackl(NAN, NAN),
202 ALL_STD_EXCEPT & ~FE_INVALID, 0, 0);
203 if (finites[i] == 0.0)
205 /* XXX FE_INEXACT shouldn't be raised here */
206 testall(cpackl(NAN, finites[i]), cpackl(NAN, NAN),
207 ALL_STD_EXCEPT & ~(FE_INVALID | FE_INEXACT), 0, 0);
210 /* cexp(NaN +- 0i) = NaN +- 0i */
211 testall(cpackl(NAN, 0.0), cpackl(NAN, 0.0), ALL_STD_EXCEPT, 0, 1);
212 testall(cpackl(NAN, -0.0), cpackl(NAN, -0.0), ALL_STD_EXCEPT, 0, 1);
214 /* cexp(inf + NaN i) = inf + nan i */
215 testall(cpackl(INFINITY, NAN), cpackl(INFINITY, NAN),
216 ALL_STD_EXCEPT, 0, 0);
217 /* cexp(-inf + NaN i) = 0 */
218 testall(cpackl(-INFINITY, NAN), cpackl(0.0, 0.0),
219 ALL_STD_EXCEPT, 0, 0);
220 /* cexp(NaN + NaN i) = NaN + NaN i */
221 testall(cpackl(NAN, NAN), cpackl(NAN, NAN),
222 ALL_STD_EXCEPT, 0, 0);
230 /* cexp(x + inf i) = NaN + NaNi and raises invalid */
231 /* cexp(inf + yi) = 0 + 0yi */
232 /* cexp(-inf + yi) = inf + inf yi (except y=0) */
233 for (i = 0; i < N(finites); i++) {
234 testall(cpackl(finites[i], INFINITY), cpackl(NAN, NAN),
235 ALL_STD_EXCEPT, FE_INVALID, 1);
236 /* XXX shouldn't raise an inexact exception */
237 testall(cpackl(-INFINITY, finites[i]),
238 cpackl(0.0, 0.0 * finites[i]),
239 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
242 testall(cpackl(INFINITY, finites[i]),
243 cpackl(INFINITY, INFINITY * finites[i]),
244 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
246 testall(cpackl(INFINITY, 0.0), cpackl(INFINITY, 0.0),
247 ALL_STD_EXCEPT, 0, 1);
248 testall(cpackl(INFINITY, -0.0), cpackl(INFINITY, -0.0),
249 ALL_STD_EXCEPT, 0, 1);
257 for (i = 0; i < N(finites); i++) {
258 /* XXX could check exceptions more meticulously */
259 test(cexp, cpackl(finites[i], 0.0),
260 cpackl(exp(finites[i]), 0.0),
261 FE_INVALID | FE_DIVBYZERO, 0, 1);
262 test(cexp, cpackl(finites[i], -0.0),
263 cpackl(exp(finites[i]), -0.0),
264 FE_INVALID | FE_DIVBYZERO, 0, 1);
265 test(cexpf, cpackl(finites[i], 0.0),
266 cpackl(expf(finites[i]), 0.0),
267 FE_INVALID | FE_DIVBYZERO, 0, 1);
268 test(cexpf, cpackl(finites[i], -0.0),
269 cpackl(expf(finites[i]), -0.0),
270 FE_INVALID | FE_DIVBYZERO, 0, 1);
275 test_imaginaries(void)
279 for (i = 0; i < N(finites); i++) {
280 test(cexp, cpackl(0.0, finites[i]),
281 cpackl(cos(finites[i]), sin(finites[i])),
282 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
283 test(cexp, cpackl(-0.0, finites[i]),
284 cpackl(cos(finites[i]), sin(finites[i])),
285 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
286 test(cexpf, cpackl(0.0, finites[i]),
287 cpackl(cosf(finites[i]), sinf(finites[i])),
288 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
289 test(cexpf, cpackl(-0.0, finites[i]),
290 cpackl(cosf(finites[i]), sinf(finites[i])),
291 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
298 static const double tests[] = {
299 /* csqrt(a + bI) = x + yI */
301 1.0, M_PI_4, M_SQRT2 * 0.5 * M_E, M_SQRT2 * 0.5 * M_E,
302 -1.0, M_PI_4, M_SQRT2 * 0.5 / M_E, M_SQRT2 * 0.5 / M_E,
303 2.0, M_PI_2, 0.0, M_E * M_E,
304 M_LN2, M_PI, -2.0, 0.0,
310 for (i = 0; i < N(tests); i += 4) {
315 test_tol(cexp, cpackl(a, b), cpackl(x, y), 3 * DBL_ULP());
317 /* float doesn't have enough precision to pass these tests */
318 if (x == 0 || y == 0)
320 test_tol(cexpf, cpackl(a, b), cpackl(x, y), 1 * FLT_ULP());
324 /* Test inputs with a real part r that would overflow exp(r). */
329 test_tol(cexp, cpackl(709.79, 0x1p-1074),
330 cpackl(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
331 test_tol(cexp, cpackl(1000, 0x1p-1074),
332 cpackl(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
333 test_tol(cexp, cpackl(1400, 0x1p-1074),
334 cpackl(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
335 test_tol(cexp, cpackl(900, 0x1.23456789abcdep-1020),
336 cpackl(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
337 test_tol(cexp, cpackl(1300, 0x1.23456789abcdep-1020),
338 cpackl(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
340 test_tol(cexpf, cpackl(88.73, 0x1p-149),
341 cpackl(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
342 test_tol(cexpf, cpackl(90, 0x1p-149),
343 cpackl(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
344 test_tol(cexpf, cpackl(192, 0x1p-149),
345 cpackl(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
346 test_tol(cexpf, cpackl(120, 0x1.234568p-120),
347 cpackl(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
348 test_tol(cexpf, cpackl(170, 0x1.234568p-120),
349 cpackl(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
353 main(int argc, char *argv[])
359 printf("ok 1 - cexp zero\n");
362 printf("ok 2 - cexp nan\n");
365 printf("ok 3 - cexp inf\n");
368 printf("ok 4 - cexp reals\n");
371 printf("ok 5 - cexp imaginaries\n");
374 printf("ok 6 - cexp small\n");
377 printf("ok 7 - cexp large\n");