xref: /freebsd/lib/msun/tests/cexp_test.c (revision 2ed3236082a4473c1da8f72c1ebc071a7b54321f)
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 <sys/param.h>
35 
36 #include <assert.h>
37 #include <complex.h>
38 #include <fenv.h>
39 #include <float.h>
40 #include <math.h>
41 #include <stdio.h>
42 
43 #include "test-utils.h"
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_t(type, func, z, result, exceptmask, excepts, checksign)	\
63 do {									\
64 	volatile long double complex _d = z;				\
65 	volatile type complex _r = result;				\
66 	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
67 	assert(cfpequal_cs((func)(_d), (_r), (checksign)));		\
68 	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
69 } while (0)
70 
71 #define	test(func, z, result, exceptmask, excepts, checksign)		\
72 	test_t(double, func, z, result, exceptmask, excepts, checksign)
73 
74 #define	test_f(func, z, result, exceptmask, excepts, checksign)		\
75 	test_t(float, func, z, result, exceptmask, excepts, checksign)
76 
77 /* Test within a given tolerance. */
78 #define	test_tol(func, z, result, tol)				do {	\
79 	volatile long double complex _d = z;				\
80 	assert(cfpequal_tol((func)(_d), (result), (tol),		\
81 	    FPE_ABS_ZERO | CS_BOTH));					\
82 } while (0)
83 
84 /* Test all the functions that compute cexp(x). */
85 #define	testall(x, result, exceptmask, excepts, checksign)	do {	\
86 	test(cexp, x, result, exceptmask, excepts, checksign);		\
87 	test_f(cexpf, x, result, exceptmask, excepts, checksign);	\
88 } while (0)
89 
90 /*
91  * Test all the functions that compute cexp(x), within a given tolerance.
92  * The tolerance is specified in ulps.
93  */
94 #define	testall_tol(x, result, tol)				do {	\
95 	test_tol(cexp, x, result, tol * DBL_ULP());			\
96 	test_tol(cexpf, x, result, tol * FLT_ULP());			\
97 } while (0)
98 
99 /* Various finite non-zero numbers to test. */
100 static const float finites[] =
101 { -42.0e20, -1.0, -1.0e-10, -0.0, 0.0, 1.0e-10, 1.0, 42.0e20 };
102 
103 
104 /* Tests for 0 */
105 static void
106 test_zero(void)
107 {
108 
109 	/* cexp(0) = 1, no exceptions raised */
110 	testall(0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
111 	testall(-0.0, 1.0, ALL_STD_EXCEPT, 0, 1);
112 	testall(CMPLXL(0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
113 	testall(CMPLXL(-0.0, -0.0), CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, 1);
114 }
115 
116 /*
117  * Tests for NaN.  The signs of the results are indeterminate unless the
118  * imaginary part is 0.
119  */
120 static void
121 test_nan(void)
122 {
123 	unsigned i;
124 
125 	/* cexp(x + NaNi) = NaN + NaNi and optionally raises invalid */
126 	/* cexp(NaN + yi) = NaN + NaNi and optionally raises invalid (|y|>0) */
127 	for (i = 0; i < nitems(finites); i++) {
128 		printf("# Run %d..\n", i);
129 		testall(CMPLXL(finites[i], NAN), CMPLXL(NAN, NAN),
130 			ALL_STD_EXCEPT & ~FE_INVALID, 0, 0);
131 		if (finites[i] == 0.0)
132 			continue;
133 		/* XXX FE_INEXACT shouldn't be raised here */
134 		testall(CMPLXL(NAN, finites[i]), CMPLXL(NAN, NAN),
135 			ALL_STD_EXCEPT & ~(FE_INVALID | FE_INEXACT), 0, 0);
136 	}
137 
138 	/* cexp(NaN +- 0i) = NaN +- 0i */
139 	testall(CMPLXL(NAN, 0.0), CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 1);
140 	testall(CMPLXL(NAN, -0.0), CMPLXL(NAN, -0.0), ALL_STD_EXCEPT, 0, 1);
141 
142 	/* cexp(inf + NaN i) = inf + nan i */
143 	testall(CMPLXL(INFINITY, NAN), CMPLXL(INFINITY, NAN),
144 		ALL_STD_EXCEPT, 0, 0);
145 	/* cexp(-inf + NaN i) = 0 */
146 	testall(CMPLXL(-INFINITY, NAN), CMPLXL(0.0, 0.0),
147 		ALL_STD_EXCEPT, 0, 0);
148 	/* cexp(NaN + NaN i) = NaN + NaN i */
149 	testall(CMPLXL(NAN, NAN), CMPLXL(NAN, NAN),
150 		ALL_STD_EXCEPT, 0, 0);
151 }
152 
153 static void
154 test_inf(void)
155 {
156 	unsigned i;
157 
158 	/* cexp(x + inf i) = NaN + NaNi and raises invalid */
159 	for (i = 0; i < nitems(finites); i++) {
160 		printf("# Run %d..\n", i);
161 		testall(CMPLXL(finites[i], INFINITY), CMPLXL(NAN, NAN),
162 			ALL_STD_EXCEPT, FE_INVALID, 1);
163 	}
164 	/* cexp(-inf + yi) = 0 * (cos(y) + sin(y)i) */
165 	/* XXX shouldn't raise an inexact exception */
166 	testall(CMPLXL(-INFINITY, M_PI_4), CMPLXL(0.0, 0.0),
167 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
168 	testall(CMPLXL(-INFINITY, 3 * M_PI_4), CMPLXL(-0.0, 0.0),
169 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
170 	testall(CMPLXL(-INFINITY, 5 * M_PI_4), CMPLXL(-0.0, -0.0),
171 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
172 	testall(CMPLXL(-INFINITY, 7 * M_PI_4), CMPLXL(0.0, -0.0),
173 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
174 	testall(CMPLXL(-INFINITY, 0.0), CMPLXL(0.0, 0.0),
175 		ALL_STD_EXCEPT, 0, 1);
176 	testall(CMPLXL(-INFINITY, -0.0), CMPLXL(0.0, -0.0),
177 		ALL_STD_EXCEPT, 0, 1);
178 	/* cexp(inf + yi) = inf * (cos(y) + sin(y)i) (except y=0) */
179 	/* XXX shouldn't raise an inexact exception */
180 	testall(CMPLXL(INFINITY, M_PI_4), CMPLXL(INFINITY, INFINITY),
181 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
182 	testall(CMPLXL(INFINITY, 3 * M_PI_4), CMPLXL(-INFINITY, INFINITY),
183 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
184 	testall(CMPLXL(INFINITY, 5 * M_PI_4), CMPLXL(-INFINITY, -INFINITY),
185 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
186 	testall(CMPLXL(INFINITY, 7 * M_PI_4), CMPLXL(INFINITY, -INFINITY),
187 		ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
188 	/* cexp(inf + 0i) = inf + 0i */
189 	testall(CMPLXL(INFINITY, 0.0), CMPLXL(INFINITY, 0.0),
190 		ALL_STD_EXCEPT, 0, 1);
191 	testall(CMPLXL(INFINITY, -0.0), CMPLXL(INFINITY, -0.0),
192 		ALL_STD_EXCEPT, 0, 1);
193 }
194 
195 static void
196 test_reals(void)
197 {
198 	unsigned i;
199 
200 	for (i = 0; i < nitems(finites); i++) {
201 		/* XXX could check exceptions more meticulously */
202 		printf("# Run %d..\n", i);
203 		test(cexp, CMPLXL(finites[i], 0.0),
204 		     CMPLXL(exp(finites[i]), 0.0),
205 		     FE_INVALID | FE_DIVBYZERO, 0, 1);
206 		test(cexp, CMPLXL(finites[i], -0.0),
207 		     CMPLXL(exp(finites[i]), -0.0),
208 		     FE_INVALID | FE_DIVBYZERO, 0, 1);
209 		test_f(cexpf, CMPLXL(finites[i], 0.0),
210 		     CMPLXL(expf(finites[i]), 0.0),
211 		     FE_INVALID | FE_DIVBYZERO, 0, 1);
212 		test_f(cexpf, CMPLXL(finites[i], -0.0),
213 		     CMPLXL(expf(finites[i]), -0.0),
214 		     FE_INVALID | FE_DIVBYZERO, 0, 1);
215 	}
216 }
217 
218 static void
219 test_imaginaries(void)
220 {
221 	unsigned i;
222 
223 	for (i = 0; i < nitems(finites); i++) {
224 		printf("# Run %d..\n", i);
225 		test(cexp, CMPLXL(0.0, finites[i]),
226 		     CMPLXL(cos(finites[i]), sin(finites[i])),
227 		     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
228 		test(cexp, CMPLXL(-0.0, finites[i]),
229 		     CMPLXL(cos(finites[i]), sin(finites[i])),
230 		     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
231 		test_f(cexpf, CMPLXL(0.0, finites[i]),
232 		     CMPLXL(cosf(finites[i]), sinf(finites[i])),
233 		     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
234 		test_f(cexpf, CMPLXL(-0.0, finites[i]),
235 		     CMPLXL(cosf(finites[i]), sinf(finites[i])),
236 		     ALL_STD_EXCEPT & ~FE_INEXACT, 0, 1);
237 	}
238 }
239 
240 static void
241 test_small(void)
242 {
243 	static const double tests[] = {
244 	     /* csqrt(a + bI) = x + yI */
245 	     /* a	b	x			y */
246 		 1.0,	M_PI_4,	M_SQRT2 * 0.5 * M_E,	M_SQRT2 * 0.5 * M_E,
247 		-1.0,	M_PI_4,	M_SQRT2 * 0.5 / M_E,	M_SQRT2 * 0.5 / M_E,
248 		 2.0,	M_PI_2,	0.0,			M_E * M_E,
249 		 M_LN2,	M_PI,	-2.0,			0.0,
250 	};
251 	double a, b;
252 	double x, y;
253 	unsigned i;
254 
255 	for (i = 0; i < nitems(tests); i += 4) {
256 		printf("# Run %d..\n", i);
257 		a = tests[i];
258 		b = tests[i + 1];
259 		x = tests[i + 2];
260 		y = tests[i + 3];
261 		test_tol(cexp, CMPLXL(a, b), CMPLXL(x, y), 3 * DBL_ULP());
262 
263 		/* float doesn't have enough precision to pass these tests */
264 		if (x == 0 || y == 0)
265 			continue;
266 		test_tol(cexpf, CMPLXL(a, b), CMPLXL(x, y), 1 * FLT_ULP());
267         }
268 }
269 
270 /* Test inputs with a real part r that would overflow exp(r). */
271 static void
272 test_large(void)
273 {
274 
275 	test_tol(cexp, CMPLXL(709.79, 0x1p-1074),
276 		 CMPLXL(INFINITY, 8.94674309915433533273e-16), DBL_ULP());
277 	test_tol(cexp, CMPLXL(1000, 0x1p-1074),
278 		 CMPLXL(INFINITY, 9.73344457300016401328e+110), DBL_ULP());
279 	test_tol(cexp, CMPLXL(1400, 0x1p-1074),
280 		 CMPLXL(INFINITY, 5.08228858149196559681e+284), DBL_ULP());
281 	test_tol(cexp, CMPLXL(900, 0x1.23456789abcdep-1020),
282 		 CMPLXL(INFINITY, 7.42156649354218408074e+83), DBL_ULP());
283 	test_tol(cexp, CMPLXL(1300, 0x1.23456789abcdep-1020),
284 		 CMPLXL(INFINITY, 3.87514844965996756704e+257), DBL_ULP());
285 
286 	test_tol(cexpf, CMPLXL(88.73, 0x1p-149),
287 		 CMPLXL(INFINITY, 4.80265603e-07), 2 * FLT_ULP());
288 	test_tol(cexpf, CMPLXL(90, 0x1p-149),
289 		 CMPLXL(INFINITY, 1.7101492622e-06f), 2 * FLT_ULP());
290 	test_tol(cexpf, CMPLXL(192, 0x1p-149),
291 		 CMPLXL(INFINITY, 3.396809344e+38f), 2 * FLT_ULP());
292 	test_tol(cexpf, CMPLXL(120, 0x1.234568p-120),
293 		 CMPLXL(INFINITY, 1.1163382522e+16f), 2 * FLT_ULP());
294 	test_tol(cexpf, CMPLXL(170, 0x1.234568p-120),
295 		 CMPLXL(INFINITY, 5.7878851079e+37f), 2 * FLT_ULP());
296 }
297 
298 int
299 main(void)
300 {
301 
302 	printf("1..7\n");
303 
304 	test_zero();
305 	printf("ok 1 - cexp zero\n");
306 
307 	test_nan();
308 	printf("ok 2 - cexp nan\n");
309 
310 	test_inf();
311 	printf("ok 3 - cexp inf\n");
312 
313 	test_reals();
314 	printf("ok 4 - cexp reals\n");
315 
316 	test_imaginaries();
317 	printf("ok 5 - cexp imaginaries\n");
318 
319 	test_small();
320 	printf("ok 6 - cexp small\n");
321 
322 	test_large();
323 	printf("ok 7 - cexp large\n");
324 
325 	return (0);
326 }
327