Changeset View
Changeset View
Standalone View
Standalone View
lib/msun/tests/logarithm_test.c
Show All 26 Lines | |||||
/* | /* | ||||
* Tests for corner cases in log*(). | * Tests for corner cases in log*(). | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <assert.h> | |||||
#include <fenv.h> | #include <fenv.h> | ||||
#include <float.h> | #include <float.h> | ||||
#include <math.h> | #include <math.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#ifdef __i386__ | #ifdef __i386__ | ||||
#include <ieeefp.h> | #include <ieeefp.h> | ||||
#endif | #endif | ||||
Show All 10 Lines | |||||
* to raise FE_OVERFLOW without raising FE_INEXACT. | * to raise FE_OVERFLOW without raising FE_INEXACT. | ||||
* | * | ||||
* These are macros instead of functions so that assert provides more | * These are macros instead of functions so that assert provides more | ||||
* meaningful error messages. | * meaningful error messages. | ||||
* | * | ||||
* XXX The volatile here is to avoid gcc's bogus constant folding and work | * XXX The volatile here is to avoid gcc's bogus constant folding and work | ||||
* around the lack of support for the FENV_ACCESS pragma. | * around the lack of support for the FENV_ACCESS pragma. | ||||
*/ | */ | ||||
#define test(func, x, result, exceptmask, excepts) do { \ | #define test(func, x, result, exceptmask, excepts) do { \ | ||||
volatile long double _d = x; \ | volatile long double _d = x; \ | ||||
assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ | ATF_CHECK_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \ | ||||
assert(fpequal((func)(_d), (result))); \ | ATF_CHECK(fpequal((func)(_d), (result))); \ | ||||
assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \ | CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \ | ||||
#func, #x); \ | |||||
} while (0) | } while (0) | ||||
#define test(func, x, result, exceptmask, excepts) do { \ | |||||
volatile long double _d = x; \ | |||||
assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ | |||||
assert(fpequal((func)(_d), (result))); \ | |||||
assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \ | |||||
} while (0) | |||||
#define test_tol(func, z, result, tol) do { \ | #define test_tol(func, z, result, tol) do { \ | ||||
volatile long double _d = z; \ | volatile long double _d = z; \ | ||||
debug(" testing %6s(%15La) ~= % .36Le\n", #func, _d, result); \ | debug(" testing %6s(%15La) ~= % .36Le\n", #func, _d, result); \ | ||||
assert(fpequal_tol((func)(_d), (result), (tol), CS_BOTH)); \ | ATF_CHECK(fpequal_tol((func)(_d), (result), (tol), CS_BOTH)); \ | ||||
} while (0) | } while (0) | ||||
/* Test all the functions that compute log(x). */ | /* Test all the functions that compute log(x). */ | ||||
#define testall0(x, result, exceptmask, excepts) do { \ | #define testall0(x, result, exceptmask, excepts) do { \ | ||||
test(log, x, result, exceptmask, excepts); \ | test(log, x, result, exceptmask, excepts); \ | ||||
test(logf, x, result, exceptmask, excepts); \ | test(logf, x, result, exceptmask, excepts); \ | ||||
test(logl, x, result, exceptmask, excepts); \ | test(logl, x, result, exceptmask, excepts); \ | ||||
test(log2, x, result, exceptmask, excepts); \ | test(log2, x, result, exceptmask, excepts); \ | ||||
test(log2f, x, result, exceptmask, excepts); \ | test(log2f, x, result, exceptmask, excepts); \ | ||||
test(log2l, x, result, exceptmask, excepts); \ | test(log2l, x, result, exceptmask, excepts); \ | ||||
test(log10, x, result, exceptmask, excepts); \ | test(log10, x, result, exceptmask, excepts); \ | ||||
test(log10f, x, result, exceptmask, excepts); \ | test(log10f, x, result, exceptmask, excepts); \ | ||||
test(log10l, x, result, exceptmask, excepts); \ | test(log10l, x, result, exceptmask, excepts); \ | ||||
} while (0) | } while (0) | ||||
/* Test all the functions that compute log(1+x). */ | /* Test all the functions that compute log(1+x). */ | ||||
#define testall1(x, result, exceptmask, excepts) do { \ | #define testall1(x, result, exceptmask, excepts) do { \ | ||||
test(log1p, x, result, exceptmask, excepts); \ | test(log1p, x, result, exceptmask, excepts); \ | ||||
test(log1pf, x, result, exceptmask, excepts); \ | test(log1pf, x, result, exceptmask, excepts); \ | ||||
test(log1pl, x, result, exceptmask, excepts); \ | test(log1pl, x, result, exceptmask, excepts); \ | ||||
} while (0) | } while (0) | ||||
static void | ATF_TC_WITHOUT_HEAD(generic_tests); | ||||
run_generic_tests(void) | ATF_TC_BODY(generic_tests, tc) | ||||
{ | { | ||||
/* log(1) == 0, no exceptions raised */ | /* log(1) == 0, no exceptions raised */ | ||||
testall0(1.0, 0.0, ALL_STD_EXCEPT, 0); | testall0(1.0, 0.0, ALL_STD_EXCEPT, 0); | ||||
testall1(0.0, 0.0, ALL_STD_EXCEPT, 0); | testall1(0.0, 0.0, ALL_STD_EXCEPT, 0); | ||||
testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0); | testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0); | ||||
/* log(NaN) == NaN, no exceptions raised */ | /* log(NaN) == NaN, no exceptions raised */ | ||||
Show All 11 Lines | ATF_TC_BODY(generic_tests, tc) | ||||
testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID); | testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID); | ||||
/* log(0) == -Inf, divide-by-zero exception */ | /* log(0) == -Inf, divide-by-zero exception */ | ||||
testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | ||||
testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | ||||
testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO); | ||||
} | } | ||||
static void | ATF_TC_WITHOUT_HEAD(log2_tests); | ||||
run_log2_tests(void) | ATF_TC_BODY(log2_tests, tc) | ||||
{ | { | ||||
unsigned i; | unsigned i; | ||||
/* | /* | ||||
* We should insist that log2() return exactly the correct | * We should insist that log2() return exactly the correct | ||||
* result and not raise an inexact exception for powers of 2. | * result and not raise an inexact exception for powers of 2. | ||||
*/ | */ | ||||
assert(feclearexcept(FE_ALL_EXCEPT) == 0); | ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); | ||||
for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { | for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { | ||||
assert(log2f(ldexpf(1.0, i)) == i); | ATF_CHECK_EQ(i, log2f(ldexpf(1.0, i))); | ||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | ||||
} | } | ||||
for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { | for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { | ||||
assert(log2(ldexp(1.0, i)) == i); | ATF_CHECK_EQ(i, log2(ldexp(1.0, i))); | ||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | ||||
} | } | ||||
for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { | for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { | ||||
assert(log2l(ldexpl(1.0, i)) == i); | ATF_CHECK_EQ(i, log2l(ldexpl(1.0, i))); | ||||
#if 0 | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | ||||
/* XXX This test does not pass yet. */ | |||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | |||||
#endif | |||||
} | } | ||||
} | } | ||||
static void | ATF_TC_WITHOUT_HEAD(roundingmode_tests); | ||||
run_roundingmode_tests(void) | ATF_TC_BODY(roundingmode_tests, tc) | ||||
{ | { | ||||
/* | /* | ||||
* Corner cases in other rounding modes. | * Corner cases in other rounding modes. | ||||
*/ | */ | ||||
fesetround(FE_DOWNWARD); | fesetround(FE_DOWNWARD); | ||||
/* These are still positive per IEEE 754R */ | /* These are still positive per IEEE 754R */ | ||||
#if 0 | #if 0 | ||||
Show All 16 Lines | #endif | ||||
testall0(1.0, 0.0, ALL_STD_EXCEPT, 0); | testall0(1.0, 0.0, ALL_STD_EXCEPT, 0); | ||||
testall1(0.0, 0.0, ALL_STD_EXCEPT, 0); | testall1(0.0, 0.0, ALL_STD_EXCEPT, 0); | ||||
/* log1p(-0.0) == -0.0 even when rounding upwards */ | /* log1p(-0.0) == -0.0 even when rounding upwards */ | ||||
testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0); | testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0); | ||||
fesetround(FE_TONEAREST); | fesetround(FE_TONEAREST); | ||||
} | } | ||||
static void | ATF_TC_WITHOUT_HEAD(accuracy_tests); | ||||
run_accuracy_tests(void) | ATF_TC_BODY(accuracy_tests, tc) | ||||
{ | { | ||||
static const struct { | static const struct { | ||||
float x; | float x; | ||||
long double log2x; | long double log2x; | ||||
long double logex; | long double logex; | ||||
long double log10x; | long double log10x; | ||||
} tests[] = { | } tests[] = { | ||||
{ 0x1p-120 + 0x1p-140, | { 0x1p-120 + 0x1p-140, | ||||
Show All 36 Lines | if (tests[i].x >= 0.5) { | ||||
test_tol(log1pf, tests[i].x - 1, tests[i].logex, | test_tol(log1pf, tests[i].x - 1, tests[i].logex, | ||||
FLT_ULP()); | FLT_ULP()); | ||||
test_tol(log1pl, tests[i].x - 1, tests[i].logex, | test_tol(log1pl, tests[i].x - 1, tests[i].logex, | ||||
LDBL_ULP()); | LDBL_ULP()); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static void | ATF_TC_WITHOUT_HEAD(log1p_accuracy_tests); | ||||
run_log1p_accuracy_tests(void) | ATF_TC_BODY(log1p_accuracy_tests, tc) | ||||
{ | { | ||||
test_tol(log1pf, 0x0.333333p0F, | test_tol(log1pf, 0x0.333333p0F, | ||||
1.82321546859847114303367992804596800640e-1L, FLT_ULP()); | 1.82321546859847114303367992804596800640e-1L, FLT_ULP()); | ||||
test_tol(log1p, 0x0.3333333333333p0, | test_tol(log1p, 0x0.3333333333333p0, | ||||
1.82321556793954589204283870982629267635e-1L, DBL_ULP()); | 1.82321556793954589204283870982629267635e-1L, DBL_ULP()); | ||||
test_tol(log1pl, 0x0.33333333333333332p0L, | test_tol(log1pl, 0x0.33333333333333332p0L, | ||||
1.82321556793954626202683007050468762914e-1L, LDBL_ULP()); | 1.82321556793954626202683007050468762914e-1L, LDBL_ULP()); | ||||
test_tol(log1pf, -0x0.333333p0F, | test_tol(log1pf, -0x0.333333p0F, | ||||
-2.23143536413048672940940199918017467652e-1L, FLT_ULP()); | -2.23143536413048672940940199918017467652e-1L, FLT_ULP()); | ||||
test_tol(log1p, -0x0.3333333333333p0, | test_tol(log1p, -0x0.3333333333333p0, | ||||
-2.23143551314209700255143859052009022937e-1L, DBL_ULP()); | -2.23143551314209700255143859052009022937e-1L, DBL_ULP()); | ||||
test_tol(log1pl, -0x0.33333333333333332p0L, | test_tol(log1pl, -0x0.33333333333333332p0L, | ||||
-2.23143551314209755752742563153765697950e-1L, LDBL_ULP()); | -2.23143551314209755752742563153765697950e-1L, LDBL_ULP()); | ||||
} | } | ||||
int | ATF_TP_ADD_TCS(tp) | ||||
main(void) | |||||
{ | { | ||||
printf("1..5\n"); | ATF_TP_ADD_TC(tp, generic_tests); | ||||
ATF_TP_ADD_TC(tp, log2_tests); | |||||
ATF_TP_ADD_TC(tp, roundingmode_tests); | |||||
ATF_TP_ADD_TC(tp, accuracy_tests); | |||||
ATF_TP_ADD_TC(tp, log1p_accuracy_tests); | |||||
run_generic_tests(); | return (atf_no_error()); | ||||
printf("ok 1 - logarithm\n"); | |||||
run_log2_tests(); | |||||
printf("ok 2 - logarithm\n"); | |||||
run_roundingmode_tests(); | |||||
printf("ok 3 - logarithm\n"); | |||||
run_accuracy_tests(); | |||||
printf("ok 4 - logarithm\n"); | |||||
run_log1p_accuracy_tests(); | |||||
printf("ok 5 - logarithm\n"); | |||||
return (0); | |||||
} | } |