2 * Copyright (c) 2010 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 * Tests for nearbyint{,f,l}()
31 * - adapt tests for rint(3)
32 * - tests for harder values (more mantissa bits than float)
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
44 #include "test-utils.h"
48 static const int rmodes[] = {
49 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
52 /* Make sure we're testing the library, not some broken compiler built-ins. */
53 static double (*libnearbyint)(double) = nearbyint;
54 static float (*libnearbyintf)(float) = nearbyintf;
55 static long double (*libnearbyintl)(long double) = nearbyintl;
56 #define nearbyintf libnearbyintf
57 #define nearbyint libnearbyint
58 #define nearbyintl libnearbyintl
62 float out[3]; /* one answer per rounding mode except towardzero */
64 /* input output (expected) */
65 { 0.0, { 0.0, 0.0, 0.0 }},
66 { 0.5, { 0.0, 0.0, 1.0 }},
67 { M_PI, { 3.0, 3.0, 4.0 }},
68 { 65536.5, { 65536, 65536, 65537 }},
69 { INFINITY, { INFINITY, INFINITY, INFINITY }},
70 { NAN, { NAN, NAN, NAN }},
73 /* Get the appropriate result for the current rounding mode. */
75 get_output(int testindex, int rmodeindex, int negative)
79 if (negative) { /* swap downwards and upwards if input is negative */
82 else if (rmodeindex == 2)
85 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
87 out = tests[testindex].out[rmodeindex];
88 return (negative ? -out : out);
92 test_nearby(int testindex)
97 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
98 fesetround(rmodes[i]);
99 feclearexcept(ALL_STD_EXCEPT);
101 in = tests[testindex].in;
102 out = get_output(testindex, i, 0);
103 assert(fpequal(out, libnearbyintf(in)));
104 assert(fpequal(out, nearbyint(in)));
105 assert(fpequal(out, nearbyintl(in)));
106 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
108 in = -tests[testindex].in;
109 out = get_output(testindex, i, 1);
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);
116 printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in);
120 test_modf(int testindex)
123 float ipartf, ipart_expected;
128 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
129 fesetround(rmodes[i]);
130 feclearexcept(ALL_STD_EXCEPT);
132 in = tests[testindex].in;
133 ipart_expected = tests[testindex].out[1];
135 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
136 ipartl = ipart = ipartf = 42.0;
138 assert(fpequal(out, modff(in, &ipartf)));
139 assert(fpequal(ipart_expected, ipartf));
140 assert(fpequal(out, modf(in, &ipart)));
141 assert(fpequal(ipart_expected, ipart));
142 assert(fpequal(out, modfl(in, &ipartl)));
143 assert(fpequal(ipart_expected, ipartl));
144 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
147 ipart_expected = -ipart_expected;
149 ipartl = ipart = ipartf = 42.0;
150 assert(fpequal(out, modff(in, &ipartf)));
151 assert(fpequal(ipart_expected, ipartf));
152 assert(fpequal(out, modf(in, &ipart)));
153 assert(fpequal(ipart_expected, ipart));
154 assert(fpequal(out, modfl(in, &ipartl)));
155 assert(fpequal(ipart_expected, ipartl));
156 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
159 printf("ok %d\t\t# modf(+%g)\n", testnum++, in);
167 printf("1..%zu\n", (size_t)(nitems(tests) * 2));
169 for (i = 0; i < nitems(tests); i++) {