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 corner cases in cexp*().
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/9/tools/regression/lib/msun/test-cexp.c 292806 2015-12-27 21:55:26Z ngie $");
33
34 #include <assert.h>
35 #include <complex.h>
36 #include <fenv.h>
37 #include <float.h>
38 #include <math.h>
39 #include <stdio.h>
40
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)
46
47 #define N(i) (sizeof(i) / sizeof((i)[0]))
48
49 #pragma STDC FENV_ACCESS ON
50 #pragma STDC CX_LIMITED_RANGE OFF
51
52 /*
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
57 * NaN + INFINITY * I.
58 */
59 static inline long double complex
cpackl(long double x,long double y)60 cpackl(long double x, long double y)
61 {
62 long double complex z;
63
64 __real__ z = x;
65 __imag__ z = y;
66 return (z);
67 }
68
69 /*
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.
76 *
77 * These are macros instead of functions so that assert provides more
78 * meaningful error messages.
79 *
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.
82 */
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))); \
88 } while (0)
89
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))); \
94 } while (0)
95
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); \
100 } while (0)
101
102 /*
103 * Test all the functions that compute cexp(x), within a given tolerance.
104 * The tolerance is specified in ulps.
105 */
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()); \
109 } while (0)
110
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 };
114
115 /*
116 * Determine whether x and y are equal, with two special rules:
117 * +0.0 != -0.0
118 * NaN == NaN
119 * If checksign is 0, we compare the absolute values instead.
120 */
121 static int
fpequal(long double x,long double y,int checksign)122 fpequal(long double x, long double y, int checksign)
123 {
124 if (isnan(x) || isnan(y))
125 return (1);
126 if (checksign)
127 return (x == y && !signbit(x) == !signbit(y));
128 else
129 return (fabsl(x) == fabsl(y));
130 }
131
132 static int
fpequal_tol(long double x,long double y,long double tol)133 fpequal_tol(long double x, long double y, long double tol)
134 {
135 fenv_t env;
136 int ret;
137
138 if (isnan(x) && isnan(y))
139 return (1);
140 if (!signbit(x) != !signbit(y))
141 return (0);
142 if (x == y)
143 return (1);
144 if (tol == 0)
145 return (0);
146
147 /* Hard case: need to check the tolerance. */
148 feholdexcept(&env);
149 /*
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.,
152 * cos(Pi/2) ~= 0.
153 */
154 if (y == 0.0)
155 ret = fabsl(x - y) <= fabsl(tol);
156 else
157 ret = fabsl(x - y) <= fabsl(y * tol);
158 fesetenv(&env);
159 return (ret);
160 }
161
162 static int
cfpequal(long double complex x,long double complex y,int checksign)163 cfpequal(long double complex x, long double complex y, int checksign)
164 {
165 return (fpequal(creal(x), creal(y), checksign)
166 && fpequal(cimag(x), cimag(y), checksign));
167 }
168
169 static int
cfpequal_tol(long double complex x,long double complex y,long double tol)170 cfpequal_tol(long double complex x, long double complex y, long double tol)
171 {
172 return (fpequal_tol(creal(x), creal(y), tol)
173 && fpequal_tol(cimag(x), cimag(y), tol));
174 }
175
176
177 /* Tests for 0 */
178 void
test_zero(void)179 test_zero(void)
180 {
181
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);
187 }
188
189 /*
190 * Tests for NaN. The signs of the results are indeterminate unless the
191 * imaginary part is 0.
192 */
193 void
test_nan()194 test_nan()
195 {
196 int i;
197
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)
204 continue;
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);
208 }
209
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);
213
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);
223 }
224
225 void
test_inf(void)226 test_inf(void)
227 {
228 int i;
229
230 /* cexp(x + inf i) = NaN + NaNi and raises invalid */
231 for (i = 0; i < N(finites); i++) {
232 testall(cpackl(finites[i], INFINITY), cpackl(NAN, NAN),
233 ALL_STD_EXCEPT, FE_INVALID, 1);
234 }
235 /* cexp(-inf + yi) = 0 * (cos(y) + sin(y)i) */
236 /* XXX shouldn't raise an inexact exception */
237 testall(cpackl(-INFINITY, M_PI_4), cpackl(0.0, 0.0),
238 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
239 testall(cpackl(-INFINITY, 3 * M_PI_4), cpackl(-0.0, 0.0),
240 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
241 testall(cpackl(-INFINITY, 5 * M_PI_4), cpackl(-0.0, -0.0),
242 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
243 testall(cpackl(-INFINITY, 7 * M_PI_4), cpackl(0.0, -0.0),
244 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
245 testall(cpackl(-INFINITY, 0.0), cpackl(0.0, 0.0),
246 ALL_STD_EXCEPT, 0, 1);
247 testall(cpackl(-INFINITY, -0.0), cpackl(0.0, -0.0),
248 ALL_STD_EXCEPT, 0, 1);
249 /* cexp(inf + yi) = inf * (cos(y) + sin(y)i) (except y=0) */
250 /* XXX shouldn't raise an inexact exception */
251 testall(cpackl(INFINITY, M_PI_4), cpackl(INFINITY, INFINITY),
252 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
253 testall(cpackl(INFINITY, 3 * M_PI_4), cpackl(-INFINITY, INFINITY),
254 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
255 testall(cpackl(INFINITY, 5 * M_PI_4), cpackl(-INFINITY, -INFINITY),
256 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
257 testall(cpackl(INFINITY, 7 * M_PI_4), cpackl(INFINITY, -INFINITY),
258 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
259 /* cexp(inf + 0i) = inf + 0i */
260 testall(cpackl(INFINITY, 0.0), cpackl(INFINITY, 0.0),
261 ALL_STD_EXCEPT, 0, 1);
262 testall(cpackl(INFINITY, -0.0), cpackl(INFINITY, -0.0),
263 ALL_STD_EXCEPT, 0, 1);
264 }
265
266 void
test_reals(void)267 test_reals(void)
268 {
269 int i;
270
271 for (i = 0; i < N(finites); i++) {
272 /* XXX could check exceptions more meticulously */
273 test(cexp, cpackl(finites[i], 0.0),
274 cpackl(exp(finites[i]), 0.0),
275 FE_INVALID | FE_DIVBYZERO, 0, 1);
276 test(cexp, cpackl(finites[i], -0.0),
277 cpackl(exp(finites[i]), -0.0),
278 FE_INVALID | FE_DIVBYZERO, 0, 1);
279 test(cexpf, cpackl(finites[i], 0.0),
280 cpackl(expf(finites[i]), 0.0),
281 FE_INVALID | FE_DIVBYZERO, 0, 1);
282 test(cexpf, cpackl(finites[i], -0.0),
283 cpackl(expf(finites[i]), -0.0),
284 FE_INVALID | FE_DIVBYZERO, 0, 1);
285 }
286 }
287
288 void
test_imaginaries(void)289 test_imaginaries(void)
290 {
291 int i;
292
293 for (i = 0; i < N(finites); i++) {
294 test(cexp, cpackl(0.0, finites[i]),
295 cpackl(cos(finites[i]), sin(finites[i])),
296 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
297 test(cexp, cpackl(-0.0, finites[i]),
298 cpackl(cos(finites[i]), sin(finites[i])),
299 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
300 test(cexpf, cpackl(0.0, finites[i]),
301 cpackl(cosf(finites[i]), sinf(finites[i])),
302 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
303 test(cexpf, cpackl(-0.0, finites[i]),
304 cpackl(cosf(finites[i]), sinf(finites[i])),
305 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
306 }
307 }
308
309 void
test_small(void)310 test_small(void)
311 {
312 static const double tests[] = {
313 /* csqrt(a + bI) = x + yI */
314 /* a b x y */
315 1.0, M_PI_4, M_SQRT2 * 0.5 * M_E, M_SQRT2 * 0.5 * M_E,
316 -1.0, M_PI_4, M_SQRT2 * 0.5 / M_E, M_SQRT2 * 0.5 / M_E,
317 2.0, M_PI_2, 0.0, M_E * M_E,
318 M_LN2, M_PI, -2.0, 0.0,
319 };
320 double a, b;
321 double x, y;
322 int i;
323
324 for (i = 0; i < N(tests); i += 4) {
325 a = tests[i];
326 b = tests[i + 1];
327 x = tests[i + 2];
328 y = tests[i + 3];
329 test_tol(cexp, cpackl(a, b), cpackl(x, y), 3 * DBL_ULP());
330
331 /* float doesn't have enough precision to pass these tests */
332 if (x == 0 || y == 0)
333 continue;
334 test_tol(cexpf, cpackl(a, b), cpackl(x, y), 1 * FLT_ULP());
335 }
336 }
337
338 /* Test inputs with a real part r that would overflow exp(r). */
339 void
test_large(void)340 test_large(void)
341 {
342
343 test_tol(cexp, cpackl(709.79, 0x1p-1074),
344 cpackl(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
345 test_tol(cexp, cpackl(1000, 0x1p-1074),
346 cpackl(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
347 test_tol(cexp, cpackl(1400, 0x1p-1074),
348 cpackl(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
349 test_tol(cexp, cpackl(900, 0x1.23456789abcdep-1020),
350 cpackl(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
351 test_tol(cexp, cpackl(1300, 0x1.23456789abcdep-1020),
352 cpackl(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
353
354 test_tol(cexpf, cpackl(88.73, 0x1p-149),
355 cpackl(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
356 test_tol(cexpf, cpackl(90, 0x1p-149),
357 cpackl(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
358 test_tol(cexpf, cpackl(192, 0x1p-149),
359 cpackl(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
360 test_tol(cexpf, cpackl(120, 0x1.234568p-120),
361 cpackl(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
362 test_tol(cexpf, cpackl(170, 0x1.234568p-120),
363 cpackl(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
364 }
365
366 int
main(int argc,char * argv[])367 main(int argc, char *argv[])
368 {
369
370 printf("1..7\n");
371
372 test_zero();
373 printf("ok 1 - cexp zero\n");
374
375 test_nan();
376 printf("ok 2 - cexp nan\n");
377
378 test_inf();
379 printf("ok 3 - cexp inf\n");
380
381 test_reals();
382 printf("ok 4 - cexp reals\n");
383
384 test_imaginaries();
385 printf("ok 5 - cexp imaginaries\n");
386
387 test_small();
388 printf("ok 6 - cexp small\n");
389
390 test_large();
391 printf("ok 7 - cexp large\n");
392
393 return (0);
394 }
395