1 /* Test signaling NaNs in issignaling, isnan, isinf, and similar functions.
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>, 2005.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
32 #include <math-tests.h>
37 double value
= 123.456;
40 static sigjmp_buf sigfpe_buf
;
42 typedef long double ldouble
;
46 myFPsighandler(int signal
,
50 siglongjmp(sigfpe_buf
, 0);
54 set_sigaction_FP(void)
57 /* register RT signal handler via sigaction */
58 sa
.sa_flags
= SA_SIGINFO
;
59 sa
.sa_sigaction
= &myFPsighandler
;
60 sigemptyset(&sa
.sa_mask
);
61 sigaction(SIGFPE
, &sa
, NULL
);
67 remove_sigaction_FP(void)
70 /* restore default RT signal handler via sigaction */
71 sa
.sa_flags
= SA_SIGINFO
;
72 sa
.sa_handler
= SIG_DFL
;
73 sigemptyset(&sa
.sa_mask
);
74 sigaction(SIGFPE
, &sa
, NULL
);
79 static int errors
= 0;
82 check (const char *testname
, int result
)
85 printf ("Failure: %s\n", testname
);
90 #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
94 /* Variables are declared volatile to forbid some compiler \
96 volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
97 /* A sNaN is only guaranteed to be representable in variables with */ \
98 /* static (or thread-local) storage duration. */ \
99 static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
100 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
105 qNaN_var = __builtin_nan ## SUFFIX (""); \
106 Inf_var = one_var / zero_var; \
112 (void) &minus_sNaN_var; \
115 set_sigaction_FP (); \
116 fegetenv(&saved_fenv); \
118 feclearexcept(FE_ALL_EXCEPT); \
119 feenableexcept (FE_ALL_EXCEPT); \
120 if (sigsetjmp(sigfpe_buf, 0)) \
122 printf (#FLOAT " issignaling (qNaN) raised SIGFPE\n"); \
125 check (#FLOAT " issignaling (qNaN)", !issignaling (qNaN_var)); \
128 feclearexcept(FE_ALL_EXCEPT); \
129 feenableexcept (FE_ALL_EXCEPT); \
130 if (sigsetjmp(sigfpe_buf, 0)) \
132 printf (#FLOAT " issignaling (-qNaN) raised SIGFPE\n"); \
135 check (#FLOAT " issignaling (-qNaN)", !issignaling (-qNaN_var)); \
138 feclearexcept(FE_ALL_EXCEPT); \
139 feenableexcept (FE_ALL_EXCEPT); \
140 if (sigsetjmp(sigfpe_buf, 0)) \
142 printf (#FLOAT " issignaling (sNaN) raised SIGFPE\n"); \
145 check (#FLOAT " issignaling (sNaN)", \
146 SNAN_TESTS (FLOAT) ? issignaling (sNaN_var) : 1); \
149 feclearexcept(FE_ALL_EXCEPT); \
150 feenableexcept (FE_ALL_EXCEPT); \
151 if (sigsetjmp(sigfpe_buf, 0)) \
153 printf (#FLOAT " issignaling (-sNaN) raised SIGFPE\n"); \
156 check (#FLOAT " issignaling (-sNaN)", \
157 SNAN_TESTS (FLOAT) ? issignaling (minus_sNaN_var) : 1); \
160 feclearexcept(FE_ALL_EXCEPT); \
161 feenableexcept (FE_ALL_EXCEPT); \
162 if (sigsetjmp(sigfpe_buf, 0)) \
164 printf (#FLOAT " isnan (qNaN) raised SIGFPE\n"); \
167 check (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
170 feclearexcept(FE_ALL_EXCEPT); \
171 feenableexcept (FE_ALL_EXCEPT); \
172 if (sigsetjmp(sigfpe_buf, 0)) \
174 printf (#FLOAT " isnan (-qNaN) raised SIGFPE\n"); \
177 check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
180 feclearexcept(FE_ALL_EXCEPT); \
181 feenableexcept (FE_ALL_EXCEPT); \
182 if (sigsetjmp(sigfpe_buf, 0)) \
184 printf (#FLOAT " isnan (sNaN) raised SIGFPE\n"); \
187 check (#FLOAT " isnan (sNaN)", \
188 SNAN_TESTS (FLOAT) ? isnan (sNaN_var) : 1); \
191 feclearexcept(FE_ALL_EXCEPT); \
192 feenableexcept (FE_ALL_EXCEPT); \
193 if (sigsetjmp(sigfpe_buf, 0)) \
195 printf (#FLOAT " isnan (-sNaN) raised SIGFPE\n"); \
198 check (#FLOAT " isnan (-sNaN)", \
199 SNAN_TESTS (FLOAT) ? isnan (minus_sNaN_var) : 1); \
202 feclearexcept(FE_ALL_EXCEPT); \
203 feenableexcept (FE_ALL_EXCEPT); \
204 if (sigsetjmp(sigfpe_buf, 0)) \
206 printf (#FLOAT " isinf (qNaN) raised SIGFPE\n"); \
209 check (#FLOAT " isinf (qNaN)", !isinf (qNaN_var)); \
212 feclearexcept(FE_ALL_EXCEPT); \
213 feenableexcept (FE_ALL_EXCEPT); \
214 if (sigsetjmp(sigfpe_buf, 0)) \
216 printf (#FLOAT " isinf (-qNaN) raised SIGFPE\n"); \
219 check (#FLOAT " isinf (-qNaN)", !isinf (-qNaN_var)); \
222 feclearexcept(FE_ALL_EXCEPT); \
223 feenableexcept (FE_ALL_EXCEPT); \
224 if (sigsetjmp(sigfpe_buf, 0)) \
226 printf (#FLOAT " isinf (sNaN) raised SIGFPE\n"); \
229 check (#FLOAT " isinf (sNaN)", \
230 SNAN_TESTS (FLOAT) ? !isinf (sNaN_var) : 1); \
233 feclearexcept(FE_ALL_EXCEPT); \
234 feenableexcept (FE_ALL_EXCEPT); \
235 if (sigsetjmp(sigfpe_buf, 0)) \
237 printf (#FLOAT " isinf (-sNaN) raised SIGFPE\n"); \
240 check (#FLOAT " isinf (-sNaN)", \
241 SNAN_TESTS (FLOAT) ? !isinf (minus_sNaN_var) : 1); \
244 feclearexcept(FE_ALL_EXCEPT); \
245 feenableexcept (FE_ALL_EXCEPT); \
246 if (sigsetjmp(sigfpe_buf, 0)) \
248 printf (#FLOAT " isfinite (qNaN) raised SIGFPE\n"); \
251 check (#FLOAT " isfinite (qNaN)", !isfinite (qNaN_var)); \
254 feclearexcept(FE_ALL_EXCEPT); \
255 feenableexcept (FE_ALL_EXCEPT); \
256 if (sigsetjmp(sigfpe_buf, 0)) \
258 printf (#FLOAT " isfinite (-qNaN) raised SIGFPE\n"); \
261 check (#FLOAT " isfinite (-qNaN)", !isfinite (-qNaN_var)); \
264 feclearexcept(FE_ALL_EXCEPT); \
265 feenableexcept (FE_ALL_EXCEPT); \
266 if (sigsetjmp(sigfpe_buf, 0)) \
268 printf (#FLOAT " isfinite (sNaN) raised SIGFPE\n"); \
271 check (#FLOAT " isfinite (sNaN)", \
272 SNAN_TESTS (FLOAT) ? !isfinite (sNaN_var) : 1); \
275 feclearexcept(FE_ALL_EXCEPT); \
276 feenableexcept (FE_ALL_EXCEPT); \
277 if (sigsetjmp(sigfpe_buf, 0)) \
279 printf (#FLOAT " isfinite (-sNaN) raised SIGFPE\n"); \
282 check (#FLOAT " isfinite (-sNaN)", \
283 SNAN_TESTS (FLOAT) ? !isfinite (minus_sNaN_var) : 1); \
286 feclearexcept(FE_ALL_EXCEPT); \
287 feenableexcept (FE_ALL_EXCEPT); \
288 if (sigsetjmp(sigfpe_buf, 0)) \
290 printf (#FLOAT " isnormal (qNaN) raised SIGFPE\n"); \
293 check (#FLOAT " isnormal (qNaN)", !isnormal (qNaN_var)); \
296 feclearexcept(FE_ALL_EXCEPT); \
297 feenableexcept (FE_ALL_EXCEPT); \
298 if (sigsetjmp(sigfpe_buf, 0)) \
300 printf (#FLOAT " isnormal (-qNaN) raised SIGFPE\n"); \
303 check (#FLOAT " isnormal (-qNaN)", !isnormal (-qNaN_var)); \
306 feclearexcept(FE_ALL_EXCEPT); \
307 feenableexcept (FE_ALL_EXCEPT); \
308 if (sigsetjmp(sigfpe_buf, 0)) \
310 printf (#FLOAT " isnormal (sNaN) isnormal SIGFPE\n"); \
313 check (#FLOAT " isnormal (sNaN)", \
314 SNAN_TESTS (FLOAT) ? !isnormal (sNaN_var) : 1); \
317 feclearexcept(FE_ALL_EXCEPT); \
318 feenableexcept (FE_ALL_EXCEPT); \
319 if (sigsetjmp(sigfpe_buf, 0)) \
321 printf (#FLOAT " isnormal (-sNaN) raised SIGFPE\n"); \
324 check (#FLOAT " isnormal (-sNaN)", \
325 SNAN_TESTS (FLOAT) ? !isnormal (minus_sNaN_var) : 1); \
328 feclearexcept(FE_ALL_EXCEPT); \
329 feenableexcept (FE_ALL_EXCEPT); \
330 if (sigsetjmp(sigfpe_buf, 0)) \
332 printf (#FLOAT " fpclassify (qNaN) raised SIGFPE\n"); \
335 check (#FLOAT " fpclassify (qNaN)", (fpclassify (qNaN_var)==FP_NAN)); \
338 feclearexcept(FE_ALL_EXCEPT); \
339 feenableexcept (FE_ALL_EXCEPT); \
340 if (sigsetjmp(sigfpe_buf, 0)) \
342 printf (#FLOAT " fpclassify (-qNaN) raised SIGFPE\n"); \
345 check (#FLOAT " fpclassify (-qNaN)", (fpclassify (-qNaN_var)==FP_NAN)); \
348 feclearexcept(FE_ALL_EXCEPT); \
349 feenableexcept (FE_ALL_EXCEPT); \
350 if (sigsetjmp(sigfpe_buf, 0)) \
352 printf (#FLOAT " fpclassify (sNaN) isnormal SIGFPE\n"); \
355 check (#FLOAT " fpclassify (sNaN)", \
356 SNAN_TESTS (FLOAT) ? fpclassify (sNaN_var) == FP_NAN : 1); \
359 feclearexcept(FE_ALL_EXCEPT); \
360 feenableexcept (FE_ALL_EXCEPT); \
361 if (sigsetjmp(sigfpe_buf, 0)) \
363 printf (#FLOAT " fpclassify (-sNaN) raised SIGFPE\n"); \
366 check (#FLOAT " fpclassify (-sNaN)", \
367 SNAN_TESTS (FLOAT) ? fpclassify (minus_sNaN_var) == FP_NAN : 1); \
370 fesetenv(&saved_fenv); /* restore saved fenv */ \
371 remove_sigaction_FP(); \
374 TEST_FUNC (float_test
, float, f
)
375 TEST_FUNC (double_test
, double, )
376 #ifndef NO_LONG_DOUBLE
377 TEST_FUNC (ldouble_test
, ldouble
, l
)
385 #ifndef NO_LONG_DOUBLE
392 #define TEST_FUNCTION do_test ()
393 #include "../test-skeleton.c"