Debian Codegraph @

gnulib-tests/snan.h

raw
1 /* Macros for signalling not-a-number.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #ifndef _SNAN_H
18 #define _SNAN_H
19
20 #include <float.h>
21 #include <limits.h>
22 #include <math.h>
23
24 #include "nan.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30
31 /* The bit that distinguishes a quiet NaN from a signalling NaN is, according to
32 <https://en.wikipedia.org/wiki/NaN#Encoding>, the most significant bit of the
33 mantissa field.
34 According to <https://en.wikipedia.org/wiki/IEEE_754#Formats>, this is the
35 next bit, right below the bit 0 of the exponent.
36 This bit is
37 * == 0 to indicate a quiet NaN or Infinity,
38 == 1 to indicate a signalling NaN,
39 on these CPUs: hppa, mips (*), sh4.
40 * == 1 to indicate a quiet NaN,
41 == 0 to indicate a signalling NaN or Infinity,
42 on all other CPUs.
43 On these platforms, additionally a signalling NaN must have some other
44 mantissa bit == 1, because when all exponent bits are == 1 and all
45 mantissa bits are == 0, the number denotes ±Infinity.
46 This NaN encoding is specified by IEEE 754-2008 § 6.2.1.
47
48 (*) On mips CPUs, it depends on the CPU model. The classical behaviour is
49 as indicated above. On some newer models, it's like on the other CPUs.
50 On some (but not all!) models this meta-info can be determined from two
51 special CPU registers: If the "Floating Point Implementation Register" (fir)
52 bit 23, also known as Has2008 bit, is set, the "Floating Point Control and
53 Status Register" (fcsr) bit 18, also known as the NAN2008 bit, has the value
54 - 0 for the classical behaviour,
55 - 1 for like on the other CPUs.
56 Both of these bits are read-only.
57 This module has determined the behaviour at configure time and defines the
58 C macros MIPS_NAN2008_FLOAT, MIPS_NAN2008_DOUBLE, MIPS_NAN2008_LONG_DOUBLE
59 accordingly. */
60
61
62 /* 'float' = IEEE 754 single-precision
63 <https://en.wikipedia.org/wiki/Single-precision_floating-point_format> */
64
65 #define NWORDS \
66 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
67 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
68
69 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
70
71 # define HAVE_SNANF 1
72
73 _GL_UNUSED static memory_float
74 construct_memory_SNaNf (float quiet_value)
75 {
76 memory_float m;
77 m.value = quiet_value;
78 /* Turn the quiet NaN into a signalling NaN. */
79 #if FLT_EXPBIT0_BIT > 0
80 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
81 #else
82 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
83 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
84 #endif
85 /* Set some arbitrary mantissa bit. */
86 if (FLT_EXPBIT0_WORD < NWORDS / 2) /* NWORDS > 1 and big endian */
87 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
88 else /* NWORDS == 1 or little endian */
89 m.word[0] |= (unsigned int) 1;
90 return m;
91 }
92
93 /* Returns a signalling 'float' NaN in memory. */
94 _GL_UNUSED static memory_float
95 memory_SNaNf ()
96 {
97 return construct_memory_SNaNf (NaNf ());
98 }
99
100 _GL_UNUSED static float
101 construct_SNaNf (float quiet_value)
102 {
103 return construct_memory_SNaNf (quiet_value).value;
104 }
105
106 /* Returns a signalling 'float' NaN.
107 Note: On 32-bit x86 processors, as well as on x86_64 processors with
108 CC="gcc -mfpmath=387", this function may return a quiet NaN instead.
109 Use memory_SNaNf() if you need to avoid this. See
110 <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
111 for details. */
112 _GL_UNUSED static float
113 SNaNf ()
114 {
115 return memory_SNaNf ().value;
116 }
117
118 #endif
119
120 #undef NWORDS
121
122
123 /* 'double' = IEEE 754 double-precision
124 <https://en.wikipedia.org/wiki/Double-precision_floating-point_format> */
125
126 #define NWORDS \
127 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
128 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
129
130 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
131
132 # define HAVE_SNAND 1
133
134 _GL_UNUSED static memory_double
135 construct_memory_SNaNd (double quiet_value)
136 {
137 memory_double m;
138 m.value = quiet_value;
139 /* Turn the quiet NaN into a signalling NaN. */
140 #if DBL_EXPBIT0_BIT > 0
141 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
142 #else
143 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
144 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
145 #endif
146 /* Set some arbitrary mantissa bit. */
147 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
148 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
149 return m;
150 }
151
152 /* Returns a signalling 'double' NaN in memory. */
153 _GL_UNUSED static memory_double
154 memory_SNaNd ()
155 {
156 return construct_memory_SNaNd (NaNd ());
157 }
158
159 _GL_UNUSED static double
160 construct_SNaNd (double quiet_value)
161 {
162 return construct_memory_SNaNd (quiet_value).value;
163 }
164
165 /* Returns a signalling 'double' NaN.
166 Note: On 32-bit x86 processors, as well as on x86_64 processors with
167 CC="gcc -mfpmath=387", this function may return a quiet NaN instead.
168 Use memory_SNaNf() if you need to avoid this. See
169 <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
170 for details. */
171 _GL_UNUSED static double
172 SNaNd ()
173 {
174 return memory_SNaNd ().value;
175 }
176
177 #endif
178
179 #undef NWORDS
180
181
182 /* 'long double' =
183 * if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE:
184 IEEE 754 double-precision
185 <https://en.wikipedia.org/wiki/Double-precision_floating-point_format>
186 * Otherwise:
187 - On i386, x86_64, ia64:
188 80-bits extended-precision
189 <https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format>
190 - On alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64:
191 IEEE 754 quadruple-precision
192 <https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#IEEE_754_quadruple-precision_binary_floating-point_format:_binary128>
193 - On powerpc, powerpc64, powerpc64le:
194 2x64-bits double-double
195 <https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic>
196 - On m68k:
197 80-bits extended-precision, padded to 96 bits, with non-IEEE exponent
198 */
199
200 #define NWORDS \
201 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
202 typedef union { long double value; unsigned int word[NWORDS]; }
203 memory_long_double;
204
205 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
206
207 # define HAVE_SNANL 1
208
209 _GL_UNUSED static memory_long_double
210 construct_memory_SNaNl (long double quiet_value)
211 {
212 memory_long_double m;
213 m.value = quiet_value;
214 #if defined __powerpc__ && LDBL_MANT_DIG == 106
215 /* This is PowerPC "double double", a pair of two doubles. Inf and NaN are
216 represented as the corresponding 64-bit IEEE values in the first double;
217 the second is ignored. Manipulate only the first double. */
218 #define HNWORDS \
219 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
220 #else
221 #define HNWORDS NWORDS
222 #endif
223 /* Turn the quiet NaN into a signalling NaN. */
224 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
225 /* In this representation, the leading 1 of the mantissa is explicitly
226 stored. */
227 #if LDBL_EXPBIT0_BIT > 1
228 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 2);
229 #else
230 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < HNWORDS / 2 ? 1 : - 1)]
231 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 2);
232 #endif
233 #elif (defined __m68k__ && LDBL_MANT_DIG == 64) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
234 /* In this representation, there is a 16-bits gap between the exponent and
235 the mantissa, and the leading 1 of the mantissa is explicitly stored. */
236 m.word[LDBL_EXPBIT0_WORD + 1]
237 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 2);
238 #else
239 /* In this representation, the leading 1 of the mantissa is implicit. */
240 #if LDBL_EXPBIT0_BIT > 0
241 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
242 #else
243 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < HNWORDS / 2 ? 1 : - 1)]
244 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
245 #endif
246 #endif
247 /* Set some arbitrary mantissa bit. */
248 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < HNWORDS / 2 ? 1 : - 1)]
249 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
250 #undef HNWORDS
251 return m;
252 }
253
254 /* Returns a signalling 'long double' NaN in memory. */
255 _GL_UNUSED static memory_long_double
256 memory_SNaNl ()
257 {
258 return construct_memory_SNaNl (NaNl ());
259 }
260
261 _GL_UNUSED static long double
262 construct_SNaNl (long double quiet_value)
263 {
264 return construct_memory_SNaNl (quiet_value).value;
265 }
266
267 /* Returns a signalling 'long double' NaN.
268 Note: On 32-bit x86 processors, as well as on x86_64 processors with
269 CC="gcc -mfpmath=387", if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE is 1, this
270 function may return a quiet NaN instead. Use memory_SNaNf() if you
271 need to avoid this. See
272 <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
273 for details. */
274 _GL_UNUSED static long double
275 SNaNl ()
276 {
277 return memory_SNaNl ().value;
278 }
279
280 #endif
281
282 #undef NWORDS
283
284
285 #ifdef __cplusplus
286 }
287 #endif
288
289 #endif /* _SNAN_H */