1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006,2007,2008,2009
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <gnu/option-groups.h>
25 extern double ____strtod_l_internal (const char *, char **, int, __locale_t);
26 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
27 int, int, __locale_t);
29 /* Configuration part. These macros are defined by `strtold.c',
30 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
31 `long double' and `float' versions of the reader. */
33 # include <math_ldbl_opt.h>
37 # define STRTOF wcstod_l
38 # define __STRTOF __wcstod_l
40 # define STRTOF strtod_l
41 # define __STRTOF __strtod_l
43 # define MPN2FLOAT __mpn_construct_double
44 # define FLOAT_HUGE_VAL HUGE_VAL
45 # define SET_MANTISSA(flt, mant) \
46 do { union ieee754_double u; \
48 if ((mant & 0xfffffffffffffULL) == 0) \
49 mant = 0x8000000000000ULL; \
50 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
51 u.ieee.mantissa1 = (mant) & 0xffffffff; \
55 /* End of configuration part. */
61 #include "../locale/localeinfo.h"
67 /* The gmp headers need some configuration frobs. */
70 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
71 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
72 #include <gmp-mparam.h>
76 #include "fpioconst.h"
82 /* We use this code for the extended locale handling where the
83 function gets as an additional argument the locale which has to be
84 used. To access the values we have to redefine the _NL_CURRENT and
85 _NL_CURRENT_WORD macros. */
87 #define _NL_CURRENT(category, item) \
88 (current->values[_NL_ITEM_INDEX (item)].string)
89 #undef _NL_CURRENT_WORD
90 #define _NL_CURRENT_WORD(category, item) \
91 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
93 #if defined _LIBC || defined HAVE_WCHAR_H
99 # define STRING_TYPE wchar_t
100 # define CHAR_TYPE wint_t
101 # define L_(Ch) L##Ch
102 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
103 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
104 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
105 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
106 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
107 # define STRNCASECMP(S1, S2, N) \
108 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
109 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
111 # define STRING_TYPE char
112 # define CHAR_TYPE char
114 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
115 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
116 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
117 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
118 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
119 # define STRNCASECMP(S1, S2, N) \
120 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
121 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
125 /* Constants we need from float.h; select the set for the FLOAT precision. */
126 #define MANT_DIG PASTE(FLT,_MANT_DIG)
127 #define DIG PASTE(FLT,_DIG)
128 #define MAX_EXP PASTE(FLT,_MAX_EXP)
129 #define MIN_EXP PASTE(FLT,_MIN_EXP)
130 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
131 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
133 /* Extra macros required to get FLT expanded before the pasting. */
134 #define PASTE(a,b) PASTE1(a,b)
135 #define PASTE1(a,b) a##b
137 /* Function to construct a floating point number from an MP integer
138 containing the fraction bits, a base 2 exponent, and a sign flag. */
139 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
141 /* Definitions according to limb size used. */
142 #if BITS_PER_MP_LIMB == 32
143 # define MAX_DIG_PER_LIMB 9
144 # define MAX_FAC_PER_LIMB 1000000000UL
145 #elif BITS_PER_MP_LIMB == 64
146 # define MAX_DIG_PER_LIMB 19
147 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
149 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
152 extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];
155 #define howmany(x,y) (((x)+((y)-1))/(y))
157 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
159 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
160 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
161 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
163 #define RETURN(val,end) \
164 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
165 return val; } while (0)
167 /* Maximum size necessary for mpn integers to hold floating point numbers. */
168 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
170 /* Declare an mpn integer variable that big. */
171 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
172 /* Copy an mpn integer value. */
173 #define MPN_ASSIGN(dst, src) \
174 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
177 /* Return a floating point number of the needed type according to the given
178 multi-precision number after possible rounding. */
180 round_and_return (mp_limb_t *retval, int exponent, int negative,
181 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
183 if (exponent < MIN_EXP - 1)
185 mp_size_t shift = MIN_EXP - 1 - exponent;
187 if (shift > MANT_DIG)
193 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
194 if (shift == MANT_DIG)
195 /* This is a special case to handle the very seldom case where
196 the mantissa will be empty after the shift. */
200 round_limb = retval[RETURN_LIMB_SIZE - 1];
201 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
202 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
203 more_bits |= retval[i] != 0;
204 MPN_ZERO (retval, RETURN_LIMB_SIZE);
206 else if (shift >= BITS_PER_MP_LIMB)
210 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
211 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
212 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
213 more_bits |= retval[i] != 0;
214 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
217 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
218 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
219 shift % BITS_PER_MP_LIMB);
220 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
221 shift / BITS_PER_MP_LIMB);
225 round_limb = retval[0];
226 round_bit = shift - 1;
227 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
229 /* This is a hook for the m68k long double format, where the
230 exponent bias is the same for normalized and denormalized
233 # define DENORM_EXP (MIN_EXP - 2)
235 exponent = DENORM_EXP;
236 __set_errno (ERANGE);
239 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
240 && (more_bits || (retval[0] & 1) != 0
241 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
243 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
245 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
246 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
247 (retval[RETURN_LIMB_SIZE - 1]
248 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
251 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
252 retval[RETURN_LIMB_SIZE - 1]
253 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
255 else if (exponent == DENORM_EXP
256 && (retval[RETURN_LIMB_SIZE - 1]
257 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
259 /* The number was denormalized but now normalized. */
260 exponent = MIN_EXP - 1;
263 if (exponent > MAX_EXP)
264 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
266 return MPN2FLOAT (retval, exponent, negative);
270 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
271 into N. Return the size of the number limbs in NSIZE at the first
272 character od the string that is not part of the integer as the function
273 value. If the EXPONENT is small enough to be taken as an additional
274 factor for the resulting number (see code) multiply by it. */
275 static const STRING_TYPE *
276 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
278 #ifndef USE_WIDE_CHAR
279 , const char *decimal, size_t decimal_len, const char *thousands
284 /* Number of digits for actual limb. */
293 if (cnt == MAX_DIG_PER_LIMB)
303 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
304 cy += __mpn_add_1 (n, n, *nsize, low);
315 /* There might be thousands separators or radix characters in
316 the string. But these all can be ignored because we know the
317 format of the number is correct and we have an exact number
318 of characters to read. */
320 if (*str < L'0' || *str > L'9')
323 if (*str < '0' || *str > '9')
326 if (thousands != NULL && *str == *thousands
327 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
328 if (thousands[inner] != str[inner])
330 thousands[inner] == '\0'; }))
336 low = low * 10 + *str++ - L_('0');
339 while (--digcnt > 0);
341 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
343 low *= _tens_in_limb[*exponent];
344 start = _tens_in_limb[cnt + *exponent];
348 start = _tens_in_limb[cnt];
358 cy = __mpn_mul_1 (n, n, *nsize, start);
359 cy += __mpn_add_1 (n, n, *nsize, low);
368 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
369 with the COUNT most significant bits of LIMB.
371 Tege doesn't like this function so I have to write it here myself. :)
374 __attribute ((always_inline))
375 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
378 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
380 /* Optimize the case of shifting by exactly a word:
381 just copy words, with no actual bit-shifting. */
383 for (i = size - 1; i > 0; --i)
389 (void) __mpn_lshift (ptr, ptr, size, count);
390 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
395 #define INTERNAL(x) INTERNAL1(x)
396 #define INTERNAL1(x) __##x##_internal
397 #ifndef ____STRTOF_INTERNAL
398 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
401 /* This file defines a function to check for correct grouping. */
402 #include "grouping.h"
405 /* Return a floating point number with the value of the given string NPTR.
406 Set *ENDPTR to the character after the last used one. If the number is
407 smaller than the smallest representable number, set `errno' to ERANGE and
408 return 0.0. If the number is too big to be represented, set `errno' to
409 ERANGE and return HUGE_VAL with the appropriate sign. */
411 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
412 const STRING_TYPE *nptr;
413 STRING_TYPE **endptr;
417 int negative; /* The sign of the number. */
418 MPN_VAR (num); /* MP representation of the number. */
419 int exponent; /* Exponent of the number. */
421 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
424 /* When we have to compute fractional digits we form a fraction with a
425 second multi-precision number (and we sometimes need a second for
426 temporary results). */
429 /* Representation for the return value. */
430 mp_limb_t retval[RETURN_LIMB_SIZE];
431 /* Number of bits currently in result value. */
434 /* Running pointer after the last character processed in the string. */
435 const STRING_TYPE *cp, *tp;
436 /* Start of significant part of the number. */
437 const STRING_TYPE *startp, *start_of_digits;
438 /* Points at the character following the integer and fractional digits. */
439 const STRING_TYPE *expp;
440 /* Total number of digit and number of digits in integer part. */
441 int dig_no, int_no, lead_zero;
442 /* Contains the last character read. */
445 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
446 there. So define it ourselves if it remains undefined. */
448 typedef unsigned int wint_t;
450 /* The radix character of the current locale. */
457 /* The thousands character of the current locale. */
459 wchar_t thousands = L'\0';
461 const char *thousands = NULL;
463 /* The numeric grouping specification of the current locale,
464 in the format described in <locale.h>. */
465 const char *grouping;
466 /* Used in several places. */
469 #if __OPTION_EGLIBC_LOCALE_CODE
470 struct locale_data *current = loc->__locales[LC_NUMERIC];
472 if (__builtin_expect (group, 0))
474 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
475 if (*grouping <= 0 || *grouping == CHAR_MAX)
479 /* Figure out the thousands separator character. */
481 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
482 _NL_NUMERIC_THOUSANDS_SEP_WC);
483 if (thousands == L'\0')
486 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
487 if (*thousands == '\0')
498 /* Find the locale's decimal point character. */
500 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
501 assert (decimal != L'\0');
502 # define decimal_len 1
504 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
505 decimal_len = strlen (decimal);
506 assert (decimal_len > 0);
508 #else /* if ! __OPTION_EGLIBC_LOCALE_CODE */
509 /* Hard-code values from the 'C' locale. */
513 # define decimal_len 1
518 #endif /* __OPTION_EGLIBC_LOCALE_CODE */
520 /* Prepare number representation. */
525 /* Parse string to get maximal legal prefix. We need the number of
526 characters of the integer part, the fractional part and the exponent. */
528 /* Ignore leading white space. */
533 /* Get sign of the result. */
539 else if (c == L_('+'))
542 /* Return 0.0 if no legal string is found.
543 No character is used even if a sign was found. */
545 if (c == (wint_t) decimal
546 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
548 /* We accept it. This funny construct is here only to indent
549 the code correctly. */
552 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
553 if (cp[cnt] != decimal[cnt])
555 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
557 /* We accept it. This funny construct is here only to indent
558 the code correctly. */
561 else if (c < L_('0') || c > L_('9'))
563 /* Check for `INF' or `INFINITY'. */
564 CHAR_TYPE lowc = TOLOWER_C (c);
566 if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
568 /* Return +/- infinity. */
570 *endptr = (STRING_TYPE *)
571 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
574 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
577 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
584 /* Match `(n-char-sequence-digit)'. */
587 const STRING_TYPE *startp = cp;
590 while ((*cp >= L_('0') && *cp <= L_('9'))
591 || ({ CHAR_TYPE lo = TOLOWER (*cp);
592 lo >= L_('a') && lo <= L_('z'); })
596 /* The closing brace is missing. Only match the NAN
601 /* This is a system-dependent way to specify the
602 bitmask used for the NaN. We expect it to be
603 a number which is put in the mantissa of the
606 unsigned long long int mant;
608 mant = STRTOULL (startp + 1, &endp, 0);
610 SET_MANTISSA (retval, mant);
612 /* Consume the closing brace. */
618 *endptr = (STRING_TYPE *) cp;
623 /* It is really a text we do not recognize. */
627 /* First look whether we are faced with a hexadecimal number. */
628 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
630 /* Okay, it is a hexa-decimal number. Remember this and skip
631 the characters. BTW: hexadecimal numbers must not be
639 /* Record the start of the digits, in case we will check their grouping. */
640 start_of_digits = startp = cp;
642 /* Ignore leading zeroes. This helps us to avoid useless computations. */
644 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
647 if (__builtin_expect (thousands == NULL, 1))
652 /* We also have the multibyte thousands string. */
657 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
658 if (thousands[cnt] != cp[cnt])
660 if (thousands[cnt] != '\0')
669 /* If no other digit but a '0' is found the result is 0.0.
670 Return current read pointer. */
671 CHAR_TYPE lowc = TOLOWER (c);
672 if (!((c >= L_('0') && c <= L_('9'))
673 || (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
676 c == (wint_t) decimal
678 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
679 if (decimal[cnt] != cp[cnt])
681 decimal[cnt] == '\0'; })
683 /* '0x.' alone is not a valid hexadecimal number.
684 '.' alone is not valid either, but that has been checked
687 || cp != start_of_digits
688 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
689 || ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
690 lo >= L_('a') && lo <= L_('f'); })))
691 || (base == 16 && (cp != start_of_digits
693 || (base != 16 && lowc == L_('e'))))
696 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
699 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
702 /* If TP is at the start of the digits, there was no correctly
703 grouped prefix of the string; so no number found. */
704 RETURN (negative ? -0.0 : 0.0,
705 tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
708 /* Remember first significant digit and read following characters until the
709 decimal point, exponent character or any non-FP number character. */
714 if ((c >= L_('0') && c <= L_('9'))
716 && ({ CHAR_TYPE lo = TOLOWER (c);
717 lo >= L_('a') && lo <= L_('f'); })))
722 if (__builtin_expect ((wint_t) thousands == L'\0', 1)
723 || c != (wint_t) thousands)
724 /* Not a digit or separator: end of the integer part. */
727 if (__builtin_expect (thousands == NULL, 1))
731 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
732 if (thousands[cnt] != cp[cnt])
734 if (thousands[cnt] != '\0')
743 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
745 /* Check the grouping of the digits. */
747 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
750 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
755 /* Less than the entire string was correctly grouped. */
757 if (tp == start_of_digits)
758 /* No valid group of numbers at all: no valid number. */
762 /* The number is validly grouped, but consists
763 only of zeroes. The whole value is zero. */
764 RETURN (negative ? -0.0 : 0.0, tp);
766 /* Recompute DIG_NO so we won't read more digits than
767 are properly grouped. */
770 for (tp = startp; tp < cp; ++tp)
771 if (*tp >= L_('0') && *tp <= L_('9'))
781 /* We have the number of digits in the integer part. Whether these
782 are all or any is really a fractional digit will be decided
785 lead_zero = int_no == 0 ? -1 : 0;
787 /* Read the fractional digits. A special case are the 'american
788 style' numbers like `16.' i.e. with decimal point but without
792 c == (wint_t) decimal
794 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
795 if (decimal[cnt] != cp[cnt])
797 decimal[cnt] == '\0'; })
803 while ((c >= L_('0') && c <= L_('9')) ||
804 (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
805 lo >= L_('a') && lo <= L_('f'); })))
807 if (c != L_('0') && lead_zero == -1)
808 lead_zero = dig_no - int_no;
814 /* Remember start of exponent (if any). */
819 if ((base == 16 && lowc == L_('p'))
820 || (base != 16 && lowc == L_('e')))
822 int exp_negative = 0;
830 else if (c == L_('+'))
833 if (c >= L_('0') && c <= L_('9'))
837 /* Get the exponent limit. */
839 exp_limit = (exp_negative ?
840 -MIN_EXP + MANT_DIG + 4 * int_no :
841 MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
843 exp_limit = (exp_negative ?
844 -MIN_10_EXP + MANT_DIG + int_no :
845 MAX_10_EXP - int_no + lead_zero + 1);
850 exponent += c - L_('0');
852 if (__builtin_expect (exponent > exp_limit, 0))
853 /* The exponent is too large/small to represent a valid
858 /* We have to take care for special situation: a joker
859 might have written "0.0e100000" which is in fact
862 result = negative ? -0.0 : 0.0;
865 /* Overflow or underflow. */
866 __set_errno (ERANGE);
867 result = (exp_negative ? (negative ? -0.0 : 0.0) :
868 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
871 /* Accept all following digits as part of the exponent. */
874 while (*cp >= L_('0') && *cp <= L_('9'));
882 while (c >= L_('0') && c <= L_('9'));
885 exponent = -exponent;
891 /* We don't want to have to work with trailing zeroes after the radix. */
894 while (expp[-1] == L_('0'))
899 assert (dig_no >= int_no);
902 if (dig_no == int_no && dig_no > 0 && exponent < 0)
905 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
908 if (expp[-1] != L_('0'))
914 exponent += base == 16 ? 4 : 1;
916 while (dig_no > 0 && exponent < 0);
920 /* The whole string is parsed. Store the address of the next character. */
922 *endptr = (STRING_TYPE *) cp;
925 return negative ? -0.0 : 0.0;
929 /* Find the decimal point */
931 while (*startp != decimal)
936 if (*startp == decimal[0])
938 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
939 if (decimal[cnt] != startp[cnt])
941 if (decimal[cnt] == '\0')
947 startp += lead_zero + decimal_len;
948 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
952 /* If the BASE is 16 we can use a simpler algorithm. */
955 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
956 4, 4, 4, 4, 4, 4, 4, 4 };
957 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
958 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
961 while (!ISXDIGIT (*startp))
963 while (*startp == L_('0'))
965 if (ISDIGIT (*startp))
966 val = *startp++ - L_('0');
968 val = 10 + TOLOWER (*startp++) - L_('a');
970 /* We cannot have a leading zero. */
973 if (pos + 1 >= 4 || pos + 1 >= bits)
975 /* We don't have to care for wrapping. This is the normal
976 case so we add the first clause in the `if' expression as
977 an optimization. It is a compile-time constant and so does
978 not cost anything. */
979 retval[idx] = val << (pos - bits + 1);
984 retval[idx--] = val >> (bits - pos - 1);
985 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
986 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
989 /* Adjust the exponent for the bits we are shifting in. */
990 exponent += bits - 1 + (int_no - 1) * 4;
992 while (--dig_no > 0 && idx >= 0)
994 if (!ISXDIGIT (*startp))
995 startp += decimal_len;
996 if (ISDIGIT (*startp))
997 val = *startp++ - L_('0');
999 val = 10 + TOLOWER (*startp++) - L_('a');
1003 retval[idx] |= val << (pos - 4 + 1);
1008 retval[idx--] |= val >> (4 - pos - 1);
1009 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1011 return round_and_return (retval, exponent, negative, val,
1012 BITS_PER_MP_LIMB - 1, dig_no > 0);
1015 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1019 /* We ran out of digits. */
1020 MPN_ZERO (retval, idx);
1022 return round_and_return (retval, exponent, negative, 0, 0, 0);
1025 /* Now we have the number of digits in total and the integer digits as well
1026 as the exponent and its sign. We can decide whether the read digits are
1027 really integer digits or belong to the fractional part; i.e. we normalize
1030 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1031 : MIN (dig_no - int_no, exponent));
1036 if (__builtin_expect (int_no + exponent > MAX_10_EXP + 1, 0))
1038 __set_errno (ERANGE);
1039 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1042 if (__builtin_expect (exponent < MIN_10_EXP - (DIG + 1), 0))
1044 __set_errno (ERANGE);
1045 return negative ? -0.0 : 0.0;
1050 /* Read the integer part as a multi-precision number to NUM. */
1051 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1052 #ifndef USE_WIDE_CHAR
1053 , decimal, decimal_len, thousands
1059 /* We now multiply the gained number by the given power of ten. */
1060 mp_limb_t *psrc = num;
1061 mp_limb_t *pdest = den;
1063 const struct mp_power *ttab = &_fpioconst_pow10[0];
1067 if ((exponent & expbit) != 0)
1069 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1073 /* FIXME: not the whole multiplication has to be
1074 done. If we have the needed number of bits we
1075 only need the information whether more non-zero
1077 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1078 cy = __mpn_mul (pdest, psrc, numsize,
1079 &__tens[ttab->arrayoff
1080 + _FPIO_CONST_OFFSET],
1083 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1084 + _FPIO_CONST_OFFSET],
1085 size, psrc, numsize);
1089 (void) SWAP (psrc, pdest);
1094 while (exponent != 0);
1097 memcpy (num, den, numsize * sizeof (mp_limb_t));
1100 /* Determine how many bits of the result we already have. */
1101 count_leading_zeros (bits, num[numsize - 1]);
1102 bits = numsize * BITS_PER_MP_LIMB - bits;
1104 /* Now we know the exponent of the number in base two.
1105 Check it against the maximum possible exponent. */
1106 if (__builtin_expect (bits > MAX_EXP, 0))
1108 __set_errno (ERANGE);
1109 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1112 /* We have already the first BITS bits of the result. Together with
1113 the information whether more non-zero bits follow this is enough
1114 to determine the result. */
1115 if (bits > MANT_DIG)
1118 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1119 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1120 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1122 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1126 memcpy (retval, &num[least_idx],
1127 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1130 for (i = least_idx; i < numsize - 1; ++i)
1131 retval[i - least_idx] = (num[i] >> least_bit)
1133 << (BITS_PER_MP_LIMB - least_bit));
1134 if (i - least_idx < RETURN_LIMB_SIZE)
1135 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1138 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1139 for (i = 0; num[i] == 0; ++i)
1142 return round_and_return (retval, bits - 1, negative,
1143 num[round_idx], round_bit,
1144 int_no < dig_no || i < round_idx);
1147 else if (dig_no == int_no)
1149 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1150 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1152 if (target_bit == is_bit)
1154 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1155 numsize * sizeof (mp_limb_t));
1156 /* FIXME: the following loop can be avoided if we assume a
1157 maximal MANT_DIG value. */
1158 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1160 else if (target_bit > is_bit)
1162 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1163 num, numsize, target_bit - is_bit);
1164 /* FIXME: the following loop can be avoided if we assume a
1165 maximal MANT_DIG value. */
1166 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1171 assert (numsize < RETURN_LIMB_SIZE);
1173 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1174 num, numsize, is_bit - target_bit);
1175 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1176 /* FIXME: the following loop can be avoided if we assume a
1177 maximal MANT_DIG value. */
1178 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1181 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1185 /* Store the bits we already have. */
1186 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1187 #if RETURN_LIMB_SIZE > 1
1188 if (numsize < RETURN_LIMB_SIZE)
1189 # if RETURN_LIMB_SIZE == 2
1190 retval[numsize] = 0;
1192 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1197 /* We have to compute at least some of the fractional digits. */
1199 /* We construct a fraction and the result of the division gives us
1200 the needed digits. The denominator is 1.0 multiplied by the
1201 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1202 123e-6 gives 123 / 1000000. */
1208 mp_limb_t *psrc = den;
1209 mp_limb_t *pdest = num;
1210 const struct mp_power *ttab = &_fpioconst_pow10[0];
1212 assert (dig_no > int_no && exponent <= 0);
1215 /* For the fractional part we need not process too many digits. One
1216 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1218 digits we should have enough bits for the result. The remaining
1219 decimal digits give us the information that more bits are following.
1220 This can be used while rounding. (Two added as a safety margin.) */
1221 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1223 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1229 neg_exp = dig_no - int_no - exponent;
1231 /* Construct the denominator. */
1236 if ((neg_exp & expbit) != 0)
1243 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1244 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1245 densize * sizeof (mp_limb_t));
1249 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1250 + _FPIO_CONST_OFFSET],
1251 ttab->arraysize - _FPIO_CONST_OFFSET,
1253 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1256 (void) SWAP (psrc, pdest);
1262 while (neg_exp != 0);
1265 memcpy (den, num, densize * sizeof (mp_limb_t));
1267 /* Read the fractional digits from the string. */
1268 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1269 #ifndef USE_WIDE_CHAR
1270 , decimal, decimal_len, thousands
1274 /* We now have to shift both numbers so that the highest bit in the
1275 denominator is set. In the same process we copy the numerator to
1276 a high place in the array so that the division constructs the wanted
1277 digits. This is done by a "quasi fix point" number representation.
1279 num: ddddddddddd . 0000000000000000000000
1281 den: ddddddddddd n >= m
1285 count_leading_zeros (cnt, den[densize - 1]);
1289 /* Don't call `mpn_shift' with a count of zero since the specification
1290 does not allow this. */
1291 (void) __mpn_lshift (den, den, densize, cnt);
1292 cy = __mpn_lshift (num, num, numsize, cnt);
1294 num[numsize++] = cy;
1297 /* Now we are ready for the division. But it is not necessary to
1298 do a full multi-precision division because we only need a small
1299 number of bits for the result. So we do not use __mpn_divmod
1300 here but instead do the division here by hand and stop whenever
1301 the needed number of bits is reached. The code itself comes
1302 from the GNU MP Library by Torbj\"orn Granlund. */
1310 mp_limb_t d, n, quot;
1315 assert (numsize == 1 && n < d);
1319 udiv_qrnnd (quot, n, n, 0, d);
1326 cnt = BITS_PER_MP_LIMB; \
1328 count_leading_zeros (cnt, quot); \
1330 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1332 used = MANT_DIG + cnt; \
1333 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1334 bits = MANT_DIG + 1; \
1338 /* Note that we only clear the second element. */ \
1339 /* The conditional is determined at compile time. */ \
1340 if (RETURN_LIMB_SIZE > 1) \
1346 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1347 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1351 used = MANT_DIG - bits; \
1353 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1355 bits += BITS_PER_MP_LIMB
1359 while (bits <= MANT_DIG);
1361 return round_and_return (retval, exponent - 1, negative,
1362 quot, BITS_PER_MP_LIMB - 1 - used,
1363 more_bits || n != 0);
1367 mp_limb_t d0, d1, n0, n1;
1374 if (numsize < densize)
1378 /* The numerator of the number occupies fewer bits than
1379 the denominator but the one limb is bigger than the
1380 high limb of the numerator. */
1387 exponent -= BITS_PER_MP_LIMB;
1390 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1391 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1392 BITS_PER_MP_LIMB, 0);
1395 used = MANT_DIG - bits;
1397 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1399 bits += BITS_PER_MP_LIMB;
1411 while (bits <= MANT_DIG)
1417 /* QUOT should be either 111..111 or 111..110. We need
1418 special treatment of this rare case as normal division
1419 would give overflow. */
1420 quot = ~(mp_limb_t) 0;
1423 if (r < d1) /* Carry in the addition? */
1425 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1428 n1 = d0 - (d0 != 0);
1433 udiv_qrnnd (quot, r, n1, n0, d1);
1434 umul_ppmm (n1, n0, d0, quot);
1438 if (n1 > r || (n1 == r && n0 > 0))
1440 /* The estimated QUOT was too large. */
1443 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1445 if (r >= d1) /* If not carry, test QUOT again. */
1448 sub_ddmmss (n1, n0, r, 0, n1, n0);
1454 return round_and_return (retval, exponent - 1, negative,
1455 quot, BITS_PER_MP_LIMB - 1 - used,
1456 more_bits || n1 != 0 || n0 != 0);
1461 mp_limb_t cy, dX, d1, n0, n1;
1465 dX = den[densize - 1];
1466 d1 = den[densize - 2];
1468 /* The division does not work if the upper limb of the two-limb
1469 numerator is greater than the denominator. */
1470 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1473 if (numsize < densize)
1475 mp_size_t empty = densize - numsize;
1479 exponent -= empty * BITS_PER_MP_LIMB;
1482 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1484 /* We make a difference here because the compiler
1485 cannot optimize the `else' case that good and
1486 this reflects all currently used FLOAT types
1487 and GMP implementations. */
1488 #if RETURN_LIMB_SIZE <= 2
1489 assert (empty == 1);
1490 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1491 BITS_PER_MP_LIMB, 0);
1493 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1494 retval[i] = retval[i - empty];
1501 used = MANT_DIG - bits;
1502 if (used >= BITS_PER_MP_LIMB)
1505 (void) __mpn_lshift (&retval[used
1506 / BITS_PER_MP_LIMB],
1509 - used / BITS_PER_MP_LIMB),
1510 used % BITS_PER_MP_LIMB);
1511 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1515 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1517 bits += empty * BITS_PER_MP_LIMB;
1519 for (i = numsize; i > 0; --i)
1520 num[i + empty] = num[i - 1];
1521 MPN_ZERO (num, empty + 1);
1526 assert (numsize == densize);
1527 for (i = numsize; i > 0; --i)
1528 num[i] = num[i - 1];
1534 while (bits <= MANT_DIG)
1537 /* This might over-estimate QUOT, but it's probably not
1538 worth the extra code here to find out. */
1539 quot = ~(mp_limb_t) 0;
1544 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1545 umul_ppmm (n1, n0, d1, quot);
1547 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1551 if (r < dX) /* I.e. "carry in previous addition?" */
1558 /* Possible optimization: We already have (q * n0) and (1 * n1)
1559 after the calculation of QUOT. Taking advantage of this, we
1560 could make this loop make two iterations less. */
1562 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1564 if (num[densize] != cy)
1566 cy = __mpn_add_n (num, num, den, densize);
1570 n0 = num[densize] = num[densize - 1];
1571 for (i = densize - 1; i > 0; --i)
1572 num[i] = num[i - 1];
1577 for (i = densize; num[i] == 0 && i >= 0; --i)
1579 return round_and_return (retval, exponent - 1, negative,
1580 quot, BITS_PER_MP_LIMB - 1 - used,
1581 more_bits || i >= 0);
1588 #if defined _LIBC && !defined USE_WIDE_CHAR
1589 libc_hidden_def (____STRTOF_INTERNAL)
1592 /* External user entry point. */
1595 #ifdef weak_function
1598 __STRTOF (nptr, endptr, loc)
1599 const STRING_TYPE *nptr;
1600 STRING_TYPE **endptr;
1603 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1606 libc_hidden_def (__STRTOF)
1607 libc_hidden_ver (__STRTOF, STRTOF)
1609 weak_alias (__STRTOF, STRTOF)
1611 #ifdef LONG_DOUBLE_COMPAT
1612 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1613 # ifdef USE_WIDE_CHAR
1614 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1616 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1619 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1620 # ifdef USE_WIDE_CHAR
1621 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1623 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);