xref: /dragonfly/tools/regression/lib/libm/test-cexp.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-cexp.c,v 1.3 2013/05/29 00:27:12 svnexp Exp $
27  */
28 
29 /*
30  * Tests for corner cases in cexp*().
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   FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
43 #define   DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
44 #define   LDBL_ULP()          ldexpl(1.0, 1 - LDBL_MANT_DIG)
45 
46 #define   N(i)      (sizeof(i) / sizeof((i)[0]))
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 /*
69  * Test that a function returns the correct value and sets the
70  * exception flags correctly. The exceptmask specifies which
71  * exceptions we should check. We need to be lenient for several
72  * reasons, but mainly because on some architectures it's impossible
73  * to raise FE_OVERFLOW without raising FE_INEXACT. In some cases,
74  * whether cexp() raises an invalid exception is unspecified.
75  *
76  * These are macros instead of functions so that assert provides more
77  * meaningful error messages.
78  *
79  * XXX The volatile here is to avoid gcc's bogus constant folding and work
80  *     around the lack of support for the FENV_ACCESS pragma.
81  */
82 #define   test(func, z, result, exceptmask, excepts, checksign)       do {      \
83           volatile long double complex _d = z;                                  \
84           assert(feclearexcept(FE_ALL_EXCEPT) == 0);                            \
85           assert(cfpequal((func)(_d), (result), (checksign)));                  \
86           assert(((func), fetestexcept(exceptmask) == (excepts)));    \
87 } while (0)
88 
89 /* Test within a given tolerance. */
90 #define   test_tol(func, z, result, tol)                                        do {      \
91           volatile long double complex _d = z;                                  \
92           assert(cfpequal_tol((func)(_d), (result), (tol)));                    \
93 } while (0)
94 
95 /* Test all the functions that compute cexp(x). */
96 #define   testall(x, result, exceptmask, excepts, checksign)          do {      \
97           test(cexp, x, result, exceptmask, excepts, checksign);                \
98           test(cexpf, x, result, exceptmask, excepts, checksign);               \
99 } while (0)
100 
101 /*
102  * Test all the functions that compute cexp(x), within a given tolerance.
103  * The tolerance is specified in ulps.
104  */
105 #define   testall_tol(x, result, tol)                                 do {      \
106           test_tol(cexp, x, result, tol * DBL_ULP());                           \
107           test_tol(cexpf, x, result, tol * FLT_ULP());                          \
108 } while (0)
109 
110 /* Various finite non-zero numbers to test. */
111 static const float finites[] =
112 { -42.0e20, -1.0, -1.0e-10, -0.0, 0.0, 1.0e-10, 1.0, 42.0e20 };
113 
114 /*
115  * Determine whether x and y are equal, with two special rules:
116  *        +0.0 != -0.0
117  *         NaN == NaN
118  * If checksign is 0, we compare the absolute values instead.
119  */
120 static int
fpequal(long double x,long double y,int checksign)121 fpequal(long double x, long double y, int checksign)
122 {
123           if (isnan(x) || isnan(y))
124                     return (1);
125           if (checksign)
126                     return (x == y && !signbit(x) == !signbit(y));
127           else
128                     return (fabsl(x) == fabsl(y));
129 }
130 
131 static int
fpequal_tol(long double x,long double y,long double tol)132 fpequal_tol(long double x, long double y, long double tol)
133 {
134           fenv_t env;
135           int ret;
136 
137           if (isnan(x) && isnan(y))
138                     return (1);
139           if (!signbit(x) != !signbit(y))
140                     return (0);
141           if (x == y)
142                     return (1);
143           if (tol == 0)
144                     return (0);
145 
146           /* Hard case: need to check the tolerance. */
147           feholdexcept(&env);
148           /*
149            * For our purposes here, if y=0, we interpret tol as an absolute
150            * tolerance. This is to account for roundoff in the input, e.g.,
151            * cos(Pi/2) ~= 0.
152            */
153           if (y == 0.0)
154                     ret = fabsl(x - y) <= fabsl(tol);
155           else
156                     ret = fabsl(x - y) <= fabsl(y * tol);
157           fesetenv(&env);
158           return (ret);
159 }
160 
161 static int
cfpequal(long double complex x,long double complex y,int checksign)162 cfpequal(long double complex x, long double complex y, int checksign)
163 {
164           return (fpequal(creal(x), creal(y), checksign)
165                     && fpequal(cimag(x), cimag(y), checksign));
166 }
167 
168 static int
cfpequal_tol(long double complex x,long double complex y,long double tol)169 cfpequal_tol(long double complex x, long double complex y, long double tol)
170 {
171           return (fpequal_tol(creal(x), creal(y), tol)
172                     && fpequal_tol(cimag(x), cimag(y), tol));
173 }
174 
175 
176 /* Tests for 0 */
177 void
test_zero(void)178 test_zero(void)
179 {
180 
181           /* cexp(0) = 1, no exceptions raised */
182           testall(0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
183           testall(-0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
184           testall(cpackl(0.0, -0.0), cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
185           testall(cpackl(-0.0, -0.0), cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
186 }
187 
188 /*
189  * Tests for NaN.  The signs of the results are indeterminate unless the
190  * imaginary part is 0.
191  */
192 void
test_nan()193 test_nan()
194 {
195           int i;
196 
197           /* cexp(x + NaNi) = NaN + NaNi and optionally raises invalid */
198           /* cexp(NaN + yi) = NaN + NaNi and optionally raises invalid (|y|>0) */
199           for (i = 0; i < N(finites); i++) {
200                     testall(cpackl(finites[i], NAN), cpackl(NAN, NAN),
201                               ALL_STD_EXCEPT & ~FE_INVALID, 0, 0);
202                     if (finites[i] == 0.0)
203                               continue;
204                     /* XXX FE_INEXACT shouldn't be raised here */
205                     testall(cpackl(NAN, finites[i]), cpackl(NAN, NAN),
206                               ALL_STD_EXCEPT & ~(FE_INVALID | FE_INEXACT), 0, 0);
207           }
208 
209           /* cexp(NaN +- 0i) = NaN +- 0i */
210           testall(cpackl(NAN, 0.0), cpackl(NAN, 0.0), ALL_STD_EXCEPT, 0, 1);
211           testall(cpackl(NAN, -0.0), cpackl(NAN, -0.0), ALL_STD_EXCEPT, 0, 1);
212 
213           /* cexp(inf + NaN i) = inf + nan i */
214           testall(cpackl(INFINITY, NAN), cpackl(INFINITY, NAN),
215                     ALL_STD_EXCEPT, 0, 0);
216           /* cexp(-inf + NaN i) = 0 */
217           testall(cpackl(-INFINITY, NAN), cpackl(0.0, 0.0),
218                     ALL_STD_EXCEPT, 0, 0);
219           /* cexp(NaN + NaN i) = NaN + NaN i */
220           testall(cpackl(NAN, NAN), cpackl(NAN, NAN),
221                     ALL_STD_EXCEPT, 0, 0);
222 }
223 
224 void
test_inf(void)225 test_inf(void)
226 {
227           int i;
228 
229           /* cexp(x + inf i) = NaN + NaNi and raises invalid */
230           for (i = 0; i < N(finites); i++) {
231                     testall(cpackl(finites[i], INFINITY), cpackl(NAN, NAN),
232                               ALL_STD_EXCEPT, FE_INVALID, 1);
233           }
234           /* cexp(-inf + yi) = 0 * (cos(y) + sin(y)i) */
235                     /* XXX shouldn't raise an inexact exception */
236           testall(cpackl(-INFINITY, M_PI_4), cpackl(0.0, 0.0),
237                               ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
238           testall(cpackl(-INFINITY, 3 * M_PI_4), cpackl(-0.0, 0.0),
239                               ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
240           testall(cpackl(-INFINITY, 5 * M_PI_4), cpackl(-0.0, -0.0),
241                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
242           testall(cpackl(-INFINITY, 7 * M_PI_4), cpackl(0.0, -0.0),
243                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
244           testall(cpackl(-INFINITY, 0.0), cpackl(0.0, 0.0),
245                     ALL_STD_EXCEPT, 0, 1);
246           testall(cpackl(-INFINITY, -0.0), cpackl(0.0, -0.0),
247                     ALL_STD_EXCEPT, 0, 1);
248           /* cexp(inf + yi) = inf * (cos(y) + sin(y)i) (except y=0) */
249           /* XXX shouldn't raise an inexact exception */
250           testall(cpackl(INFINITY, M_PI_4), cpackl(INFINITY, INFINITY),
251                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
252           testall(cpackl(INFINITY, 3 * M_PI_4), cpackl(-INFINITY, INFINITY),
253                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
254           testall(cpackl(INFINITY, 5 * M_PI_4), cpackl(-INFINITY, -INFINITY),
255                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
256           testall(cpackl(INFINITY, 7 * M_PI_4), cpackl(INFINITY, -INFINITY),
257                     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
258           /* cexp(inf + 0i) = inf + 0i */
259           testall(cpackl(INFINITY, 0.0), cpackl(INFINITY, 0.0),
260                     ALL_STD_EXCEPT, 0, 1);
261           testall(cpackl(INFINITY, -0.0), cpackl(INFINITY, -0.0),
262                     ALL_STD_EXCEPT, 0, 1);
263 }
264 
265 void
test_reals(void)266 test_reals(void)
267 {
268           int i;
269 
270           for (i = 0; i < N(finites); i++) {
271                     /* XXX could check exceptions more meticulously */
272                     test(cexp, cpackl(finites[i], 0.0),
273                          cpackl(exp(finites[i]), 0.0),
274                          FE_INVALID | FE_DIVBYZERO, 0, 1);
275                     test(cexp, cpackl(finites[i], -0.0),
276                          cpackl(exp(finites[i]), -0.0),
277                          FE_INVALID | FE_DIVBYZERO, 0, 1);
278                     test(cexpf, cpackl(finites[i], 0.0),
279                          cpackl(expf(finites[i]), 0.0),
280                          FE_INVALID | FE_DIVBYZERO, 0, 1);
281                     test(cexpf, cpackl(finites[i], -0.0),
282                          cpackl(expf(finites[i]), -0.0),
283                          FE_INVALID | FE_DIVBYZERO, 0, 1);
284           }
285 }
286 
287 void
test_imaginaries(void)288 test_imaginaries(void)
289 {
290           int i;
291 
292           for (i = 0; i < N(finites); i++) {
293                     test(cexp, cpackl(0.0, finites[i]),
294                          cpackl(cos(finites[i]), sin(finites[i])),
295                          ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
296                     test(cexp, cpackl(-0.0, finites[i]),
297                          cpackl(cos(finites[i]), sin(finites[i])),
298                          ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
299                     test(cexpf, cpackl(0.0, finites[i]),
300                          cpackl(cosf(finites[i]), sinf(finites[i])),
301                          ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
302                     test(cexpf, cpackl(-0.0, finites[i]),
303                          cpackl(cosf(finites[i]), sinf(finites[i])),
304                          ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
305           }
306 }
307 
308 void
test_small(void)309 test_small(void)
310 {
311           static const double tests[] = {
312                /* csqrt(a + bI) = x + yI */
313                /* a b         x                             y */
314                      1.0,     M_PI_4,   M_SQRT2 * 0.5 * M_E,          M_SQRT2 * 0.5 * M_E,
315                     -1.0,     M_PI_4,   M_SQRT2 * 0.5 / M_E,          M_SQRT2 * 0.5 / M_E,
316                      2.0,     M_PI_2,   0.0,                          M_E * M_E,
317                      M_LN2,   M_PI,     -2.0,                         0.0,
318           };
319           double a, b;
320           double x, y;
321           int i;
322 
323           for (i = 0; i < N(tests); i += 4) {
324                     a = tests[i];
325                     b = tests[i + 1];
326                     x = tests[i + 2];
327                     y = tests[i + 3];
328                     test_tol(cexp, cpackl(a, b), cpackl(x, y), 3 * DBL_ULP());
329 
330                     /* float doesn't have enough precision to pass these tests */
331                     if (x == 0 || y == 0)
332                               continue;
333                     test_tol(cexpf, cpackl(a, b), cpackl(x, y), 1 * FLT_ULP());
334         }
335 }
336 
337 /* Test inputs with a real part r that would overflow exp(r). */
338 void
test_large(void)339 test_large(void)
340 {
341 
342           test_tol(cexp, cpackl(709.79, 0x1p-1074),
343                      cpackl(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
344           test_tol(cexp, cpackl(1000, 0x1p-1074),
345                      cpackl(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
346           test_tol(cexp, cpackl(1400, 0x1p-1074),
347                      cpackl(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
348           test_tol(cexp, cpackl(900, 0x1.23456789abcdep-1020),
349                      cpackl(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
350           test_tol(cexp, cpackl(1300, 0x1.23456789abcdep-1020),
351                      cpackl(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
352 
353           test_tol(cexpf, cpackl(88.73, 0x1p-149),
354                      cpackl(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
355           test_tol(cexpf, cpackl(90, 0x1p-149),
356                      cpackl(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
357           test_tol(cexpf, cpackl(192, 0x1p-149),
358                      cpackl(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
359           test_tol(cexpf, cpackl(120, 0x1.234568p-120),
360                      cpackl(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
361           test_tol(cexpf, cpackl(170, 0x1.234568p-120),
362                      cpackl(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
363 }
364 
365 int
main(int argc,char * argv[])366 main(int argc, char *argv[])
367 {
368 
369           printf("1..7\n");
370 
371           test_zero();
372           printf("ok 1 - cexp zero\n");
373 
374           test_nan();
375           printf("ok 2 - cexp nan\n");
376 
377           test_inf();
378           printf("ok 3 - cexp inf\n");
379 
380           test_reals();
381           printf("ok 4 - cexp reals\n");
382 
383           test_imaginaries();
384           printf("ok 5 - cexp imaginaries\n");
385 
386           test_small();
387           printf("ok 6 - cexp small\n");
388 
389           test_large();
390           printf("ok 7 - cexp large\n");
391 
392           return (0);
393 }
394