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 * $FreeBSD: src/tools/regression/lib/msun/test-nearbyint.c,v 1.2 2011/10/21 06:36:40 das Exp $
27 */
28
29 /*
30 * Tests for nearbyint{,f,l}()
31 *
32 * TODO:
33 * - adapt tests for rint(3)
34 * - tests for harder values (more mantissa bits than float)
35 */
36
37 #include <assert.h>
38 #include <fenv.h>
39 #include <math.h>
40 #include <stdio.h>
41
42 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
43 FE_OVERFLOW | FE_UNDERFLOW)
44
45 static int testnum;
46
47 static const int rmodes[] = {
48 FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
49 };
50
51 static const struct {
52 float in;
53 float out[3]; /* one answer per rounding mode except towardzero */
54 } tests[] = {
55 /* input output (expected) */
56 { 0.0, { 0.0, 0.0, 0.0 }},
57 { 0.5, { 0.0, 0.0, 1.0 }},
58 { M_PI, { 3.0, 3.0, 4.0 }},
59 { 65536.5, { 65536, 65536, 65537 }},
60 { INFINITY, { INFINITY, INFINITY, INFINITY }},
61 { NAN, { NAN, NAN, NAN }},
62 };
63
64 static const int ntests = sizeof(tests) / sizeof(tests[0]);
65
66 /*
67 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
68 * Fail an assertion if they differ.
69 */
70 static int
fpequal(long double d1,long double d2)71 fpequal(long double d1, long double d2)
72 {
73
74 if (d1 != d2)
75 return (isnan(d1) && isnan(d2));
76 return (copysignl(1.0, d1) == copysignl(1.0, d2));
77 }
78
79 /* Get the appropriate result for the current rounding mode. */
80 static float
get_output(int testindex,int rmodeindex,int negative)81 get_output(int testindex, int rmodeindex, int negative)
82 {
83 double out;
84
85 if (negative) { /* swap downwards and upwards if input is negative */
86 if (rmodeindex == 1)
87 rmodeindex = 2;
88 else if (rmodeindex == 2)
89 rmodeindex = 1;
90 }
91 if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
92 rmodeindex = 1;
93 out = tests[testindex].out[rmodeindex];
94 return (negative ? -out : out);
95 }
96
97 static void
test_nearby(int testindex)98 test_nearby(int testindex)
99 {
100 float in, out;
101 int i;
102
103 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
104 fesetround(rmodes[i]);
105 feclearexcept(ALL_STD_EXCEPT);
106
107 in = tests[testindex].in;
108 out = get_output(testindex, i, 0);
109 assert(fpequal(out, nearbyintf(in)));
110 assert(fpequal(out, nearbyint(in)));
111 assert(fpequal(out, nearbyintl(in)));
112 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
113
114 in = -tests[testindex].in;
115 out = get_output(testindex, i, 1);
116 assert(fpequal(out, nearbyintf(in)));
117 assert(fpequal(out, nearbyint(in)));
118 assert(fpequal(out, nearbyintl(in)));
119 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
120 }
121
122 printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in);
123 }
124
125 static void
test_modf(int testindex)126 test_modf(int testindex)
127 {
128 float in, out;
129 float ipartf, ipart_expected;
130 double ipart;
131 long double ipartl;
132 int i;
133
134 for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
135 fesetround(rmodes[i]);
136 feclearexcept(ALL_STD_EXCEPT);
137
138 in = tests[testindex].in;
139 ipart_expected = tests[testindex].out[1];
140 out = copysignf(
141 isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
142 ipartl = ipart = ipartf = 42.0;
143
144 assert(fpequal(out, modff(in, &ipartf)));
145 assert(fpequal(ipart_expected, ipartf));
146 assert(fpequal(out, modf(in, &ipart)));
147 assert(fpequal(ipart_expected, ipart));
148 assert(fpequal(out, modfl(in, &ipartl)));
149 assert(fpequal(ipart_expected, ipartl));
150 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
151
152 in = -in;
153 ipart_expected = -ipart_expected;
154 out = -out;
155 ipartl = ipart = ipartf = 42.0;
156 assert(fpequal(out, modff(in, &ipartf)));
157 assert(fpequal(ipart_expected, ipartf));
158 assert(fpequal(out, modf(in, &ipart)));
159 assert(fpequal(ipart_expected, ipart));
160 assert(fpequal(out, modfl(in, &ipartl)));
161 assert(fpequal(ipart_expected, ipartl));
162 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
163 }
164
165 printf("ok %d\t\t# modf(+%g)\n", testnum++, in);
166 }
167
168 int
main(int argc,char * argv[])169 main(int argc, char *argv[])
170 {
171 int i;
172
173 printf("1..%d\n", ntests * 2);
174 testnum = 1;
175 for (i = 0; i < ntests; i++) {
176 test_nearby(i);
177 test_modf(i);
178 }
179
180 return (0);
181 }
182