2 * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Test for remainder functions: remainder, remainderf, remainderl,
29 * remquo, remquof, and remquol.
30 * Missing tests: fmod, fmodf.
33 #include <sys/cdefs.h>
40 #include "test-utils.h"
42 static void test_invalid(long double, long double);
43 static void testl(long double, long double, long double, int);
44 static void testd(double, double, double, int);
45 static void testf(float, float, float, int);
47 #define test(x, y, e_r, e_q) do { \
48 testl(x, y, e_r, e_q); \
49 testd(x, y, e_r, e_q); \
50 testf(x, y, e_r, e_q); \
53 ATF_TC_WITHOUT_HEAD(rem1);
56 test_invalid(0.0, 0.0);
57 test_invalid(1.0, 0.0);
58 test_invalid(INFINITY, 0.0);
59 test_invalid(INFINITY, 1.0);
60 test_invalid(-INFINITY, 1.0);
61 test_invalid(NAN, 1.0);
62 test_invalid(1.0, NAN);
66 testd(0x1p-1074, 1, 0x1p-1074, 0);
67 testf(0x1p-149, 1, 0x1p-149, 0);
69 test(3.0, -4, -1, -1);
70 testd(275 * 1193040, 275, 0, 1193040);
71 test(4.5 * 7.5, 4.5, -2.25, 8); /* we should get the even one */
72 testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1);
73 #if LDBL_MANT_DIG > 53
74 testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L,
75 0x1.b72ea61d950c862p-2001L, -1);
79 ATF_TC_WITHOUT_HEAD(rem2);
83 * The actual quotient here is 864062210.50000003..., but
84 * double-precision division gets -8.64062210.5, which rounds
85 * the wrong way. This test ensures that remquo() is smart
86 * enough to get the low-order bit right.
88 testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332,
89 0x1.fb3165b82de72p-333, -864062211);
90 /* Even harder cases with greater exponent separation */
91 test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577);
92 testd(-0x1.abcdefp120, 0x1.87654321p-120, -0x1.69c78ec4p-121,
96 ATF_TC_WITHOUT_HEAD(rem3);
99 test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008);
100 testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1);
101 testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1);
102 testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189);
103 testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189);
107 test_invalid(long double x, long double y)
113 ATF_CHECK(isnan(remainder(x, y)));
114 ATF_CHECK(isnan(remquo(x, y, &q)));
116 ATF_CHECK(q == 0xdeadbeef);
119 ATF_CHECK(isnan(remainderf(x, y)));
120 ATF_CHECK(isnan(remquof(x, y, &q)));
122 ATF_CHECK(q == 0xdeadbeef);
125 ATF_CHECK(isnan(remainderl(x, y)));
126 ATF_CHECK(isnan(remquol(x, y, &q)));
128 ATF_CHECK(q == 0xdeadbeef);
132 /* 0x012345 ==> 0x01ffff */
136 return ((unsigned)~0 >> (32 - fls(x)));
140 testl(long double x, long double y, long double expected_rem, int expected_quo)
146 rem = remainderl(x, y);
147 ATF_CHECK(rem == expected_rem);
148 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
149 rem = remquol(x, y, &q);
150 ATF_CHECK(rem == expected_rem);
151 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
152 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
153 ATF_CHECK((q & 0x7) == (expected_quo & 0x7));
155 ATF_CHECK((q > 0) ^ !(expected_quo > 0));
157 ATF_CHECK(q == (abs(expected_quo) & mask(q)));
162 testd(double x, double y, double expected_rem, int expected_quo)
168 rem = remainder(x, y);
169 ATF_CHECK(rem == expected_rem);
170 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
171 rem = remquo(x, y, &q);
172 ATF_CHECK(rem == expected_rem);
173 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
174 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
175 ATF_CHECK((q & 0x7) == (expected_quo & 0x7));
177 ATF_CHECK((q > 0) ^ !(expected_quo > 0));
179 ATF_CHECK(q == (abs(expected_quo) & mask(q)));
184 testf(float x, float y, float expected_rem, int expected_quo)
190 rem = remainderf(x, y);
191 ATF_CHECK(rem == expected_rem);
192 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
193 rem = remquof(x, y, &q);
194 ATF_CHECK(rem == expected_rem);
195 ATF_CHECK(!signbit(rem) == !signbit(expected_rem));
196 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
197 ATF_CHECK((q & 0x7) == (expected_quo & 0x7));
199 ATF_CHECK((q > 0) ^ !(expected_quo > 0));
201 ATF_CHECK((q & mask(q)) == (abs(expected_quo) & mask(q)));
207 ATF_TP_ADD_TC(tp, rem1);
208 ATF_TP_ADD_TC(tp, rem2);
209 ATF_TP_ADD_TC(tp, rem3);
211 return (atf_no_error());