1175463Sdas/*-
2216214Sdas * Copyright (c) 2008-2010 David Schultz <das@FreeBSD.org>
3175463Sdas * All rights reserved.
4175463Sdas *
5175463Sdas * Redistribution and use in source and binary forms, with or without
6175463Sdas * modification, are permitted provided that the following conditions
7175463Sdas * are met:
8175463Sdas * 1. Redistributions of source code must retain the above copyright
9175463Sdas *    notice, this list of conditions and the following disclaimer.
10175463Sdas * 2. Redistributions in binary form must reproduce the above copyright
11175463Sdas *    notice, this list of conditions and the following disclaimer in the
12175463Sdas *    documentation and/or other materials provided with the distribution.
13175463Sdas *
14175463Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15175463Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16175463Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17175463Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18175463Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19175463Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20175463Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21175463Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22175463Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23175463Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24175463Sdas * SUCH DAMAGE.
25175463Sdas */
26175463Sdas
27175463Sdas/*
28216214Sdas * Tests for corner cases in log*().
29175463Sdas */
30175463Sdas
31175463Sdas#include <sys/cdefs.h>
32175463Sdas__FBSDID("$FreeBSD$");
33175463Sdas
34175463Sdas#include <assert.h>
35175463Sdas#include <fenv.h>
36175463Sdas#include <float.h>
37175463Sdas#include <math.h>
38175463Sdas#include <stdio.h>
39175463Sdas
40175463Sdas#ifdef __i386__
41175463Sdas#include <ieeefp.h>
42175463Sdas#endif
43175463Sdas
44251241Sdas#include "test-utils.h"
45175463Sdas
46175463Sdas#pragma STDC FENV_ACCESS ON
47175463Sdas
48175463Sdas/*
49175463Sdas * Test that a function returns the correct value and sets the
50175463Sdas * exception flags correctly. The exceptmask specifies which
51175463Sdas * exceptions we should check. We need to be lenient for several
52175463Sdas * reasoons, but mainly because on some architectures it's impossible
53175463Sdas * to raise FE_OVERFLOW without raising FE_INEXACT.
54175463Sdas *
55175463Sdas * These are macros instead of functions so that assert provides more
56175463Sdas * meaningful error messages.
57175463Sdas *
58175463Sdas * XXX The volatile here is to avoid gcc's bogus constant folding and work
59175463Sdas *     around the lack of support for the FENV_ACCESS pragma.
60175463Sdas */
61175463Sdas#define	test(func, x, result, exceptmask, excepts)	do {		\
62175463Sdas	volatile long double _d = x;					\
63175463Sdas	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
64175463Sdas	assert(fpequal((func)(_d), (result)));				 \
65251241Sdas	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
66175463Sdas} while (0)
67175463Sdas
68251293Sdas#define	test(func, x, result, exceptmask, excepts)	do {		\
69251293Sdas	volatile long double _d = x;					\
70251293Sdas	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
71251293Sdas	assert(fpequal((func)(_d), (result)));				 \
72251293Sdas	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
73251293Sdas} while (0)
74251293Sdas
75251293Sdas#define	test_tol(func, z, result, tol)			do {		\
76251293Sdas	volatile long double _d = z;					\
77251293Sdas	debug("  testing %6s(%15La) ~= % .36Le\n", #func, _d, result);	\
78251293Sdas	assert(fpequal_tol((func)(_d), (result), (tol), CS_BOTH));	\
79251293Sdas} while (0)
80251293Sdas
81216214Sdas/* Test all the functions that compute log(x). */
82175463Sdas#define	testall0(x, result, exceptmask, excepts)	do {		\
83216214Sdas	test(log, x, result, exceptmask, excepts);			\
84216214Sdas	test(logf, x, result, exceptmask, excepts);			\
85251293Sdas	test(logl, x, result, exceptmask, excepts);			\
86216214Sdas	test(log2, x, result, exceptmask, excepts);			\
87216214Sdas	test(log2f, x, result, exceptmask, excepts);			\
88251293Sdas	test(log2l, x, result, exceptmask, excepts);			\
89216214Sdas	test(log10, x, result, exceptmask, excepts);			\
90216214Sdas	test(log10f, x, result, exceptmask, excepts);			\
91251293Sdas	test(log10l, x, result, exceptmask, excepts);			\
92175463Sdas} while (0)
93175463Sdas
94216214Sdas/* Test all the functions that compute log(1+x). */
95175463Sdas#define	testall1(x, result, exceptmask, excepts)	do {		\
96216214Sdas	test(log1p, x, result, exceptmask, excepts);			\
97216214Sdas	test(log1pf, x, result, exceptmask, excepts);			\
98251293Sdas	test(log1pl, x, result, exceptmask, excepts);			\
99175463Sdas} while (0)
100175463Sdas
101175463Sdasvoid
102175463Sdasrun_generic_tests(void)
103175463Sdas{
104175463Sdas
105226378Sdas	/* log(1) == 0, no exceptions raised */
106216214Sdas	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
107175463Sdas	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
108175463Sdas	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
109175463Sdas
110216214Sdas	/* log(NaN) == NaN, no exceptions raised */
111175463Sdas	testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
112175463Sdas	testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
113175463Sdas
114216214Sdas	/* log(Inf) == Inf, no exceptions raised */
115175463Sdas	testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
116175463Sdas	testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
117175463Sdas
118216214Sdas	/* log(x) == NaN for x < 0, invalid exception raised */
119216214Sdas	testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
120216214Sdas	testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
121216214Sdas	testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
122216214Sdas	testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
123175463Sdas
124216214Sdas	/* log(0) == -Inf, divide-by-zero exception */
125216214Sdas	testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
126216214Sdas	testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
127216214Sdas	testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
128175463Sdas}
129175463Sdas
130175463Sdasvoid
131216214Sdasrun_log2_tests(void)
132175463Sdas{
133175463Sdas	int i;
134175463Sdas
135175463Sdas	/*
136216214Sdas	 * We should insist that log2() return exactly the correct
137216214Sdas	 * result and not raise an inexact exception for powers of 2.
138175463Sdas	 */
139175463Sdas	feclearexcept(FE_ALL_EXCEPT);
140175463Sdas	for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
141216214Sdas		assert(log2f(ldexpf(1.0, i)) == i);
142175463Sdas		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
143175463Sdas	}
144175463Sdas	for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
145216214Sdas		assert(log2(ldexp(1.0, i)) == i);
146175463Sdas		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
147175463Sdas	}
148251293Sdas	for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
149251293Sdas		assert(log2l(ldexpl(1.0, i)) == i);
150251293Sdas#if 0
151251293Sdas		/* XXX This test does not pass yet. */
152251293Sdas		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
153251293Sdas#endif
154251293Sdas	}
155175463Sdas}
156175463Sdas
157226378Sdasvoid
158226378Sdasrun_roundingmode_tests(void)
159226378Sdas{
160226378Sdas
161226378Sdas	/*
162226378Sdas	 * Corner cases in other rounding modes.
163226378Sdas	 */
164226378Sdas	fesetround(FE_DOWNWARD);
165226378Sdas	/* These are still positive per IEEE 754R */
166251293Sdas#if 0
167226378Sdas	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
168251293Sdas#else
169251293Sdas	/* logl, log2l, and log10l don't pass yet. */
170251293Sdas	test(log, 1.0, 0.0, ALL_STD_EXCEPT, 0);
171251293Sdas	test(logf, 1.0, 0.0, ALL_STD_EXCEPT, 0);
172251293Sdas	test(log2, 1.0, 0.0, ALL_STD_EXCEPT, 0);
173251293Sdas	test(log2f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
174251293Sdas	test(log10, 1.0, 0.0, ALL_STD_EXCEPT, 0);
175251293Sdas	test(log10f, 1.0, 0.0, ALL_STD_EXCEPT, 0);
176251293Sdas#endif
177226378Sdas	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
178226378Sdas	fesetround(FE_TOWARDZERO);
179226378Sdas	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
180226378Sdas	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
181226378Sdas
182226378Sdas	fesetround(FE_UPWARD);
183226378Sdas	testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
184226378Sdas	testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
185226378Sdas	/* log1p(-0.0) == -0.0 even when rounding upwards */
186226378Sdas	testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
187226378Sdas
188226378Sdas	fesetround(FE_TONEAREST);
189226378Sdas}
190226378Sdas
191251293Sdasvoid
192251293Sdasrun_accuracy_tests(void)
193251293Sdas{
194251293Sdas	static const struct {
195251293Sdas		float x;
196251293Sdas		long double log2x;
197251293Sdas		long double logex;
198251293Sdas		long double log10x;
199251293Sdas        } tests[] = {
200251293Sdas		{  0x1p-120 + 0x1p-140,
201251293Sdas		  -1.19999998624139449158861798943319717e2L,
202251293Sdas		  -8.31776607135195754708796206665656732e1L,
203251293Sdas		  -3.61235990655024477716980559136055915e1L,
204251293Sdas		},
205251293Sdas		{  1.0 - 0x1p-20,
206251293Sdas		  -1.37586186296463416424364914705656460e-6L,
207251293Sdas		  -9.53674771153890007250243736279163253e-7L,
208251293Sdas		  -4.14175690642480911859354110516159131e-7L, },
209251293Sdas		{  1.0 + 0x1p-20,
210251293Sdas		   1.37586055084113820105668028340371476e-6L,
211251293Sdas		   9.53673861659188233908415514963336144e-7L,
212251293Sdas		   4.14175295653950611453333571759200697e-7L },
213251293Sdas		{  19.75,
214251293Sdas		   4.30378074817710292442728634194115348e0L,
215251293Sdas		   2.98315349134713087533848129856505779e0L,
216251293Sdas		   1.29556709996247903756734359702926363e0L },
217251293Sdas		{  19.75 * 0x1p100,
218251293Sdas		   1.043037807481771029244272863419411534e2L,
219251293Sdas		   7.229787154734166181706169344438271459e1L,
220251293Sdas		   3.139856666636059855894123306947856631e1L },
221251293Sdas	};
222251293Sdas        int i;
223251293Sdas
224251293Sdas	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
225251293Sdas		test_tol(log2, tests[i].x, tests[i].log2x, DBL_ULP());
226251293Sdas		test_tol(log2f, tests[i].x, tests[i].log2x, FLT_ULP());
227251293Sdas		test_tol(log2l, tests[i].x, tests[i].log2x, LDBL_ULP());
228251293Sdas		test_tol(log, tests[i].x, tests[i].logex, DBL_ULP());
229251293Sdas		test_tol(logf, tests[i].x, tests[i].logex, FLT_ULP());
230251293Sdas		test_tol(logl, tests[i].x, tests[i].logex, LDBL_ULP());
231251293Sdas		test_tol(log10, tests[i].x, tests[i].log10x, DBL_ULP());
232251293Sdas		test_tol(log10f, tests[i].x, tests[i].log10x, FLT_ULP());
233251293Sdas		test_tol(log10l, tests[i].x, tests[i].log10x, LDBL_ULP());
234251293Sdas		if (tests[i].x >= 0.5) {
235251293Sdas			test_tol(log1p, tests[i].x - 1, tests[i].logex,
236251293Sdas				 DBL_ULP());
237251293Sdas			test_tol(log1pf, tests[i].x - 1, tests[i].logex,
238251293Sdas				 FLT_ULP());
239251293Sdas			test_tol(log1pl, tests[i].x - 1, tests[i].logex,
240251293Sdas				 LDBL_ULP());
241251293Sdas		}
242251293Sdas	}
243251293Sdas}
244251293Sdas
245251293Sdasvoid
246251293Sdasrun_log1p_accuracy_tests(void)
247251293Sdas{
248251293Sdas
249251293Sdas	test_tol(log1pf, 0x0.333333p0F,
250251293Sdas		 1.82321546859847114303367992804596800640e-1L, FLT_ULP());
251251293Sdas	test_tol(log1p, 0x0.3333333333333p0,
252251293Sdas		 1.82321556793954589204283870982629267635e-1L, DBL_ULP());
253251293Sdas	test_tol(log1pl, 0x0.33333333333333332p0L,
254251293Sdas		 1.82321556793954626202683007050468762914e-1L, LDBL_ULP());
255251293Sdas
256251293Sdas	test_tol(log1pf, -0x0.333333p0F,
257251293Sdas		 -2.23143536413048672940940199918017467652e-1L, FLT_ULP());
258251293Sdas	test_tol(log1p, -0x0.3333333333333p0,
259251293Sdas		 -2.23143551314209700255143859052009022937e-1L, DBL_ULP());
260251293Sdas	test_tol(log1pl, -0x0.33333333333333332p0L,
261251293Sdas		 -2.23143551314209755752742563153765697950e-1L, LDBL_ULP());
262251293Sdas}
263251293Sdas
264175463Sdasint
265175463Sdasmain(int argc, char *argv[])
266175463Sdas{
267175463Sdas
268251293Sdas	printf("1..5\n");
269175463Sdas
270175463Sdas	run_generic_tests();
271216214Sdas	printf("ok 1 - logarithm\n");
272175463Sdas
273216214Sdas	run_log2_tests();
274216214Sdas	printf("ok 2 - logarithm\n");
275175463Sdas
276226378Sdas	run_roundingmode_tests();
277226378Sdas	printf("ok 3 - logarithm\n");
278226378Sdas
279251293Sdas	run_accuracy_tests();
280251293Sdas	printf("ok 4 - logarithm\n");
281251293Sdas
282251293Sdas	run_log1p_accuracy_tests();
283251293Sdas	printf("ok 5 - logarithm\n");
284251293Sdas
285175463Sdas	return (0);
286175463Sdas}
287