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