1 /*- 2 * Copyright (c) 2005-2008 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 * Test for remainder functions: remainder, remainderf, remainderl, 29 * remquo, remquof, and remquol. 30 * Missing tests: fmod, fmodf. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <float.h> 37 #include <math.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <strings.h> 41 42 #include "test-utils.h" 43 44 static void test_invalid(long double, long double); 45 static void testl(long double, long double, long double, int); 46 static void testd(double, double, double, int); 47 static void testf(float, float, float, int); 48 49 #define test(x, y, e_r, e_q) do { \ 50 testl(x, y, e_r, e_q); \ 51 testd(x, y, e_r, e_q); \ 52 testf(x, y, e_r, e_q); \ 53 } while (0) 54 55 ATF_TC_WITHOUT_HEAD(rem1); 56 ATF_TC_BODY(rem1, tc) 57 { 58 test_invalid(0.0, 0.0); 59 test_invalid(1.0, 0.0); 60 test_invalid(INFINITY, 0.0); 61 test_invalid(INFINITY, 1.0); 62 test_invalid(-INFINITY, 1.0); 63 test_invalid(NAN, 1.0); 64 test_invalid(1.0, NAN); 65 66 test(4, 4, 0, 1); 67 test(0, 3.0, 0, 0); 68 testd(0x1p-1074, 1, 0x1p-1074, 0); 69 testf(0x1p-149, 1, 0x1p-149, 0); 70 test(3.0, 4, -1, 1); 71 test(3.0, -4, -1, -1); 72 testd(275 * 1193040, 275, 0, 1193040); 73 test(4.5 * 7.5, 4.5, -2.25, 8); /* we should get the even one */ 74 testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1); 75 #if LDBL_MANT_DIG > 53 76 testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L, 77 0x1.b72ea61d950c862p-2001L, -1); 78 #endif 79 } 80 81 ATF_TC_WITHOUT_HEAD(rem2); 82 ATF_TC_BODY(rem2, tc) 83 { 84 /* 85 * The actual quotient here is 864062210.50000003..., but 86 * double-precision division gets -8.64062210.5, which rounds 87 * the wrong way. This test ensures that remquo() is smart 88 * enough to get the low-order bit right. 89 */ 90 testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332, 91 0x1.fb3165b82de72p-333, -864062211); 92 /* Even harder cases with greater exponent separation */ 93 test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577); 94 testd(-0x1.abcdefp120, 0x1.87654321p-120, -0x1.69c78ec4p-121, 95 -63816414); 96 } 97 98 ATF_TC_WITHOUT_HEAD(rem3); 99 ATF_TC_BODY(rem3, tc) 100 { 101 test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008); 102 testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); 103 testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); 104 testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); 105 testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); 106 } 107 108 static void 109 test_invalid(long double x, long double y) 110 { 111 int q; 112 113 q = 0xdeadbeef; 114 115 ATF_CHECK(isnan(remainder(x, y))); 116 ATF_CHECK(isnan(remquo(x, y, &q))); 117 #ifdef STRICT 118 ATF_CHECK(q == 0xdeadbeef); 119 #endif 120 121 ATF_CHECK(isnan(remainderf(x, y))); 122 ATF_CHECK(isnan(remquof(x, y, &q))); 123 #ifdef STRICT 124 ATF_CHECK(q == 0xdeadbeef); 125 #endif 126 127 ATF_CHECK(isnan(remainderl(x, y))); 128 ATF_CHECK(isnan(remquol(x, y, &q))); 129 #ifdef STRICT 130 ATF_CHECK(q == 0xdeadbeef); 131 #endif 132 } 133 134 /* 0x012345 ==> 0x01ffff */ 135 static inline int 136 mask(int x) 137 { 138 return ((unsigned)~0 >> (32 - fls(x))); 139 } 140 141 static void 142 testl(long double x, long double y, long double expected_rem, int expected_quo) 143 { 144 int q; 145 long double rem; 146 147 q = random(); 148 rem = remainderl(x, y); 149 ATF_CHECK(rem == expected_rem); 150 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 151 rem = remquol(x, y, &q); 152 ATF_CHECK(rem == expected_rem); 153 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 154 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 155 ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); 156 if (q != 0) { 157 ATF_CHECK((q > 0) ^ !(expected_quo > 0)); 158 q = abs(q); 159 ATF_CHECK(q == (abs(expected_quo) & mask(q))); 160 } 161 } 162 163 static void 164 testd(double x, double y, double expected_rem, int expected_quo) 165 { 166 int q; 167 double rem; 168 169 q = random(); 170 rem = remainder(x, y); 171 ATF_CHECK(rem == expected_rem); 172 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 173 rem = remquo(x, y, &q); 174 ATF_CHECK(rem == expected_rem); 175 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 176 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 177 ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); 178 if (q != 0) { 179 ATF_CHECK((q > 0) ^ !(expected_quo > 0)); 180 q = abs(q); 181 ATF_CHECK(q == (abs(expected_quo) & mask(q))); 182 } 183 } 184 185 static void 186 testf(float x, float y, float expected_rem, int expected_quo) 187 { 188 int q; 189 float rem; 190 191 q = random(); 192 rem = remainderf(x, y); 193 ATF_CHECK(rem == expected_rem); 194 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 195 rem = remquof(x, y, &q); 196 ATF_CHECK(rem == expected_rem); 197 ATF_CHECK(!signbit(rem) == !signbit(expected_rem)); 198 ATF_CHECK((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 199 ATF_CHECK((q & 0x7) == (expected_quo & 0x7)); 200 if (q != 0) { 201 ATF_CHECK((q > 0) ^ !(expected_quo > 0)); 202 q = abs(q); 203 ATF_CHECK((q & mask(q)) == (abs(expected_quo) & mask(q))); 204 } 205 } 206 207 ATF_TP_ADD_TCS(tp) 208 { 209 ATF_TP_ADD_TC(tp, rem1); 210 ATF_TP_ADD_TC(tp, rem2); 211 ATF_TP_ADD_TC(tp, rem3); 212 213 return (atf_no_error()); 214 } 215