xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Convert string for NaN payload to corresponding NaN. |
| 2 | Copyright (C) 1997-2016 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 | <http://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 | internal_function |
| 33 | FLOAT |
| 34 | STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc) |
| 35 | { |
| 36 | const STRING_TYPE *cp = str; |
| 37 | |
| 38 | while ((*cp >= L_('0') && *cp <= L_('9')) |
| 39 | || (*cp >= L_('A') && *cp <= L_('Z')) |
| 40 | || (*cp >= L_('a') && *cp <= L_('z')) |
| 41 | || *cp == L_('_')) |
| 42 | ++cp; |
| 43 | |
| 44 | FLOAT retval = NAN; |
| 45 | if (*cp != endc) |
| 46 | goto out; |
| 47 | |
| 48 | /* This is a system-dependent way to specify the bitmask used for |
| 49 | the NaN. We expect it to be a number which is put in the |
| 50 | mantissa of the number. */ |
| 51 | STRING_TYPE *endp; |
| 52 | unsigned long long int mant; |
| 53 | |
| 54 | mant = STRTOULL (str, &endp, 0); |
| 55 | if (endp == cp) |
| 56 | SET_MANTISSA (retval, mant); |
| 57 | |
| 58 | out: |
| 59 | if (endptr != NULL) |
| 60 | *endptr = (STRING_TYPE *) cp; |
| 61 | return retval; |
| 62 | } |
| 63 | libc_hidden_def (STRTOD_NAN) |