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