1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/* Test feature wrapper for formatted real input.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <tgmath.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <support/support.h>
/* Reference data is a case-inensitive signed datum, which is either a
hexadecimal floating constant or a literal representing infinity or
NaN data in any of the forms accepted by 'strtod' family functions,
whose binary representation is to match against byte-wise.
We need to be careful with parsing reference data in the handling of
the sign as with the IBM long double binary format unary negation
of a positive value whose lower-magnitude part is zero produces a
bit pattern that is different from one produced by initialization or
conversions made by functions such as 'scanf' or 'strtod' from the
complementing negative value. The difference is in the sign of zero
held in the lower-magnitude part and therefore the bit patterns are
arithmetically equivalent, but they do not match byte-wise.
Therefore we set the sign with the initial value of the significand,
either -0.0 or 0.0, which sets the sign of both parts according to
what 'scanf' does, and never negate it afterwards.
Additionally the Intel long double binary format uses only 80 bits
out of 96 that the data type occupies. Therefore preinitialize the
reference value with the same 0xa5 bit pattern that the value under
test has been so that the byte-wise comparison matches as well for
the unused parts of the two data pieces.
We use 'ldexp' to assemble the significand with the exponent, which
does not compromise verification, because internally this exercises
a code path different from one used by 'scanf' family functions for
this purpose. Specifically 'ldexp' uses 'scalbn', whereas 'scanf'
defers to 'strtod' which uses '__mpn_construct_double'. Analogously
for the remaining floating-point data types. */
#define nan(v, x) \
_Generic((v), float: nanf, double: nan, long double: nanl) (x)
#define pointer_to_value(val) (&(val))
#define initialize_value(val) \
memset (&val, 0xa5, sizeof (val))
#define compare_real(x, y) \
(memcmp (&(x), &(y), sizeof (y)) == 0)
#define verify_input(f, val, count, errp) \
({ \
__label__ out; \
bool match = true; \
int err = 0; \
type_t v; \
\
initialize_value (v); \
/* Make sure it's been committed. */ \
__asm__ ("" : : : "memory"); \
v = read_real (&err); \
if (err < 0) \
goto out; \
match = compare_real (val, v); \
\
out: \
if (err || !match) \
{ \
union \
{ \
type_t v; \
unsigned char x[sizeof (type_t)]; \
} \
uv = { .v = v }, ui = { .v = val }; \
\
printf ("error: %s:%d: input buffer: `", __FILE__, __LINE__); \
for (size_t j = 0; j < sizeof (ui.x); j++) \
printf ("%02hhx", ui.x[j]); \
printf ("'\n"); \
printf ("error: %s:%d: value buffer: `", __FILE__, __LINE__); \
for (size_t j = 0; j < sizeof (uv.x); j++) \
printf ("%02hhx", uv.x[j]); \
printf ("'\n"); \
} \
\
*errp = err; \
match; \
})
#define read_real(errp) \
({ \
__label__ out; \
bool m = false; \
int err = 0; \
type_t v; \
int ch; \
\
ch = read_input (); \
if (ch == '-' || ch == '+') \
{ \
m = ch == '-'; \
ch = read_input (); \
} \
\
switch (ch) \
{ \
case '0': \
break; \
case 'I': \
case 'i': \
{ \
static const char unf[] = { 'N', 'F' }; \
static const char lnf[] = { 'n', 'f' }; \
size_t i; \
\
for (i = 0; i < sizeof (unf); i++) \
{ \
ch = read_input (); \
if (ch != unf[i] && ch != lnf[i]) \
{ \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
} \
\
ch = read_input (); \
if (ch == ':') \
{ \
v = m ? -INFINITY : +INFINITY; \
goto out; \
} \
\
static const char uinity[] = { 'I', 'N', 'I', 'T', 'Y' }; \
static const char linity[] = { 'i', 'n', 'i', 't', 'y' }; \
\
for (i = 0; i < sizeof (uinity); i++) \
{ \
if (ch != uinity[i] && ch != linity[i]) \
{ \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
ch = read_input (); \
} \
if (ch == ':') \
{ \
v = m ? -INFINITY : +INFINITY; \
goto out; \
} \
} \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
\
case 'N': \
case 'n': \
{ \
static const char uan[] = { 'A', 'N' }; \
static const char lan[] = { 'a', 'n' }; \
size_t i; \
\
for (i = 0; i < sizeof (uan); i++) \
{ \
ch = read_input (); \
if (ch != uan[i] && ch != lan[i]) \
{ \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
} \
\
ch = read_input (); \
if (ch == ':') \
{ \
v = m ? -nan (v, ".") : nan (v, "."); \
goto out; \
} \
\
size_t seq_size = 0; \
char *seq = NULL; \
i = 0; \
if (ch == '(') \
while (1) \
{ \
ch = read_input (); \
if (ch == ')') \
break; \
if (ch != '_' && !isdigit (ch) \
&& !(ch >= 'A' && ch <= 'Z') \
&& !(ch >= 'a' && ch <= 'z')) \
{ \
free (seq); \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
if (i == seq_size) \
{ \
seq_size += SIZE_CHUNK; \
seq = xrealloc (seq, seq_size); \
} \
seq[i++] = ch; \
} \
seq[i] = '\0'; \
\
ch = read_input (); \
if (ch == ':') \
{ \
v = m ? -nan (v, seq) : nan (v, seq); \
free (seq); \
goto out; \
} \
free (seq); \
} \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
\
default: \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
\
ch = read_input (); \
if (ch != 'X' && ch != 'x') \
{ \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
\
type_t f = m ? -1.0 : 1.0; \
v = m ? -0.0 : 0.0; \
int i = 0; \
do \
{ \
int d = 0; \
\
ch = read_input (); \
\
if (i == 1) \
switch (ch) \
{ \
case '.': \
i++; \
continue; \
\
case ':': \
case 'P': \
case 'p': \
break; \
\
default: \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
\
switch (ch) \
{ \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
d = ch - '0'; \
break; \
\
case 'A': \
case 'B': \
case 'C': \
case 'D': \
case 'E': \
case 'F': \
d = ch - 'A' + 10; \
break; \
\
case 'a': \
case 'b': \
case 'c': \
case 'd': \
case 'e': \
case 'f': \
d = ch - 'a' + 10; \
break; \
\
case ':': \
case 'P': \
case 'p': \
if (i == 0) \
{ \
err = INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
break; \
\
default: \
err = ch < 0 ? ch : INPUT_FORMAT; \
v = NAN; \
goto out; \
} \
\
v += f * d; \
f /= 16.0l; \
i++; \
} \
while (ch != ':' && ch != 'P' && ch != 'p'); \
\
long long exp = 0; \
if (ch == 'P' || ch == 'p') \
{ \
exp = read_integer (&err); \
if (err) \
{ \
v = NAN; \
goto out; \
} \
} \
\
errno = 0; \
v = ldexp (v, exp); \
if ((v == HUGE_VALL || v == -HUGE_VALL) && errno != 0) \
{ \
err = INPUT_OVERFLOW; \
v = NAN; \
goto out; \
} \
\
out: \
*errp = err; \
v; \
})
|