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