1144094Sdas/*- 2177767Sdas * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.org> 3144094Sdas * All rights reserved. 4144094Sdas * 5144094Sdas * Redistribution and use in source and binary forms, with or without 6144094Sdas * modification, are permitted provided that the following conditions 7144094Sdas * are met: 8144094Sdas * 1. Redistributions of source code must retain the above copyright 9144094Sdas * notice, this list of conditions and the following disclaimer. 10144094Sdas * 2. Redistributions in binary form must reproduce the above copyright 11144094Sdas * notice, this list of conditions and the following disclaimer in the 12144094Sdas * documentation and/or other materials provided with the distribution. 13144094Sdas * 14144094Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15144094Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16144094Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17144094Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18144094Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19144094Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20144094Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21144094Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22144094Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23144094Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24144094Sdas * SUCH DAMAGE. 25144094Sdas */ 26144094Sdas 27144094Sdas/* 28177767Sdas * Test for remainder functions: remainder, remainderf, remainderl, 29177767Sdas * remquo, remquof, and remquol. 30144094Sdas * Missing tests: fmod, fmodf. 31144094Sdas */ 32144094Sdas 33144094Sdas#include <sys/cdefs.h> 34144094Sdas__FBSDID("$FreeBSD: stable/11/lib/msun/tests/rem_test.c 315121 2017-03-12 04:52:09Z ngie $"); 35144094Sdas 36144094Sdas#include <assert.h> 37177767Sdas#include <float.h> 38144094Sdas#include <math.h> 39144094Sdas#include <stdio.h> 40144094Sdas#include <stdlib.h> 41144094Sdas#include <strings.h> 42144094Sdas 43177767Sdasstatic void test_invalid(long double, long double); 44177767Sdasstatic void testl(long double, long double, long double, int); 45144094Sdasstatic void testd(double, double, double, int); 46144094Sdasstatic void testf(float, float, float, int); 47144094Sdas 48144094Sdas#define test(x, y, e_r, e_q) do { \ 49177767Sdas testl(x, y, e_r, e_q); \ 50144094Sdas testd(x, y, e_r, e_q); \ 51144094Sdas testf(x, y, e_r, e_q); \ 52144094Sdas} while (0) 53144094Sdas 54144094Sdasint 55315121Sngiemain(void) 56144094Sdas{ 57144094Sdas 58233974Sdas printf("1..3\n"); 59144094Sdas 60144094Sdas test_invalid(0.0, 0.0); 61144094Sdas test_invalid(1.0, 0.0); 62144094Sdas test_invalid(INFINITY, 0.0); 63144094Sdas test_invalid(INFINITY, 1.0); 64144094Sdas test_invalid(-INFINITY, 1.0); 65144094Sdas test_invalid(NAN, 1.0); 66144094Sdas test_invalid(1.0, NAN); 67144094Sdas 68144094Sdas test(4, 4, 0, 1); 69144094Sdas test(0, 3.0, 0, 0); 70251120Sdas testd(0x1p-1074, 1, 0x1p-1074, 0); 71251120Sdas testf(0x1p-149, 1, 0x1p-149, 0); 72144094Sdas test(3.0, 4, -1, 1); 73144094Sdas test(3.0, -4, -1, -1); 74144094Sdas testd(275 * 1193040, 275, 0, 1193040); 75144094Sdas test(4.5 * 7.5, 4.5, -2.25, 8); /* we should get the even one */ 76144094Sdas testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1); 77177767Sdas#if LDBL_MANT_DIG > 53 78177767Sdas testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L, 79177767Sdas 0x1.b72ea61d950c862p-2001L, -1); 80177767Sdas#endif 81144094Sdas 82144094Sdas printf("ok 1 - rem\n"); 83144094Sdas 84144094Sdas /* 85144094Sdas * The actual quotient here is 864062210.50000003..., but 86144094Sdas * double-precision division gets -8.64062210.5, which rounds 87144094Sdas * the wrong way. This test ensures that remquo() is smart 88144094Sdas * enough to get the low-order bit right. 89144094Sdas */ 90144094Sdas testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332, 91144094Sdas 0x1.fb3165b82de72p-333, -864062211); 92144094Sdas /* Even harder cases with greater exponent separation */ 93144094Sdas test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577); 94144094Sdas testd(-0x1.abcdefp120, 0x1.87654321p-120, 95144094Sdas -0x1.69c78ec4p-121, -63816414); 96144094Sdas 97144094Sdas printf("ok 2 - rem\n"); 98144094Sdas 99233974Sdas test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008); 100233974Sdas testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); 101233974Sdas testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1); 102233974Sdas testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); 103233974Sdas testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189); 104233974Sdas 105233974Sdas printf("ok 3 - rem\n"); 106233974Sdas 107144094Sdas return (0); 108144094Sdas} 109144094Sdas 110144094Sdasstatic void 111177767Sdastest_invalid(long double x, long double y) 112144094Sdas{ 113144094Sdas int q; 114144094Sdas 115144094Sdas q = 0xdeadbeef; 116144094Sdas 117144094Sdas assert(isnan(remainder(x, y))); 118144094Sdas assert(isnan(remquo(x, y, &q))); 119165856Sdas#ifdef STRICT 120144094Sdas assert(q == 0xdeadbeef); 121165856Sdas#endif 122144094Sdas 123144094Sdas assert(isnan(remainderf(x, y))); 124144094Sdas assert(isnan(remquof(x, y, &q))); 125165856Sdas#ifdef STRICT 126144094Sdas assert(q == 0xdeadbeef); 127165856Sdas#endif 128177767Sdas 129177767Sdas assert(isnan(remainderl(x, y))); 130177767Sdas assert(isnan(remquol(x, y, &q))); 131177767Sdas#ifdef STRICT 132177767Sdas assert(q == 0xdeadbeef); 133177767Sdas#endif 134144094Sdas} 135144094Sdas 136144094Sdas/* 0x012345 ==> 0x01ffff */ 137144094Sdasstatic inline int 138144094Sdasmask(int x) 139144094Sdas{ 140144094Sdas return ((unsigned)~0 >> (32 - fls(x))); 141144094Sdas} 142144094Sdas 143144094Sdasstatic void 144177767Sdastestl(long double x, long double y, long double expected_rem, int expected_quo) 145177767Sdas{ 146177767Sdas int q; 147233974Sdas long double rem; 148177767Sdas 149177767Sdas q = random(); 150233974Sdas rem = remainderl(x, y); 151233974Sdas assert(rem == expected_rem); 152233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 153233974Sdas rem = remquol(x, y, &q); 154233974Sdas assert(rem == expected_rem); 155233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 156233974Sdas assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 157177767Sdas assert((q & 0x7) == (expected_quo & 0x7)); 158177767Sdas if (q != 0) { 159177767Sdas assert((q > 0) ^ !(expected_quo > 0)); 160177767Sdas q = abs(q); 161177767Sdas assert(q == (abs(expected_quo) & mask(q))); 162177767Sdas } 163177767Sdas} 164177767Sdas 165177767Sdasstatic void 166144094Sdastestd(double x, double y, double expected_rem, int expected_quo) 167144094Sdas{ 168144094Sdas int q; 169233974Sdas double rem; 170144094Sdas 171144094Sdas q = random(); 172233974Sdas rem = remainder(x, y); 173233974Sdas assert(rem == expected_rem); 174233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 175233974Sdas rem = remquo(x, y, &q); 176233974Sdas assert(rem == expected_rem); 177233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 178233974Sdas assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 179144094Sdas assert((q & 0x7) == (expected_quo & 0x7)); 180165856Sdas if (q != 0) { 181165856Sdas assert((q > 0) ^ !(expected_quo > 0)); 182165856Sdas q = abs(q); 183165856Sdas assert(q == (abs(expected_quo) & mask(q))); 184165856Sdas } 185144094Sdas} 186144094Sdas 187144094Sdasstatic void 188144094Sdastestf(float x, float y, float expected_rem, int expected_quo) 189144094Sdas{ 190144094Sdas int q; 191233974Sdas float rem; 192144094Sdas 193144094Sdas q = random(); 194233974Sdas rem = remainderf(x, y); 195233974Sdas assert(rem == expected_rem); 196233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 197233974Sdas rem = remquof(x, y, &q); 198233974Sdas assert(rem == expected_rem); 199233974Sdas assert(!signbit(rem) == !signbit(expected_rem)); 200233974Sdas assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */ 201144094Sdas assert((q & 0x7) == (expected_quo & 0x7)); 202165856Sdas if (q != 0) { 203165856Sdas assert((q > 0) ^ !(expected_quo > 0)); 204165856Sdas q = abs(q); 205165856Sdas assert((q & mask(q)) == (abs(expected_quo) & mask(q))); 206165856Sdas } 207144094Sdas} 208