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