1 /*-
2 * Copyright (c) 2008 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 fma{,f,l}().
29 */
30
31 #include <sys/param.h>
32 #include <fenv.h>
33 #include <float.h>
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "test-utils.h"
39
40 #pragma STDC FENV_ACCESS ON
41
42 /*
43 * Test that a function returns the correct value and sets the
44 * exception flags correctly. The exceptmask specifies which
45 * exceptions we should check. We need to be lenient for several
46 * reasons, but mainly because on some architectures it's impossible
47 * to raise FE_OVERFLOW without raising FE_INEXACT.
48 *
49 * These are macros instead of functions so that assert provides more
50 * meaningful error messages.
51 */
52 #define test(func, x, y, z, result, exceptmask, excepts) do { \
53 volatile long double _vx = (x), _vy = (y), _vz = (z); \
54 ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0); \
55 CHECK_FPEQUAL((func)(_vx, _vy, _vz), (result)); \
56 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
57 #func, #x); \
58 } while (0)
59
60 #define testall(x, y, z, result, exceptmask, excepts) do { \
61 test(fma, (double)(x), (double)(y), (double)(z), \
62 (double)(result), (exceptmask), (excepts)); \
63 test(fmaf, (float)(x), (float)(y), (float)(z), \
64 (float)(result), (exceptmask), (excepts)); \
65 test(fmal, (x), (y), (z), (result), (exceptmask), (excepts)); \
66 } while (0)
67
68 /* Test in all rounding modes. */
69 #define testrnd(func, x, y, z, rn, ru, rd, rz, exceptmask, excepts) do { \
70 fesetround(FE_TONEAREST); \
71 test((func), (x), (y), (z), (rn), (exceptmask), (excepts)); \
72 fesetround(FE_UPWARD); \
73 test((func), (x), (y), (z), (ru), (exceptmask), (excepts)); \
74 fesetround(FE_DOWNWARD); \
75 test((func), (x), (y), (z), (rd), (exceptmask), (excepts)); \
76 fesetround(FE_TOWARDZERO); \
77 test((func), (x), (y), (z), (rz), (exceptmask), (excepts)); \
78 } while (0)
79
80 /*
81 * This is needed because clang constant-folds fma in ways that are incorrect
82 * in rounding modes other than FE_TONEAREST.
83 */
84 static volatile double one = 1.0;
85
86 static void
test_zeroes(void)87 test_zeroes(void)
88 {
89 const int rd = (fegetround() == FE_DOWNWARD);
90
91 testall(0.0, 0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0);
92 testall(1.0, 0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0);
93 testall(0.0, 1.0, 0.0, 0.0, ALL_STD_EXCEPT, 0);
94 testall(0.0, 0.0, 1.0, 1.0, ALL_STD_EXCEPT, 0);
95
96 testall(-0.0, 0.0, 0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
97 testall(0.0, -0.0, 0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
98 testall(-0.0, -0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0);
99 testall(0.0, 0.0, -0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
100 testall(-0.0, -0.0, -0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
101
102 testall(-0.0, 0.0, -0.0, -0.0, ALL_STD_EXCEPT, 0);
103 testall(0.0, -0.0, -0.0, -0.0, ALL_STD_EXCEPT, 0);
104
105 testall(-one, one, one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
106 testall(one, -one, one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
107 testall(-one, -one, -one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0);
108
109 switch (fegetround()) {
110 case FE_TONEAREST:
111 case FE_TOWARDZERO:
112 test(fmaf, -FLT_MIN, FLT_MIN, 0.0, -0.0,
113 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW);
114 test(fma, -DBL_MIN, DBL_MIN, 0.0, -0.0,
115 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW);
116 test(fmal, -LDBL_MIN, LDBL_MIN, 0.0, -0.0,
117 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW);
118 }
119 }
120
121 static void
test_infinities(void)122 test_infinities(void)
123 {
124 testall(INFINITY, 1.0, -1.0, INFINITY, ALL_STD_EXCEPT, 0);
125 testall(-1.0, INFINITY, 0.0, -INFINITY, ALL_STD_EXCEPT, 0);
126 testall(0.0, 0.0, INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
127 testall(1.0, 1.0, INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
128 testall(1.0, 1.0, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0);
129
130 testall(INFINITY, -INFINITY, 1.0, -INFINITY, ALL_STD_EXCEPT, 0);
131 testall(INFINITY, INFINITY, 1.0, INFINITY, ALL_STD_EXCEPT, 0);
132 testall(-INFINITY, -INFINITY, INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
133
134 testall(0.0, INFINITY, 1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
135 testall(INFINITY, 0.0, -0.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
136
137 /* The invalid exception is optional in this case. */
138 testall(INFINITY, 0.0, NAN, NAN, ALL_STD_EXCEPT & ~FE_INVALID, 0);
139
140 testall(INFINITY, INFINITY, -INFINITY, NAN,
141 ALL_STD_EXCEPT, FE_INVALID);
142 testall(-INFINITY, INFINITY, INFINITY, NAN,
143 ALL_STD_EXCEPT, FE_INVALID);
144 testall(INFINITY, -1.0, INFINITY, NAN,
145 ALL_STD_EXCEPT, FE_INVALID);
146
147 test(fmaf, FLT_MAX, FLT_MAX, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0);
148 test(fma, DBL_MAX, DBL_MAX, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0);
149 test(fmal, LDBL_MAX, LDBL_MAX, -INFINITY, -INFINITY,
150 ALL_STD_EXCEPT, 0);
151 test(fmaf, FLT_MAX, -FLT_MAX, INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
152 test(fma, DBL_MAX, -DBL_MAX, INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
153 test(fmal, LDBL_MAX, -LDBL_MAX, INFINITY, INFINITY,
154 ALL_STD_EXCEPT, 0);
155 }
156
157 static void
test_nans(void)158 test_nans(void)
159 {
160 testall(NAN, 0.0, 0.0, NAN, ALL_STD_EXCEPT, 0);
161 testall(1.0, NAN, 1.0, NAN, ALL_STD_EXCEPT, 0);
162 testall(1.0, -1.0, NAN, NAN, ALL_STD_EXCEPT, 0);
163 testall(0.0, 0.0, NAN, NAN, ALL_STD_EXCEPT, 0);
164 testall(NAN, NAN, NAN, NAN, ALL_STD_EXCEPT, 0);
165
166 /* x*y should not raise an inexact/overflow/underflow if z is NaN. */
167 testall(M_PI, M_PI, NAN, NAN, ALL_STD_EXCEPT, 0);
168 test(fmaf, FLT_MIN, FLT_MIN, NAN, NAN, ALL_STD_EXCEPT, 0);
169 test(fma, DBL_MIN, DBL_MIN, NAN, NAN, ALL_STD_EXCEPT, 0);
170 test(fmal, LDBL_MIN, LDBL_MIN, NAN, NAN, ALL_STD_EXCEPT, 0);
171 test(fmaf, FLT_MAX, FLT_MAX, NAN, NAN, ALL_STD_EXCEPT, 0);
172 test(fma, DBL_MAX, DBL_MAX, NAN, NAN, ALL_STD_EXCEPT, 0);
173 test(fmal, LDBL_MAX, LDBL_MAX, NAN, NAN, ALL_STD_EXCEPT, 0);
174 }
175
176 /*
177 * Tests for cases where z is very small compared to x*y.
178 */
179 static void
test_small_z(void)180 test_small_z(void)
181 {
182 /* x*y positive, z positive */
183 if (fegetround() == FE_UPWARD) {
184 test(fmaf, one, one, 0x1.0p-100, 1.0 + FLT_EPSILON,
185 ALL_STD_EXCEPT, FE_INEXACT);
186 test(fma, one, one, 0x1.0p-200, 1.0 + DBL_EPSILON,
187 ALL_STD_EXCEPT, FE_INEXACT);
188 test(fmal, one, one, 0x1.0p-200, 1.0 + LDBL_EPSILON,
189 ALL_STD_EXCEPT, FE_INEXACT);
190 } else {
191 testall(0x1.0p100, one, 0x1.0p-100, 0x1.0p100,
192 ALL_STD_EXCEPT, FE_INEXACT);
193 }
194
195 /* x*y negative, z negative */
196 if (fegetround() == FE_DOWNWARD) {
197 test(fmaf, -one, one, -0x1.0p-100, -(1.0 + FLT_EPSILON),
198 ALL_STD_EXCEPT, FE_INEXACT);
199 test(fma, -one, one, -0x1.0p-200, -(1.0 + DBL_EPSILON),
200 ALL_STD_EXCEPT, FE_INEXACT);
201 test(fmal, -one, one, -0x1.0p-200, -(1.0 + LDBL_EPSILON),
202 ALL_STD_EXCEPT, FE_INEXACT);
203 } else {
204 testall(0x1.0p100, -one, -0x1.0p-100, -0x1.0p100,
205 ALL_STD_EXCEPT, FE_INEXACT);
206 }
207
208 /* x*y positive, z negative */
209 if (fegetround() == FE_DOWNWARD || fegetround() == FE_TOWARDZERO) {
210 test(fmaf, one, one, -0x1.0p-100, 1.0 - FLT_EPSILON / 2,
211 ALL_STD_EXCEPT, FE_INEXACT);
212 test(fma, one, one, -0x1.0p-200, 1.0 - DBL_EPSILON / 2,
213 ALL_STD_EXCEPT, FE_INEXACT);
214 test(fmal, one, one, -0x1.0p-200, 1.0 - LDBL_EPSILON / 2,
215 ALL_STD_EXCEPT, FE_INEXACT);
216 } else {
217 testall(0x1.0p100, one, -0x1.0p-100, 0x1.0p100,
218 ALL_STD_EXCEPT, FE_INEXACT);
219 }
220
221 /* x*y negative, z positive */
222 if (fegetround() == FE_UPWARD || fegetround() == FE_TOWARDZERO) {
223 test(fmaf, -one, one, 0x1.0p-100, -1.0 + FLT_EPSILON / 2,
224 ALL_STD_EXCEPT, FE_INEXACT);
225 test(fma, -one, one, 0x1.0p-200, -1.0 + DBL_EPSILON / 2,
226 ALL_STD_EXCEPT, FE_INEXACT);
227 test(fmal, -one, one, 0x1.0p-200, -1.0 + LDBL_EPSILON / 2,
228 ALL_STD_EXCEPT, FE_INEXACT);
229 } else {
230 testall(-0x1.0p100, one, 0x1.0p-100, -0x1.0p100,
231 ALL_STD_EXCEPT, FE_INEXACT);
232 }
233 }
234
235 /*
236 * Tests for cases where z is very large compared to x*y.
237 */
238 static void
test_big_z(void)239 test_big_z(void)
240 {
241 /* z positive, x*y positive */
242 if (fegetround() == FE_UPWARD) {
243 test(fmaf, 0x1.0p-50, 0x1.0p-50, 1.0, 1.0 + FLT_EPSILON,
244 ALL_STD_EXCEPT, FE_INEXACT);
245 test(fma, 0x1.0p-100, 0x1.0p-100, 1.0, 1.0 + DBL_EPSILON,
246 ALL_STD_EXCEPT, FE_INEXACT);
247 test(fmal, 0x1.0p-100, 0x1.0p-100, 1.0, 1.0 + LDBL_EPSILON,
248 ALL_STD_EXCEPT, FE_INEXACT);
249 } else {
250 testall(-0x1.0p-50, -0x1.0p-50, 0x1.0p100, 0x1.0p100,
251 ALL_STD_EXCEPT, FE_INEXACT);
252 }
253
254 /* z negative, x*y negative */
255 if (fegetround() == FE_DOWNWARD) {
256 test(fmaf, -0x1.0p-50, 0x1.0p-50, -1.0, -(1.0 + FLT_EPSILON),
257 ALL_STD_EXCEPT, FE_INEXACT);
258 test(fma, -0x1.0p-100, 0x1.0p-100, -1.0, -(1.0 + DBL_EPSILON),
259 ALL_STD_EXCEPT, FE_INEXACT);
260 test(fmal, -0x1.0p-100, 0x1.0p-100, -1.0, -(1.0 + LDBL_EPSILON),
261 ALL_STD_EXCEPT, FE_INEXACT);
262 } else {
263 testall(0x1.0p-50, -0x1.0p-50, -0x1.0p100, -0x1.0p100,
264 ALL_STD_EXCEPT, FE_INEXACT);
265 }
266
267 /* z negative, x*y positive */
268 if (fegetround() == FE_UPWARD || fegetround() == FE_TOWARDZERO) {
269 test(fmaf, -0x1.0p-50, -0x1.0p-50, -1.0,
270 -1.0 + FLT_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT);
271 test(fma, -0x1.0p-100, -0x1.0p-100, -1.0,
272 -1.0 + DBL_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT);
273 test(fmal, -0x1.0p-100, -0x1.0p-100, -1.0,
274 -1.0 + LDBL_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT);
275 } else {
276 testall(0x1.0p-50, 0x1.0p-50, -0x1.0p100, -0x1.0p100,
277 ALL_STD_EXCEPT, FE_INEXACT);
278 }
279
280 /* z positive, x*y negative */
281 if (fegetround() == FE_DOWNWARD || fegetround() == FE_TOWARDZERO) {
282 test(fmaf, 0x1.0p-50, -0x1.0p-50, 1.0, 1.0 - FLT_EPSILON / 2,
283 ALL_STD_EXCEPT, FE_INEXACT);
284 test(fma, 0x1.0p-100, -0x1.0p-100, 1.0, 1.0 - DBL_EPSILON / 2,
285 ALL_STD_EXCEPT, FE_INEXACT);
286 test(fmal, 0x1.0p-100, -0x1.0p-100, 1.0, 1.0 - LDBL_EPSILON / 2,
287 ALL_STD_EXCEPT, FE_INEXACT);
288 } else {
289 testall(-0x1.0p-50, 0x1.0p-50, 0x1.0p100, 0x1.0p100,
290 ALL_STD_EXCEPT, FE_INEXACT);
291 }
292 }
293
294 static void
test_accuracy(void)295 test_accuracy(void)
296 {
297
298 /* ilogb(x*y) - ilogb(z) = 20 */
299 testrnd(fmaf, -0x1.c139d8p-51, -0x1.600e7ap32, 0x1.26558cp-38,
300 0x1.34e48ap-18, 0x1.34e48cp-18, 0x1.34e48ap-18, 0x1.34e48ap-18,
301 ALL_STD_EXCEPT, FE_INEXACT);
302 testrnd(fma, -0x1.c139d7b84f1a3p-51, -0x1.600e7a2a16484p32,
303 0x1.26558cac31580p-38, 0x1.34e48a78aae97p-18,
304 0x1.34e48a78aae97p-18, 0x1.34e48a78aae96p-18,
305 0x1.34e48a78aae96p-18, ALL_STD_EXCEPT, FE_INEXACT);
306 #if LDBL_MANT_DIG == 113
307 testrnd(fmal, -0x1.c139d7b84f1a3079263afcc5bae3p-51L,
308 -0x1.600e7a2a164840edbe2e7d301a72p32L,
309 0x1.26558cac315807eb07e448042101p-38L,
310 0x1.34e48a78aae96c76ed36077dd387p-18L,
311 0x1.34e48a78aae96c76ed36077dd388p-18L,
312 0x1.34e48a78aae96c76ed36077dd387p-18L,
313 0x1.34e48a78aae96c76ed36077dd387p-18L,
314 ALL_STD_EXCEPT, FE_INEXACT);
315 #elif LDBL_MANT_DIG == 64
316 testrnd(fmal, -0x1.c139d7b84f1a307ap-51L, -0x1.600e7a2a164840eep32L,
317 0x1.26558cac315807ecp-38L, 0x1.34e48a78aae96c78p-18L,
318 0x1.34e48a78aae96c78p-18L, 0x1.34e48a78aae96c76p-18L,
319 0x1.34e48a78aae96c76p-18L, ALL_STD_EXCEPT, FE_INEXACT);
320 #elif LDBL_MANT_DIG == 53
321 testrnd(fmal, -0x1.c139d7b84f1a3p-51L, -0x1.600e7a2a16484p32L,
322 0x1.26558cac31580p-38L, 0x1.34e48a78aae97p-18L,
323 0x1.34e48a78aae97p-18L, 0x1.34e48a78aae96p-18L,
324 0x1.34e48a78aae96p-18L, ALL_STD_EXCEPT, FE_INEXACT);
325 #endif
326
327 /* ilogb(x*y) - ilogb(z) = -40 */
328 testrnd(fmaf, 0x1.98210ap53, 0x1.9556acp-24, 0x1.d87da4p70,
329 0x1.d87da4p70, 0x1.d87da6p70, 0x1.d87da4p70, 0x1.d87da4p70,
330 ALL_STD_EXCEPT, FE_INEXACT);
331 testrnd(fma, 0x1.98210ac83fe2bp53, 0x1.9556ac1475f0fp-24,
332 0x1.d87da3aafc60ep70, 0x1.d87da3aafda40p70,
333 0x1.d87da3aafda40p70, 0x1.d87da3aafda3fp70,
334 0x1.d87da3aafda3fp70, ALL_STD_EXCEPT, FE_INEXACT);
335 #if LDBL_MANT_DIG == 113
336 testrnd(fmal, 0x1.98210ac83fe2a8f65b6278b74cebp53L,
337 0x1.9556ac1475f0f28968b61d0de65ap-24L,
338 0x1.d87da3aafc60d830aa4c6d73b749p70L,
339 0x1.d87da3aafda3f36a69eb86488224p70L,
340 0x1.d87da3aafda3f36a69eb86488225p70L,
341 0x1.d87da3aafda3f36a69eb86488224p70L,
342 0x1.d87da3aafda3f36a69eb86488224p70L,
343 ALL_STD_EXCEPT, FE_INEXACT);
344 #elif LDBL_MANT_DIG == 64
345 testrnd(fmal, 0x1.98210ac83fe2a8f6p53L, 0x1.9556ac1475f0f28ap-24L,
346 0x1.d87da3aafc60d83p70L, 0x1.d87da3aafda3f36ap70L,
347 0x1.d87da3aafda3f36ap70L, 0x1.d87da3aafda3f368p70L,
348 0x1.d87da3aafda3f368p70L, ALL_STD_EXCEPT, FE_INEXACT);
349 #elif LDBL_MANT_DIG == 53
350 testrnd(fmal, 0x1.98210ac83fe2bp53L, 0x1.9556ac1475f0fp-24L,
351 0x1.d87da3aafc60ep70L, 0x1.d87da3aafda40p70L,
352 0x1.d87da3aafda40p70L, 0x1.d87da3aafda3fp70L,
353 0x1.d87da3aafda3fp70L, ALL_STD_EXCEPT, FE_INEXACT);
354 #endif
355
356 /* ilogb(x*y) - ilogb(z) = 0 */
357 testrnd(fmaf, 0x1.31ad02p+100, 0x1.2fbf7ap-42, -0x1.c3e106p+58,
358 -0x1.64c27cp+56, -0x1.64c27ap+56, -0x1.64c27cp+56,
359 -0x1.64c27ap+56, ALL_STD_EXCEPT, FE_INEXACT);
360 testrnd(fma, 0x1.31ad012ede8aap+100, 0x1.2fbf79c839067p-42,
361 -0x1.c3e106929056ep+58, -0x1.64c282b970a5fp+56,
362 -0x1.64c282b970a5ep+56, -0x1.64c282b970a5fp+56,
363 -0x1.64c282b970a5ep+56, ALL_STD_EXCEPT, FE_INEXACT);
364 #if LDBL_MANT_DIG == 113
365 testrnd(fmal, 0x1.31ad012ede8aa282fa1c19376d16p+100L,
366 0x1.2fbf79c839066f0f5c68f6d2e814p-42L,
367 -0x1.c3e106929056ec19de72bfe64215p+58L,
368 -0x1.64c282b970a612598fc025ca8cddp+56L,
369 -0x1.64c282b970a612598fc025ca8cddp+56L,
370 -0x1.64c282b970a612598fc025ca8cdep+56L,
371 -0x1.64c282b970a612598fc025ca8cddp+56L,
372 ALL_STD_EXCEPT, FE_INEXACT);
373 #elif LDBL_MANT_DIG == 64
374 testrnd(fmal, 0x1.31ad012ede8aa4eap+100L, 0x1.2fbf79c839066aeap-42L,
375 -0x1.c3e106929056e61p+58L, -0x1.64c282b970a60298p+56L,
376 -0x1.64c282b970a60298p+56L, -0x1.64c282b970a6029ap+56L,
377 -0x1.64c282b970a60298p+56L, ALL_STD_EXCEPT, FE_INEXACT);
378 #elif LDBL_MANT_DIG == 53
379 testrnd(fmal, 0x1.31ad012ede8aap+100L, 0x1.2fbf79c839067p-42L,
380 -0x1.c3e106929056ep+58L, -0x1.64c282b970a5fp+56L,
381 -0x1.64c282b970a5ep+56L, -0x1.64c282b970a5fp+56L,
382 -0x1.64c282b970a5ep+56L, ALL_STD_EXCEPT, FE_INEXACT);
383 #endif
384
385 /* x*y (rounded) ~= -z */
386 /* XXX spurious inexact exceptions */
387 testrnd(fmaf, 0x1.bbffeep-30, -0x1.1d164cp-74, 0x1.ee7296p-104,
388 -0x1.c46ea8p-128, -0x1.c46ea8p-128, -0x1.c46ea8p-128,
389 -0x1.c46ea8p-128, ALL_STD_EXCEPT & ~FE_INEXACT, 0);
390 testrnd(fma, 0x1.bbffeea6fc7d6p-30, 0x1.1d164c6cbf078p-74,
391 -0x1.ee72993aff948p-104, -0x1.71f72ac7d9d8p-159,
392 -0x1.71f72ac7d9d8p-159, -0x1.71f72ac7d9d8p-159,
393 -0x1.71f72ac7d9d8p-159, ALL_STD_EXCEPT & ~FE_INEXACT, 0);
394 #if LDBL_MANT_DIG == 113
395 testrnd(fmal, 0x1.bbffeea6fc7d65927d147f437675p-30L,
396 0x1.1d164c6cbf078b7a22607d1cd6a2p-74L,
397 -0x1.ee72993aff94973876031bec0944p-104L,
398 0x1.64e086175b3a2adc36e607058814p-217L,
399 0x1.64e086175b3a2adc36e607058814p-217L,
400 0x1.64e086175b3a2adc36e607058814p-217L,
401 0x1.64e086175b3a2adc36e607058814p-217L,
402 ALL_STD_EXCEPT & ~FE_INEXACT, 0);
403 #elif LDBL_MANT_DIG == 64
404 testrnd(fmal, 0x1.bbffeea6fc7d6592p-30L, 0x1.1d164c6cbf078b7ap-74L,
405 -0x1.ee72993aff949736p-104L, 0x1.af190e7a1ee6ad94p-168L,
406 0x1.af190e7a1ee6ad94p-168L, 0x1.af190e7a1ee6ad94p-168L,
407 0x1.af190e7a1ee6ad94p-168L, ALL_STD_EXCEPT & ~FE_INEXACT, 0);
408 #elif LDBL_MANT_DIG == 53
409 testrnd(fmal, 0x1.bbffeea6fc7d6p-30L, 0x1.1d164c6cbf078p-74L,
410 -0x1.ee72993aff948p-104L, -0x1.71f72ac7d9d8p-159L,
411 -0x1.71f72ac7d9d8p-159L, -0x1.71f72ac7d9d8p-159L,
412 -0x1.71f72ac7d9d8p-159L, ALL_STD_EXCEPT & ~FE_INEXACT, 0);
413 #endif
414 }
415
416 static void
test_double_rounding(void)417 test_double_rounding(void)
418 {
419
420 /*
421 * a = 0x1.8000000000001p0
422 * b = 0x1.8000000000001p0
423 * c = -0x0.0000000000000000000000000080...1p+1
424 * a * b = 0x1.2000000000001800000000000080p+1
425 *
426 * The correct behavior is to round DOWN to 0x1.2000000000001p+1 in
427 * round-to-nearest mode. An implementation that computes a*b+c in
428 * double+double precision, however, will get 0x1.20000000000018p+1,
429 * and then round UP.
430 */
431 fesetround(FE_TONEAREST);
432 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0,
433 -0x1.0000000000001p-104, 0x1.2000000000001p+1,
434 ALL_STD_EXCEPT, FE_INEXACT);
435 fesetround(FE_DOWNWARD);
436 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0,
437 -0x1.0000000000001p-104, 0x1.2000000000001p+1,
438 ALL_STD_EXCEPT, FE_INEXACT);
439 fesetround(FE_UPWARD);
440 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0,
441 -0x1.0000000000001p-104, 0x1.2000000000002p+1,
442 ALL_STD_EXCEPT, FE_INEXACT);
443
444 fesetround(FE_TONEAREST);
445 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200002p+1,
446 ALL_STD_EXCEPT, FE_INEXACT);
447 fesetround(FE_DOWNWARD);
448 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200002p+1,
449 ALL_STD_EXCEPT, FE_INEXACT);
450 fesetround(FE_UPWARD);
451 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200004p+1,
452 ALL_STD_EXCEPT, FE_INEXACT);
453
454 fesetround(FE_TONEAREST);
455 #if LDBL_MANT_DIG == 64
456 test(fmal, 0x1.4p+0L, 0x1.0000000000000004p+0L, 0x1p-128L,
457 0x1.4000000000000006p+0L, ALL_STD_EXCEPT, FE_INEXACT);
458 #elif LDBL_MANT_DIG == 113
459 test(fmal, 0x1.8000000000000000000000000001p+0L,
460 0x1.8000000000000000000000000001p+0L,
461 -0x1.0000000000000000000000000001p-224L,
462 0x1.2000000000000000000000000001p+1L, ALL_STD_EXCEPT, FE_INEXACT);
463 #endif
464
465 }
466
467 static const int rmodes[] = {
468 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
469 };
470
471 ATF_TC_WITHOUT_HEAD(zeroes);
ATF_TC_BODY(zeroes,tc)472 ATF_TC_BODY(zeroes, tc)
473 {
474 for (size_t i = 0; i < nitems(rmodes); i++) {
475 printf("rmode = %d\n", rmodes[i]);
476 fesetround(rmodes[i]);
477 test_zeroes();
478 }
479 }
480
481 ATF_TC_WITHOUT_HEAD(infinities);
ATF_TC_BODY(infinities,tc)482 ATF_TC_BODY(infinities, tc)
483 {
484 for (size_t i = 0; i < nitems(rmodes); i++) {
485 printf("rmode = %d\n", rmodes[i]);
486 fesetround(rmodes[i]);
487 test_infinities();
488 }
489 }
490
491 ATF_TC_WITHOUT_HEAD(nans);
ATF_TC_BODY(nans,tc)492 ATF_TC_BODY(nans, tc)
493 {
494 fesetround(FE_TONEAREST);
495 test_nans();
496 }
497
498
499 ATF_TC_WITHOUT_HEAD(small_z);
ATF_TC_BODY(small_z,tc)500 ATF_TC_BODY(small_z, tc)
501 {
502 for (size_t i = 0; i < nitems(rmodes); i++) {
503 printf("rmode = %d\n", rmodes[i]);
504 fesetround(rmodes[i]);
505 test_small_z();
506 }
507 }
508
509
510 ATF_TC_WITHOUT_HEAD(big_z);
ATF_TC_BODY(big_z,tc)511 ATF_TC_BODY(big_z, tc)
512 {
513 for (size_t i = 0; i < nitems(rmodes); i++) {
514 printf("rmode = %d\n", rmodes[i]);
515 fesetround(rmodes[i]);
516 test_big_z();
517 }
518 }
519
520 ATF_TC_WITHOUT_HEAD(accuracy);
ATF_TC_BODY(accuracy,tc)521 ATF_TC_BODY(accuracy, tc)
522 {
523 fesetround(FE_TONEAREST);
524 test_accuracy();
525 }
526
527 ATF_TC_WITHOUT_HEAD(double_rounding);
ATF_TC_BODY(double_rounding,tc)528 ATF_TC_BODY(double_rounding, tc) {
529 test_double_rounding();
530 }
531
ATF_TP_ADD_TCS(tp)532 ATF_TP_ADD_TCS(tp)
533 {
534 ATF_TP_ADD_TC(tp, zeroes);
535 ATF_TP_ADD_TC(tp, infinities);
536 ATF_TP_ADD_TC(tp, nans);
537 ATF_TP_ADD_TC(tp, small_z);
538 ATF_TP_ADD_TC(tp, big_z);
539 ATF_TP_ADD_TC(tp, accuracy);
540 ATF_TP_ADD_TC(tp, double_rounding);
541 /*
542 * TODO:
543 * - Tests for subnormals
544 * - Cancellation tests (e.g., z = (double)x*y, but x*y is inexact)
545 */
546 return (atf_no_error());
547 }
548