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