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 #include <sys/param.h>
41 #include "test-utils.h"
43 static const int rmodes[] = {
44 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
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
57 float out[3]; /* one answer per rounding mode except towardzero */
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 }},
68 /* Get the appropriate result for the current rounding mode. */
70 get_output(int testindex, int rmodeindex, int negative)
74 if (negative) { /* swap downwards and upwards if input is negative */
77 else if (rmodeindex == 2)
80 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
82 out = tests[testindex].out[rmodeindex];
83 return (negative ? -out : out);
87 test_nearby(int testindex)
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));
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);
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);
113 test_modf(int testindex)
116 float ipartf, ipart_expected;
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));
125 in = tests[testindex].in;
126 ipart_expected = tests[testindex].out[1];
128 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
129 ipartl = ipart = ipartf = 42.0;
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);
140 ipart_expected = -ipart_expected;
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);
153 ATF_TC_WITHOUT_HEAD(nearbyint);
154 ATF_TC_BODY(nearbyint, tc)
158 for (i = 0; i < nitems(tests); i++) {
166 ATF_TP_ADD_TC(tp, nearbyint);
168 return (atf_no_error());