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