Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/* Test of turning floating-point exceptions into traps (signals).
Copyright (C) 2023-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2023. */
#include <config.h>
/* Specification. */
#include <fenv.h>
#include <stdio.h>
#include "infinity.h"
#include "snan.h"
#include "macros.h"
/* Operations that should raise various floating-point exceptions. */
/* For a list, see the glibc documentation
<https://www.gnu.org/software/libc/manual/html_node/FP-Exceptions.html> */
#define infinityf my_infinityf /* Avoid collision with Cygwin's <math.h> */
static float volatile infinityf, qnanf;
static double volatile infinityd, qnand;
static long double volatile infinityl, qnanl;
static void
raise_invalid_addition (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = infinityf; b = - infinityf;
c = a + b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = infinityd; b = - infinityd;
c = a + b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = infinityl; b = - infinityl;
c = a + b;
}
break;
}
}
static void
raise_invalid_multiplication (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = infinityf; b = 0.0f;
c = a * b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = infinityd; b = 0.0;
c = a * b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = infinityl; b = 0.0L;
c = a * b;
}
break;
}
}
static void
raise_invalid_division (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = infinityf; b = - infinityf;
c = a / b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = infinityd; b = - infinityd;
c = a / b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = infinityl; b = - infinityl;
c = a / b;
}
break;
}
}
static void
raise_invalid_comparison (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED int volatile c;
a = qnanf; b = qnanf;
c = a > b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED int volatile c;
a = qnand; b = qnand;
c = a > b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED int volatile c;
a = qnanl; b = qnanl;
c = a > b;
}
break;
}
}
static void
raise_invalid_snan (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = SNaNf (); b = 1.0f;
c = a + b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = SNaNd (); b = 1.0;
c = a + b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = SNaNl (); b = 1.0L;
c = a + b;
}
break;
}
}
static void
raise_divbyzero (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = 2.5f; b = - 0.0f;
c = a / b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = 2.5; b = - 0.0;
c = a / b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = 2.5L; b = - 0.0L;
c = a / b;
}
break;
}
}
static void
raise_overflow (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = 1e20f; b = 1e30f;
c = a * b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = 1e160; b = 1e260;
c = a * b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = 1e200L; b = 1e300L;
c = a * b;
c = c * c;
c = c * c;
c = c * c;
c = c * c;
}
break;
}
}
static void
raise_underflow (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = 1e-20f; b = 1e-30f;
c = a * b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = 1e-160; b = 1e-260;
c = a * b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = 1e-200L; b = 1e-300L;
c = a * b;
c = c * c;
c = c * c;
c = c * c;
c = c * c;
}
break;
}
}
static void
raise_inexact (char type)
{
switch (type)
{
case 'f':
{
float volatile a, b;
_GL_UNUSED float volatile c;
a = 2.5f; b = 3.0f;
c = a / b;
}
break;
case 'd':
{
double volatile a, b;
_GL_UNUSED double volatile c;
a = 2.5; b = 3.0;
c = a / b;
}
break;
case 'l':
{
long double volatile a, b;
_GL_UNUSED long double volatile c;
a = 2.5L; b = 3.0L;
c = a / b;
}
break;
}
}
int
main (int argc, char *argv[])
{
if (argc > 3)
{
const char *operation_arg = argv[1];
const char *procedure_arg = argv[2];
const char *type_arg = argv[3];
void (*operation) (char) = NULL;
int expected_exceptions = 0;
int possible_exceptions = 0;
/* Preparations (to be executed before we call feenableexcept). */
infinityf = Infinityf ();
qnanf = NaNf ();
infinityd = Infinityd ();
qnand = NaNd ();
infinityl = Infinityl ();
qnanl = NaNl ();
feclearexcept (FE_ALL_EXCEPT);
/* Analyze the operation argument. */
switch (operation_arg[0])
{
case '1':
operation = raise_invalid_addition;
expected_exceptions = FE_INVALID;
break;
case '2':
operation = raise_invalid_multiplication;
expected_exceptions = FE_INVALID;
break;
case '3':
operation = raise_invalid_division;
expected_exceptions = FE_INVALID;
break;
case '4':
operation = raise_invalid_comparison;
expected_exceptions = FE_INVALID;
break;
case '5':
operation = raise_invalid_snan;
expected_exceptions = FE_INVALID;
break;
case '6':
operation = raise_divbyzero;
expected_exceptions = FE_DIVBYZERO;
break;
case '7':
operation = raise_overflow;
expected_exceptions = FE_OVERFLOW;
possible_exceptions = FE_INEXACT;
break;
case '8':
operation = raise_underflow;
expected_exceptions = FE_UNDERFLOW;
possible_exceptions = FE_INEXACT;
break;
case '9':
operation = raise_inexact;
expected_exceptions = FE_INEXACT;
break;
}
/* Analyze the procedure argument. */
switch (procedure_arg[0])
{
/* These three procedures should lead to a trap. */
case 'p':
if (feenableexcept (expected_exceptions) == -1)
goto skip;
break;
case 'q':
if (feenableexcept (FE_ALL_EXCEPT) == -1)
goto skip;
break;
case 'r':
if (feenableexcept (FE_ALL_EXCEPT) == -1)
goto skip;
ASSERT (fedisableexcept (FE_ALL_EXCEPT & ~expected_exceptions)
== FE_ALL_EXCEPT);
break;
/* This procedure should *not* lead to a trap. */
case 's':
if (feenableexcept (FE_ALL_EXCEPT & ~(expected_exceptions | possible_exceptions)) == -1)
goto skip;
break;
}
/* Avoid known test failures. */
int known_failure = 0;
/* The '4' tests do not work
- on glibc/{i386,x86_64}, with gcc < 8 (except when option -mno-ieee-fp
is used) or with clang,
- on glibc/powerpc* and glibc/s390*,
- as well as on
GNU/kFreeBSD/i386, GNU/kFreeBSD/x86_64,
musl libc/i386, musl libc/powerpc64le,
macOS/i386, macOS/x86_64, macOS/arm64,
FreeBSD/i386, FreeBSD/x86_64, FreeBSD/powerpc64,
NetBSD/i386, NetBSD/x86_64, NetBSD/powerpc,
OpenBSD/i386, OpenBSD/x86_64, OpenBSD/mips64,
Minix/i386,
AIX/powerpc,
Solaris/i386, Solaris/x86_64,
Cygwin/x86_64,
native Windows/i386, native Windows/x86_64,
Haiku/i386.
Explanation of some of the {i386,x86_64} cases:
- Quoting the Intel 64 and IA-32 Architectures Software Developer's
Manual:
"The UCOMISD instruction differs from the COMISD instruction in that
it signals a SIMD floating-point invalid operation exception (#I)
only when a source operand is an SNaN. The COMISD instruction
signals an invalid numeric exception only if a source operand is
either an SNaN or a QNaN."
- gcc < 8 (except when option -mno-ieee-fp is used) and clang generate
'ucom*' or 'fucom*' instructions and thus fail the test.
- gcc >= 8 generates 'com*' or 'fcom*' instructions and thus passes
the test. */
#if (__GLIBC__ >= 2 && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86)) && (__GNUC__ < 8 || defined __clang__)) \
|| (__GLIBC__ >= 2 && (defined __powerpc__ || (defined __s390__ || defined __s390x__))) \
|| (__GLIBC__ >= 2 && __FreeBSD_kernel__ && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86))) \
|| (defined MUSL_LIBC && ((defined __i386 || defined _M_IX86) || defined __powerpc__)) \
|| ((defined __APPLE__ && defined __MACH__) && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86) || defined __aarch64__)) \
|| ((defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__) && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86))) \
|| ((defined __FreeBSD__ || defined __NetBSD__) && defined __powerpc__) \
|| (defined __OpenBSD__ && defined __mips64) \
|| (defined __minix && (defined __i386 || defined _M_IX86)) \
|| (defined _AIX && defined __powerpc__) \
|| (defined __sun && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86))) \
|| (defined __CYGWIN__ && (defined __x86_64__ || defined _M_X64)) \
|| (defined _WIN32 && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86))) \
|| (defined __HAIKU__ && (defined __i386 || defined _M_IX86))
known_failure |= (operation_arg[0] == '4');
#endif
/* The '7' and '8' tests, with types 'f' and 'd', do not work reliably
on Linux/i386. */
#if defined __i386 || defined _M_IX86
known_failure |= (operation_arg[0] == '7' || operation_arg[0] == '8');
#endif
/* The '9' tests do not work on Linux/alpha. */
#if (__GLIBC__ >= 2 && defined __alpha)
known_failure |= (operation_arg[0] == '9');
#endif
/* The 'l' tests do not work on Linux/loongarch64 with glibc 2.37.
Likewise on Linux/alpha with glibc 2.7 on Linux 2.6.26.
Likewise on FreeBSD 12.2/sparc, NetBSD 8.0/sparc, OpenBSD 7.2/sparc64.
Likewise on OpenBSD 7.4/mips64.
Cause unknown. */
#if (__GLIBC__ >= 2 && defined __loongarch__) \
|| ((__GLIBC__ == 2 && __GLIBC_MINOR__ < 36) && defined __alpha) \
|| ((defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__) && defined __sparc) \
|| (defined __OpenBSD__ && defined __mips64)
known_failure |= (type_arg[0] == 'l');
#endif
if (known_failure)
{
if (test_exit_status != EXIT_SUCCESS)
return test_exit_status;
fputs ("Skipping test: known failure on this platform\n", stderr);
return 77;
}
/* Analyze the type argument. */
switch (type_arg[0])
{
case 'f':
case 'd':
case 'l':
operation (type_arg[0]);
break;
}
}
return test_exit_status;
skip:
if (test_exit_status != EXIT_SUCCESS)
return test_exit_status;
fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr);
return 77;
}