xref: /openbsd-src/regress/lib/libm/msun/exponential_test.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1*498c7b5eSderaadt /*	$OpenBSD: exponential_test.c,v 1.3 2021/12/13 18:04:28 deraadt Exp $	*/
2c36e572eSmbuhl /*-
3c36e572eSmbuhl  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl  * All rights reserved.
5c36e572eSmbuhl  *
6c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8c36e572eSmbuhl  * are met:
9c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl  *
15c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl  * SUCH DAMAGE.
26c36e572eSmbuhl  */
27c36e572eSmbuhl 
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl 
30c36e572eSmbuhl /*
31c36e572eSmbuhl  * Tests for corner cases in exp*().
32c36e572eSmbuhl  */
33c36e572eSmbuhl 
34c36e572eSmbuhl #include <fenv.h>
35c36e572eSmbuhl #include <float.h>
36c36e572eSmbuhl #include <math.h>
37c36e572eSmbuhl #include <stdio.h>
38c36e572eSmbuhl 
39c36e572eSmbuhl #ifdef __i386__
40c36e572eSmbuhl #include <ieeefp.h>
41c36e572eSmbuhl #endif
42c36e572eSmbuhl 
43c36e572eSmbuhl #include "test-utils.h"
44c36e572eSmbuhl 
45c36e572eSmbuhl #pragma STDC FENV_ACCESS ON
46c36e572eSmbuhl 
47c36e572eSmbuhl /*
48c36e572eSmbuhl  * Test that a function returns the correct value and sets the
49c36e572eSmbuhl  * exception flags correctly. The exceptmask specifies which
50c36e572eSmbuhl  * exceptions we should check. We need to be lenient for several
51c36e572eSmbuhl  * reasoons, but mainly because on some architectures it's impossible
52c36e572eSmbuhl  * to raise FE_OVERFLOW without raising FE_INEXACT.
53c36e572eSmbuhl  *
54c36e572eSmbuhl  * These are macros instead of functions so that assert provides more
55c36e572eSmbuhl  * meaningful error messages.
56c36e572eSmbuhl  *
57c36e572eSmbuhl  * XXX The volatile here is to avoid gcc's bogus constant folding and work
58c36e572eSmbuhl  *     around the lack of support for the FENV_ACCESS pragma.
59c36e572eSmbuhl  */
60c36e572eSmbuhl #define	test(func, x, result, exceptmask, excepts)	do {		\
61c36e572eSmbuhl 	volatile long double _d = x;					\
62c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
63c36e572eSmbuhl 	CHECK_FPEQUAL((func)(_d), (result));			\
64c36e572eSmbuhl 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
65c36e572eSmbuhl 	    #func, #x);							\
66c36e572eSmbuhl } while (0)
67c36e572eSmbuhl 
68c36e572eSmbuhl /* Test all the functions that compute b^x. */
69c36e572eSmbuhl #define	_testall0(x, result, exceptmask, excepts)	do {		\
70c36e572eSmbuhl 	test(exp, x, result, exceptmask, excepts);			\
71c36e572eSmbuhl 	test(expf, x, result, exceptmask, excepts);			\
72c36e572eSmbuhl 	test(exp2, x, result, exceptmask, excepts);			\
73c36e572eSmbuhl 	test(exp2f, x, result, exceptmask, excepts);			\
74c36e572eSmbuhl } while (0)
75c36e572eSmbuhl 
76c36e572eSmbuhl /* Skip over exp2l on platforms that don't support it. */
77c36e572eSmbuhl #if LDBL_PREC == 53
78c36e572eSmbuhl #define	testall0	_testall0
79c36e572eSmbuhl #else
80c36e572eSmbuhl #define	testall0(x, result, exceptmask, excepts)	do {		\
81c36e572eSmbuhl 	_testall0(x, result, exceptmask, excepts); 			\
82c36e572eSmbuhl 	test(exp2l, x, result, exceptmask, excepts);			\
83c36e572eSmbuhl } while (0)
84c36e572eSmbuhl #endif
85c36e572eSmbuhl 
86c36e572eSmbuhl /* Test all the functions that compute b^x - 1. */
87c36e572eSmbuhl #define	testall1(x, result, exceptmask, excepts)	do {		\
88c36e572eSmbuhl 	test(expm1, x, result, exceptmask, excepts);			\
89c36e572eSmbuhl 	test(expm1f, x, result, exceptmask, excepts);			\
90c36e572eSmbuhl } while (0)
91c36e572eSmbuhl 
92c36e572eSmbuhl static void
run_generic_tests(void)93c36e572eSmbuhl run_generic_tests(void)
94c36e572eSmbuhl {
95c36e572eSmbuhl 
96c36e572eSmbuhl 	/* exp(0) == 1, no exceptions raised */
97c36e572eSmbuhl 	testall0(0.0, 1.0, ALL_STD_EXCEPT, 0);
98c36e572eSmbuhl 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
99c36e572eSmbuhl 	testall0(-0.0, 1.0, ALL_STD_EXCEPT, 0);
100c36e572eSmbuhl 	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
101c36e572eSmbuhl 
102c36e572eSmbuhl 	/* exp(NaN) == NaN, no exceptions raised */
103c36e572eSmbuhl 	testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
104c36e572eSmbuhl 	testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
105c36e572eSmbuhl 
106c36e572eSmbuhl 	/* exp(Inf) == Inf, no exceptions raised */
107c36e572eSmbuhl 	testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
108c36e572eSmbuhl 	testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
109c36e572eSmbuhl 
110c36e572eSmbuhl 	/* exp(-Inf) == 0, no exceptions raised */
111c36e572eSmbuhl 	testall0(-INFINITY, 0.0, ALL_STD_EXCEPT, 0);
112c36e572eSmbuhl 	testall1(-INFINITY, -1.0, ALL_STD_EXCEPT, 0);
113c36e572eSmbuhl 
114c36e572eSmbuhl #if !defined(__i386__)
115c36e572eSmbuhl 	/* exp(big) == Inf, overflow exception */
116c36e572eSmbuhl 	testall0(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW);
117c36e572eSmbuhl 	testall1(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW);
118c36e572eSmbuhl 
119c36e572eSmbuhl 	/* exp(small) == 0, underflow and inexact exceptions */
120c36e572eSmbuhl 	testall0(-50000.0, 0.0, ALL_STD_EXCEPT, FE_UNDERFLOW | FE_INEXACT);
121c36e572eSmbuhl #endif
122c36e572eSmbuhl 	testall1(-50000.0, -1.0, ALL_STD_EXCEPT, FE_INEXACT);
123c36e572eSmbuhl }
124c36e572eSmbuhl 
125c36e572eSmbuhl 
126c36e572eSmbuhl /*
127c36e572eSmbuhl  * We should insist that exp2() return exactly the correct
128c36e572eSmbuhl  * result and not raise an inexact exception for integer
129c36e572eSmbuhl  * arguments.
130c36e572eSmbuhl  */
131c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(exp2f);
ATF_TC_BODY(exp2f,tc)132c36e572eSmbuhl ATF_TC_BODY(exp2f, tc)
133c36e572eSmbuhl {
134c36e572eSmbuhl 	int i;
135c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
136c36e572eSmbuhl 	for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
137c36e572eSmbuhl 		ATF_CHECK_EQ(exp2f(i), ldexpf(1.0, i));
138c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
139c36e572eSmbuhl 	}
140c36e572eSmbuhl }
141c36e572eSmbuhl 
142c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(exp2);
ATF_TC_BODY(exp2,tc)143c36e572eSmbuhl ATF_TC_BODY(exp2, tc)
144c36e572eSmbuhl {
145a6e25c72Smbuhl 	int i;
146c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
147c36e572eSmbuhl 	for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
148c36e572eSmbuhl 		ATF_CHECK_EQ(exp2(i), ldexp(1.0, i));
149c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
150c36e572eSmbuhl 	}
151c36e572eSmbuhl }
152c36e572eSmbuhl 
153c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(exp2l);
ATF_TC_BODY(exp2l,tc)154c36e572eSmbuhl ATF_TC_BODY(exp2l, tc)
155c36e572eSmbuhl {
156c36e572eSmbuhl 	int i;
157c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
158c36e572eSmbuhl 	for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
159c36e572eSmbuhl 		ATF_CHECK_EQ(exp2l(i), ldexpl(1.0, i));
160c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
161c36e572eSmbuhl 	}
162c36e572eSmbuhl }
163c36e572eSmbuhl 
164c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(generic);
ATF_TC_BODY(generic,tc)165c36e572eSmbuhl ATF_TC_BODY(generic, tc)
166c36e572eSmbuhl {
167c36e572eSmbuhl 	run_generic_tests();
168c36e572eSmbuhl }
169c36e572eSmbuhl 
170c36e572eSmbuhl #ifndef __OpenBSD__
171c36e572eSmbuhl #ifdef __i386__
172c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(generic_fp_pe);
ATF_TC_BODY(generic_fp_pe,tc)173c36e572eSmbuhl ATF_TC_BODY(generic_fp_pe, tc)
174c36e572eSmbuhl {
175c36e572eSmbuhl 	fpsetprec(FP_PE);
176c36e572eSmbuhl 	run_generic_tests();
177c36e572eSmbuhl }
178c36e572eSmbuhl #endif
179c36e572eSmbuhl #endif
180c36e572eSmbuhl 
ATF_TP_ADD_TCS(tp)181c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
182c36e572eSmbuhl {
183c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, generic);
184c36e572eSmbuhl #ifndef __OpenBSD__
185c36e572eSmbuhl #ifdef __i386__
186c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, generic_fp_pe);
187c36e572eSmbuhl #endif
188c36e572eSmbuhl #endif
189c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, exp2);
190c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, exp2f);
191c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, exp2l);
192c36e572eSmbuhl 
193c36e572eSmbuhl 	return (atf_no_error());
194c36e572eSmbuhl }
195