1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 extern double ____strtod_l_internal (const char *, char **, int, __locale_t
);
24 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
25 int, int, __locale_t
);
27 /* Configuration part. These macros are defined by `strtold.c',
28 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
29 `long double' and `float' versions of the reader. */
31 # include <math_ldbl_opt.h>
35 # define STRTOF wcstod_l
36 # define __STRTOF __wcstod_l
38 # define STRTOF strtod_l
39 # define __STRTOF __strtod_l
41 # define MPN2FLOAT __mpn_construct_double
42 # define FLOAT_HUGE_VAL HUGE_VAL
43 # define SET_MANTISSA(flt, mant) \
44 do { union ieee754_double u; \
46 if ((mant & 0xfffffffffffffULL) == 0) \
47 mant = 0x8000000000000ULL; \
48 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
49 u.ieee.mantissa1 = (mant) & 0xffffffff; \
53 /* End of configuration part. */
59 #include "../locale/localeinfo.h"
65 /* The gmp headers need some configuration frobs. */
68 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
69 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
70 #include <gmp-mparam.h>
74 #include "fpioconst.h"
80 /* We use this code for the extended locale handling where the
81 function gets as an additional argument the locale which has to be
82 used. To access the values we have to redefine the _NL_CURRENT and
83 _NL_CURRENT_WORD macros. */
85 #define _NL_CURRENT(category, item) \
86 (current->values[_NL_ITEM_INDEX (item)].string)
87 #undef _NL_CURRENT_WORD
88 #define _NL_CURRENT_WORD(category, item) \
89 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
91 #if defined _LIBC || defined HAVE_WCHAR_H
97 # define STRING_TYPE wchar_t
98 # define CHAR_TYPE wint_t
100 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
101 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
102 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
103 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
104 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
105 # define STRNCASECMP(S1, S2, N) \
106 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
107 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
109 # define STRING_TYPE char
110 # define CHAR_TYPE char
112 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
113 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
114 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
115 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
116 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
117 # define STRNCASECMP(S1, S2, N) \
118 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
119 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
123 /* Constants we need from float.h; select the set for the FLOAT precision. */
124 #define MANT_DIG PASTE(FLT,_MANT_DIG)
125 #define DIG PASTE(FLT,_DIG)
126 #define MAX_EXP PASTE(FLT,_MAX_EXP)
127 #define MIN_EXP PASTE(FLT,_MIN_EXP)
128 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
129 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
131 /* Extra macros required to get FLT expanded before the pasting. */
132 #define PASTE(a,b) PASTE1(a,b)
133 #define PASTE1(a,b) a##b
135 /* Function to construct a floating point number from an MP integer
136 containing the fraction bits, a base 2 exponent, and a sign flag. */
137 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
139 /* Definitions according to limb size used. */
140 #if BITS_PER_MP_LIMB == 32
141 # define MAX_DIG_PER_LIMB 9
142 # define MAX_FAC_PER_LIMB 1000000000UL
143 #elif BITS_PER_MP_LIMB == 64
144 # define MAX_DIG_PER_LIMB 19
145 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
147 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
151 /* Local data structure. */
152 static const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1] =
154 1000, 10000, 100000L,
155 1000000L, 10000000L, 100000000L,
157 #if BITS_PER_MP_LIMB > 32
158 , 10000000000ULL, 100000000000ULL,
159 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
160 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
161 1000000000000000000ULL, 10000000000000000000ULL
163 #if BITS_PER_MP_LIMB > 64
164 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
169 #define howmany(x,y) (((x)+((y)-1))/(y))
171 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
173 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
174 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
175 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
177 #define RETURN(val,end) \
178 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
179 return val; } while (0)
181 /* Maximum size necessary for mpn integers to hold floating point numbers. */
182 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
184 /* Declare an mpn integer variable that big. */
185 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
186 /* Copy an mpn integer value. */
187 #define MPN_ASSIGN(dst, src) \
188 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
191 /* Return a floating point number of the needed type according to the given
192 multi-precision number after possible rounding. */
194 round_and_return (mp_limb_t
*retval
, int exponent
, int negative
,
195 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
197 if (exponent
< MIN_EXP
- 1)
199 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
201 if (shift
> MANT_DIG
)
207 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
208 if (shift
== MANT_DIG
)
209 /* This is a special case to handle the very seldom case where
210 the mantissa will be empty after the shift. */
214 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
215 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
216 for (i
= 0; i
< RETURN_LIMB_SIZE
; ++i
)
217 more_bits
|= retval
[i
] != 0;
218 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
220 else if (shift
>= BITS_PER_MP_LIMB
)
224 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
225 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
226 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
227 more_bits
|= retval
[i
] != 0;
228 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
231 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
232 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
233 shift
% BITS_PER_MP_LIMB
);
234 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
235 shift
/ BITS_PER_MP_LIMB
);
239 round_limb
= retval
[0];
240 round_bit
= shift
- 1;
241 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
243 /* This is a hook for the m68k long double format, where the
244 exponent bias is the same for normalized and denormalized
247 # define DENORM_EXP (MIN_EXP - 2)
249 exponent
= DENORM_EXP
;
252 if ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
253 && (more_bits
|| (retval
[0] & 1) != 0
254 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
256 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
258 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
259 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
260 (retval
[RETURN_LIMB_SIZE
- 1]
261 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
264 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
265 retval
[RETURN_LIMB_SIZE
- 1]
266 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
268 else if (exponent
== DENORM_EXP
269 && (retval
[RETURN_LIMB_SIZE
- 1]
270 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
272 /* The number was denormalized but now normalized. */
273 exponent
= MIN_EXP
- 1;
276 if (exponent
> MAX_EXP
)
277 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
279 return MPN2FLOAT (retval
, exponent
, negative
);
283 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
284 into N. Return the size of the number limbs in NSIZE at the first
285 character od the string that is not part of the integer as the function
286 value. If the EXPONENT is small enough to be taken as an additional
287 factor for the resulting number (see code) multiply by it. */
288 static const STRING_TYPE
*
289 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
291 #ifndef USE_WIDE_CHAR
292 , const char *decimal
, size_t decimal_len
, const char *thousands
297 /* Number of digits for actual limb. */
306 if (cnt
== MAX_DIG_PER_LIMB
)
316 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
317 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
328 /* There might be thousands separators or radix characters in
329 the string. But these all can be ignored because we know the
330 format of the number is correct and we have an exact number
331 of characters to read. */
333 if (*str
< L
'0' || *str
> L
'9')
336 if (*str
< '0' || *str
> '9')
339 if (thousands
!= NULL
&& *str
== *thousands
340 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
341 if (thousands
[inner
] != str
[inner
])
343 thousands
[inner
] == '\0'; }))
349 low
= low
* 10 + *str
++ - L_('0');
352 while (--digcnt
> 0);
354 if (*exponent
> 0 && cnt
+ *exponent
<= MAX_DIG_PER_LIMB
)
356 low
*= _tens_in_limb
[*exponent
];
357 start
= _tens_in_limb
[cnt
+ *exponent
];
361 start
= _tens_in_limb
[cnt
];
371 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
372 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
381 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
382 with the COUNT most significant bits of LIMB.
384 Tege doesn't like this function so I have to write it here myself. :)
387 __attribute ((always_inline
))
388 __mpn_lshift_1 (mp_limb_t
*ptr
, mp_size_t size
, unsigned int count
,
391 if (__builtin_constant_p (count
) && count
== BITS_PER_MP_LIMB
)
393 /* Optimize the case of shifting by exactly a word:
394 just copy words, with no actual bit-shifting. */
396 for (i
= size
- 1; i
> 0; --i
)
402 (void) __mpn_lshift (ptr
, ptr
, size
, count
);
403 ptr
[0] |= limb
>> (BITS_PER_MP_LIMB
- count
);
408 #define INTERNAL(x) INTERNAL1(x)
409 #define INTERNAL1(x) __##x##_internal
410 #ifndef ____STRTOF_INTERNAL
411 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
414 /* This file defines a function to check for correct grouping. */
415 #include "grouping.h"
418 /* Return a floating point number with the value of the given string NPTR.
419 Set *ENDPTR to the character after the last used one. If the number is
420 smaller than the smallest representable number, set `errno' to ERANGE and
421 return 0.0. If the number is too big to be represented, set `errno' to
422 ERANGE and return HUGE_VAL with the appropriate sign. */
424 ____STRTOF_INTERNAL (nptr
, endptr
, group
, loc
)
425 const STRING_TYPE
*nptr
;
426 STRING_TYPE
**endptr
;
430 int negative
; /* The sign of the number. */
431 MPN_VAR (num
); /* MP representation of the number. */
432 int exponent
; /* Exponent of the number. */
434 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
437 /* When we have to compute fractional digits we form a fraction with a
438 second multi-precision number (and we sometimes need a second for
439 temporary results). */
442 /* Representation for the return value. */
443 mp_limb_t retval
[RETURN_LIMB_SIZE
];
444 /* Number of bits currently in result value. */
447 /* Running pointer after the last character processed in the string. */
448 const STRING_TYPE
*cp
, *tp
;
449 /* Start of significant part of the number. */
450 const STRING_TYPE
*startp
, *start_of_digits
;
451 /* Points at the character following the integer and fractional digits. */
452 const STRING_TYPE
*expp
;
453 /* Total number of digit and number of digits in integer part. */
454 int dig_no
, int_no
, lead_zero
;
455 /* Contains the last character read. */
458 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
459 there. So define it ourselves if it remains undefined. */
461 typedef unsigned int wint_t;
463 /* The radix character of the current locale. */
470 /* The thousands character of the current locale. */
472 wchar_t thousands
= L
'\0';
474 const char *thousands
= NULL
;
476 /* The numeric grouping specification of the current locale,
477 in the format described in <locale.h>. */
478 const char *grouping
;
479 /* Used in several places. */
482 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
486 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
487 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
491 /* Figure out the thousands separator character. */
493 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
494 _NL_NUMERIC_THOUSANDS_SEP_WC
);
495 if (thousands
== L
'\0')
498 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
499 if (*thousands
== '\0')
510 /* Find the locale's decimal point character. */
512 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
513 assert (decimal
!= L
'\0');
514 # define decimal_len 1
516 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
517 decimal_len
= strlen (decimal
);
518 assert (decimal_len
> 0);
521 /* Prepare number representation. */
526 /* Parse string to get maximal legal prefix. We need the number of
527 characters of the integer part, the fractional part and the exponent. */
529 /* Ignore leading white space. */
534 /* Get sign of the result. */
540 else if (c
== L_('+'))
543 /* Return 0.0 if no legal string is found.
544 No character is used even if a sign was found. */
546 if (c
== (wint_t) decimal
547 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
549 /* We accept it. This funny construct is here only to indent
550 the code directly. */
553 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
554 if (cp
[cnt
] != decimal
[cnt
])
556 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
558 /* We accept it. This funny construct is here only to indent
559 the code directly. */
562 else if (c
< L_('0') || c
> L_('9'))
564 /* Check for `INF' or `INFINITY'. */
565 if (TOLOWER_C (c
) == L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
567 /* Return +/- infinity. */
569 *endptr
= (STRING_TYPE
*)
570 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
573 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
576 if (TOLOWER_C (c
) == L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
583 /* Match `(n-char-sequence-digit)'. */
586 const STRING_TYPE
*startp
= cp
;
589 while ((*cp
>= L_('0') && *cp
<= L_('9'))
590 || (TOLOWER (*cp
) >= L_('a') && TOLOWER (*cp
) <= L_('z'))
594 /* The closing brace is missing. Only match the NAN
599 /* This is a system-dependent way to specify the
600 bitmask used for the NaN. We expect it to be
601 a number which is put in the mantissa of the
604 unsigned long long int mant
;
606 mant
= STRTOULL (startp
+ 1, &endp
, 0);
608 SET_MANTISSA (retval
, mant
);
613 *endptr
= (STRING_TYPE
*) cp
;
618 /* It is really a text we do not recognize. */
622 /* First look whether we are faced with a hexadecimal number. */
623 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
625 /* Okay, it is a hexa-decimal number. Remember this and skip
626 the characters. BTW: hexadecimal numbers must not be
634 /* Record the start of the digits, in case we will check their grouping. */
635 start_of_digits
= startp
= cp
;
637 /* Ignore leading zeroes. This helps us to avoid useless computations. */
639 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
642 if (thousands
== NULL
)
647 /* We also have the multibyte thousands string. */
652 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
653 if (c
!= thousands
[cnt
])
655 if (thousands
[cnt
] != '\0')
663 /* If no other digit but a '0' is found the result is 0.0.
664 Return current read pointer. */
665 if ((c
< L_('0') || c
> L_('9'))
666 && (base
== 16 && (c
< (CHAR_TYPE
) TOLOWER (L_('a'))
667 || c
> (CHAR_TYPE
) TOLOWER (L_('f'))))
669 && c
!= (wint_t) decimal
671 && ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
672 if (decimal
[cnt
] != cp
[cnt
])
674 decimal
[cnt
] != '\0'; })
676 && (base
== 16 && (cp
== start_of_digits
677 || (CHAR_TYPE
) TOLOWER (c
) != L_('p')))
678 && (base
!= 16 && (CHAR_TYPE
) TOLOWER (c
) != L_('e')))
681 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
684 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
687 /* If TP is at the start of the digits, there was no correctly
688 grouped prefix of the string; so no number found. */
689 RETURN (0.0, tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
692 /* Remember first significant digit and read following characters until the
693 decimal point, exponent character or any non-FP number character. */
698 if ((c
>= L_('0') && c
<= L_('9'))
699 || (base
== 16 && (wint_t) TOLOWER (c
) >= L_('a')
700 && (wint_t) TOLOWER (c
) <= L_('f')))
705 if ((wint_t) thousands
== L
'\0' || c
!= (wint_t) thousands
)
706 /* Not a digit or separator: end of the integer part. */
709 if (thousands
== NULL
)
713 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
714 if (thousands
[cnt
] != cp
[cnt
])
716 if (thousands
[cnt
] != '\0')
724 if (grouping
&& dig_no
> 0)
726 /* Check the grouping of the digits. */
728 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
731 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
736 /* Less than the entire string was correctly grouped. */
738 if (tp
== start_of_digits
)
739 /* No valid group of numbers at all: no valid number. */
743 /* The number is validly grouped, but consists
744 only of zeroes. The whole value is zero. */
747 /* Recompute DIG_NO so we won't read more digits than
748 are properly grouped. */
751 for (tp
= startp
; tp
< cp
; ++tp
)
752 if (*tp
>= L_('0') && *tp
<= L_('9'))
762 /* We have the number digits in the integer part. Whether these are all or
763 any is really a fractional digit will be decided later. */
765 lead_zero
= int_no
== 0 ? -1 : 0;
767 /* Read the fractional digits. A special case are the 'american style'
768 numbers like `16.' i.e. with decimal but without trailing digits. */
771 c
== (wint_t) decimal
773 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
774 if (decimal
[cnt
] != cp
[cnt
])
776 decimal
[cnt
] == '\0'; })
782 while ((c
>= L_('0') && c
<= L_('9')) ||
783 (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
785 if (c
!= L_('0') && lead_zero
== -1)
786 lead_zero
= dig_no
- int_no
;
792 /* Remember start of exponent (if any). */
796 if ((base
== 16 && TOLOWER (c
) == L_('p'))
797 || (base
!= 16 && TOLOWER (c
) == L_('e')))
799 int exp_negative
= 0;
807 else if (c
== L_('+'))
810 if (c
>= L_('0') && c
<= L_('9'))
814 /* Get the exponent limit. */
816 exp_limit
= (exp_negative
?
817 -MIN_EXP
+ MANT_DIG
+ 4 * int_no
:
818 MAX_EXP
- 4 * int_no
+ lead_zero
);
820 exp_limit
= (exp_negative
?
821 -MIN_10_EXP
+ MANT_DIG
+ int_no
:
822 MAX_10_EXP
- int_no
+ lead_zero
);
828 if (exponent
> exp_limit
)
829 /* The exponent is too large/small to represent a valid
834 /* We have to take care for special situation: a joker
835 might have written "0.0e100000" which is in fact
838 result
= negative
? -0.0 : 0.0;
841 /* Overflow or underflow. */
842 __set_errno (ERANGE
);
843 result
= (exp_negative
? 0.0 :
844 negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
);
847 /* Accept all following digits as part of the exponent. */
850 while (*cp
>= L_('0') && *cp
<= L_('9'));
856 exponent
+= c
- L_('0');
859 while (c
>= L_('0') && c
<= L_('9'));
862 exponent
= -exponent
;
868 /* We don't want to have to work with trailing zeroes after the radix. */
871 while (expp
[-1] == L_('0'))
876 assert (dig_no
>= int_no
);
879 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
882 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
885 if (expp
[-1] != L_('0'))
893 while (dig_no
> 0 && exponent
< 0);
897 /* The whole string is parsed. Store the address of the next character. */
899 *endptr
= (STRING_TYPE
*) cp
;
902 return negative
? -0.0 : 0.0;
906 /* Find the decimal point */
908 while (*startp
!= decimal
)
913 if (*startp
== decimal
[0])
915 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
916 if (decimal
[cnt
] != startp
[cnt
])
918 if (decimal
[cnt
] == '\0')
924 startp
+= lead_zero
+ decimal_len
;
925 exponent
-= base
== 16 ? 4 * lead_zero
: lead_zero
;
929 /* If the BASE is 16 we can use a simpler algorithm. */
932 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
933 4, 4, 4, 4, 4, 4, 4, 4 };
934 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
935 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
938 while (!ISXDIGIT (*startp
))
940 while (*startp
== L_('0'))
942 if (ISDIGIT (*startp
))
943 val
= *startp
++ - L_('0');
945 val
= 10 + TOLOWER (*startp
++) - L_('a');
947 /* We cannot have a leading zero. */
950 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
952 /* We don't have to care for wrapping. This is the normal
953 case so we add the first clause in the `if' expression as
954 an optimization. It is a compile-time constant and so does
955 not cost anything. */
956 retval
[idx
] = val
<< (pos
- bits
+ 1);
961 retval
[idx
--] = val
>> (bits
- pos
- 1);
962 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
963 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
966 /* Adjust the exponent for the bits we are shifting in. */
967 exponent
+= bits
- 1 + (int_no
- 1) * 4;
969 while (--dig_no
> 0 && idx
>= 0)
971 if (!ISXDIGIT (*startp
))
972 startp
+= decimal_len
;
973 if (ISDIGIT (*startp
))
974 val
= *startp
++ - L_('0');
976 val
= 10 + TOLOWER (*startp
++) - L_('a');
980 retval
[idx
] |= val
<< (pos
- 4 + 1);
985 retval
[idx
--] |= val
>> (4 - pos
- 1);
986 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
988 return round_and_return (retval
, exponent
, negative
, val
,
989 BITS_PER_MP_LIMB
- 1, dig_no
> 0);
992 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
996 /* We ran out of digits. */
997 MPN_ZERO (retval
, idx
);
999 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1002 /* Now we have the number of digits in total and the integer digits as well
1003 as the exponent and its sign. We can decide whether the read digits are
1004 really integer digits or belong to the fractional part; i.e. we normalize
1007 register int incr
= (exponent
< 0 ? MAX (-int_no
, exponent
)
1008 : MIN (dig_no
- int_no
, exponent
));
1013 if (int_no
+ exponent
> MAX_10_EXP
+ 1)
1015 __set_errno (ERANGE
);
1016 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1019 if (exponent
< MIN_10_EXP
- (DIG
+ 1))
1021 __set_errno (ERANGE
);
1027 /* Read the integer part as a multi-precision number to NUM. */
1028 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1029 #ifndef USE_WIDE_CHAR
1030 , decimal
, decimal_len
, thousands
1036 /* We now multiply the gained number by the given power of ten. */
1037 mp_limb_t
*psrc
= num
;
1038 mp_limb_t
*pdest
= den
;
1040 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1044 if ((exponent
& expbit
) != 0)
1046 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1050 /* FIXME: not the whole multiplication has to be
1051 done. If we have the needed number of bits we
1052 only need the information whether more non-zero
1054 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1055 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1056 &__tens
[ttab
->arrayoff
1057 + _FPIO_CONST_OFFSET
],
1060 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1061 + _FPIO_CONST_OFFSET
],
1062 size
, psrc
, numsize
);
1066 (void) SWAP (psrc
, pdest
);
1071 while (exponent
!= 0);
1074 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1077 /* Determine how many bits of the result we already have. */
1078 count_leading_zeros (bits
, num
[numsize
- 1]);
1079 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1081 /* Now we know the exponent of the number in base two.
1082 Check it against the maximum possible exponent. */
1085 __set_errno (ERANGE
);
1086 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1089 /* We have already the first BITS bits of the result. Together with
1090 the information whether more non-zero bits follow this is enough
1091 to determine the result. */
1092 if (bits
> MANT_DIG
)
1095 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1096 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1097 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1099 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1103 memcpy (retval
, &num
[least_idx
],
1104 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1107 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1108 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1110 << (BITS_PER_MP_LIMB
- least_bit
));
1111 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1112 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1115 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1116 for (i
= 0; num
[i
] == 0; ++i
)
1119 return round_and_return (retval
, bits
- 1, negative
,
1120 num
[round_idx
], round_bit
,
1121 int_no
< dig_no
|| i
< round_idx
);
1124 else if (dig_no
== int_no
)
1126 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1127 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1129 if (target_bit
== is_bit
)
1131 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1132 numsize
* sizeof (mp_limb_t
));
1133 /* FIXME: the following loop can be avoided if we assume a
1134 maximal MANT_DIG value. */
1135 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1137 else if (target_bit
> is_bit
)
1139 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1140 num
, numsize
, target_bit
- is_bit
);
1141 /* FIXME: the following loop can be avoided if we assume a
1142 maximal MANT_DIG value. */
1143 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1148 assert (numsize
< RETURN_LIMB_SIZE
);
1150 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1151 num
, numsize
, is_bit
- target_bit
);
1152 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1153 /* FIXME: the following loop can be avoided if we assume a
1154 maximal MANT_DIG value. */
1155 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1158 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1162 /* Store the bits we already have. */
1163 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1164 #if RETURN_LIMB_SIZE > 1
1165 if (numsize
< RETURN_LIMB_SIZE
)
1166 # if RETURN_LIMB_SIZE == 2
1167 retval
[numsize
] = 0;
1169 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1174 /* We have to compute at least some of the fractional digits. */
1176 /* We construct a fraction and the result of the division gives us
1177 the needed digits. The denominator is 1.0 multiplied by the
1178 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1179 123e-6 gives 123 / 1000000. */
1185 mp_limb_t
*psrc
= den
;
1186 mp_limb_t
*pdest
= num
;
1187 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1189 assert (dig_no
> int_no
&& exponent
<= 0);
1192 /* For the fractional part we need not process too many digits. One
1193 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1195 digits we should have enough bits for the result. The remaining
1196 decimal digits give us the information that more bits are following.
1197 This can be used while rounding. (Two added as a safety margin.) */
1198 if (dig_no
- int_no
> (MANT_DIG
- bits
+ 2) / 3 + 2)
1200 dig_no
= int_no
+ (MANT_DIG
- bits
+ 2) / 3 + 2;
1206 neg_exp
= dig_no
- int_no
- exponent
;
1208 /* Construct the denominator. */
1213 if ((neg_exp
& expbit
) != 0)
1220 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1221 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1222 densize
* sizeof (mp_limb_t
));
1226 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1227 + _FPIO_CONST_OFFSET
],
1228 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1230 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1233 (void) SWAP (psrc
, pdest
);
1239 while (neg_exp
!= 0);
1242 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1244 /* Read the fractional digits from the string. */
1245 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1246 #ifndef USE_WIDE_CHAR
1247 , decimal
, decimal_len
, thousands
1251 /* We now have to shift both numbers so that the highest bit in the
1252 denominator is set. In the same process we copy the numerator to
1253 a high place in the array so that the division constructs the wanted
1254 digits. This is done by a "quasi fix point" number representation.
1256 num: ddddddddddd . 0000000000000000000000
1258 den: ddddddddddd n >= m
1262 count_leading_zeros (cnt
, den
[densize
- 1]);
1266 /* Don't call `mpn_shift' with a count of zero since the specification
1267 does not allow this. */
1268 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1269 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1271 num
[numsize
++] = cy
;
1274 /* Now we are ready for the division. But it is not necessary to
1275 do a full multi-precision division because we only need a small
1276 number of bits for the result. So we do not use __mpn_divmod
1277 here but instead do the division here by hand and stop whenever
1278 the needed number of bits is reached. The code itself comes
1279 from the GNU MP Library by Torbj\"orn Granlund. */
1287 mp_limb_t d
, n
, quot
;
1292 assert (numsize
== 1 && n
< d
);
1296 udiv_qrnnd (quot
, n
, n
, 0, d
);
1303 cnt = BITS_PER_MP_LIMB; \
1305 count_leading_zeros (cnt, quot); \
1307 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1309 used = MANT_DIG + cnt; \
1310 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1311 bits = MANT_DIG + 1; \
1315 /* Note that we only clear the second element. */ \
1316 /* The conditional is determined at compile time. */ \
1317 if (RETURN_LIMB_SIZE > 1) \
1323 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1324 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1328 used = MANT_DIG - bits; \
1330 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1332 bits += BITS_PER_MP_LIMB
1336 while (bits
<= MANT_DIG
);
1338 return round_and_return (retval
, exponent
- 1, negative
,
1339 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1340 more_bits
|| n
!= 0);
1344 mp_limb_t d0
, d1
, n0
, n1
;
1351 if (numsize
< densize
)
1355 /* The numerator of the number occupies fewer bits than
1356 the denominator but the one limb is bigger than the
1357 high limb of the numerator. */
1364 exponent
-= BITS_PER_MP_LIMB
;
1367 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1368 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1369 BITS_PER_MP_LIMB
, 0);
1372 used
= MANT_DIG
- bits
;
1374 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1376 bits
+= BITS_PER_MP_LIMB
;
1388 while (bits
<= MANT_DIG
)
1394 /* QUOT should be either 111..111 or 111..110. We need
1395 special treatment of this rare case as normal division
1396 would give overflow. */
1397 quot
= ~(mp_limb_t
) 0;
1400 if (r
< d1
) /* Carry in the addition? */
1402 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1405 n1
= d0
- (d0
!= 0);
1410 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1411 umul_ppmm (n1
, n0
, d0
, quot
);
1415 if (n1
> r
|| (n1
== r
&& n0
> 0))
1417 /* The estimated QUOT was too large. */
1420 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1422 if (r
>= d1
) /* If not carry, test QUOT again. */
1425 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1431 return round_and_return (retval
, exponent
- 1, negative
,
1432 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1433 more_bits
|| n1
!= 0 || n0
!= 0);
1438 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1442 dX
= den
[densize
- 1];
1443 d1
= den
[densize
- 2];
1445 /* The division does not work if the upper limb of the two-limb
1446 numerator is greater than the denominator. */
1447 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1450 if (numsize
< densize
)
1452 mp_size_t empty
= densize
- numsize
;
1456 exponent
-= empty
* BITS_PER_MP_LIMB
;
1459 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1461 /* We make a difference here because the compiler
1462 cannot optimize the `else' case that good and
1463 this reflects all currently used FLOAT types
1464 and GMP implementations. */
1465 #if RETURN_LIMB_SIZE <= 2
1466 assert (empty
== 1);
1467 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1468 BITS_PER_MP_LIMB
, 0);
1470 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1471 retval
[i
] = retval
[i
- empty
];
1478 used
= MANT_DIG
- bits
;
1479 if (used
>= BITS_PER_MP_LIMB
)
1482 (void) __mpn_lshift (&retval
[used
1483 / BITS_PER_MP_LIMB
],
1484 retval
, RETURN_LIMB_SIZE
,
1485 used
% BITS_PER_MP_LIMB
);
1486 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1490 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1492 bits
+= empty
* BITS_PER_MP_LIMB
;
1494 for (i
= numsize
; i
> 0; --i
)
1495 num
[i
+ empty
] = num
[i
- 1];
1496 MPN_ZERO (num
, empty
+ 1);
1501 assert (numsize
== densize
);
1502 for (i
= numsize
; i
> 0; --i
)
1503 num
[i
] = num
[i
- 1];
1509 while (bits
<= MANT_DIG
)
1512 /* This might over-estimate QUOT, but it's probably not
1513 worth the extra code here to find out. */
1514 quot
= ~(mp_limb_t
) 0;
1519 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1520 umul_ppmm (n1
, n0
, d1
, quot
);
1522 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1526 if (r
< dX
) /* I.e. "carry in previous addition?" */
1533 /* Possible optimization: We already have (q * n0) and (1 * n1)
1534 after the calculation of QUOT. Taking advantage of this, we
1535 could make this loop make two iterations less. */
1537 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1539 if (num
[densize
] != cy
)
1541 cy
= __mpn_add_n (num
, num
, den
, densize
);
1545 n0
= num
[densize
] = num
[densize
- 1];
1546 for (i
= densize
- 1; i
> 0; --i
)
1547 num
[i
] = num
[i
- 1];
1552 for (i
= densize
; num
[i
] == 0 && i
>= 0; --i
)
1554 return round_and_return (retval
, exponent
- 1, negative
,
1555 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1556 more_bits
|| i
>= 0);
1563 #if defined _LIBC && !defined USE_WIDE_CHAR
1564 libc_hidden_def (____STRTOF_INTERNAL
)
1567 /* External user entry point. */
1570 #ifdef weak_function
1573 __STRTOF (nptr
, endptr
, loc
)
1574 const STRING_TYPE
*nptr
;
1575 STRING_TYPE
**endptr
;
1578 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1580 weak_alias (__STRTOF
, STRTOF
)
1582 #ifdef LONG_DOUBLE_COMPAT
1583 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1584 # ifdef USE_WIDE_CHAR
1585 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1587 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1590 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1591 # ifdef USE_WIDE_CHAR
1592 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1594 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);