xref: /openbsd-src/regress/lib/libm/msun/logarithm_test.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1*498c7b5eSderaadt /*	$OpenBSD: logarithm_test.c,v 1.3 2021/12/13 18:04:28 deraadt Exp $	*/
2c36e572eSmbuhl /*-
3c36e572eSmbuhl  * Copyright (c) 2008-2010 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl  * All rights reserved.
5c36e572eSmbuhl  *
6c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8c36e572eSmbuhl  * are met:
9c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl  *
15c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl  * SUCH DAMAGE.
26c36e572eSmbuhl  */
27c36e572eSmbuhl 
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl 
30c36e572eSmbuhl /*
31c36e572eSmbuhl  * Tests for corner cases in log*().
32c36e572eSmbuhl  */
33c36e572eSmbuhl 
3449a6e16fSderaadt #include <sys/types.h>
35c36e572eSmbuhl #include <fenv.h>
36c36e572eSmbuhl #include <float.h>
37c36e572eSmbuhl #include <math.h>
38c36e572eSmbuhl #include <stdio.h>
39c36e572eSmbuhl 
40c36e572eSmbuhl #ifdef __i386__
41c36e572eSmbuhl #include <ieeefp.h>
42c36e572eSmbuhl #endif
43c36e572eSmbuhl 
44c36e572eSmbuhl #include "test-utils.h"
45c36e572eSmbuhl 
46c36e572eSmbuhl #pragma STDC FENV_ACCESS ON
47c36e572eSmbuhl 
48c36e572eSmbuhl /*
49c36e572eSmbuhl  * Test that a function returns the correct value and sets the
50c36e572eSmbuhl  * exception flags correctly. The exceptmask specifies which
51c36e572eSmbuhl  * exceptions we should check. We need to be lenient for several
52c36e572eSmbuhl  * reasoons, but mainly because on some architectures it's impossible
53c36e572eSmbuhl  * to raise FE_OVERFLOW without raising FE_INEXACT.
54c36e572eSmbuhl  *
55c36e572eSmbuhl  * These are macros instead of functions so that assert provides more
56c36e572eSmbuhl  * meaningful error messages.
57c36e572eSmbuhl  *
58c36e572eSmbuhl  * XXX The volatile here is to avoid gcc's bogus constant folding and work
59c36e572eSmbuhl  *     around the lack of support for the FENV_ACCESS pragma.
60c36e572eSmbuhl  */
61c36e572eSmbuhl #define	test(func, x, result, exceptmask, excepts)	do { \
62c36e572eSmbuhl 	volatile long double _d = x;                           \
63c36e572eSmbuhl 	ATF_CHECK_EQ(0, feclearexcept(FE_ALL_EXCEPT));			\
64c36e572eSmbuhl 	CHECK_FPEQUAL((func)(_d), (result));			\
65c36e572eSmbuhl 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
66c36e572eSmbuhl 	    #func, #x);							\
67c36e572eSmbuhl } while (0)
68c36e572eSmbuhl 
69c36e572eSmbuhl #define	test_tol(func, z, result, tol)			do {		\
70c36e572eSmbuhl 	volatile long double _d = z;					\
71c36e572eSmbuhl 	debug("  testing %6s(%15La) ~= % .36Le\n", #func, _d, result);	\
72c36e572eSmbuhl 	CHECK_FPEQUAL_TOL((func)(_d), (result), (tol), CS_BOTH);	\
73c36e572eSmbuhl } while (0)
74c36e572eSmbuhl 
75c36e572eSmbuhl /* Test all the functions that compute log(x). */
76c36e572eSmbuhl #define	testall0(x, result, exceptmask, excepts)	do {		\
77c36e572eSmbuhl 	test(log, x, result, exceptmask, excepts);			\
78c36e572eSmbuhl 	test(logf, x, result, exceptmask, excepts);			\
79c36e572eSmbuhl 	test(logl, x, result, exceptmask, excepts);			\
80c36e572eSmbuhl 	test(log2, x, result, exceptmask, excepts);			\
81c36e572eSmbuhl 	test(log2f, x, result, exceptmask, excepts);			\
82c36e572eSmbuhl 	test(log2l, x, result, exceptmask, excepts);			\
83c36e572eSmbuhl 	test(log10, x, result, exceptmask, excepts);			\
84c36e572eSmbuhl 	test(log10f, x, result, exceptmask, excepts);			\
85c36e572eSmbuhl 	test(log10l, x, result, exceptmask, excepts);			\
86c36e572eSmbuhl } while (0)
87c36e572eSmbuhl 
88c36e572eSmbuhl /* Test all the functions that compute log(1+x). */
89c36e572eSmbuhl #define	testall1(x, result, exceptmask, excepts)	do {		\
90c36e572eSmbuhl 	test(log1p, x, result, exceptmask, excepts);			\
91c36e572eSmbuhl 	test(log1pf, x, result, exceptmask, excepts);			\
92c36e572eSmbuhl 	test(log1pl, x, result, exceptmask, excepts);			\
93c36e572eSmbuhl } while (0)
94c36e572eSmbuhl 
95c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(generic_tests);
ATF_TC_BODY(generic_tests,tc)96c36e572eSmbuhl ATF_TC_BODY(generic_tests, tc)
97c36e572eSmbuhl {
98c36e572eSmbuhl 
99c36e572eSmbuhl 	/* log(1) == 0, no exceptions raised */
100c36e572eSmbuhl 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
101c36e572eSmbuhl 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
102c36e572eSmbuhl 	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
103c36e572eSmbuhl 
104c36e572eSmbuhl 	/* log(NaN) == NaN, no exceptions raised */
105c36e572eSmbuhl 	testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
106c36e572eSmbuhl 	testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
107c36e572eSmbuhl 
108c36e572eSmbuhl 	/* log(Inf) == Inf, no exceptions raised */
109c36e572eSmbuhl 	testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
110c36e572eSmbuhl 	testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
111c36e572eSmbuhl 
112c36e572eSmbuhl 	/* log(x) == NaN for x < 0, invalid exception raised */
113c36e572eSmbuhl 	testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
114c36e572eSmbuhl 	testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
115c36e572eSmbuhl 	testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
116c36e572eSmbuhl 	testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
117c36e572eSmbuhl 
118c36e572eSmbuhl 	/* log(0) == -Inf, divide-by-zero exception */
119c36e572eSmbuhl 	testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
120c36e572eSmbuhl 	testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
121c36e572eSmbuhl 	testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
122c36e572eSmbuhl }
123c36e572eSmbuhl 
124c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(log2_tests);
ATF_TC_BODY(log2_tests,tc)125c36e572eSmbuhl ATF_TC_BODY(log2_tests, tc)
126c36e572eSmbuhl {
127c36e572eSmbuhl 	unsigned i;
128c36e572eSmbuhl 
129c36e572eSmbuhl 	/*
130c36e572eSmbuhl 	 * We should insist that log2() return exactly the correct
131c36e572eSmbuhl 	 * result and not raise an inexact exception for powers of 2.
132c36e572eSmbuhl 	 */
133c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
134c36e572eSmbuhl 	for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
135c36e572eSmbuhl 		ATF_CHECK_EQ(i, log2f(ldexpf(1.0, i)));
136c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
137c36e572eSmbuhl 	}
138c36e572eSmbuhl 	for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
139c36e572eSmbuhl 		ATF_CHECK_EQ(i, log2(ldexp(1.0, i)));
140c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
141c36e572eSmbuhl 	}
142c36e572eSmbuhl 	for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
143c36e572eSmbuhl 		ATF_CHECK_EQ(i, log2l(ldexpl(1.0, i)));
144c36e572eSmbuhl 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
145c36e572eSmbuhl 	}
146c36e572eSmbuhl }
147c36e572eSmbuhl 
148c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(roundingmode_tests);
ATF_TC_BODY(roundingmode_tests,tc)149c36e572eSmbuhl ATF_TC_BODY(roundingmode_tests, tc)
150c36e572eSmbuhl {
151c36e572eSmbuhl 
152c36e572eSmbuhl 	/*
153c36e572eSmbuhl 	 * Corner cases in other rounding modes.
154c36e572eSmbuhl 	 */
155c36e572eSmbuhl 	fesetround(FE_DOWNWARD);
156c36e572eSmbuhl 	/* These are still positive per IEEE 754R */
157c36e572eSmbuhl #if 0
158c36e572eSmbuhl 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
159c36e572eSmbuhl #else
160c36e572eSmbuhl 	/* logl, log2l, and log10l don't pass yet. */
161c36e572eSmbuhl 	test(log, 1.0, 0.0, ALL_STD_EXCEPT, 0);
162c36e572eSmbuhl 	test(logf, 1.0, 0.0, ALL_STD_EXCEPT, 0);
163c36e572eSmbuhl 	test(log2, 1.0, 0.0, ALL_STD_EXCEPT, 0);
164c36e572eSmbuhl 	test(log2f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
165c36e572eSmbuhl 	test(log10, 1.0, 0.0, ALL_STD_EXCEPT, 0);
166c36e572eSmbuhl 	test(log10f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
167c36e572eSmbuhl #endif
168c36e572eSmbuhl 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
169c36e572eSmbuhl 	fesetround(FE_TOWARDZERO);
170c36e572eSmbuhl 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
171c36e572eSmbuhl 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
172c36e572eSmbuhl 
173c36e572eSmbuhl 	fesetround(FE_UPWARD);
174c36e572eSmbuhl 	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
175c36e572eSmbuhl 	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
176c36e572eSmbuhl 	/* log1p(-0.0) == -0.0 even when rounding upwards */
177c36e572eSmbuhl 	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
178c36e572eSmbuhl 
179c36e572eSmbuhl 	fesetround(FE_TONEAREST);
180c36e572eSmbuhl }
181c36e572eSmbuhl 
182c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(accuracy_tests);
ATF_TC_BODY(accuracy_tests,tc)183c36e572eSmbuhl ATF_TC_BODY(accuracy_tests, tc)
184c36e572eSmbuhl {
185c36e572eSmbuhl 	static const struct {
186c36e572eSmbuhl 		float x;
187c36e572eSmbuhl 		long double log2x;
188c36e572eSmbuhl 		long double logex;
189c36e572eSmbuhl 		long double log10x;
190c36e572eSmbuhl         } tests[] = {
191c36e572eSmbuhl 		{  0x1p-120 + 0x1p-140,
192c36e572eSmbuhl 		  -1.19999998624139449158861798943319717e2L,
193c36e572eSmbuhl 		  -8.31776607135195754708796206665656732e1L,
194c36e572eSmbuhl 		  -3.61235990655024477716980559136055915e1L,
195c36e572eSmbuhl 		},
196c36e572eSmbuhl 		{  1.0 - 0x1p-20,
197c36e572eSmbuhl 		  -1.37586186296463416424364914705656460e-6L,
198c36e572eSmbuhl 		  -9.53674771153890007250243736279163253e-7L,
199c36e572eSmbuhl 		  -4.14175690642480911859354110516159131e-7L, },
200c36e572eSmbuhl 		{  1.0 + 0x1p-20,
201c36e572eSmbuhl 		   1.37586055084113820105668028340371476e-6L,
202c36e572eSmbuhl 		   9.53673861659188233908415514963336144e-7L,
203c36e572eSmbuhl 		   4.14175295653950611453333571759200697e-7L },
204c36e572eSmbuhl 		{  19.75,
205c36e572eSmbuhl 		   4.30378074817710292442728634194115348e0L,
206c36e572eSmbuhl 		   2.98315349134713087533848129856505779e0L,
207c36e572eSmbuhl 		   1.29556709996247903756734359702926363e0L },
208c36e572eSmbuhl 		{  19.75 * 0x1p100,
209c36e572eSmbuhl 		   1.043037807481771029244272863419411534e2L,
210c36e572eSmbuhl 		   72.29787154734166181706169344438271459357255439172762452L,
211c36e572eSmbuhl 		   3.139856666636059855894123306947856631e1L },
212c36e572eSmbuhl 	};
213c36e572eSmbuhl         unsigned i;
214c36e572eSmbuhl 
215c36e572eSmbuhl 	long double log1p_ldbl_ulp = LDBL_ULP();
216c36e572eSmbuhl #if LDBL_MANT_DIG > 64
217c36e572eSmbuhl 	/*
218c36e572eSmbuhl 	 * On ld128 platforms the log1p() implementation provides less accuracy,
219c36e572eSmbuhl 	 * but does still match the ld80 precision. Use the ld80 LDBL_ULP()
220c36e572eSmbuhl 	 * value for now to avoid losing test coverage for the other functions.
221c36e572eSmbuhl 	 * Reported as https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253984
222c36e572eSmbuhl 	 */
223c36e572eSmbuhl 	log1p_ldbl_ulp = ldexpl(1.0, 1 - 64);
224c36e572eSmbuhl #endif
225c36e572eSmbuhl 
226c36e572eSmbuhl 	for (i = 0; i < nitems(tests); i++) {
227c36e572eSmbuhl 		test_tol(log2, tests[i].x, tests[i].log2x, DBL_ULP());
228c36e572eSmbuhl 		test_tol(log2f, tests[i].x, tests[i].log2x, FLT_ULP());
229c36e572eSmbuhl 		test_tol(log2l, tests[i].x, tests[i].log2x, LDBL_ULP());
230c36e572eSmbuhl 		test_tol(log, tests[i].x, tests[i].logex, DBL_ULP());
231c36e572eSmbuhl 		test_tol(logf, tests[i].x, tests[i].logex, FLT_ULP());
232c36e572eSmbuhl 		test_tol(logl, tests[i].x, tests[i].logex, LDBL_ULP());
233c36e572eSmbuhl 		test_tol(log10, tests[i].x, tests[i].log10x, DBL_ULP());
234c36e572eSmbuhl 		test_tol(log10f, tests[i].x, tests[i].log10x, FLT_ULP());
235c36e572eSmbuhl 		test_tol(log10l, tests[i].x, tests[i].log10x, LDBL_ULP());
236c36e572eSmbuhl 		if (tests[i].x >= 0.5) {
237c36e572eSmbuhl 			test_tol(log1p, tests[i].x - 1, tests[i].logex,
238c36e572eSmbuhl 				 DBL_ULP());
239c36e572eSmbuhl 			test_tol(log1pf, tests[i].x - 1, tests[i].logex,
240c36e572eSmbuhl 				 FLT_ULP());
241c36e572eSmbuhl 			test_tol(log1pl, tests[i].x - 1, tests[i].logex,
242c36e572eSmbuhl 				 log1p_ldbl_ulp);
243c36e572eSmbuhl 		}
244c36e572eSmbuhl 	}
245c36e572eSmbuhl }
246c36e572eSmbuhl 
247c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(log1p_accuracy_tests);
ATF_TC_BODY(log1p_accuracy_tests,tc)248c36e572eSmbuhl ATF_TC_BODY(log1p_accuracy_tests, tc)
249c36e572eSmbuhl {
250c36e572eSmbuhl #if LDBL_MANT_DIG > 64
251c36e572eSmbuhl 	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
252c36e572eSmbuhl 		atf_tc_expect_fail("https://bugs.freebsd.org/253984");
253c36e572eSmbuhl #endif
254c36e572eSmbuhl 
255c36e572eSmbuhl 	test_tol(log1pf, 0x0.333333p0F,
256c36e572eSmbuhl 		 1.82321546859847114303367992804596800640e-1L, FLT_ULP());
257c36e572eSmbuhl 	test_tol(log1p, 0x0.3333333333333p0,
258c36e572eSmbuhl 		 1.82321556793954589204283870982629267635e-1L, DBL_ULP());
259c36e572eSmbuhl 	test_tol(log1pl, 0x0.33333333333333332p0L,
260c36e572eSmbuhl 		 1.82321556793954626202683007050468762914e-1L, LDBL_ULP());
261c36e572eSmbuhl 
262c36e572eSmbuhl 	test_tol(log1pf, -0x0.333333p0F,
263c36e572eSmbuhl 		 -2.23143536413048672940940199918017467652e-1L, FLT_ULP());
264c36e572eSmbuhl 	test_tol(log1p, -0x0.3333333333333p0,
265c36e572eSmbuhl 		 -2.23143551314209700255143859052009022937e-1L, DBL_ULP());
266c36e572eSmbuhl 	test_tol(log1pl, -0x0.33333333333333332p0L,
267c36e572eSmbuhl 		 -2.23143551314209755752742563153765697950e-1L, LDBL_ULP());
268c36e572eSmbuhl }
269c36e572eSmbuhl 
ATF_TP_ADD_TCS(tp)270c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
271c36e572eSmbuhl {
272c36e572eSmbuhl 
273c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, generic_tests);
274c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, log2_tests);
275c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, roundingmode_tests);
276c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, accuracy_tests);
277c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, log1p_accuracy_tests);
278c36e572eSmbuhl 
279c36e572eSmbuhl 	return (atf_no_error());
280c36e572eSmbuhl }
281