1 /* Convert string for NaN payload to corresponding NaN.
2 Copyright (C) 1997-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 #include <ieee754.h>
20 #include <locale.h>
21 #include <math.h>
22 #include <stdlib.h>
23 #include <wchar.h>
24
25
26 /* If STR starts with an optional n-char-sequence as defined by ISO C
27 (a sequence of ASCII letters, digits and underscores), followed by
28 ENDC, return a NaN whose payload is set based on STR. Otherwise,
29 return a default NAN. If ENDPTR is not NULL, set *ENDPTR to point
30 to the character after the initial n-char-sequence. */
31
32 FLOAT
STRTOD_NAN(const STRING_TYPE * str,STRING_TYPE ** endptr,STRING_TYPE endc)33 STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc)
34 {
35 const STRING_TYPE *cp = str;
36
37 while ((*cp >= L_('0') && *cp <= L_('9'))
38 || (*cp >= L_('A') && *cp <= L_('Z'))
39 || (*cp >= L_('a') && *cp <= L_('z'))
40 || *cp == L_('_'))
41 ++cp;
42
43 FLOAT retval = NAN;
44 if (*cp != endc)
45 goto out;
46
47 /* This is a system-dependent way to specify the bitmask used for
48 the NaN. We expect it to be a number which is put in the
49 mantissa of the number. */
50 STRING_TYPE *endp;
51 unsigned long long int mant;
52
53 mant = STRTOULL (str, &endp, 0);
54 if (endp == cp)
55 SET_NAN_PAYLOAD (retval, mant);
56
57 out:
58 if (endptr != NULL)
59 *endptr = (STRING_TYPE *) cp;
60 return retval;
61 }
62 libc_hidden_def (STRTOD_NAN)
63