1 /*-
2 * Copyright (c) 2008-2010 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 log*().
29 */
30
31 #include <sys/param.h>
32 #include <fenv.h>
33 #include <float.h>
34 #include <math.h>
35 #include <stdio.h>
36
37 #ifdef __i386__
38 #include <ieeefp.h>
39 #endif
40
41 #include "test-utils.h"
42
43 #pragma STDC FENV_ACCESS ON
44
45 /*
46 * Test that a function returns the correct value and sets the
47 * exception flags correctly. The exceptmask specifies which
48 * exceptions we should check. We need to be lenient for several
49 * reasoons, but mainly because on some architectures it's impossible
50 * to raise FE_OVERFLOW without raising FE_INEXACT.
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(func, x, result, exceptmask, excepts) do { \
59 volatile long double _d = x; \
60 ATF_CHECK_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
61 CHECK_FPEQUAL((func)(_d), (result)); \
62 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
63 #func, #x); \
64 } while (0)
65
66 #define test_tol(func, z, result, tol) do { \
67 volatile long double _d = z; \
68 debug(" testing %6s(%15La) ~= % .36Le\n", #func, _d, result); \
69 CHECK_FPEQUAL_TOL((func)(_d), (result), (tol), CS_BOTH); \
70 } while (0)
71
72 /* Test all the functions that compute log(x). */
73 #define testall0(x, result, exceptmask, excepts) do { \
74 test(log, x, result, exceptmask, excepts); \
75 test(logf, x, result, exceptmask, excepts); \
76 test(logl, x, result, exceptmask, excepts); \
77 test(log2, x, result, exceptmask, excepts); \
78 test(log2f, x, result, exceptmask, excepts); \
79 test(log2l, x, result, exceptmask, excepts); \
80 test(log10, x, result, exceptmask, excepts); \
81 test(log10f, x, result, exceptmask, excepts); \
82 test(log10l, x, result, exceptmask, excepts); \
83 } while (0)
84
85 /* Test all the functions that compute log(1+x). */
86 #define testall1(x, result, exceptmask, excepts) do { \
87 test(log1p, x, result, exceptmask, excepts); \
88 test(log1pf, x, result, exceptmask, excepts); \
89 test(log1pl, x, result, exceptmask, excepts); \
90 } while (0)
91
92 ATF_TC_WITHOUT_HEAD(generic_tests);
ATF_TC_BODY(generic_tests,tc)93 ATF_TC_BODY(generic_tests, tc)
94 {
95 #if defined(__aarch64__) || defined(__riscv)
96 atf_tc_expect_fail("https://bugs.freebsd.org/290099");
97 #endif
98 /* log(1) == 0, no exceptions raised */
99 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
100 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
101 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
102
103 /* log(NaN) == NaN, no exceptions raised */
104 testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
105 testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
106
107 /* log(Inf) == Inf, no exceptions raised */
108 testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
109 testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
110
111 /* log(x) == NaN for x < 0, invalid exception raised */
112 testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
113 testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
114 testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
115 testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
116
117 /* log(0) == -Inf, divide-by-zero exception */
118 testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
119 testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
120 testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
121 }
122
123 ATF_TC_WITHOUT_HEAD(log2_tests);
ATF_TC_BODY(log2_tests,tc)124 ATF_TC_BODY(log2_tests, tc)
125 {
126 unsigned i;
127
128 /*
129 * We should insist that log2() return exactly the correct
130 * result and not raise an inexact exception for powers of 2.
131 */
132 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
133 for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
134 ATF_CHECK_EQ(i, log2f(ldexpf(1.0, i)));
135 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
136 }
137 for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
138 ATF_CHECK_EQ(i, log2(ldexp(1.0, i)));
139 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
140 }
141 for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
142 ATF_CHECK_EQ(i, log2l(ldexpl(1.0, i)));
143 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
144 }
145 }
146
147 ATF_TC_WITHOUT_HEAD(roundingmode_tests);
ATF_TC_BODY(roundingmode_tests,tc)148 ATF_TC_BODY(roundingmode_tests, tc)
149 {
150
151 /*
152 * Corner cases in other rounding modes.
153 */
154 fesetround(FE_DOWNWARD);
155 /* These are still positive per IEEE 754R */
156 #if 0
157 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
158 #else
159 /* logl, log2l, and log10l don't pass yet. */
160 test(log, 1.0, 0.0, ALL_STD_EXCEPT, 0);
161 test(logf, 1.0, 0.0, ALL_STD_EXCEPT, 0);
162 test(log2, 1.0, 0.0, ALL_STD_EXCEPT, 0);
163 test(log2f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
164 test(log10, 1.0, 0.0, ALL_STD_EXCEPT, 0);
165 test(log10f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
166 #endif
167 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
168 fesetround(FE_TOWARDZERO);
169 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
170 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
171
172 fesetround(FE_UPWARD);
173 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
174 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
175 /* log1p(-0.0) == -0.0 even when rounding upwards */
176 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
177
178 fesetround(FE_TONEAREST);
179 }
180
181 ATF_TC_WITHOUT_HEAD(accuracy_tests);
ATF_TC_BODY(accuracy_tests,tc)182 ATF_TC_BODY(accuracy_tests, tc)
183 {
184 #if defined(__riscv)
185 atf_tc_expect_death("https://bugs.freebsd.org/290099");
186 #endif
187 static const struct {
188 float x;
189 long double log2x;
190 long double logex;
191 long double log10x;
192 } tests[] = {
193 { 0x1p-120 + 0x1p-140,
194 -1.19999998624139449158861798943319717e2L,
195 -8.31776607135195754708796206665656732e1L,
196 -3.61235990655024477716980559136055915e1L,
197 },
198 { 1.0 - 0x1p-20,
199 -1.37586186296463416424364914705656460e-6L,
200 -9.53674771153890007250243736279163253e-7L,
201 -4.14175690642480911859354110516159131e-7L, },
202 { 1.0 + 0x1p-20,
203 1.37586055084113820105668028340371476e-6L,
204 9.53673861659188233908415514963336144e-7L,
205 4.14175295653950611453333571759200697e-7L },
206 { 19.75,
207 4.30378074817710292442728634194115348e0L,
208 2.98315349134713087533848129856505779e0L,
209 1.29556709996247903756734359702926363e0L },
210 { 19.75 * 0x1p100,
211 1.043037807481771029244272863419411534e2L,
212 72.29787154734166181706169344438271459357255439172762452L,
213 3.139856666636059855894123306947856631e1L },
214 };
215 unsigned i;
216
217 long double log1p_ldbl_ulp = LDBL_ULP();
218 #if LDBL_MANT_DIG > 64
219 /*
220 * On ld128 platforms the log1p() implementation provides less accuracy,
221 * but does still match the ld80 precision. Use the ld80 LDBL_ULP()
222 * value for now to avoid losing test coverage for the other functions.
223 * Reported as https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253984
224 */
225 log1p_ldbl_ulp = ldexpl(1.0, 1 - 64);
226 #endif
227
228 for (i = 0; i < nitems(tests); i++) {
229 test_tol(log2, tests[i].x, tests[i].log2x, DBL_ULP());
230 test_tol(log2f, tests[i].x, tests[i].log2x, FLT_ULP());
231 test_tol(log2l, tests[i].x, tests[i].log2x, LDBL_ULP());
232 test_tol(log, tests[i].x, tests[i].logex, DBL_ULP());
233 test_tol(logf, tests[i].x, tests[i].logex, FLT_ULP());
234 test_tol(logl, tests[i].x, tests[i].logex, LDBL_ULP());
235 test_tol(log10, tests[i].x, tests[i].log10x, DBL_ULP());
236 test_tol(log10f, tests[i].x, tests[i].log10x, FLT_ULP());
237 test_tol(log10l, tests[i].x, tests[i].log10x, LDBL_ULP());
238 if (tests[i].x >= 0.5) {
239 test_tol(log1p, tests[i].x - 1, tests[i].logex,
240 DBL_ULP());
241 test_tol(log1pf, tests[i].x - 1, tests[i].logex,
242 FLT_ULP());
243 test_tol(log1pl, tests[i].x - 1, tests[i].logex,
244 log1p_ldbl_ulp);
245 }
246 }
247 }
248
249 ATF_TC_WITHOUT_HEAD(log1p_accuracy_tests);
ATF_TC_BODY(log1p_accuracy_tests,tc)250 ATF_TC_BODY(log1p_accuracy_tests, tc)
251 {
252 #if LDBL_MANT_DIG > 64
253 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
254 atf_tc_expect_fail("https://bugs.freebsd.org/253984");
255 #endif
256 #if defined(__riscv)
257 atf_tc_expect_death("https://bugs.freebsd.org/290099");
258 #endif
259 test_tol(log1pf, 0x0.333333p0F,
260 1.82321546859847114303367992804596800640e-1L, FLT_ULP());
261 test_tol(log1p, 0x0.3333333333333p0,
262 1.82321556793954589204283870982629267635e-1L, DBL_ULP());
263 test_tol(log1pl, 0x0.33333333333333332p0L,
264 1.82321556793954626202683007050468762914e-1L, LDBL_ULP());
265
266 test_tol(log1pf, -0x0.333333p0F,
267 -2.23143536413048672940940199918017467652e-1L, FLT_ULP());
268 test_tol(log1p, -0x0.3333333333333p0,
269 -2.23143551314209700255143859052009022937e-1L, DBL_ULP());
270 test_tol(log1pl, -0x0.33333333333333332p0L,
271 -2.23143551314209755752742563153765697950e-1L, LDBL_ULP());
272 }
273
ATF_TP_ADD_TCS(tp)274 ATF_TP_ADD_TCS(tp)
275 {
276
277 ATF_TP_ADD_TC(tp, generic_tests);
278 ATF_TP_ADD_TC(tp, log2_tests);
279 ATF_TP_ADD_TC(tp, roundingmode_tests);
280 ATF_TP_ADD_TC(tp, accuracy_tests);
281 ATF_TP_ADD_TC(tp, log1p_accuracy_tests);
282
283 return (atf_no_error());
284 }
285