-
Notifications
You must be signed in to change notification settings - Fork 0
/
cexp_test.c
315 lines (280 loc) · 10.5 KB
/
cexp_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* $OpenBSD$ */
/*-
* Copyright (c) 2008-2011 David Schultz <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "macros.h"
/*
* Tests for corner cases in cexp*().
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <complex.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include "test-utils.h"
#pragma STDC FENV_ACCESS ON
#pragma STDC CX_LIMITED_RANGE OFF
/*
* Test that a function returns the correct value and sets the
* exception flags correctly. The exceptmask specifies which
* exceptions we should check. We need to be lenient for several
* reasons, but mainly because on some architectures it's impossible
* to raise FE_OVERFLOW without raising FE_INEXACT. In some cases,
* whether cexp() raises an invalid exception is unspecified.
*
* These are macros instead of functions so that assert provides more
* meaningful error messages.
*
* XXX The volatile here is to avoid gcc's bogus constant folding and work
* around the lack of support for the FENV_ACCESS pragma.
*/
#define test_t(type, func, z, result, exceptmask, excepts, checksign) \
do { \
volatile long double complex _d = z; \
volatile type complex _r = result; \
ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
CHECK_CFPEQUAL_CS((func)(_d), (_r), (checksign)); \
CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
#func, #z); \
} while (0)
#define test(func, z, result, exceptmask, excepts, checksign) \
test_t(double, func, z, result, exceptmask, excepts, checksign)
#define test_f(func, z, result, exceptmask, excepts, checksign) \
test_t(float, func, z, result, exceptmask, excepts, checksign)
#define test_l(func, z, result, exceptmask, excepts, checksign) \
test_t(long double, func, z, result, exceptmask, excepts, \
checksign)
/* Test within a given tolerance. */
#define test_tol(func, z, result, tol) do { \
CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), \
FPE_ABS_ZERO | CS_BOTH); \
} while (0)
/* Test all the functions that compute cexp(x). */
#define testall(x, result, exceptmask, excepts, checksign) do { \
test(cexp, x, result, exceptmask, excepts, checksign); \
test_f(cexpf, x, result, exceptmask, excepts, checksign); \
test_l(cexpl, x, result, exceptmask, excepts, checksign); \
} while (0)
/*
* Test all the functions that compute cexp(x), within a given tolerance.
* The tolerance is specified in ulps.
*/
#define testall_tol(x, result, tol) do { \
test_tol(cexp, x, result, tol * DBL_ULP()); \
test_tol(cexpf, x, result, tol * FLT_ULP()); \
} while (0)
/* Various finite non-zero numbers to test. */
static const float finites[] =
{ -42.0e20, -1.0, -1.0e-10, -0.0, 0.0, 1.0e-10, 1.0, 42.0e20 };
/* Tests for 0 */
ATF_TC_WITHOUT_HEAD(zero);
ATF_TC_BODY(zero, tc)
{
/* cexp(0) = 1, no exceptions raised */
testall(0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
testall(-0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
testall(CMPLXL(0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
testall(CMPLXL(-0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
}
/*
* Tests for NaN. The signs of the results are indeterminate unless the
* imaginary part is 0.
*/
ATF_TC_WITHOUT_HEAD(nan);
ATF_TC_BODY(nan, tc)
{
unsigned i;
/* cexp(x + NaNi) = NaN + NaNi and optionally raises invalid */
/* cexp(NaN + yi) = NaN + NaNi and optionally raises invalid (|y|>0) */
for (i = 0; i < nitems(finites); i++) {
testall(CMPLXL(finites[i], NAN), CMPLXL(NAN, NAN),
ALL_STD_EXCEPT & ~FE_INVALID, 0, 0);
if (finites[i] == 0.0)
continue;
#ifndef __OpenBSD__
/* XXX FE_INEXACT shouldn't be raised here */
testall(CMPLXL(NAN, finites[i]), CMPLXL(NAN, NAN),
ALL_STD_EXCEPT & ~(FE_INVALID | FE_INEXACT), 0, 0);
#else
testall(CMPLXL(NAN, finites[i]), CMPLXL(NAN, NAN),
ALL_STD_EXCEPT & ~(FE_INVALID), 0, 0);
#endif
}
/* cexp(NaN +- 0i) = NaN +- 0i */
testall(CMPLXL(NAN, 0.0), CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 1);
testall(CMPLXL(NAN, -0.0), CMPLXL(NAN, -0.0), ALL_STD_EXCEPT, 0, 1);
/* cexp(inf + NaN i) = inf + nan i */
testall(CMPLXL(INFINITY, NAN), CMPLXL(INFINITY, NAN),
ALL_STD_EXCEPT, 0, 0);
/* cexp(-inf + NaN i) = 0 */
testall(CMPLXL(-INFINITY, NAN), CMPLXL(0.0, 0.0),
ALL_STD_EXCEPT, 0, 0);
/* cexp(NaN + NaN i) = NaN + NaN i */
testall(CMPLXL(NAN, NAN), CMPLXL(NAN, NAN),
ALL_STD_EXCEPT, 0, 0);
}
ATF_TC_WITHOUT_HEAD(inf);
ATF_TC_BODY(inf, tc)
{
unsigned i;
/* cexp(x + inf i) = NaN + NaNi and raises invalid */
for (i = 0; i < nitems(finites); i++) {
testall(CMPLXL(finites[i], INFINITY), CMPLXL(NAN, NAN),
ALL_STD_EXCEPT, FE_INVALID, 1);
}
/* cexp(-inf + yi) = 0 * (cos(y) + sin(y)i) */
/* XXX shouldn't raise an inexact exception */
testall(CMPLXL(-INFINITY, M_PI_4), CMPLXL(0.0, 0.0),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(-INFINITY, 3 * M_PI_4), CMPLXL(-0.0, 0.0),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(-INFINITY, 5 * M_PI_4), CMPLXL(-0.0, -0.0),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(-INFINITY, 7 * M_PI_4), CMPLXL(0.0, -0.0),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(-INFINITY, 0.0), CMPLXL(0.0, 0.0),
ALL_STD_EXCEPT, 0, 1);
testall(CMPLXL(-INFINITY, -0.0), CMPLXL(0.0, -0.0),
ALL_STD_EXCEPT, 0, 1);
/* cexp(inf + yi) = inf * (cos(y) + sin(y)i) (except y=0) */
/* XXX shouldn't raise an inexact exception */
testall(CMPLXL(INFINITY, M_PI_4), CMPLXL(INFINITY, INFINITY),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(INFINITY, 3 * M_PI_4), CMPLXL(-INFINITY, INFINITY),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(INFINITY, 5 * M_PI_4), CMPLXL(-INFINITY, -INFINITY),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
testall(CMPLXL(INFINITY, 7 * M_PI_4), CMPLXL(INFINITY, -INFINITY),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
/* cexp(inf + 0i) = inf + 0i */
testall(CMPLXL(INFINITY, 0.0), CMPLXL(INFINITY, 0.0),
ALL_STD_EXCEPT, 0, 1);
testall(CMPLXL(INFINITY, -0.0), CMPLXL(INFINITY, -0.0),
ALL_STD_EXCEPT, 0, 1);
}
ATF_TC_WITHOUT_HEAD(reals);
ATF_TC_BODY(reals, tc)
{
unsigned i;
for (i = 0; i < nitems(finites); i++) {
/* XXX could check exceptions more meticulously */
test(cexp, CMPLXL(finites[i], 0.0),
CMPLXL(exp(finites[i]), 0.0),
FE_INVALID | FE_DIVBYZERO, 0, 1);
test(cexp, CMPLXL(finites[i], -0.0),
CMPLXL(exp(finites[i]), -0.0),
FE_INVALID | FE_DIVBYZERO, 0, 1);
test_f(cexpf, CMPLXL(finites[i], 0.0),
CMPLXL(expf(finites[i]), 0.0),
FE_INVALID | FE_DIVBYZERO, 0, 1);
test_f(cexpf, CMPLXL(finites[i], -0.0),
CMPLXL(expf(finites[i]), -0.0),
FE_INVALID | FE_DIVBYZERO, 0, 1);
}
}
ATF_TC_WITHOUT_HEAD(imaginaries);
ATF_TC_BODY(imaginaries, tc)
{
unsigned i;
for (i = 0; i < nitems(finites); i++) {
test(cexp, CMPLXL(0.0, finites[i]),
CMPLXL(cos(finites[i]), sin(finites[i])),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
test(cexp, CMPLXL(-0.0, finites[i]),
CMPLXL(cos(finites[i]), sin(finites[i])),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
test_f(cexpf, CMPLXL(0.0, finites[i]),
CMPLXL(cosf(finites[i]), sinf(finites[i])),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
test_f(cexpf, CMPLXL(-0.0, finites[i]),
CMPLXL(cosf(finites[i]), sinf(finites[i])),
ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
}
}
ATF_TC_WITHOUT_HEAD(small);
ATF_TC_BODY(small, tc)
{
static const double tests[] = {
/* csqrt(a + bI) = x + yI */
/* a b x y */
1.0, M_PI_4, M_SQRT2 * 0.5 * M_E, M_SQRT2 * 0.5 * M_E,
-1.0, M_PI_4, M_SQRT2 * 0.5 / M_E, M_SQRT2 * 0.5 / M_E,
2.0, M_PI_2, 0.0, M_E * M_E,
M_LN2, M_PI, -2.0, 0.0,
};
double a, b;
double x, y;
unsigned i;
for (i = 0; i < nitems(tests); i += 4) {
a = tests[i];
b = tests[i + 1];
x = tests[i + 2];
y = tests[i + 3];
test_tol(cexp, CMPLXL(a, b), CMPLXL(x, y), 3 * DBL_ULP());
/* float doesn't have enough precision to pass these tests */
if (x == 0 || y == 0)
continue;
test_tol(cexpf, CMPLXL(a, b), CMPLXL(x, y), 1 * FLT_ULP());
}
}
/* Test inputs with a real part r that would overflow exp(r). */
ATF_TC_WITHOUT_HEAD(large);
ATF_TC_BODY(large, tc)
{
test_tol(cexp, CMPLXL(709.79, 0x1p-1074),
CMPLXL(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
test_tol(cexp, CMPLXL(1000, 0x1p-1074),
CMPLXL(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
test_tol(cexp, CMPLXL(1400, 0x1p-1074),
CMPLXL(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
test_tol(cexp, CMPLXL(900, 0x1.23456789abcdep-1020),
CMPLXL(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
test_tol(cexp, CMPLXL(1300, 0x1.23456789abcdep-1020),
CMPLXL(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
test_tol(cexpf, CMPLXL(88.73, 0x1p-149),
CMPLXL(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
test_tol(cexpf, CMPLXL(90, 0x1p-149),
CMPLXL(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
test_tol(cexpf, CMPLXL(192, 0x1p-149),
CMPLXL(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
test_tol(cexpf, CMPLXL(120, 0x1.234568p-120),
CMPLXL(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
test_tol(cexpf, CMPLXL(170, 0x1.234568p-120),
CMPLXL(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, zero);
ATF_TP_ADD_TC(tp, nan);
ATF_TP_ADD_TC(tp, inf);
ATF_TP_ADD_TC(tp, reals);
ATF_TP_ADD_TC(tp, imaginaries);
ATF_TP_ADD_TC(tp, small);
ATF_TP_ADD_TC(tp, large);
return (atf_no_error());
}