nearbyint_test.c revision 226605
1/*- 2 * Copyright (c) 2010 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 * Tests for nearbyint{,f,l}() 29 * 30 * TODO: 31 * - adapt tests for rint(3) 32 * - tests for harder values (more mantissa bits than float) 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-nearbyint.c 226605 2011-10-21 06:36:40Z das $"); 37 38#include <assert.h> 39#include <fenv.h> 40#include <math.h> 41#include <stdio.h> 42 43#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \ 44 FE_OVERFLOW | FE_UNDERFLOW) 45 46static int testnum; 47 48static const int rmodes[] = { 49 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO, 50}; 51 52static const struct { 53 float in; 54 float out[3]; /* one answer per rounding mode except towardzero */ 55} tests[] = { 56/* input output (expected) */ 57 { 0.0, { 0.0, 0.0, 0.0 }}, 58 { 0.5, { 0.0, 0.0, 1.0 }}, 59 { M_PI, { 3.0, 3.0, 4.0 }}, 60 { 65536.5, { 65536, 65536, 65537 }}, 61 { INFINITY, { INFINITY, INFINITY, INFINITY }}, 62 { NAN, { NAN, NAN, NAN }}, 63}; 64 65static const int ntests = sizeof(tests) / sizeof(tests[0]); 66 67/* 68 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0. 69 * Fail an assertion if they differ. 70 */ 71static int 72fpequal(long double d1, long double d2) 73{ 74 75 if (d1 != d2) 76 return (isnan(d1) && isnan(d2)); 77 return (copysignl(1.0, d1) == copysignl(1.0, d2)); 78} 79 80/* Get the appropriate result for the current rounding mode. */ 81static float 82get_output(int testindex, int rmodeindex, int negative) 83{ 84 double out; 85 86 if (negative) { /* swap downwards and upwards if input is negative */ 87 if (rmodeindex == 1) 88 rmodeindex = 2; 89 else if (rmodeindex == 2) 90 rmodeindex = 1; 91 } 92 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */ 93 rmodeindex = 1; 94 out = tests[testindex].out[rmodeindex]; 95 return (negative ? -out : out); 96} 97 98static void 99test_nearby(int testindex) 100{ 101 float in, out; 102 int i; 103 104 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { 105 fesetround(rmodes[i]); 106 feclearexcept(ALL_STD_EXCEPT); 107 108 in = tests[testindex].in; 109 out = get_output(testindex, i, 0); 110 assert(fpequal(out, nearbyintf(in))); 111 assert(fpequal(out, nearbyint(in))); 112 assert(fpequal(out, nearbyintl(in))); 113 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 114 115 in = -tests[testindex].in; 116 out = get_output(testindex, i, 1); 117 assert(fpequal(out, nearbyintf(in))); 118 assert(fpequal(out, nearbyint(in))); 119 assert(fpequal(out, nearbyintl(in))); 120 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 121 } 122 123 printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in); 124} 125 126static void 127test_modf(int testindex) 128{ 129 float in, out; 130 float ipartf, ipart_expected; 131 double ipart; 132 long double ipartl; 133 int i; 134 135 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { 136 fesetround(rmodes[i]); 137 feclearexcept(ALL_STD_EXCEPT); 138 139 in = tests[testindex].in; 140 ipart_expected = tests[testindex].out[1]; 141 out = copysignf( 142 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in); 143 ipartl = ipart = ipartf = 42.0; 144 145 assert(fpequal(out, modff(in, &ipartf))); 146 assert(fpequal(ipart_expected, ipartf)); 147 assert(fpequal(out, modf(in, &ipart))); 148 assert(fpequal(ipart_expected, ipart)); 149 assert(fpequal(out, modfl(in, &ipartl))); 150 assert(fpequal(ipart_expected, ipartl)); 151 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 152 153 in = -in; 154 ipart_expected = -ipart_expected; 155 out = -out; 156 ipartl = ipart = ipartf = 42.0; 157 assert(fpequal(out, modff(in, &ipartf))); 158 assert(fpequal(ipart_expected, ipartf)); 159 assert(fpequal(out, modf(in, &ipart))); 160 assert(fpequal(ipart_expected, ipart)); 161 assert(fpequal(out, modfl(in, &ipartl))); 162 assert(fpequal(ipart_expected, ipartl)); 163 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 164 } 165 166 printf("ok %d\t\t# modf(+%g)\n", testnum++, in); 167} 168 169int 170main(int argc, char *argv[]) 171{ 172 int i; 173 174 printf("1..%d\n", ntests * 2); 175 testnum = 1; 176 for (i = 0; i < ntests; i++) { 177 test_nearby(i); 178 test_modf(i); 179 } 180 181 return (0); 182} 183