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