1 /* Test of saving the floating-point exception status flags.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
26 #include "fpe-trapping.h"
29 /* musl libc does not support floating-point exception trapping, even where
30 the hardware supports it. See
31 <https://wiki.musl-libc.org/functional-differences-from-glibc.html> */
32 #if HAVE_FPE_TRAPPING && (!MUSL_LIBC || GNULIB_FEENABLEEXCEPT)
34 /* Check that fesetexceptflag() does not trigger a trap. */
36 static volatile double a
, b
;
37 static volatile long double al
, bl
;
42 fexcept_t saved_flags_1
;
44 /* Test setting all exception flags. */
45 if (feraiseexcept (FE_INVALID
| FE_DIVBYZERO
| FE_OVERFLOW
| FE_UNDERFLOW
| FE_INEXACT
) != 0)
47 fputs ("Skipping test: floating-point exceptions are not supported on this machine.\n", stderr
);
51 /* Fill saved_flags_1. */
52 ASSERT (fegetexceptflag (&saved_flags_1
,
53 FE_INVALID
| FE_DIVBYZERO
| FE_OVERFLOW
| FE_UNDERFLOW
| FE_INEXACT
)
56 /* Clear exceptions from past operations. */
57 feclearexcept (FE_ALL_EXCEPT
);
59 /* An FE_INVALID exception shall trigger a SIGFPE signal, which by default
60 terminates the program. */
61 if (sigfpe_on_invalid () < 0)
63 if (test_exit_status
!= EXIT_SUCCESS
)
64 return test_exit_status
;
65 fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr
);
69 /* Attempt to set the FE_INVALID exception flag. */
70 _GL_UNUSED
int rc
= fesetexceptflag (&saved_flags_1
, FE_INVALID
);
71 /* On older i386 and on PowerPC, there is no way to implement
72 fesetexceptflag() such that it does not trigger a trap. fesetexceptflag()
73 is expected to fail in this case. */
74 # if !((defined __i386 || defined _M_IX86) || defined __powerpc__)
78 /* Do a harmless floating-point operation (since on some CPUs, floating-point
79 exceptions trigger a trap only at the next floating-point operation). */
81 al
= 1.0L; bl
= al
+ al
;
83 return test_exit_status
;
91 fputs ("Skipping test: feenableexcept not available\n", stderr
);