logarithm_test.c revision 293267
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: stable/10/lib/msun/tests/logarithm_test.c 293267 2016-01-06 20:21:40Z ngie $");
33
34#include <assert.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	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
64	assert(fpequal((func)(_d), (result)));				 \
65	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
66} while (0)
67
68#define	test(func, x, result, exceptmask, excepts)	do {		\
69	volatile long double _d = x;					\
70	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
71	assert(fpequal((func)(_d), (result)));				 \
72	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
73} while (0)
74
75#define	test_tol(func, z, result, tol)			do {		\
76	volatile long double _d = z;					\
77	debug("  testing %6s(%15La) ~= % .36Le\n", #func, _d, result);	\
78	assert(fpequal_tol((func)(_d), (result), (tol), CS_BOTH));	\
79} while (0)
80
81/* Test all the functions that compute log(x). */
82#define	testall0(x, result, exceptmask, excepts)	do {		\
83	test(log, x, result, exceptmask, excepts);			\
84	test(logf, x, result, exceptmask, excepts);			\
85	test(logl, x, result, exceptmask, excepts);			\
86	test(log2, x, result, exceptmask, excepts);			\
87	test(log2f, x, result, exceptmask, excepts);			\
88	test(log2l, x, result, exceptmask, excepts);			\
89	test(log10, x, result, exceptmask, excepts);			\
90	test(log10f, x, result, exceptmask, excepts);			\
91	test(log10l, x, result, exceptmask, excepts);			\
92} while (0)
93
94/* Test all the functions that compute log(1+x). */
95#define	testall1(x, result, exceptmask, excepts)	do {		\
96	test(log1p, x, result, exceptmask, excepts);			\
97	test(log1pf, x, result, exceptmask, excepts);			\
98	test(log1pl, x, result, exceptmask, excepts);			\
99} while (0)
100
101void
102run_generic_tests(void)
103{
104
105	/* log(1) == 0, no exceptions raised */
106	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
107	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
108	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
109
110	/* log(NaN) == NaN, no exceptions raised */
111	testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
112	testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
113
114	/* log(Inf) == Inf, no exceptions raised */
115	testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
116	testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
117
118	/* log(x) == NaN for x < 0, invalid exception raised */
119	testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
120	testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
121	testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
122	testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
123
124	/* log(0) == -Inf, divide-by-zero exception */
125	testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
126	testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
127	testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
128}
129
130void
131run_log2_tests(void)
132{
133	int i;
134
135	/*
136	 * We should insist that log2() return exactly the correct
137	 * result and not raise an inexact exception for powers of 2.
138	 */
139	feclearexcept(FE_ALL_EXCEPT);
140	for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
141		assert(log2f(ldexpf(1.0, i)) == i);
142		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
143	}
144	for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
145		assert(log2(ldexp(1.0, i)) == i);
146		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
147	}
148	for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
149		assert(log2l(ldexpl(1.0, i)) == i);
150#if 0
151		/* XXX This test does not pass yet. */
152		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
153#endif
154	}
155}
156
157void
158run_roundingmode_tests(void)
159{
160
161	/*
162	 * Corner cases in other rounding modes.
163	 */
164	fesetround(FE_DOWNWARD);
165	/* These are still positive per IEEE 754R */
166#if 0
167	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
168#else
169	/* logl, log2l, and log10l don't pass yet. */
170	test(log, 1.0, 0.0, ALL_STD_EXCEPT, 0);
171	test(logf, 1.0, 0.0, ALL_STD_EXCEPT, 0);
172	test(log2, 1.0, 0.0, ALL_STD_EXCEPT, 0);
173	test(log2f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
174	test(log10, 1.0, 0.0, ALL_STD_EXCEPT, 0);
175	test(log10f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
176#endif
177	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
178	fesetround(FE_TOWARDZERO);
179	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
180	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
181
182	fesetround(FE_UPWARD);
183	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
184	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
185	/* log1p(-0.0) == -0.0 even when rounding upwards */
186	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
187
188	fesetround(FE_TONEAREST);
189}
190
191void
192run_accuracy_tests(void)
193{
194	static const struct {
195		float x;
196		long double log2x;
197		long double logex;
198		long double log10x;
199        } tests[] = {
200		{  0x1p-120 + 0x1p-140,
201		  -1.19999998624139449158861798943319717e2L,
202		  -8.31776607135195754708796206665656732e1L,
203		  -3.61235990655024477716980559136055915e1L,
204		},
205		{  1.0 - 0x1p-20,
206		  -1.37586186296463416424364914705656460e-6L,
207		  -9.53674771153890007250243736279163253e-7L,
208		  -4.14175690642480911859354110516159131e-7L, },
209		{  1.0 + 0x1p-20,
210		   1.37586055084113820105668028340371476e-6L,
211		   9.53673861659188233908415514963336144e-7L,
212		   4.14175295653950611453333571759200697e-7L },
213		{  19.75,
214		   4.30378074817710292442728634194115348e0L,
215		   2.98315349134713087533848129856505779e0L,
216		   1.29556709996247903756734359702926363e0L },
217		{  19.75 * 0x1p100,
218		   1.043037807481771029244272863419411534e2L,
219		   7.229787154734166181706169344438271459e1L,
220		   3.139856666636059855894123306947856631e1L },
221	};
222        int i;
223
224	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); 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				 LDBL_ULP());
241		}
242	}
243}
244
245void
246run_log1p_accuracy_tests(void)
247{
248
249	test_tol(log1pf, 0x0.333333p0F,
250		 1.82321546859847114303367992804596800640e-1L, FLT_ULP());
251	test_tol(log1p, 0x0.3333333333333p0,
252		 1.82321556793954589204283870982629267635e-1L, DBL_ULP());
253	test_tol(log1pl, 0x0.33333333333333332p0L,
254		 1.82321556793954626202683007050468762914e-1L, LDBL_ULP());
255
256	test_tol(log1pf, -0x0.333333p0F,
257		 -2.23143536413048672940940199918017467652e-1L, FLT_ULP());
258	test_tol(log1p, -0x0.3333333333333p0,
259		 -2.23143551314209700255143859052009022937e-1L, DBL_ULP());
260	test_tol(log1pl, -0x0.33333333333333332p0L,
261		 -2.23143551314209755752742563153765697950e-1L, LDBL_ULP());
262}
263
264int
265main(int argc, char *argv[])
266{
267
268	printf("1..5\n");
269
270	run_generic_tests();
271	printf("ok 1 - logarithm\n");
272
273	run_log2_tests();
274	printf("ok 2 - logarithm\n");
275
276	run_roundingmode_tests();
277	printf("ok 3 - logarithm\n");
278
279	run_accuracy_tests();
280	printf("ok 4 - logarithm\n");
281
282	run_log1p_accuracy_tests();
283	printf("ok 5 - logarithm\n");
284
285	return (0);
286}
287