1 /* Test signaling NaNs in isnan, isinf, and similar functions.
2 Copyright (C) 2008-2013 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/>. */
34 double value = 123.456;
37 static sigjmp_buf sigfpe_buf;
39 typedef long double ldouble;
43 myFPsighandler(int signal,
47 siglongjmp(sigfpe_buf, 0);
51 set_sigaction_FP(void)
54 /* register RT signal handler via sigaction */
55 sa.sa_flags = SA_SIGINFO;
56 sa.sa_sigaction = &myFPsighandler;
57 sigemptyset(&sa.sa_mask);
58 sigaction(SIGFPE, &sa, NULL);
64 remove_sigaction_FP(void)
67 /* restore default RT signal handler via sigaction */
68 sa.sa_flags = SA_SIGINFO;
69 sa.sa_handler = SIG_DFL;
70 sigemptyset(&sa.sa_mask);
71 sigaction(SIGFPE, &sa, NULL);
76 static int errors = 0;
79 check (const char *testname, int result)
82 printf ("Failure: %s\n", testname);
87 #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
91 /* Variables are declared volatile to forbid some compiler \
93 volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
94 /* A sNaN is only guaranteed to be representable in variables with */ \
95 /* static (or thread-local) storage duration. */ \
96 static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
97 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
102 qNaN_var = __builtin_nan ## SUFFIX (""); \
103 Inf_var = one_var / zero_var; \
109 (void) &minus_sNaN_var; \
112 set_sigaction_FP (); \
113 fegetenv(&saved_fenv); \
115 feclearexcept(FE_ALL_EXCEPT); \
116 feenableexcept (FE_ALL_EXCEPT); \
117 if (sigsetjmp(sigfpe_buf, 0)) \
119 printf (#FLOAT " isnan (qNaN) raised SIGFPE\n"); \
122 check (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
125 feclearexcept(FE_ALL_EXCEPT); \
126 feenableexcept (FE_ALL_EXCEPT); \
127 if (sigsetjmp(sigfpe_buf, 0)) \
129 printf (#FLOAT " isnan (-qNaN) raised SIGFPE\n"); \
132 check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
135 feclearexcept(FE_ALL_EXCEPT); \
136 feenableexcept (FE_ALL_EXCEPT); \
137 if (sigsetjmp(sigfpe_buf, 0)) \
139 printf (#FLOAT " isnan (sNaN) raised SIGFPE\n"); \
142 check (#FLOAT " isnan (sNaN)", isnan (sNaN_var)); \
145 feclearexcept(FE_ALL_EXCEPT); \
146 feenableexcept (FE_ALL_EXCEPT); \
147 if (sigsetjmp(sigfpe_buf, 0)) \
149 printf (#FLOAT " isnan (-sNaN) raised SIGFPE\n"); \
152 check (#FLOAT " isnan (-sNaN)", isnan (minus_sNaN_var)); \
155 feclearexcept(FE_ALL_EXCEPT); \
156 feenableexcept (FE_ALL_EXCEPT); \
157 if (sigsetjmp(sigfpe_buf, 0)) \
159 printf (#FLOAT " isinf (qNaN) raised SIGFPE\n"); \
162 check (#FLOAT " isinf (qNaN)", !isinf (qNaN_var)); \
165 feclearexcept(FE_ALL_EXCEPT); \
166 feenableexcept (FE_ALL_EXCEPT); \
167 if (sigsetjmp(sigfpe_buf, 0)) \
169 printf (#FLOAT " isinf (-qNaN) raised SIGFPE\n"); \
172 check (#FLOAT " isinf (-qNaN)", !isinf (-qNaN_var)); \
175 feclearexcept(FE_ALL_EXCEPT); \
176 feenableexcept (FE_ALL_EXCEPT); \
177 if (sigsetjmp(sigfpe_buf, 0)) \
179 printf (#FLOAT " isinf (sNaN) raised SIGFPE\n"); \
182 check (#FLOAT " isinf (sNaN)", !isinf (sNaN_var)); \
185 feclearexcept(FE_ALL_EXCEPT); \
186 feenableexcept (FE_ALL_EXCEPT); \
187 if (sigsetjmp(sigfpe_buf, 0)) \
189 printf (#FLOAT " isinf (-sNaN) raised SIGFPE\n"); \
192 check (#FLOAT " isinf (-sNaN)", !isinf (minus_sNaN_var)); \
195 feclearexcept(FE_ALL_EXCEPT); \
196 feenableexcept (FE_ALL_EXCEPT); \
197 if (sigsetjmp(sigfpe_buf, 0)) \
199 printf (#FLOAT " isfinite (qNaN) raised SIGFPE\n"); \
202 check (#FLOAT " isfinite (qNaN)", !isfinite (qNaN_var)); \
205 feclearexcept(FE_ALL_EXCEPT); \
206 feenableexcept (FE_ALL_EXCEPT); \
207 if (sigsetjmp(sigfpe_buf, 0)) \
209 printf (#FLOAT " isfinite (-qNaN) raised SIGFPE\n"); \
212 check (#FLOAT " isfinite (-qNaN)", !isfinite (-qNaN_var)); \
215 feclearexcept(FE_ALL_EXCEPT); \
216 feenableexcept (FE_ALL_EXCEPT); \
217 if (sigsetjmp(sigfpe_buf, 0)) \
219 printf (#FLOAT " isfinite (sNaN) raised SIGFPE\n"); \
222 check (#FLOAT " isfinite (sNaN)", !isfinite (sNaN_var)); \
225 feclearexcept(FE_ALL_EXCEPT); \
226 feenableexcept (FE_ALL_EXCEPT); \
227 if (sigsetjmp(sigfpe_buf, 0)) \
229 printf (#FLOAT " isfinite (-sNaN) raised SIGFPE\n"); \
232 check (#FLOAT " isfinite (-sNaN)", !isfinite (minus_sNaN_var)); \
235 feclearexcept(FE_ALL_EXCEPT); \
236 feenableexcept (FE_ALL_EXCEPT); \
237 if (sigsetjmp(sigfpe_buf, 0)) \
239 printf (#FLOAT " isnormal (qNaN) raised SIGFPE\n"); \
242 check (#FLOAT " isnormal (qNaN)", !isnormal (qNaN_var)); \
245 feclearexcept(FE_ALL_EXCEPT); \
246 feenableexcept (FE_ALL_EXCEPT); \
247 if (sigsetjmp(sigfpe_buf, 0)) \
249 printf (#FLOAT " isnormal (-qNaN) raised SIGFPE\n"); \
252 check (#FLOAT " isnormal (-qNaN)", !isnormal (-qNaN_var)); \
255 feclearexcept(FE_ALL_EXCEPT); \
256 feenableexcept (FE_ALL_EXCEPT); \
257 if (sigsetjmp(sigfpe_buf, 0)) \
259 printf (#FLOAT " isnormal (sNaN) isnormal SIGFPE\n"); \
262 check (#FLOAT " isnormal (sNaN)", !isnormal (sNaN_var)); \
265 feclearexcept(FE_ALL_EXCEPT); \
266 feenableexcept (FE_ALL_EXCEPT); \
267 if (sigsetjmp(sigfpe_buf, 0)) \
269 printf (#FLOAT " isnormal (-sNaN) raised SIGFPE\n"); \
272 check (#FLOAT " isnormal (-sNaN)", !isnormal (minus_sNaN_var)); \
275 feclearexcept(FE_ALL_EXCEPT); \
276 feenableexcept (FE_ALL_EXCEPT); \
277 if (sigsetjmp(sigfpe_buf, 0)) \
279 printf (#FLOAT " fpclassify (qNaN) raised SIGFPE\n"); \
282 check (#FLOAT " fpclassify (qNaN)", (fpclassify (qNaN_var)==FP_NAN)); \
285 feclearexcept(FE_ALL_EXCEPT); \
286 feenableexcept (FE_ALL_EXCEPT); \
287 if (sigsetjmp(sigfpe_buf, 0)) \
289 printf (#FLOAT " fpclassify (-qNaN) raised SIGFPE\n"); \
292 check (#FLOAT " fpclassify (-qNaN)", (fpclassify (-qNaN_var)==FP_NAN)); \
295 feclearexcept(FE_ALL_EXCEPT); \
296 feenableexcept (FE_ALL_EXCEPT); \
297 if (sigsetjmp(sigfpe_buf, 0)) \
299 printf (#FLOAT " fpclassify (sNaN) isnormal SIGFPE\n"); \
302 check (#FLOAT " fpclassify (sNaN)", fpclassify (sNaN_var) == FP_NAN); \
305 feclearexcept(FE_ALL_EXCEPT); \
306 feenableexcept (FE_ALL_EXCEPT); \
307 if (sigsetjmp(sigfpe_buf, 0)) \
309 printf (#FLOAT " fpclassify (-sNaN) raised SIGFPE\n"); \
312 check (#FLOAT " fpclassify (-sNaN)", \
313 fpclassify (minus_sNaN_var) == FP_NAN); \
316 fesetenv(&saved_fenv); /* restore saved fenv */ \
317 remove_sigaction_FP(); \
320 TEST_FUNC (float_test, float, f)
321 TEST_FUNC (double_test, double, )
322 #ifndef NO_LONG_DOUBLE
323 TEST_FUNC (ldouble_test, ldouble, l)
331 #ifndef NO_LONG_DOUBLE
338 #define TEST_FUNCTION do_test ()
339 #include "../test-skeleton.c"