Update copyright dates not handled by scripts/update-copyrights.
[glibc.git] / math / test-snan.c
blobffc8bf4bcaa54bb0698b887fc53c387171258b00
1 /* Test signaling NaNs in issignaling, isnan, isinf, and similar functions.
2 Copyright (C) 2008-2017 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/>. */
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <math.h>
23 #include <float.h>
24 #include <fenv.h>
25 #include <signal.h>
26 #include <setjmp.h>
28 #include <math-tests.h>
31 static sigjmp_buf sigfpe_buf;
33 static void
34 myFPsighandler (int signal)
36 siglongjmp (sigfpe_buf, 1);
39 static int errors = 0;
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)
57 #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
58 static void \
59 NAME (void) \
60 { \
61 /* Variables are declared volatile to forbid some compiler \
62 optimizations. */ \
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 (""); \
67 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
68 fenv_t saved_fenv; \
70 zero_var = 0.0; \
71 one_var = 1.0; \
72 qNaN_var = __builtin_nan ## SUFFIX (""); \
73 Inf_var = one_var / zero_var; \
75 (void) &zero_var; \
76 (void) &one_var; \
77 (void) &qNaN_var; \
78 (void) &sNaN_var; \
79 (void) &minus_sNaN_var; \
80 (void) &Inf_var; \
82 fegetenv (&saved_fenv); \
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); \
121 fesetenv (&saved_fenv); /* restore saved fenv */ \
124 TEST_FUNC (float_test, float, f)
125 TEST_FUNC (double_test, double, )
126 #ifndef NO_LONG_DOUBLE
127 TEST_FUNC (ldouble_test, long double, l)
128 #endif
130 static int
131 do_test (void)
133 signal (SIGFPE, &myFPsighandler);
135 float_test ();
136 double_test ();
137 #ifndef NO_LONG_DOUBLE
138 ldouble_test ();
139 #endif
141 return errors != 0;
144 #define TEST_FUNCTION do_test ()
145 #include "../test-skeleton.c"