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