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>
43 #include "test-utils.h"
45 static const int rmodes[] = {
46 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
49 /* Make sure we're testing the library, not some broken compiler built-ins. */
50 static double (*libnearbyint)(double) = nearbyint;
51 static float (*libnearbyintf)(float) = nearbyintf;
52 static long double (*libnearbyintl)(long double) = nearbyintl;
53 #define nearbyintf libnearbyintf
54 #define nearbyint libnearbyint
55 #define nearbyintl libnearbyintl
59 float out[3]; /* one answer per rounding mode except towardzero */
61 /* input output (expected) */
62 { 0.0, { 0.0, 0.0, 0.0 }},
63 { 0.5, { 0.0, 0.0, 1.0 }},
64 { M_PI, { 3.0, 3.0, 4.0 }},
65 { 65536.5, { 65536, 65536, 65537 }},
66 { INFINITY, { INFINITY, INFINITY, INFINITY }},
67 { NAN, { NAN, NAN, NAN }},
70 /* Get the appropriate result for the current rounding mode. */
72 get_output(int testindex, int rmodeindex, int negative)
76 if (negative) { /* swap downwards and upwards if input is negative */
79 else if (rmodeindex == 2)
82 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
84 out = tests[testindex].out[rmodeindex];
85 return (negative ? -out : out);
89 test_nearby(int testindex)
94 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
95 ATF_REQUIRE_EQ(0, fesetround(rmodes[i]));
96 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT));
98 in = tests[testindex].in;
99 out = get_output(testindex, i, 0);
100 CHECK_FPEQUAL(out, libnearbyintf(in));
101 CHECK_FPEQUAL(out, nearbyint(in));
102 CHECK_FPEQUAL(out, nearbyintl(in));
103 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
105 in = -tests[testindex].in;
106 out = get_output(testindex, i, 1);
107 CHECK_FPEQUAL(out, nearbyintf(in));
108 CHECK_FPEQUAL(out, nearbyint(in));
109 CHECK_FPEQUAL(out, nearbyintl(in));
110 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
115 test_modf(int testindex)
118 float ipartf, ipart_expected;
123 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
124 ATF_REQUIRE_EQ(0, fesetround(rmodes[i]));
125 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT));
127 in = tests[testindex].in;
128 ipart_expected = tests[testindex].out[1];
130 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
131 ipartl = ipart = ipartf = 42.0;
133 CHECK_FPEQUAL(out, modff(in, &ipartf));
134 CHECK_FPEQUAL(ipart_expected, ipartf);
135 CHECK_FPEQUAL(out, modf(in, &ipart));
136 CHECK_FPEQUAL(ipart_expected, ipart);
137 CHECK_FPEQUAL(out, modfl(in, &ipartl));
138 CHECK_FPEQUAL(ipart_expected, ipartl);
139 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
142 ipart_expected = -ipart_expected;
144 ipartl = ipart = ipartf = 42.0;
145 CHECK_FPEQUAL(out, modff(in, &ipartf));
146 CHECK_FPEQUAL(ipart_expected, ipartf);
147 CHECK_FPEQUAL(out, modf(in, &ipart));
148 CHECK_FPEQUAL(ipart_expected, ipart);
149 CHECK_FPEQUAL(out, modfl(in, &ipartl));
150 CHECK_FPEQUAL(ipart_expected, ipartl);
151 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
155 ATF_TC_WITHOUT_HEAD(nearbyint);
156 ATF_TC_BODY(nearbyint, tc)
160 for (i = 0; i < nitems(tests); i++) {
168 ATF_TP_ADD_TC(tp, nearbyint);
170 return (atf_no_error());