]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/lib/msun/test-cexp.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 #include "test-utils.h"
42
43 #define N(i)    (sizeof(i) / sizeof((i)[0]))
44
45 #pragma STDC FENV_ACCESS        ON
46 #pragma STDC CX_LIMITED_RANGE   OFF
47
48 /*
49  * Test that a function returns the correct value and sets the
50  * exception flags correctly. The exceptmask specifies which
51  * exceptions we should check. We need to be lenient for several
52  * reasons, but mainly because on some architectures it's impossible
53  * to raise FE_OVERFLOW without raising FE_INEXACT. In some cases,
54  * whether cexp() raises an invalid exception is unspecified.
55  *
56  * These are macros instead of functions so that assert provides more
57  * meaningful error messages.
58  *
59  * XXX The volatile here is to avoid gcc's bogus constant folding and work
60  *     around the lack of support for the FENV_ACCESS pragma.
61  */
62 #define test(func, z, result, exceptmask, excepts, checksign)   do {    \
63         volatile long double complex _d = z;                            \
64         assert(feclearexcept(FE_ALL_EXCEPT) == 0);                      \
65         assert(cfpequal_cs((func)(_d), (result), (checksign)));         \
66         assert(((void)(func), fetestexcept(exceptmask) == (excepts)));  \
67 } while (0)
68
69 /* Test within a given tolerance. */
70 #define test_tol(func, z, result, tol)                          do {    \
71         volatile long double complex _d = z;                            \
72         assert(cfpequal_tol((func)(_d), (result), (tol),                \
73             FPE_ABS_ZERO | CS_BOTH));                                   \
74 } while (0)
75
76 /* Test all the functions that compute cexp(x). */
77 #define testall(x, result, exceptmask, excepts, checksign)      do {    \
78         test(cexp, x, result, exceptmask, excepts, checksign);          \
79         test(cexpf, x, result, exceptmask, excepts, checksign);         \
80 } while (0)
81
82 /*
83  * Test all the functions that compute cexp(x), within a given tolerance.
84  * The tolerance is specified in ulps.
85  */
86 #define testall_tol(x, result, tol)                             do {    \
87         test_tol(cexp, x, result, tol * DBL_ULP());                     \
88         test_tol(cexpf, x, result, tol * FLT_ULP());                    \
89 } while (0)
90
91 /* Various finite non-zero numbers to test. */
92 static const float finites[] =
93 { -42.0e20, -1.0, -1.0e-10, -0.0, 0.0, 1.0e-10, 1.0, 42.0e20 };
94
95
96 /* Tests for 0 */
97 void
98 test_zero(void)
99 {
100
101         /* cexp(0) = 1, no exceptions raised */
102         testall(0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
103         testall(-0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
104         testall(CMPLXL(0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
105         testall(CMPLXL(-0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
106 }
107
108 /*
109  * Tests for NaN.  The signs of the results are indeterminate unless the
110  * imaginary part is 0.
111  */
112 void
113 test_nan()
114 {
115         int i;
116
117         /* cexp(x + NaNi) = NaN + NaNi and optionally raises invalid */
118         /* cexp(NaN + yi) = NaN + NaNi and optionally raises invalid (|y|>0) */
119         for (i = 0; i < N(finites); i++) {
120                 testall(CMPLXL(finites[i], NAN), CMPLXL(NAN, NAN),
121                         ALL_STD_EXCEPT & ~FE_INVALID, 0, 0);
122                 if (finites[i] == 0.0)
123                         continue;
124                 /* XXX FE_INEXACT shouldn't be raised here */
125                 testall(CMPLXL(NAN, finites[i]), CMPLXL(NAN, NAN),
126                         ALL_STD_EXCEPT & ~(FE_INVALID | FE_INEXACT), 0, 0);
127         }
128
129         /* cexp(NaN +- 0i) = NaN +- 0i */
130         testall(CMPLXL(NAN, 0.0), CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 1);
131         testall(CMPLXL(NAN, -0.0), CMPLXL(NAN, -0.0), ALL_STD_EXCEPT, 0, 1);
132
133         /* cexp(inf + NaN i) = inf + nan i */
134         testall(CMPLXL(INFINITY, NAN), CMPLXL(INFINITY, NAN),
135                 ALL_STD_EXCEPT, 0, 0);
136         /* cexp(-inf + NaN i) = 0 */
137         testall(CMPLXL(-INFINITY, NAN), CMPLXL(0.0, 0.0),
138                 ALL_STD_EXCEPT, 0, 0);
139         /* cexp(NaN + NaN i) = NaN + NaN i */
140         testall(CMPLXL(NAN, NAN), CMPLXL(NAN, NAN),
141                 ALL_STD_EXCEPT, 0, 0);
142 }
143
144 void
145 test_inf(void)
146 {
147         int i;
148
149         /* cexp(x + inf i) = NaN + NaNi and raises invalid */
150         for (i = 0; i < N(finites); i++) {
151                 testall(CMPLXL(finites[i], INFINITY), CMPLXL(NAN, NAN),
152                         ALL_STD_EXCEPT, FE_INVALID, 1);
153         }
154         /* cexp(-inf + yi) = 0 * (cos(y) + sin(y)i) */
155         /* XXX shouldn't raise an inexact exception */
156         testall(CMPLXL(-INFINITY, M_PI_4), CMPLXL(0.0, 0.0),
157                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
158         testall(CMPLXL(-INFINITY, 3 * M_PI_4), CMPLXL(-0.0, 0.0),
159                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
160         testall(CMPLXL(-INFINITY, 5 * M_PI_4), CMPLXL(-0.0, -0.0),
161                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
162         testall(CMPLXL(-INFINITY, 7 * M_PI_4), CMPLXL(0.0, -0.0),
163                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
164         testall(CMPLXL(-INFINITY, 0.0), CMPLXL(0.0, 0.0),
165                 ALL_STD_EXCEPT, 0, 1);
166         testall(CMPLXL(-INFINITY, -0.0), CMPLXL(0.0, -0.0),
167                 ALL_STD_EXCEPT, 0, 1);
168         /* cexp(inf + yi) = inf * (cos(y) + sin(y)i) (except y=0) */
169         /* XXX shouldn't raise an inexact exception */
170         testall(CMPLXL(INFINITY, M_PI_4), CMPLXL(INFINITY, INFINITY),
171                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
172         testall(CMPLXL(INFINITY, 3 * M_PI_4), CMPLXL(-INFINITY, INFINITY),
173                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
174         testall(CMPLXL(INFINITY, 5 * M_PI_4), CMPLXL(-INFINITY, -INFINITY),
175                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
176         testall(CMPLXL(INFINITY, 7 * M_PI_4), CMPLXL(INFINITY, -INFINITY),
177                 ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
178         /* cexp(inf + 0i) = inf + 0i */
179         testall(CMPLXL(INFINITY, 0.0), CMPLXL(INFINITY, 0.0),
180                 ALL_STD_EXCEPT, 0, 1);
181         testall(CMPLXL(INFINITY, -0.0), CMPLXL(INFINITY, -0.0),
182                 ALL_STD_EXCEPT, 0, 1);
183 }
184
185 void
186 test_reals(void)
187 {
188         int i;
189
190         for (i = 0; i < N(finites); i++) {
191                 /* XXX could check exceptions more meticulously */
192                 test(cexp, CMPLXL(finites[i], 0.0),
193                      CMPLXL(exp(finites[i]), 0.0),
194                      FE_INVALID | FE_DIVBYZERO, 0, 1);
195                 test(cexp, CMPLXL(finites[i], -0.0),
196                      CMPLXL(exp(finites[i]), -0.0),
197                      FE_INVALID | FE_DIVBYZERO, 0, 1);
198                 test(cexpf, CMPLXL(finites[i], 0.0),
199                      CMPLXL(expf(finites[i]), 0.0),
200                      FE_INVALID | FE_DIVBYZERO, 0, 1);
201                 test(cexpf, CMPLXL(finites[i], -0.0),
202                      CMPLXL(expf(finites[i]), -0.0),
203                      FE_INVALID | FE_DIVBYZERO, 0, 1);
204         }
205 }
206
207 void
208 test_imaginaries(void)
209 {
210         int i;
211
212         for (i = 0; i < N(finites); i++) {
213                 test(cexp, CMPLXL(0.0, finites[i]),
214                      CMPLXL(cos(finites[i]), sin(finites[i])),
215                      ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
216                 test(cexp, CMPLXL(-0.0, finites[i]),
217                      CMPLXL(cos(finites[i]), sin(finites[i])),
218                      ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
219                 test(cexpf, CMPLXL(0.0, finites[i]),
220                      CMPLXL(cosf(finites[i]), sinf(finites[i])),
221                      ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
222                 test(cexpf, CMPLXL(-0.0, finites[i]),
223                      CMPLXL(cosf(finites[i]), sinf(finites[i])),
224                      ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
225         }
226 }
227
228 void
229 test_small(void)
230 {
231         static const double tests[] = {
232              /* csqrt(a + bI) = x + yI */
233              /* a       b       x                       y */
234                  1.0,   M_PI_4, M_SQRT2 * 0.5 * M_E,    M_SQRT2 * 0.5 * M_E,
235                 -1.0,   M_PI_4, M_SQRT2 * 0.5 / M_E,    M_SQRT2 * 0.5 / M_E,
236                  2.0,   M_PI_2, 0.0,                    M_E * M_E,
237                  M_LN2, M_PI,   -2.0,                   0.0,
238         };
239         double a, b;
240         double x, y;
241         int i;
242
243         for (i = 0; i < N(tests); i += 4) {
244                 a = tests[i];
245                 b = tests[i + 1];
246                 x = tests[i + 2];
247                 y = tests[i + 3];
248                 test_tol(cexp, CMPLXL(a, b), CMPLXL(x, y), 3 * DBL_ULP());
249
250                 /* float doesn't have enough precision to pass these tests */
251                 if (x == 0 || y == 0)
252                         continue;
253                 test_tol(cexpf, CMPLXL(a, b), CMPLXL(x, y), 1 * FLT_ULP());
254         }
255 }
256
257 /* Test inputs with a real part r that would overflow exp(r). */
258 void
259 test_large(void)
260 {
261
262         test_tol(cexp, CMPLXL(709.79, 0x1p-1074),
263                  CMPLXL(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
264         test_tol(cexp, CMPLXL(1000, 0x1p-1074),
265                  CMPLXL(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
266         test_tol(cexp, CMPLXL(1400, 0x1p-1074),
267                  CMPLXL(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
268         test_tol(cexp, CMPLXL(900, 0x1.23456789abcdep-1020),
269                  CMPLXL(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
270         test_tol(cexp, CMPLXL(1300, 0x1.23456789abcdep-1020),
271                  CMPLXL(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
272
273         test_tol(cexpf, CMPLXL(88.73, 0x1p-149),
274                  CMPLXL(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
275         test_tol(cexpf, CMPLXL(90, 0x1p-149),
276                  CMPLXL(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
277         test_tol(cexpf, CMPLXL(192, 0x1p-149),
278                  CMPLXL(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
279         test_tol(cexpf, CMPLXL(120, 0x1.234568p-120),
280                  CMPLXL(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
281         test_tol(cexpf, CMPLXL(170, 0x1.234568p-120),
282                  CMPLXL(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
283 }
284
285 int
286 main(int argc, char *argv[])
287 {
288
289         printf("1..7\n");
290
291         test_zero();
292         printf("ok 1 - cexp zero\n");
293
294         test_nan();
295         printf("ok 2 - cexp nan\n");
296
297         test_inf();
298         printf("ok 3 - cexp inf\n");
299
300         test_reals();
301         printf("ok 4 - cexp reals\n");
302
303         test_imaginaries();
304         printf("ok 5 - cexp imaginaries\n");
305
306         test_small();
307         printf("ok 6 - cexp small\n");
308
309         test_large();
310         printf("ok 7 - cexp large\n");
311
312         return (0);
313 }