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 #include <sys/param.h> 37 #include <fenv.h> 38 #include <math.h> 39 #include <stdio.h> 40 41 #include "test-utils.h" 42 43 static const int rmodes[] = { 44 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO, 45 }; 46 47 /* Make sure we're testing the library, not some broken compiler built-ins. */ 48 static double (*libnearbyint)(double) = nearbyint; 49 static float (*libnearbyintf)(float) = nearbyintf; 50 static long double (*libnearbyintl)(long double) = nearbyintl; 51 #define nearbyintf libnearbyintf 52 #define nearbyint libnearbyint 53 #define nearbyintl libnearbyintl 54 55 static const struct { 56 float in; 57 float out[3]; /* one answer per rounding mode except towardzero */ 58 } tests[] = { 59 /* input output (expected) */ 60 { 0.0, { 0.0, 0.0, 0.0 }}, 61 { 0.5, { 0.0, 0.0, 1.0 }}, 62 { M_PI, { 3.0, 3.0, 4.0 }}, 63 { 65536.5, { 65536, 65536, 65537 }}, 64 { INFINITY, { INFINITY, INFINITY, INFINITY }}, 65 { NAN, { NAN, NAN, NAN }}, 66 }; 67 68 /* Get the appropriate result for the current rounding mode. */ 69 static float 70 get_output(int testindex, int rmodeindex, int negative) 71 { 72 double out; 73 74 if (negative) { /* swap downwards and upwards if input is negative */ 75 if (rmodeindex == 1) 76 rmodeindex = 2; 77 else if (rmodeindex == 2) 78 rmodeindex = 1; 79 } 80 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */ 81 rmodeindex = 1; 82 out = tests[testindex].out[rmodeindex]; 83 return (negative ? -out : out); 84 } 85 86 static void 87 test_nearby(int testindex) 88 { 89 float in, out; 90 unsigned i; 91 92 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { 93 ATF_REQUIRE_EQ(0, fesetround(rmodes[i])); 94 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); 95 96 in = tests[testindex].in; 97 out = get_output(testindex, i, 0); 98 CHECK_FPEQUAL(out, libnearbyintf(in)); 99 CHECK_FPEQUAL(out, nearbyint(in)); 100 CHECK_FPEQUAL(out, nearbyintl(in)); 101 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 102 103 in = -tests[testindex].in; 104 out = get_output(testindex, i, 1); 105 CHECK_FPEQUAL(out, nearbyintf(in)); 106 CHECK_FPEQUAL(out, nearbyint(in)); 107 CHECK_FPEQUAL(out, nearbyintl(in)); 108 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 109 } 110 } 111 112 static void 113 test_modf(int testindex) 114 { 115 float in, out; 116 float ipartf, ipart_expected; 117 double ipart; 118 long double ipartl; 119 unsigned i; 120 121 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { 122 ATF_REQUIRE_EQ(0, fesetround(rmodes[i])); 123 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); 124 125 in = tests[testindex].in; 126 ipart_expected = tests[testindex].out[1]; 127 out = copysignf( 128 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in); 129 ipartl = ipart = ipartf = 42.0; 130 131 CHECK_FPEQUAL(out, modff(in, &ipartf)); 132 CHECK_FPEQUAL(ipart_expected, ipartf); 133 CHECK_FPEQUAL(out, modf(in, &ipart)); 134 CHECK_FPEQUAL(ipart_expected, ipart); 135 CHECK_FPEQUAL(out, modfl(in, &ipartl)); 136 CHECK_FPEQUAL(ipart_expected, ipartl); 137 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 138 139 in = -in; 140 ipart_expected = -ipart_expected; 141 out = -out; 142 ipartl = ipart = ipartf = 42.0; 143 CHECK_FPEQUAL(out, modff(in, &ipartf)); 144 CHECK_FPEQUAL(ipart_expected, ipartf); 145 CHECK_FPEQUAL(out, modf(in, &ipart)); 146 CHECK_FPEQUAL(ipart_expected, ipart); 147 CHECK_FPEQUAL(out, modfl(in, &ipartl)); 148 CHECK_FPEQUAL(ipart_expected, ipartl); 149 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 150 } 151 } 152 153 ATF_TC_WITHOUT_HEAD(nearbyint); 154 ATF_TC_BODY(nearbyint, tc) 155 { 156 unsigned i; 157 158 for (i = 0; i < nitems(tests); i++) { 159 test_nearby(i); 160 test_modf(i); 161 } 162 } 163 164 ATF_TP_ADD_TCS(tp) 165 { 166 ATF_TP_ADD_TC(tp, nearbyint); 167 168 return (atf_no_error()); 169 } 170