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