Changeset View
Changeset View
Standalone View
Standalone View
lib/msun/tests/rem_test.c
Show All 27 Lines | |||||
* Test for remainder functions: remainder, remainderf, remainderl, | * Test for remainder functions: remainder, remainderf, remainderl, | ||||
* remquo, remquof, and remquol. | * remquo, remquof, and remquol. | ||||
* Missing tests: fmod, fmodf. | * Missing tests: fmod, fmodf. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <assert.h> | |||||
#include <float.h> | #include <float.h> | ||||
#include <math.h> | #include <math.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <strings.h> | #include <strings.h> | ||||
#include "test-utils.h" | |||||
static void test_invalid(long double, long double); | static void test_invalid(long double, long double); | ||||
static void testl(long double, long double, long double, int); | static void testl(long double, long double, long double, int); | ||||
static void testd(double, double, double, int); | static void testd(double, double, double, int); | ||||
static void testf(float, float, float, int); | static void testf(float, float, float, int); | ||||
#define test(x, y, e_r, e_q) do { \ | #define test(x, y, e_r, e_q) do { \ | ||||
testl(x, y, e_r, e_q); \ | testl(x, y, e_r, e_q); \ | ||||
testd(x, y, e_r, e_q); \ | testd(x, y, e_r, e_q); \ | ||||
testf(x, y, e_r, e_q); \ | testf(x, y, e_r, e_q); \ | ||||
} while (0) | } while (0) | ||||
int | ATF_TC_WITHOUT_HEAD(rem1); | ||||
main(void) | ATF_TC_BODY(rem1, tc) | ||||
{ | { | ||||
printf("1..3\n"); | |||||
test_invalid(0.0, 0.0); | test_invalid(0.0, 0.0); | ||||
test_invalid(1.0, 0.0); | test_invalid(1.0, 0.0); | ||||
test_invalid(INFINITY, 0.0); | test_invalid(INFINITY, 0.0); | ||||
test_invalid(INFINITY, 1.0); | test_invalid(INFINITY, 1.0); | ||||
test_invalid(-INFINITY, 1.0); | test_invalid(-INFINITY, 1.0); | ||||
test_invalid(NAN, 1.0); | test_invalid(NAN, 1.0); | ||||
test_invalid(1.0, NAN); | test_invalid(1.0, NAN); | ||||
test(4, 4, 0, 1); | test(4, 4, 0, 1); | ||||
test(0, 3.0, 0, 0); | test(0, 3.0, 0, 0); | ||||
testd(0x1p-1074, 1, 0x1p-1074, 0); | testd(0x1p-1074, 1, 0x1p-1074, 0); | ||||
testf(0x1p-149, 1, 0x1p-149, 0); | testf(0x1p-149, 1, 0x1p-149, 0); | ||||
test(3.0, 4, -1, 1); | test(3.0, 4, -1, 1); | ||||
test(3.0, -4, -1, -1); | test(3.0, -4, -1, -1); | ||||
testd(275 * 1193040, 275, 0, 1193040); | testd(275 * 1193040, 275, 0, 1193040); | ||||
test(4.5 * 7.5, 4.5, -2.25, 8); /* we should get the even one */ | test(4.5 * 7.5, 4.5, -2.25, 8); /* we should get the even one */ | ||||
testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1); | testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1); | ||||
#if LDBL_MANT_DIG > 53 | #if LDBL_MANT_DIG > 53 | ||||
testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L, | testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L, | ||||
0x1.b72ea61d950c862p-2001L, -1); | 0x1.b72ea61d950c862p-2001L, -1); | ||||
#endif | #endif | ||||
} | |||||
printf("ok 1 - rem\n"); | ATF_TC_WITHOUT_HEAD(rem2); | ||||
ATF_TC_BODY(rem2, tc) | |||||
{ | |||||
/* | /* | ||||
* The actual quotient here is 864062210.50000003..., but | * The actual quotient here is 864062210.50000003..., but | ||||
* double-precision division gets -8.64062210.5, which rounds | * double-precision division gets -8.64062210.5, which rounds | ||||
* the wrong way. This test ensures that remquo() is smart | * the wrong way. This test ensures that remquo() is smart | ||||
* enough to get the low-order bit right. | * enough to get the low-order bit right. | ||||
*/ | */ | ||||
testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332, | testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332, | ||||
0x1.fb3165b82de72p-333, -864062211); | 0x1.fb3165b82de72p-333, -864062211); | ||||
/* Even harder cases with greater exponent separation */ | /* Even harder cases with greater exponent separation */ | ||||
test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577); | test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577); | ||||
testd(-0x1.abcdefp120, 0x1.87654321p-120, | testd(-0x1.abcdefp120, 0x1.87654321p-120, -0x1.69c78ec4p-121, | ||||
-0x1.69c78ec4p-121, -63816414); | -63816414); | ||||
} | |||||
printf("ok 2 - rem\n"); | ATF_TC_WITHOUT_HEAD(rem3); | ||||
ATF_TC_BODY(rem3, tc) | |||||
{ | |||||
test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008); | test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008); | ||||
testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); | testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); | ||||
testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); | testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); | ||||
testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); | testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); | ||||
testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); | testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); | ||||
printf("ok 3 - rem\n"); | |||||
return (0); | |||||
} | } | ||||
static void | static void | ||||
test_invalid(long double x, long double y) | test_invalid(long double x, long double y) | ||||
{ | { | ||||
int q; | int q; | ||||
q = 0xdeadbeef; | q = 0xdeadbeef; | ||||
assert(isnan(remainder(x, y))); | ATF_CHECK(isnan(remainder(x, y))); | ||||
assert(isnan(remquo(x, y, &q))); | ATF_CHECK(isnan(remquo(x, y, &q))); | ||||
#ifdef STRICT | #ifdef STRICT | ||||
assert(q == 0xdeadbeef); | ATF_CHECK(q == 0xdeadbeef); | ||||
#endif | #endif | ||||
assert(isnan(remainderf(x, y))); | ATF_CHECK(isnan(remainderf(x, y))); | ||||
assert(isnan(remquof(x, y, &q))); | ATF_CHECK(isnan(remquof(x, y, &q))); | ||||
#ifdef STRICT | #ifdef STRICT | ||||
assert(q == 0xdeadbeef); | ATF_CHECK(q == 0xdeadbeef); | ||||
#endif | #endif | ||||
assert(isnan(remainderl(x, y))); | ATF_CHECK(isnan(remainderl(x, y))); | ||||
assert(isnan(remquol(x, y, &q))); | ATF_CHECK(isnan(remquol(x, y, &q))); | ||||
#ifdef STRICT | #ifdef STRICT | ||||
assert(q == 0xdeadbeef); | ATF_CHECK(q == 0xdeadbeef); | ||||
#endif | #endif | ||||
} | } | ||||
/* 0x012345 ==> 0x01ffff */ | /* 0x012345 ==> 0x01ffff */ | ||||
static inline int | static inline int | ||||
mask(int x) | mask(int x) | ||||
{ | { | ||||
return ((unsigned)~0 >> (32 - fls(x))); | return ((unsigned)~0 >> (32 - fls(x))); | ||||
} | } | ||||
static void | static void | ||||
testl(long double x, long double y, long double expected_rem, int expected_quo) | testl(long double x, long double y, long double expected_rem, int expected_quo) | ||||
{ | { | ||||
int q; | int q; | ||||
long double rem; | long double rem; | ||||
q = random(); | q = random(); | ||||
rem = remainderl(x, y); | rem = remainderl(x, y); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
rem = remquol(x, y, &q); | rem = remquol(x, y, &q); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ||||
assert((q & 0x7) == (expected_quo & 0x7)); | ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); | ||||
if (q != 0) { | if (q != 0) { | ||||
assert((q > 0) ^ !(expected_quo > 0)); | ATF_CHECK((q > 0) ^ !(expected_quo > 0)); | ||||
q = abs(q); | q = abs(q); | ||||
assert(q == (abs(expected_quo) & mask(q))); | ATF_CHECK(q == (abs(expected_quo) & mask(q))); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
testd(double x, double y, double expected_rem, int expected_quo) | testd(double x, double y, double expected_rem, int expected_quo) | ||||
{ | { | ||||
int q; | int q; | ||||
double rem; | double rem; | ||||
q = random(); | q = random(); | ||||
rem = remainder(x, y); | rem = remainder(x, y); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
rem = remquo(x, y, &q); | rem = remquo(x, y, &q); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ||||
assert((q & 0x7) == (expected_quo & 0x7)); | ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); | ||||
if (q != 0) { | if (q != 0) { | ||||
assert((q > 0) ^ !(expected_quo > 0)); | ATF_CHECK((q > 0) ^ !(expected_quo > 0)); | ||||
q = abs(q); | q = abs(q); | ||||
assert(q == (abs(expected_quo) & mask(q))); | ATF_CHECK(q == (abs(expected_quo) & mask(q))); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
testf(float x, float y, float expected_rem, int expected_quo) | testf(float x, float y, float expected_rem, int expected_quo) | ||||
{ | { | ||||
int q; | int q; | ||||
float rem; | float rem; | ||||
q = random(); | q = random(); | ||||
rem = remainderf(x, y); | rem = remainderf(x, y); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
rem = remquof(x, y, &q); | rem = remquof(x, y, &q); | ||||
assert(rem == expected_rem); | ATF_CHECK(rem == expected_rem); | ||||
assert(!signbit(rem) == !signbit(expected_rem)); | ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); | ||||
assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ | ||||
assert((q & 0x7) == (expected_quo & 0x7)); | ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); | ||||
if (q != 0) { | if (q != 0) { | ||||
assert((q > 0) ^ !(expected_quo > 0)); | ATF_CHECK((q > 0) ^ !(expected_quo > 0)); | ||||
q = abs(q); | q = abs(q); | ||||
assert((q & mask(q)) == (abs(expected_quo) & mask(q))); | ATF_CHECK((q & mask(q)) == (abs(expected_quo) & mask(q))); | ||||
} | } | ||||
} | |||||
ATF_TP_ADD_TCS(tp) | |||||
{ | |||||
ATF_TP_ADD_TC(tp, rem1); | |||||
ATF_TP_ADD_TC(tp, rem2); | |||||
ATF_TP_ADD_TC(tp, rem3); | |||||
return (atf_no_error()); | |||||
} | } |