xref: /freebsd/lib/msun/tests/logarithm_test.c (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
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);
93 ATF_TC_BODY(generic_tests, tc)
94 {
95 
96 	/* log(1) == 0, no exceptions raised */
97 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
98 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
99 	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
100 
101 	/* log(NaN) == NaN, no exceptions raised */
102 	testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
103 	testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
104 
105 	/* log(Inf) == Inf, no exceptions raised */
106 	testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
107 	testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
108 
109 	/* log(x) == NaN for x < 0, invalid exception raised */
110 	testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
111 	testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
112 	testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
113 	testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
114 
115 	/* log(0) == -Inf, divide-by-zero exception */
116 	testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
117 	testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
118 	testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
119 }
120 
121 ATF_TC_WITHOUT_HEAD(log2_tests);
122 ATF_TC_BODY(log2_tests, tc)
123 {
124 	unsigned i;
125 
126 	/*
127 	 * We should insist that log2() return exactly the correct
128 	 * result and not raise an inexact exception for powers of 2.
129 	 */
130 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
131 	for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
132 		ATF_CHECK_EQ(i, log2f(ldexpf(1.0, i)));
133 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
134 	}
135 	for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
136 		ATF_CHECK_EQ(i, log2(ldexp(1.0, i)));
137 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
138 	}
139 	for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
140 		ATF_CHECK_EQ(i, log2l(ldexpl(1.0, i)));
141 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
142 	}
143 }
144 
145 ATF_TC_WITHOUT_HEAD(roundingmode_tests);
146 ATF_TC_BODY(roundingmode_tests, tc)
147 {
148 
149 	/*
150 	 * Corner cases in other rounding modes.
151 	 */
152 	fesetround(FE_DOWNWARD);
153 	/* These are still positive per IEEE 754R */
154 #if 0
155 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
156 #else
157 	/* logl, log2l, and log10l don't pass yet. */
158 	test(log, 1.0, 0.0, ALL_STD_EXCEPT, 0);
159 	test(logf, 1.0, 0.0, ALL_STD_EXCEPT, 0);
160 	test(log2, 1.0, 0.0, ALL_STD_EXCEPT, 0);
161 	test(log2f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
162 	test(log10, 1.0, 0.0, ALL_STD_EXCEPT, 0);
163 	test(log10f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
164 #endif
165 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
166 	fesetround(FE_TOWARDZERO);
167 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
168 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
169 
170 	fesetround(FE_UPWARD);
171 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
172 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
173 	/* log1p(-0.0) == -0.0 even when rounding upwards */
174 	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
175 
176 	fesetround(FE_TONEAREST);
177 }
178 
179 ATF_TC_WITHOUT_HEAD(accuracy_tests);
180 ATF_TC_BODY(accuracy_tests, tc)
181 {
182 	static const struct {
183 		float x;
184 		long double log2x;
185 		long double logex;
186 		long double log10x;
187         } tests[] = {
188 		{  0x1p-120 + 0x1p-140,
189 		  -1.19999998624139449158861798943319717e2L,
190 		  -8.31776607135195754708796206665656732e1L,
191 		  -3.61235990655024477716980559136055915e1L,
192 		},
193 		{  1.0 - 0x1p-20,
194 		  -1.37586186296463416424364914705656460e-6L,
195 		  -9.53674771153890007250243736279163253e-7L,
196 		  -4.14175690642480911859354110516159131e-7L, },
197 		{  1.0 + 0x1p-20,
198 		   1.37586055084113820105668028340371476e-6L,
199 		   9.53673861659188233908415514963336144e-7L,
200 		   4.14175295653950611453333571759200697e-7L },
201 		{  19.75,
202 		   4.30378074817710292442728634194115348e0L,
203 		   2.98315349134713087533848129856505779e0L,
204 		   1.29556709996247903756734359702926363e0L },
205 		{  19.75 * 0x1p100,
206 		   1.043037807481771029244272863419411534e2L,
207 		   72.29787154734166181706169344438271459357255439172762452L,
208 		   3.139856666636059855894123306947856631e1L },
209 	};
210         unsigned i;
211 
212 	long double log1p_ldbl_ulp = LDBL_ULP();
213 #if LDBL_MANT_DIG > 64
214 	/*
215 	 * On ld128 platforms the log1p() implementation provides less accuracy,
216 	 * but does still match the ld80 precision. Use the ld80 LDBL_ULP()
217 	 * value for now to avoid losing test coverage for the other functions.
218 	 * Reported as https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253984
219 	 */
220 	log1p_ldbl_ulp = ldexpl(1.0, 1 - 64);
221 #endif
222 
223 	for (i = 0; i < nitems(tests); i++) {
224 		test_tol(log2, tests[i].x, tests[i].log2x, DBL_ULP());
225 		test_tol(log2f, tests[i].x, tests[i].log2x, FLT_ULP());
226 		test_tol(log2l, tests[i].x, tests[i].log2x, LDBL_ULP());
227 		test_tol(log, tests[i].x, tests[i].logex, DBL_ULP());
228 		test_tol(logf, tests[i].x, tests[i].logex, FLT_ULP());
229 		test_tol(logl, tests[i].x, tests[i].logex, LDBL_ULP());
230 		test_tol(log10, tests[i].x, tests[i].log10x, DBL_ULP());
231 		test_tol(log10f, tests[i].x, tests[i].log10x, FLT_ULP());
232 		test_tol(log10l, tests[i].x, tests[i].log10x, LDBL_ULP());
233 		if (tests[i].x >= 0.5) {
234 			test_tol(log1p, tests[i].x - 1, tests[i].logex,
235 				 DBL_ULP());
236 			test_tol(log1pf, tests[i].x - 1, tests[i].logex,
237 				 FLT_ULP());
238 			test_tol(log1pl, tests[i].x - 1, tests[i].logex,
239 				 log1p_ldbl_ulp);
240 		}
241 	}
242 }
243 
244 ATF_TC_WITHOUT_HEAD(log1p_accuracy_tests);
245 ATF_TC_BODY(log1p_accuracy_tests, tc)
246 {
247 #if LDBL_MANT_DIG > 64
248 	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
249 		atf_tc_expect_fail("https://bugs.freebsd.org/253984");
250 #endif
251 
252 	test_tol(log1pf, 0x0.333333p0F,
253 		 1.82321546859847114303367992804596800640e-1L, FLT_ULP());
254 	test_tol(log1p, 0x0.3333333333333p0,
255 		 1.82321556793954589204283870982629267635e-1L, DBL_ULP());
256 	test_tol(log1pl, 0x0.33333333333333332p0L,
257 		 1.82321556793954626202683007050468762914e-1L, LDBL_ULP());
258 
259 	test_tol(log1pf, -0x0.333333p0F,
260 		 -2.23143536413048672940940199918017467652e-1L, FLT_ULP());
261 	test_tol(log1p, -0x0.3333333333333p0,
262 		 -2.23143551314209700255143859052009022937e-1L, DBL_ULP());
263 	test_tol(log1pl, -0x0.33333333333333332p0L,
264 		 -2.23143551314209755752742563153765697950e-1L, LDBL_ULP());
265 }
266 
267 ATF_TP_ADD_TCS(tp)
268 {
269 
270 	ATF_TP_ADD_TC(tp, generic_tests);
271 	ATF_TP_ADD_TC(tp, log2_tests);
272 	ATF_TP_ADD_TC(tp, roundingmode_tests);
273 	ATF_TP_ADD_TC(tp, accuracy_tests);
274 	ATF_TP_ADD_TC(tp, log1p_accuracy_tests);
275 
276 	return (atf_no_error());
277 }
278