]>
Commit | Line | Data |
---|---|---|
57267616 | 1 | /* Test signaling NaNs in issignaling, isnan, isinf, and similar functions. |
bfff8b1b | 2 | Copyright (C) 2008-2017 Free Software Foundation, Inc. |
5f7aead5 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Andreas Jaeger <aj@suse.de>, 2005. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
5f7aead5 | 19 | |
d643bac1 | 20 | #define _GNU_SOURCE 1 |
5f7aead5 | 21 | #include <stdio.h> |
5f7aead5 UD |
22 | #include <math.h> |
23 | #include <float.h> | |
24 | #include <fenv.h> | |
25 | #include <signal.h> | |
26 | #include <setjmp.h> | |
5f7aead5 | 27 | |
5aa4a1a1 TS |
28 | #include <math-tests.h> |
29 | ||
30 | ||
5f7aead5 UD |
31 | static sigjmp_buf sigfpe_buf; |
32 | ||
92b67e8d RM |
33 | static void |
34 | myFPsighandler (int signal) | |
5f7aead5 | 35 | { |
92b67e8d | 36 | siglongjmp (sigfpe_buf, 1); |
5f7aead5 UD |
37 | } |
38 | ||
39 | static int errors = 0; | |
40 | ||
92b67e8d RM |
41 | #define CHECK(testname, expr) \ |
42 | do { \ | |
43 | feclearexcept (FE_ALL_EXCEPT); \ | |
44 | feenableexcept (FE_ALL_EXCEPT); \ | |
45 | if (sigsetjmp (sigfpe_buf, 0)) \ | |
46 | { \ | |
47 | printf ("%s raised SIGFPE\n", testname); \ | |
48 | ++errors; \ | |
49 | } \ | |
50 | else if (!(expr)) \ | |
51 | { \ | |
52 | printf ("Failure: %s\n", testname); \ | |
53 | ++errors; \ | |
54 | } \ | |
55 | } while (0) | |
5f7aead5 | 56 | |
777b0332 | 57 | #define TEST_FUNC(NAME, FLOAT, SUFFIX) \ |
5f7aead5 UD |
58 | static void \ |
59 | NAME (void) \ | |
60 | { \ | |
61 | /* Variables are declared volatile to forbid some compiler \ | |
62 | optimizations. */ \ | |
777b0332 TS |
63 | volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \ |
64 | /* A sNaN is only guaranteed to be representable in variables with */ \ | |
65 | /* static (or thread-local) storage duration. */ \ | |
66 | static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \ | |
6cbec759 | 67 | static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \ |
5f7aead5 UD |
68 | fenv_t saved_fenv; \ |
69 | \ | |
70 | zero_var = 0.0; \ | |
71 | one_var = 1.0; \ | |
777b0332 | 72 | qNaN_var = __builtin_nan ## SUFFIX (""); \ |
5f7aead5 UD |
73 | Inf_var = one_var / zero_var; \ |
74 | \ | |
75 | (void) &zero_var; \ | |
76 | (void) &one_var; \ | |
67e971f1 TS |
77 | (void) &qNaN_var; \ |
78 | (void) &sNaN_var; \ | |
6cbec759 | 79 | (void) &minus_sNaN_var; \ |
5f7aead5 UD |
80 | (void) &Inf_var; \ |
81 | \ | |
92b67e8d RM |
82 | fegetenv (&saved_fenv); \ |
83 | \ | |
84 | CHECK (#FLOAT " issignaling (qNaN)", !issignaling (qNaN_var)); \ | |
85 | CHECK (#FLOAT " issignaling (-qNaN)", !issignaling (-qNaN_var)); \ | |
86 | CHECK (#FLOAT " issignaling (sNaN)", \ | |
87 | SNAN_TESTS (FLOAT) ? issignaling (sNaN_var) : 1); \ | |
88 | CHECK (#FLOAT " issignaling (-sNaN)", \ | |
89 | SNAN_TESTS (FLOAT) ? issignaling (minus_sNaN_var) : 1); \ | |
90 | CHECK (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \ | |
91 | CHECK (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \ | |
92 | CHECK (#FLOAT " isnan (sNaN)", \ | |
93 | SNAN_TESTS (FLOAT) ? isnan (sNaN_var) : 1); \ | |
94 | CHECK (#FLOAT " isnan (-sNaN)", \ | |
95 | SNAN_TESTS (FLOAT) ? isnan (minus_sNaN_var) : 1); \ | |
96 | CHECK (#FLOAT " isinf (qNaN)", !isinf (qNaN_var)); \ | |
97 | CHECK (#FLOAT " isinf (-qNaN)", !isinf (-qNaN_var)); \ | |
98 | CHECK (#FLOAT " isinf (sNaN)", \ | |
99 | SNAN_TESTS (FLOAT) ? !isinf (sNaN_var) : 1); \ | |
100 | CHECK (#FLOAT " isinf (-sNaN)", \ | |
101 | SNAN_TESTS (FLOAT) ? !isinf (minus_sNaN_var) : 1); \ | |
102 | CHECK (#FLOAT " isfinite (qNaN)", !isfinite (qNaN_var)); \ | |
103 | CHECK (#FLOAT " isfinite (-qNaN)", !isfinite (-qNaN_var)); \ | |
104 | CHECK (#FLOAT " isfinite (sNaN)", \ | |
105 | SNAN_TESTS (FLOAT) ? !isfinite (sNaN_var) : 1); \ | |
106 | CHECK (#FLOAT " isfinite (-sNaN)", \ | |
107 | SNAN_TESTS (FLOAT) ? !isfinite (minus_sNaN_var) : 1); \ | |
108 | CHECK (#FLOAT " isnormal (qNaN)", !isnormal (qNaN_var)); \ | |
109 | CHECK (#FLOAT " isnormal (-qNaN)", !isnormal (-qNaN_var)); \ | |
110 | CHECK (#FLOAT " isnormal (sNaN)", \ | |
111 | SNAN_TESTS (FLOAT) ? !isnormal (sNaN_var) : 1); \ | |
112 | CHECK (#FLOAT " isnormal (-sNaN)", \ | |
113 | SNAN_TESTS (FLOAT) ? !isnormal (minus_sNaN_var) : 1); \ | |
114 | CHECK (#FLOAT " fpclassify (qNaN)", (fpclassify (qNaN_var)==FP_NAN)); \ | |
115 | CHECK (#FLOAT " fpclassify (-qNaN)", (fpclassify (-qNaN_var)==FP_NAN)); \ | |
116 | CHECK (#FLOAT " fpclassify (sNaN)", \ | |
117 | SNAN_TESTS (FLOAT) ? fpclassify (sNaN_var) == FP_NAN : 1); \ | |
118 | CHECK (#FLOAT " fpclassify (-sNaN)", \ | |
119 | SNAN_TESTS (FLOAT) ? fpclassify (minus_sNaN_var) == FP_NAN : 1); \ | |
120 | \ | |
121 | fesetenv (&saved_fenv); /* restore saved fenv */ \ | |
122 | } \ | |
5f7aead5 | 123 | |
777b0332 TS |
124 | TEST_FUNC (float_test, float, f) |
125 | TEST_FUNC (double_test, double, ) | |
92b67e8d | 126 | TEST_FUNC (ldouble_test, long double, l) |
5f7aead5 UD |
127 | |
128 | static int | |
129 | do_test (void) | |
130 | { | |
92b67e8d RM |
131 | signal (SIGFPE, &myFPsighandler); |
132 | ||
133 | float_test (); | |
134 | double_test (); | |
92b67e8d | 135 | ldouble_test (); |
5f7aead5 UD |
136 | |
137 | return errors != 0; | |
138 | } | |
139 | ||
140 | #define TEST_FUNCTION do_test () | |
141 | #include "../test-skeleton.c" |