]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/regression/lib/msun/test-cexp.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / regression / lib / msun / test-cexp.c
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$");
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
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
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
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
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
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
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
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
226 test_inf(void)
227 {
228         int i;
229
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);
240                 if (finites[i] == 0)
241                         continue;
242                 testall(cpackl(INFINITY, finites[i]),
243                         cpackl(INFINITY, INFINITY * finites[i]),
244                         ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
245         }
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);
250 }
251
252 void
253 test_reals(void)
254 {
255         int i;
256
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);
271         }
272 }
273
274 void
275 test_imaginaries(void)
276 {
277         int i;
278
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);
292         }
293 }
294
295 void
296 test_small(void)
297 {
298         static const double tests[] = {
299              /* csqrt(a + bI) = x + yI */
300              /* a       b       x                       y */
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,
305         };
306         double a, b;
307         double x, y;
308         int i;
309
310         for (i = 0; i < N(tests); i += 4) {
311                 a = tests[i];
312                 b = tests[i + 1];
313                 x = tests[i + 2];
314                 y = tests[i + 3];
315                 test_tol(cexp, cpackl(a, b), cpackl(x, y), 3 * DBL_ULP());
316
317                 /* float doesn't have enough precision to pass these tests */
318                 if (x == 0 || y == 0)
319                         continue;
320                 test_tol(cexpf, cpackl(a, b), cpackl(x, y), 1 * FLT_ULP());
321         }
322 }
323
324 /* Test inputs with a real part r that would overflow exp(r). */
325 void
326 test_large(void)
327 {
328
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());
339
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());
350 }
351
352 int
353 main(int argc, char *argv[])
354 {
355
356         printf("1..7\n");
357
358         test_zero();
359         printf("ok 1 - cexp zero\n");
360
361         test_nan();
362         printf("ok 2 - cexp nan\n");
363
364         test_inf();
365         printf("ok 3 - cexp inf\n");
366
367         test_reals();
368         printf("ok 4 - cexp reals\n");
369
370         test_imaginaries();
371         printf("ok 5 - cexp imaginaries\n");
372
373         test_small();
374         printf("ok 6 - cexp small\n");
375
376         test_large();
377         printf("ok 7 - cexp large\n");
378
379         return (0);
380 }