1 /* Copyright (C) 1999-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
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.
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.
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/>. */
23 #include <math-tests.h>
26 static int errors
= 0;
30 check (const char *testname
, int result
)
33 printf ("Failure: %s\n", testname
);
38 #define TEST_FUNC(NAME, FLOAT, SUFFIX, EPSILON, HUGEVAL) \
42 /* Variables are declared volatile to forbid some compiler \
44 volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
45 /* A sNaN is only guaranteed to be representable in variables with */ \
46 /* static (or thread-local) storage duration. */ \
47 static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
48 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
53 qNaN_var = __builtin_nan ## SUFFIX (""); \
54 Inf_var = one_var / zero_var; \
60 (void) &minus_sNaN_var; \
64 check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1); \
65 check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1); \
66 check (#FLOAT " !isinf (1)", !(isinf (one_var))); \
67 check (#FLOAT " !isinf (qNaN)", !(isinf (qNaN_var))); \
68 if (SNAN_TESTS (FLOAT)) \
69 check (#FLOAT " !isinf (sNaN)", !(isinf (sNaN_var))); \
71 check (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
72 if (SNAN_TESTS (FLOAT)) \
73 check (#FLOAT " isnan (sNaN)", isnan (sNaN_var)); \
74 check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
75 if (SNAN_TESTS (FLOAT)) \
76 check (#FLOAT " isnan (-sNaN)", isnan (minus_sNaN_var)); \
77 check (#FLOAT " !isnan (1)", !(isnan (one_var))); \
78 check (#FLOAT " !isnan (inf)", !(isnan (Inf_var))); \
80 check (#FLOAT " !issignaling (qNaN)", !(issignaling (qNaN_var))); \
81 if (SNAN_TESTS (FLOAT)) \
82 check (#FLOAT " issignaling (sNaN)", issignaling (sNaN_var)); \
83 check (#FLOAT " !issignaling (-qNaN)", !(issignaling (-qNaN_var))); \
84 if (SNAN_TESTS (FLOAT)) \
85 check (#FLOAT " issignaling (-sNaN)", issignaling (minus_sNaN_var)); \
86 check (#FLOAT " !issignaling (1)", !(issignaling (one_var))); \
87 check (#FLOAT " !issignaling (inf)", !(issignaling (Inf_var))); \
89 check (#FLOAT " inf == inf", Inf_var == Inf_var); \
90 check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
91 check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
92 check (#FLOAT " qNaN != qNaN", qNaN_var != qNaN_var); \
93 if (SNAN_TESTS (FLOAT)) \
94 check (#FLOAT " sNaN != sNaN", sNaN_var != sNaN_var); \
95 if (SNAN_TESTS (FLOAT)) \
96 check (#FLOAT " qNaN != sNaN", qNaN_var != sNaN_var); \
97 if (SNAN_TESTS (FLOAT)) \
98 check (#FLOAT " -sNaN != -sNaN", minus_sNaN_var != minus_sNaN_var); \
99 if (SNAN_TESTS (FLOAT)) \
100 check (#FLOAT " sNaN != -sNaN", sNaN_var != minus_sNaN_var); \
101 if (SNAN_TESTS (FLOAT)) \
102 check (#FLOAT " qNaN != -sNaN", qNaN_var != minus_sNaN_var); \
105 the same tests but this time with NAN from <bits/nan.h> \
106 NAN is a double const \
108 check (#FLOAT " isnan (NAN)", isnan (NAN)); \
109 check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
110 check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
111 check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
112 check (#FLOAT " NAN != NAN", NAN != NAN); \
113 check (#FLOAT " NAN != qNaN", NAN != qNaN_var); \
114 if (SNAN_TESTS (FLOAT)) \
115 check (#FLOAT " NAN != sNaN", NAN != sNaN_var); \
116 if (SNAN_TESTS (FLOAT)) \
117 check (#FLOAT " NAN != -sNaN", NAN != minus_sNaN_var); \
120 And again with the value returned by the `nan' function. \
122 check (#FLOAT " isnan (nan (\"\"))", isnan (nan ## SUFFIX (""))); \
123 check (#FLOAT " isnan (-nan (\"\"))", isnan (-nan ## SUFFIX (""))); \
124 check (#FLOAT " !isinf (nan (\"\"))", !(isinf (nan ## SUFFIX ("")))); \
125 check (#FLOAT " !isinf (-nan (\"\"))", !(isinf (-nan ## SUFFIX ("")))); \
126 check (#FLOAT " nan (\"\") != nan (\"\")", \
127 nan ## SUFFIX ("") != nan ## SUFFIX ("")); \
128 check (#FLOAT " nan (\"\") != qNaN", nan ## SUFFIX ("") != qNaN_var); \
129 if (SNAN_TESTS (FLOAT)) \
130 check (#FLOAT " nan (\"\") != sNaN", nan ## SUFFIX ("") != sNaN_var); \
131 if (SNAN_TESTS (FLOAT)) \
132 check (#FLOAT " nan (\"\") != -sNaN", \
133 nan ## SUFFIX ("") != minus_sNaN_var); \
135 /* test if EPSILON is ok */ \
138 check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
142 check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
144 /* test if HUGE_VALx is ok */ \
146 check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
148 check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
151 #define TEST_CONVERT(NAME, FLOAT, DOUBLE, SUFFIX) \
155 volatile DOUBLE Inf_var, qNaN_var, zero_var, one_var; \
156 /* A sNaN is only guaranteed to be representable in variables with */ \
157 /* static (or thread-local) storage duration. */ \
158 static volatile DOUBLE sNaN_var = __builtin_nans ## SUFFIX (""); \
163 qNaN_var = __builtin_nan ## SUFFIX (""); \
164 Inf_var = one_var / zero_var; \
170 x1 = (FLOAT) qNaN_var; \
171 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, isnan", isnan (x1)); \
172 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, !issignaling", \
173 !issignaling (x1)); \
174 if (SNAN_TESTS (FLOAT)) \
176 x1 = (FLOAT) sNaN_var; \
177 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, isnan", isnan (x1)); \
178 if (SNAN_TESTS_TYPE_CAST) \
180 /* Upon type conversion, a sNaN is converted into a qNaN plus an */ \
181 /* INVALID exception (not checked here). */ \
182 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, !issignaling", \
183 !issignaling (x1)); \
186 x2 = (FLOAT) Inf_var; \
187 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") Inf", isinf (x2) != 0); \
190 TEST_FUNC (float_test
, float, f
, FLT_EPSILON
, HUGE_VALF
)
191 TEST_FUNC (double_test
, double, , DBL_EPSILON
, HUGE_VAL
)
192 TEST_CONVERT (convert_dfsf_test
, float, double, )
193 TEST_CONVERT (convert_sfdf_test
, double, float, f
)
194 #if LDBL_MANT_DIG > DBL_MANT_DIG
195 TEST_FUNC (ldouble_test
, long double, l
, LDBL_EPSILON
, HUGE_VALL
)
196 TEST_CONVERT (convert_tfsf_test
, float, long double, l
)
197 TEST_CONVERT (convert_sftf_test
, long double, float, f
)
198 TEST_CONVERT (convert_tfdf_test
, double, long double, l
)
199 TEST_CONVERT (convert_dftf_test
, long double, double, )
210 #if LDBL_MANT_DIG > DBL_MANT_DIG
221 #define TEST_FUNCTION do_test ()
222 #include "../test-skeleton.c"