1 /* $OpenBSD: fma_test.c,v 1.1 2021/10/22 18:00:22 mbuhl Exp $ */ 2 /*- 3 * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "macros.h" 29 30 /* 31 * Tests for fma{,f,l}(). 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <fenv.h> 39 #include <float.h> 40 #include <math.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 44 #include "test-utils.h" 45 46 #pragma STDC FENV_ACCESS ON 47 48 /* 49 * Test that a function returns the correct value and sets the 50 * exception flags correctly. The exceptmask specifies which 51 * exceptions we should check. We need to be lenient for several 52 * reasons, but mainly because on some architectures it's impossible 53 * to raise FE_OVERFLOW without raising FE_INEXACT. 54 * 55 * These are macros instead of functions so that assert provides more 56 * meaningful error messages. 57 */ 58 #define test(func, x, y, z, result, exceptmask, excepts) do { \ 59 volatile long double _vx = (x), _vy = (y), _vz = (z); \ 60 ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0); \ 61 CHECK_FPEQUAL((func)(_vx, _vy, _vz), (result)); \ 62 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \ 63 #func, #x); \ 64 } while (0) 65 66 #define testall(x, y, z, result, exceptmask, excepts) do { \ 67 test(fma, (double)(x), (double)(y), (double)(z), \ 68 (double)(result), (exceptmask), (excepts)); \ 69 test(fmaf, (float)(x), (float)(y), (float)(z), \ 70 (float)(result), (exceptmask), (excepts)); \ 71 test(fmal, (x), (y), (z), (result), (exceptmask), (excepts)); \ 72 } while (0) 73 74 /* Test in all rounding modes. */ 75 #define testrnd(func, x, y, z, rn, ru, rd, rz, exceptmask, excepts) do { \ 76 fesetround(FE_TONEAREST); \ 77 test((func), (x), (y), (z), (rn), (exceptmask), (excepts)); \ 78 fesetround(FE_UPWARD); \ 79 test((func), (x), (y), (z), (ru), (exceptmask), (excepts)); \ 80 fesetround(FE_DOWNWARD); \ 81 test((func), (x), (y), (z), (rd), (exceptmask), (excepts)); \ 82 fesetround(FE_TOWARDZERO); \ 83 test((func), (x), (y), (z), (rz), (exceptmask), (excepts)); \ 84 } while (0) 85 86 /* 87 * This is needed because clang constant-folds fma in ways that are incorrect 88 * in rounding modes other than FE_TONEAREST. 89 */ 90 static volatile double one = 1.0; 91 92 static void 93 test_zeroes(void) 94 { 95 const int rd = (fegetround() == FE_DOWNWARD); 96 97 testall(0.0, 0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0); 98 testall(1.0, 0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0); 99 testall(0.0, 1.0, 0.0, 0.0, ALL_STD_EXCEPT, 0); 100 testall(0.0, 0.0, 1.0, 1.0, ALL_STD_EXCEPT, 0); 101 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 testall(-0.0, -0.0, 0.0, 0.0, ALL_STD_EXCEPT, 0); 105 testall(0.0, 0.0, -0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0); 106 testall(-0.0, -0.0, -0.0, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0); 107 108 testall(-0.0, 0.0, -0.0, -0.0, ALL_STD_EXCEPT, 0); 109 testall(0.0, -0.0, -0.0, -0.0, ALL_STD_EXCEPT, 0); 110 111 testall(-one, one, one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0); 112 testall(one, -one, one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0); 113 testall(-one, -one, -one, rd ? -0.0 : 0.0, ALL_STD_EXCEPT, 0); 114 115 switch (fegetround()) { 116 case FE_TONEAREST: 117 case FE_TOWARDZERO: 118 test(fmaf, -FLT_MIN, FLT_MIN, 0.0, -0.0, 119 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW); 120 test(fma, -DBL_MIN, DBL_MIN, 0.0, -0.0, 121 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW); 122 test(fmal, -LDBL_MIN, LDBL_MIN, 0.0, -0.0, 123 ALL_STD_EXCEPT, FE_INEXACT | FE_UNDERFLOW); 124 } 125 } 126 127 static void 128 test_infinities(void) 129 { 130 testall(INFINITY, 1.0, -1.0, INFINITY, ALL_STD_EXCEPT, 0); 131 testall(-1.0, INFINITY, 0.0, -INFINITY, ALL_STD_EXCEPT, 0); 132 testall(0.0, 0.0, INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 133 testall(1.0, 1.0, INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 134 testall(1.0, 1.0, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0); 135 136 testall(INFINITY, -INFINITY, 1.0, -INFINITY, ALL_STD_EXCEPT, 0); 137 testall(INFINITY, INFINITY, 1.0, INFINITY, ALL_STD_EXCEPT, 0); 138 testall(-INFINITY, -INFINITY, INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 139 140 testall(0.0, INFINITY, 1.0, NAN, ALL_STD_EXCEPT, FE_INVALID); 141 testall(INFINITY, 0.0, -0.0, NAN, ALL_STD_EXCEPT, FE_INVALID); 142 143 /* The invalid exception is optional in this case. */ 144 testall(INFINITY, 0.0, NAN, NAN, ALL_STD_EXCEPT & ~FE_INVALID, 0); 145 146 testall(INFINITY, INFINITY, -INFINITY, NAN, 147 ALL_STD_EXCEPT, FE_INVALID); 148 testall(-INFINITY, INFINITY, INFINITY, NAN, 149 ALL_STD_EXCEPT, FE_INVALID); 150 testall(INFINITY, -1.0, INFINITY, NAN, 151 ALL_STD_EXCEPT, FE_INVALID); 152 153 test(fmaf, FLT_MAX, FLT_MAX, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0); 154 test(fma, DBL_MAX, DBL_MAX, -INFINITY, -INFINITY, ALL_STD_EXCEPT, 0); 155 test(fmal, LDBL_MAX, LDBL_MAX, -INFINITY, -INFINITY, 156 ALL_STD_EXCEPT, 0); 157 test(fmaf, FLT_MAX, -FLT_MAX, INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 158 test(fma, DBL_MAX, -DBL_MAX, INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 159 test(fmal, LDBL_MAX, -LDBL_MAX, INFINITY, INFINITY, 160 ALL_STD_EXCEPT, 0); 161 } 162 163 static void 164 test_nans(void) 165 { 166 testall(NAN, 0.0, 0.0, NAN, ALL_STD_EXCEPT, 0); 167 testall(1.0, NAN, 1.0, NAN, ALL_STD_EXCEPT, 0); 168 testall(1.0, -1.0, NAN, NAN, ALL_STD_EXCEPT, 0); 169 testall(0.0, 0.0, NAN, NAN, ALL_STD_EXCEPT, 0); 170 testall(NAN, NAN, NAN, NAN, ALL_STD_EXCEPT, 0); 171 172 /* x*y should not raise an inexact/overflow/underflow if z is NaN. */ 173 testall(M_PI, M_PI, NAN, NAN, ALL_STD_EXCEPT, 0); 174 test(fmaf, FLT_MIN, FLT_MIN, NAN, NAN, ALL_STD_EXCEPT, 0); 175 test(fma, DBL_MIN, DBL_MIN, NAN, NAN, ALL_STD_EXCEPT, 0); 176 test(fmal, LDBL_MIN, LDBL_MIN, NAN, NAN, ALL_STD_EXCEPT, 0); 177 test(fmaf, FLT_MAX, FLT_MAX, NAN, NAN, ALL_STD_EXCEPT, 0); 178 test(fma, DBL_MAX, DBL_MAX, NAN, NAN, ALL_STD_EXCEPT, 0); 179 test(fmal, LDBL_MAX, LDBL_MAX, NAN, NAN, ALL_STD_EXCEPT, 0); 180 } 181 182 /* 183 * Tests for cases where z is very small compared to x*y. 184 */ 185 static void 186 test_small_z(void) 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 /* z positive, x*y positive */ 248 if (fegetround() == FE_UPWARD) { 249 test(fmaf, 0x1.0p-50, 0x1.0p-50, 1.0, 1.0 + FLT_EPSILON, 250 ALL_STD_EXCEPT, FE_INEXACT); 251 test(fma, 0x1.0p-100, 0x1.0p-100, 1.0, 1.0 + DBL_EPSILON, 252 ALL_STD_EXCEPT, FE_INEXACT); 253 test(fmal, 0x1.0p-100, 0x1.0p-100, 1.0, 1.0 + LDBL_EPSILON, 254 ALL_STD_EXCEPT, FE_INEXACT); 255 } else { 256 testall(-0x1.0p-50, -0x1.0p-50, 0x1.0p100, 0x1.0p100, 257 ALL_STD_EXCEPT, FE_INEXACT); 258 } 259 260 /* z negative, x*y negative */ 261 if (fegetround() == FE_DOWNWARD) { 262 test(fmaf, -0x1.0p-50, 0x1.0p-50, -1.0, -(1.0 + FLT_EPSILON), 263 ALL_STD_EXCEPT, FE_INEXACT); 264 test(fma, -0x1.0p-100, 0x1.0p-100, -1.0, -(1.0 + DBL_EPSILON), 265 ALL_STD_EXCEPT, FE_INEXACT); 266 test(fmal, -0x1.0p-100, 0x1.0p-100, -1.0, -(1.0 + LDBL_EPSILON), 267 ALL_STD_EXCEPT, FE_INEXACT); 268 } else { 269 testall(0x1.0p-50, -0x1.0p-50, -0x1.0p100, -0x1.0p100, 270 ALL_STD_EXCEPT, FE_INEXACT); 271 } 272 273 /* z negative, x*y positive */ 274 if (fegetround() == FE_UPWARD || fegetround() == FE_TOWARDZERO) { 275 test(fmaf, -0x1.0p-50, -0x1.0p-50, -1.0, 276 -1.0 + FLT_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT); 277 test(fma, -0x1.0p-100, -0x1.0p-100, -1.0, 278 -1.0 + DBL_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT); 279 test(fmal, -0x1.0p-100, -0x1.0p-100, -1.0, 280 -1.0 + LDBL_EPSILON / 2, ALL_STD_EXCEPT, FE_INEXACT); 281 } else { 282 testall(0x1.0p-50, 0x1.0p-50, -0x1.0p100, -0x1.0p100, 283 ALL_STD_EXCEPT, FE_INEXACT); 284 } 285 286 /* z positive, x*y negative */ 287 if (fegetround() == FE_DOWNWARD || fegetround() == FE_TOWARDZERO) { 288 test(fmaf, 0x1.0p-50, -0x1.0p-50, 1.0, 1.0 - FLT_EPSILON / 2, 289 ALL_STD_EXCEPT, FE_INEXACT); 290 test(fma, 0x1.0p-100, -0x1.0p-100, 1.0, 1.0 - DBL_EPSILON / 2, 291 ALL_STD_EXCEPT, FE_INEXACT); 292 test(fmal, 0x1.0p-100, -0x1.0p-100, 1.0, 1.0 - LDBL_EPSILON / 2, 293 ALL_STD_EXCEPT, FE_INEXACT); 294 } else { 295 testall(-0x1.0p-50, 0x1.0p-50, 0x1.0p100, 0x1.0p100, 296 ALL_STD_EXCEPT, FE_INEXACT); 297 } 298 } 299 300 static void 301 test_accuracy(void) 302 { 303 304 /* ilogb(x*y) - ilogb(z) = 20 */ 305 testrnd(fmaf, -0x1.c139d8p-51, -0x1.600e7ap32, 0x1.26558cp-38, 306 0x1.34e48ap-18, 0x1.34e48cp-18, 0x1.34e48ap-18, 0x1.34e48ap-18, 307 ALL_STD_EXCEPT, FE_INEXACT); 308 testrnd(fma, -0x1.c139d7b84f1a3p-51, -0x1.600e7a2a16484p32, 309 0x1.26558cac31580p-38, 0x1.34e48a78aae97p-18, 310 0x1.34e48a78aae97p-18, 0x1.34e48a78aae96p-18, 311 0x1.34e48a78aae96p-18, ALL_STD_EXCEPT, FE_INEXACT); 312 #if LDBL_MANT_DIG == 113 313 testrnd(fmal, -0x1.c139d7b84f1a3079263afcc5bae3p-51L, 314 -0x1.600e7a2a164840edbe2e7d301a72p32L, 315 0x1.26558cac315807eb07e448042101p-38L, 316 0x1.34e48a78aae96c76ed36077dd387p-18L, 317 0x1.34e48a78aae96c76ed36077dd388p-18L, 318 0x1.34e48a78aae96c76ed36077dd387p-18L, 319 0x1.34e48a78aae96c76ed36077dd387p-18L, 320 ALL_STD_EXCEPT, FE_INEXACT); 321 #elif LDBL_MANT_DIG == 64 322 testrnd(fmal, -0x1.c139d7b84f1a307ap-51L, -0x1.600e7a2a164840eep32L, 323 0x1.26558cac315807ecp-38L, 0x1.34e48a78aae96c78p-18L, 324 0x1.34e48a78aae96c78p-18L, 0x1.34e48a78aae96c76p-18L, 325 0x1.34e48a78aae96c76p-18L, ALL_STD_EXCEPT, FE_INEXACT); 326 #elif LDBL_MANT_DIG == 53 327 testrnd(fmal, -0x1.c139d7b84f1a3p-51L, -0x1.600e7a2a16484p32L, 328 0x1.26558cac31580p-38L, 0x1.34e48a78aae97p-18L, 329 0x1.34e48a78aae97p-18L, 0x1.34e48a78aae96p-18L, 330 0x1.34e48a78aae96p-18L, ALL_STD_EXCEPT, FE_INEXACT); 331 #endif 332 333 /* ilogb(x*y) - ilogb(z) = -40 */ 334 testrnd(fmaf, 0x1.98210ap53, 0x1.9556acp-24, 0x1.d87da4p70, 335 0x1.d87da4p70, 0x1.d87da6p70, 0x1.d87da4p70, 0x1.d87da4p70, 336 ALL_STD_EXCEPT, FE_INEXACT); 337 testrnd(fma, 0x1.98210ac83fe2bp53, 0x1.9556ac1475f0fp-24, 338 0x1.d87da3aafc60ep70, 0x1.d87da3aafda40p70, 339 0x1.d87da3aafda40p70, 0x1.d87da3aafda3fp70, 340 0x1.d87da3aafda3fp70, ALL_STD_EXCEPT, FE_INEXACT); 341 #if LDBL_MANT_DIG == 113 342 testrnd(fmal, 0x1.98210ac83fe2a8f65b6278b74cebp53L, 343 0x1.9556ac1475f0f28968b61d0de65ap-24L, 344 0x1.d87da3aafc60d830aa4c6d73b749p70L, 345 0x1.d87da3aafda3f36a69eb86488224p70L, 346 0x1.d87da3aafda3f36a69eb86488225p70L, 347 0x1.d87da3aafda3f36a69eb86488224p70L, 348 0x1.d87da3aafda3f36a69eb86488224p70L, 349 ALL_STD_EXCEPT, FE_INEXACT); 350 #elif LDBL_MANT_DIG == 64 351 testrnd(fmal, 0x1.98210ac83fe2a8f6p53L, 0x1.9556ac1475f0f28ap-24L, 352 0x1.d87da3aafc60d83p70L, 0x1.d87da3aafda3f36ap70L, 353 0x1.d87da3aafda3f36ap70L, 0x1.d87da3aafda3f368p70L, 354 0x1.d87da3aafda3f368p70L, ALL_STD_EXCEPT, FE_INEXACT); 355 #elif LDBL_MANT_DIG == 53 356 testrnd(fmal, 0x1.98210ac83fe2bp53L, 0x1.9556ac1475f0fp-24L, 357 0x1.d87da3aafc60ep70L, 0x1.d87da3aafda40p70L, 358 0x1.d87da3aafda40p70L, 0x1.d87da3aafda3fp70L, 359 0x1.d87da3aafda3fp70L, ALL_STD_EXCEPT, FE_INEXACT); 360 #endif 361 362 /* ilogb(x*y) - ilogb(z) = 0 */ 363 testrnd(fmaf, 0x1.31ad02p+100, 0x1.2fbf7ap-42, -0x1.c3e106p+58, 364 -0x1.64c27cp+56, -0x1.64c27ap+56, -0x1.64c27cp+56, 365 -0x1.64c27ap+56, ALL_STD_EXCEPT, FE_INEXACT); 366 testrnd(fma, 0x1.31ad012ede8aap+100, 0x1.2fbf79c839067p-42, 367 -0x1.c3e106929056ep+58, -0x1.64c282b970a5fp+56, 368 -0x1.64c282b970a5ep+56, -0x1.64c282b970a5fp+56, 369 -0x1.64c282b970a5ep+56, ALL_STD_EXCEPT, FE_INEXACT); 370 #if LDBL_MANT_DIG == 113 371 testrnd(fmal, 0x1.31ad012ede8aa282fa1c19376d16p+100L, 372 0x1.2fbf79c839066f0f5c68f6d2e814p-42L, 373 -0x1.c3e106929056ec19de72bfe64215p+58L, 374 -0x1.64c282b970a612598fc025ca8cddp+56L, 375 -0x1.64c282b970a612598fc025ca8cddp+56L, 376 -0x1.64c282b970a612598fc025ca8cdep+56L, 377 -0x1.64c282b970a612598fc025ca8cddp+56L, 378 ALL_STD_EXCEPT, FE_INEXACT); 379 #elif LDBL_MANT_DIG == 64 380 testrnd(fmal, 0x1.31ad012ede8aa4eap+100L, 0x1.2fbf79c839066aeap-42L, 381 -0x1.c3e106929056e61p+58L, -0x1.64c282b970a60298p+56L, 382 -0x1.64c282b970a60298p+56L, -0x1.64c282b970a6029ap+56L, 383 -0x1.64c282b970a60298p+56L, ALL_STD_EXCEPT, FE_INEXACT); 384 #elif LDBL_MANT_DIG == 53 385 testrnd(fmal, 0x1.31ad012ede8aap+100L, 0x1.2fbf79c839067p-42L, 386 -0x1.c3e106929056ep+58L, -0x1.64c282b970a5fp+56L, 387 -0x1.64c282b970a5ep+56L, -0x1.64c282b970a5fp+56L, 388 -0x1.64c282b970a5ep+56L, ALL_STD_EXCEPT, FE_INEXACT); 389 #endif 390 391 /* x*y (rounded) ~= -z */ 392 /* XXX spurious inexact exceptions */ 393 testrnd(fmaf, 0x1.bbffeep-30, -0x1.1d164cp-74, 0x1.ee7296p-104, 394 -0x1.c46ea8p-128, -0x1.c46ea8p-128, -0x1.c46ea8p-128, 395 -0x1.c46ea8p-128, ALL_STD_EXCEPT & ~FE_INEXACT, 0); 396 testrnd(fma, 0x1.bbffeea6fc7d6p-30, 0x1.1d164c6cbf078p-74, 397 -0x1.ee72993aff948p-104, -0x1.71f72ac7d9d8p-159, 398 -0x1.71f72ac7d9d8p-159, -0x1.71f72ac7d9d8p-159, 399 -0x1.71f72ac7d9d8p-159, ALL_STD_EXCEPT & ~FE_INEXACT, 0); 400 #if LDBL_MANT_DIG == 113 401 testrnd(fmal, 0x1.bbffeea6fc7d65927d147f437675p-30L, 402 0x1.1d164c6cbf078b7a22607d1cd6a2p-74L, 403 -0x1.ee72993aff94973876031bec0944p-104L, 404 0x1.64e086175b3a2adc36e607058814p-217L, 405 0x1.64e086175b3a2adc36e607058814p-217L, 406 0x1.64e086175b3a2adc36e607058814p-217L, 407 0x1.64e086175b3a2adc36e607058814p-217L, 408 ALL_STD_EXCEPT & ~FE_INEXACT, 0); 409 #elif LDBL_MANT_DIG == 64 410 testrnd(fmal, 0x1.bbffeea6fc7d6592p-30L, 0x1.1d164c6cbf078b7ap-74L, 411 -0x1.ee72993aff949736p-104L, 0x1.af190e7a1ee6ad94p-168L, 412 0x1.af190e7a1ee6ad94p-168L, 0x1.af190e7a1ee6ad94p-168L, 413 0x1.af190e7a1ee6ad94p-168L, ALL_STD_EXCEPT & ~FE_INEXACT, 0); 414 #elif LDBL_MANT_DIG == 53 415 testrnd(fmal, 0x1.bbffeea6fc7d6p-30L, 0x1.1d164c6cbf078p-74L, 416 -0x1.ee72993aff948p-104L, -0x1.71f72ac7d9d8p-159L, 417 -0x1.71f72ac7d9d8p-159L, -0x1.71f72ac7d9d8p-159L, 418 -0x1.71f72ac7d9d8p-159L, ALL_STD_EXCEPT & ~FE_INEXACT, 0); 419 #endif 420 } 421 422 static void 423 test_double_rounding(void) 424 { 425 426 /* 427 * a = 0x1.8000000000001p0 428 * b = 0x1.8000000000001p0 429 * c = -0x0.0000000000000000000000000080...1p+1 430 * a * b = 0x1.2000000000001800000000000080p+1 431 * 432 * The correct behavior is to round DOWN to 0x1.2000000000001p+1 in 433 * round-to-nearest mode. An implementation that computes a*b+c in 434 * double+double precision, however, will get 0x1.20000000000018p+1, 435 * and then round UP. 436 */ 437 fesetround(FE_TONEAREST); 438 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0, 439 -0x1.0000000000001p-104, 0x1.2000000000001p+1, 440 ALL_STD_EXCEPT, FE_INEXACT); 441 fesetround(FE_DOWNWARD); 442 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0, 443 -0x1.0000000000001p-104, 0x1.2000000000001p+1, 444 ALL_STD_EXCEPT, FE_INEXACT); 445 fesetround(FE_UPWARD); 446 test(fma, 0x1.8000000000001p0, 0x1.8000000000001p0, 447 -0x1.0000000000001p-104, 0x1.2000000000002p+1, 448 ALL_STD_EXCEPT, FE_INEXACT); 449 450 fesetround(FE_TONEAREST); 451 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200002p+1, 452 ALL_STD_EXCEPT, FE_INEXACT); 453 fesetround(FE_DOWNWARD); 454 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200002p+1, 455 ALL_STD_EXCEPT, FE_INEXACT); 456 fesetround(FE_UPWARD); 457 test(fmaf, 0x1.800002p+0, 0x1.800002p+0, -0x1.000002p-46, 0x1.200004p+1, 458 ALL_STD_EXCEPT, FE_INEXACT); 459 460 fesetround(FE_TONEAREST); 461 #if LDBL_MANT_DIG == 64 462 test(fmal, 0x1.4p+0L, 0x1.0000000000000004p+0L, 0x1p-128L, 463 0x1.4000000000000006p+0L, ALL_STD_EXCEPT, FE_INEXACT); 464 #elif LDBL_MANT_DIG == 113 465 test(fmal, 0x1.8000000000000000000000000001p+0L, 466 0x1.8000000000000000000000000001p+0L, 467 -0x1.0000000000000000000000000001p-224L, 468 0x1.2000000000000000000000000001p+1L, ALL_STD_EXCEPT, FE_INEXACT); 469 #endif 470 471 } 472 473 static const int rmodes[] = { 474 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO 475 }; 476 477 ATF_TC_WITHOUT_HEAD(zeroes); 478 ATF_TC_BODY(zeroes, tc) 479 { 480 size_t i; 481 for (i = 0; i < nitems(rmodes); i++) { 482 printf("rmode = %d\n", rmodes[i]); 483 fesetround(rmodes[i]); 484 test_zeroes(); 485 } 486 } 487 488 ATF_TC_WITHOUT_HEAD(infinities); 489 ATF_TC_BODY(infinities, tc) 490 { 491 size_t i; 492 for (i = 0; i < nitems(rmodes); i++) { 493 printf("rmode = %d\n", rmodes[i]); 494 fesetround(rmodes[i]); 495 test_infinities(); 496 } 497 } 498 499 ATF_TC_WITHOUT_HEAD(nans); 500 ATF_TC_BODY(nans, tc) 501 { 502 fesetround(FE_TONEAREST); 503 test_nans(); 504 } 505 506 507 ATF_TC_WITHOUT_HEAD(small_z); 508 ATF_TC_BODY(small_z, tc) 509 { 510 size_t i; 511 for (i = 0; i < nitems(rmodes); i++) { 512 printf("rmode = %d\n", rmodes[i]); 513 fesetround(rmodes[i]); 514 test_small_z(); 515 } 516 } 517 518 519 ATF_TC_WITHOUT_HEAD(big_z); 520 ATF_TC_BODY(big_z, tc) 521 { 522 size_t i; 523 for (i = 0; i < nitems(rmodes); i++) { 524 printf("rmode = %d\n", rmodes[i]); 525 fesetround(rmodes[i]); 526 test_big_z(); 527 } 528 } 529 530 ATF_TC_WITHOUT_HEAD(accuracy); 531 ATF_TC_BODY(accuracy, tc) 532 { 533 fesetround(FE_TONEAREST); 534 test_accuracy(); 535 } 536 537 ATF_TC_WITHOUT_HEAD(double_rounding); 538 ATF_TC_BODY(double_rounding, tc) { 539 test_double_rounding(); 540 } 541 542 ATF_TP_ADD_TCS(tp) 543 { 544 ATF_TP_ADD_TC(tp, zeroes); 545 ATF_TP_ADD_TC(tp, infinities); 546 ATF_TP_ADD_TC(tp, nans); 547 ATF_TP_ADD_TC(tp, small_z); 548 ATF_TP_ADD_TC(tp, big_z); 549 ATF_TP_ADD_TC(tp, accuracy); 550 ATF_TP_ADD_TC(tp, double_rounding); 551 /* 552 * TODO: 553 * - Tests for subnormals 554 * - Cancellation tests (e.g., z = (double)x*y, but x*y is inexact) 555 */ 556 return (atf_no_error()); 557 } 558