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