1 /* Test signaling NaNs in issignaling, isnan, isinf, and similar functions.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 #include <math-tests.h>
28 #include <support/check.h>
31 #ifdef __SUPPORT_SNAN__
32 static sigjmp_buf sigfpe_buf
;
35 myFPsighandler (int signal
)
37 siglongjmp (sigfpe_buf
, 1);
40 static int errors
= 0;
42 #define CHECK(testname, expr) \
44 feclearexcept (FE_ALL_EXCEPT); \
45 feenableexcept (FE_ALL_EXCEPT); \
46 if (sigsetjmp (sigfpe_buf, 0)) \
48 printf ("%s raised SIGFPE\n", testname); \
53 printf ("Failure: %s\n", testname); \
58 #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
62 /* Variables are declared volatile to forbid some compiler \
64 volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
65 /* A sNaN is only guaranteed to be representable in variables with */ \
66 /* static (or thread-local) storage duration. */ \
67 static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
68 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
73 qNaN_var = __builtin_nan ## SUFFIX (""); \
74 Inf_var = one_var / zero_var; \
80 (void) &minus_sNaN_var; \
83 fegetenv (&saved_fenv); \
85 CHECK (#FLOAT " issignaling (qNaN)", !issignaling (qNaN_var)); \
86 CHECK (#FLOAT " issignaling (-qNaN)", !issignaling (-qNaN_var)); \
87 CHECK (#FLOAT " issignaling (sNaN)", \
88 SNAN_TESTS (FLOAT) ? issignaling (sNaN_var) : 1); \
89 CHECK (#FLOAT " issignaling (-sNaN)", \
90 SNAN_TESTS (FLOAT) ? issignaling (minus_sNaN_var) : 1); \
91 CHECK (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
92 CHECK (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
93 CHECK (#FLOAT " isnan (sNaN)", \
94 SNAN_TESTS (FLOAT) ? isnan (sNaN_var) : 1); \
95 CHECK (#FLOAT " isnan (-sNaN)", \
96 SNAN_TESTS (FLOAT) ? isnan (minus_sNaN_var) : 1); \
97 CHECK (#FLOAT " isinf (qNaN)", !isinf (qNaN_var)); \
98 CHECK (#FLOAT " isinf (-qNaN)", !isinf (-qNaN_var)); \
99 CHECK (#FLOAT " isinf (sNaN)", \
100 SNAN_TESTS (FLOAT) ? !isinf (sNaN_var) : 1); \
101 CHECK (#FLOAT " isinf (-sNaN)", \
102 SNAN_TESTS (FLOAT) ? !isinf (minus_sNaN_var) : 1); \
103 CHECK (#FLOAT " isfinite (qNaN)", !isfinite (qNaN_var)); \
104 CHECK (#FLOAT " isfinite (-qNaN)", !isfinite (-qNaN_var)); \
105 CHECK (#FLOAT " isfinite (sNaN)", \
106 SNAN_TESTS (FLOAT) ? !isfinite (sNaN_var) : 1); \
107 CHECK (#FLOAT " isfinite (-sNaN)", \
108 SNAN_TESTS (FLOAT) ? !isfinite (minus_sNaN_var) : 1); \
109 CHECK (#FLOAT " isnormal (qNaN)", !isnormal (qNaN_var)); \
110 CHECK (#FLOAT " isnormal (-qNaN)", !isnormal (-qNaN_var)); \
111 CHECK (#FLOAT " isnormal (sNaN)", \
112 SNAN_TESTS (FLOAT) ? !isnormal (sNaN_var) : 1); \
113 CHECK (#FLOAT " isnormal (-sNaN)", \
114 SNAN_TESTS (FLOAT) ? !isnormal (minus_sNaN_var) : 1); \
115 CHECK (#FLOAT " fpclassify (qNaN)", (fpclassify (qNaN_var)==FP_NAN)); \
116 CHECK (#FLOAT " fpclassify (-qNaN)", (fpclassify (-qNaN_var)==FP_NAN)); \
117 CHECK (#FLOAT " fpclassify (sNaN)", \
118 SNAN_TESTS (FLOAT) ? fpclassify (sNaN_var) == FP_NAN : 1); \
119 CHECK (#FLOAT " fpclassify (-sNaN)", \
120 SNAN_TESTS (FLOAT) ? fpclassify (minus_sNaN_var) == FP_NAN : 1); \
122 fesetenv (&saved_fenv); /* restore saved fenv */ \
125 TEST_FUNC (float_test, float, f)
126 TEST_FUNC (double_test
, double, )
127 TEST_FUNC (ldouble_test
, long double, l
)
133 #ifdef __SUPPORT_SNAN__
134 signal (SIGFPE
, &myFPsighandler
);
142 FAIL_UNSUPPORTED ("compiler does not support -fsignaling-nans");
146 #include <support/test-driver.c>