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 && ((CHAR_TYPE
) TOLOWER (c
) >= L_('a')
667 && (CHAR_TYPE
) TOLOWER (c
) <= 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
&& cp
> start_of_digits
)
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 of digits in the integer part. Whether these
763 are all or any is really a fractional digit will be decided
766 lead_zero
= int_no
== 0 ? -1 : 0;
768 /* Read the fractional digits. A special case are the 'american
769 style' numbers like `16.' i.e. with decimal point but without
773 c
== (wint_t) decimal
775 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
776 if (decimal
[cnt
] != cp
[cnt
])
778 decimal
[cnt
] == '\0'; })
784 while ((c
>= L_('0') && c
<= L_('9')) ||
785 (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
787 if (c
!= L_('0') && lead_zero
== -1)
788 lead_zero
= dig_no
- int_no
;
794 /* Remember start of exponent (if any). */
798 if ((base
== 16 && TOLOWER (c
) == L_('p'))
799 || (base
!= 16 && TOLOWER (c
) == L_('e')))
801 int exp_negative
= 0;
809 else if (c
== L_('+'))
812 if (c
>= L_('0') && c
<= L_('9'))
816 /* Get the exponent limit. */
818 exp_limit
= (exp_negative
?
819 -MIN_EXP
+ MANT_DIG
+ 4 * int_no
:
820 MAX_EXP
- 4 * int_no
+ 4 * lead_zero
+ 3);
822 exp_limit
= (exp_negative
?
823 -MIN_10_EXP
+ MANT_DIG
+ int_no
:
824 MAX_10_EXP
- int_no
+ lead_zero
+ 1);
829 exponent
+= c
- L_('0');
831 if (exponent
> exp_limit
)
832 /* The exponent is too large/small to represent a valid
837 /* We have to take care for special situation: a joker
838 might have written "0.0e100000" which is in fact
841 result
= negative
? -0.0 : 0.0;
844 /* Overflow or underflow. */
845 __set_errno (ERANGE
);
846 result
= (exp_negative
? 0.0 :
847 negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
);
850 /* Accept all following digits as part of the exponent. */
853 while (*cp
>= L_('0') && *cp
<= L_('9'));
861 while (c
>= L_('0') && c
<= L_('9'));
864 exponent
= -exponent
;
870 /* We don't want to have to work with trailing zeroes after the radix. */
873 while (expp
[-1] == L_('0'))
878 assert (dig_no
>= int_no
);
881 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
884 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
887 if (expp
[-1] != L_('0'))
893 exponent
+= base
== 16 ? 4 : 1;
895 while (dig_no
> 0 && exponent
< 0);
899 /* The whole string is parsed. Store the address of the next character. */
901 *endptr
= (STRING_TYPE
*) cp
;
904 return negative
? -0.0 : 0.0;
908 /* Find the decimal point */
910 while (*startp
!= decimal
)
915 if (*startp
== decimal
[0])
917 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
918 if (decimal
[cnt
] != startp
[cnt
])
920 if (decimal
[cnt
] == '\0')
926 startp
+= lead_zero
+ decimal_len
;
927 exponent
-= base
== 16 ? 4 * lead_zero
: lead_zero
;
931 /* If the BASE is 16 we can use a simpler algorithm. */
934 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
935 4, 4, 4, 4, 4, 4, 4, 4 };
936 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
937 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
940 while (!ISXDIGIT (*startp
))
942 while (*startp
== L_('0'))
944 if (ISDIGIT (*startp
))
945 val
= *startp
++ - L_('0');
947 val
= 10 + TOLOWER (*startp
++) - L_('a');
949 /* We cannot have a leading zero. */
952 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
954 /* We don't have to care for wrapping. This is the normal
955 case so we add the first clause in the `if' expression as
956 an optimization. It is a compile-time constant and so does
957 not cost anything. */
958 retval
[idx
] = val
<< (pos
- bits
+ 1);
963 retval
[idx
--] = val
>> (bits
- pos
- 1);
964 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
965 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
968 /* Adjust the exponent for the bits we are shifting in. */
969 exponent
+= bits
- 1 + (int_no
- 1) * 4;
971 while (--dig_no
> 0 && idx
>= 0)
973 if (!ISXDIGIT (*startp
))
974 startp
+= decimal_len
;
975 if (ISDIGIT (*startp
))
976 val
= *startp
++ - L_('0');
978 val
= 10 + TOLOWER (*startp
++) - L_('a');
982 retval
[idx
] |= val
<< (pos
- 4 + 1);
987 retval
[idx
--] |= val
>> (4 - pos
- 1);
988 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
990 return round_and_return (retval
, exponent
, negative
, val
,
991 BITS_PER_MP_LIMB
- 1, dig_no
> 0);
994 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
998 /* We ran out of digits. */
999 MPN_ZERO (retval
, idx
);
1001 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1004 /* Now we have the number of digits in total and the integer digits as well
1005 as the exponent and its sign. We can decide whether the read digits are
1006 really integer digits or belong to the fractional part; i.e. we normalize
1009 register int incr
= (exponent
< 0 ? MAX (-int_no
, exponent
)
1010 : MIN (dig_no
- int_no
, exponent
));
1015 if (int_no
+ exponent
> MAX_10_EXP
+ 1)
1017 __set_errno (ERANGE
);
1018 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1021 if (exponent
< MIN_10_EXP
- (DIG
+ 1))
1023 __set_errno (ERANGE
);
1029 /* Read the integer part as a multi-precision number to NUM. */
1030 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1031 #ifndef USE_WIDE_CHAR
1032 , decimal
, decimal_len
, thousands
1038 /* We now multiply the gained number by the given power of ten. */
1039 mp_limb_t
*psrc
= num
;
1040 mp_limb_t
*pdest
= den
;
1042 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1046 if ((exponent
& expbit
) != 0)
1048 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1052 /* FIXME: not the whole multiplication has to be
1053 done. If we have the needed number of bits we
1054 only need the information whether more non-zero
1056 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1057 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1058 &__tens
[ttab
->arrayoff
1059 + _FPIO_CONST_OFFSET
],
1062 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1063 + _FPIO_CONST_OFFSET
],
1064 size
, psrc
, numsize
);
1068 (void) SWAP (psrc
, pdest
);
1073 while (exponent
!= 0);
1076 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1079 /* Determine how many bits of the result we already have. */
1080 count_leading_zeros (bits
, num
[numsize
- 1]);
1081 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1083 /* Now we know the exponent of the number in base two.
1084 Check it against the maximum possible exponent. */
1087 __set_errno (ERANGE
);
1088 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1091 /* We have already the first BITS bits of the result. Together with
1092 the information whether more non-zero bits follow this is enough
1093 to determine the result. */
1094 if (bits
> MANT_DIG
)
1097 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1098 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1099 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1101 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1105 memcpy (retval
, &num
[least_idx
],
1106 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1109 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1110 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1112 << (BITS_PER_MP_LIMB
- least_bit
));
1113 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1114 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1117 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1118 for (i
= 0; num
[i
] == 0; ++i
)
1121 return round_and_return (retval
, bits
- 1, negative
,
1122 num
[round_idx
], round_bit
,
1123 int_no
< dig_no
|| i
< round_idx
);
1126 else if (dig_no
== int_no
)
1128 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1129 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1131 if (target_bit
== is_bit
)
1133 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1134 numsize
* sizeof (mp_limb_t
));
1135 /* FIXME: the following loop can be avoided if we assume a
1136 maximal MANT_DIG value. */
1137 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1139 else if (target_bit
> is_bit
)
1141 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1142 num
, numsize
, target_bit
- is_bit
);
1143 /* FIXME: the following loop can be avoided if we assume a
1144 maximal MANT_DIG value. */
1145 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1150 assert (numsize
< RETURN_LIMB_SIZE
);
1152 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1153 num
, numsize
, is_bit
- target_bit
);
1154 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1155 /* FIXME: the following loop can be avoided if we assume a
1156 maximal MANT_DIG value. */
1157 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1160 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1164 /* Store the bits we already have. */
1165 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1166 #if RETURN_LIMB_SIZE > 1
1167 if (numsize
< RETURN_LIMB_SIZE
)
1168 # if RETURN_LIMB_SIZE == 2
1169 retval
[numsize
] = 0;
1171 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1176 /* We have to compute at least some of the fractional digits. */
1178 /* We construct a fraction and the result of the division gives us
1179 the needed digits. The denominator is 1.0 multiplied by the
1180 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1181 123e-6 gives 123 / 1000000. */
1187 mp_limb_t
*psrc
= den
;
1188 mp_limb_t
*pdest
= num
;
1189 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1191 assert (dig_no
> int_no
&& exponent
<= 0);
1194 /* For the fractional part we need not process too many digits. One
1195 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1197 digits we should have enough bits for the result. The remaining
1198 decimal digits give us the information that more bits are following.
1199 This can be used while rounding. (Two added as a safety margin.) */
1200 if (dig_no
- int_no
> (MANT_DIG
- bits
+ 2) / 3 + 2)
1202 dig_no
= int_no
+ (MANT_DIG
- bits
+ 2) / 3 + 2;
1208 neg_exp
= dig_no
- int_no
- exponent
;
1210 /* Construct the denominator. */
1215 if ((neg_exp
& expbit
) != 0)
1222 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1223 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1224 densize
* sizeof (mp_limb_t
));
1228 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1229 + _FPIO_CONST_OFFSET
],
1230 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1232 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1235 (void) SWAP (psrc
, pdest
);
1241 while (neg_exp
!= 0);
1244 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1246 /* Read the fractional digits from the string. */
1247 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1248 #ifndef USE_WIDE_CHAR
1249 , decimal
, decimal_len
, thousands
1253 /* We now have to shift both numbers so that the highest bit in the
1254 denominator is set. In the same process we copy the numerator to
1255 a high place in the array so that the division constructs the wanted
1256 digits. This is done by a "quasi fix point" number representation.
1258 num: ddddddddddd . 0000000000000000000000
1260 den: ddddddddddd n >= m
1264 count_leading_zeros (cnt
, den
[densize
- 1]);
1268 /* Don't call `mpn_shift' with a count of zero since the specification
1269 does not allow this. */
1270 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1271 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1273 num
[numsize
++] = cy
;
1276 /* Now we are ready for the division. But it is not necessary to
1277 do a full multi-precision division because we only need a small
1278 number of bits for the result. So we do not use __mpn_divmod
1279 here but instead do the division here by hand and stop whenever
1280 the needed number of bits is reached. The code itself comes
1281 from the GNU MP Library by Torbj\"orn Granlund. */
1289 mp_limb_t d
, n
, quot
;
1294 assert (numsize
== 1 && n
< d
);
1298 udiv_qrnnd (quot
, n
, n
, 0, d
);
1305 cnt = BITS_PER_MP_LIMB; \
1307 count_leading_zeros (cnt, quot); \
1309 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1311 used = MANT_DIG + cnt; \
1312 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1313 bits = MANT_DIG + 1; \
1317 /* Note that we only clear the second element. */ \
1318 /* The conditional is determined at compile time. */ \
1319 if (RETURN_LIMB_SIZE > 1) \
1325 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1326 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1330 used = MANT_DIG - bits; \
1332 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1334 bits += BITS_PER_MP_LIMB
1338 while (bits
<= MANT_DIG
);
1340 return round_and_return (retval
, exponent
- 1, negative
,
1341 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1342 more_bits
|| n
!= 0);
1346 mp_limb_t d0
, d1
, n0
, n1
;
1353 if (numsize
< densize
)
1357 /* The numerator of the number occupies fewer bits than
1358 the denominator but the one limb is bigger than the
1359 high limb of the numerator. */
1366 exponent
-= BITS_PER_MP_LIMB
;
1369 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1370 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1371 BITS_PER_MP_LIMB
, 0);
1374 used
= MANT_DIG
- bits
;
1376 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1378 bits
+= BITS_PER_MP_LIMB
;
1390 while (bits
<= MANT_DIG
)
1396 /* QUOT should be either 111..111 or 111..110. We need
1397 special treatment of this rare case as normal division
1398 would give overflow. */
1399 quot
= ~(mp_limb_t
) 0;
1402 if (r
< d1
) /* Carry in the addition? */
1404 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1407 n1
= d0
- (d0
!= 0);
1412 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1413 umul_ppmm (n1
, n0
, d0
, quot
);
1417 if (n1
> r
|| (n1
== r
&& n0
> 0))
1419 /* The estimated QUOT was too large. */
1422 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1424 if (r
>= d1
) /* If not carry, test QUOT again. */
1427 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1433 return round_and_return (retval
, exponent
- 1, negative
,
1434 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1435 more_bits
|| n1
!= 0 || n0
!= 0);
1440 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1444 dX
= den
[densize
- 1];
1445 d1
= den
[densize
- 2];
1447 /* The division does not work if the upper limb of the two-limb
1448 numerator is greater than the denominator. */
1449 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1452 if (numsize
< densize
)
1454 mp_size_t empty
= densize
- numsize
;
1458 exponent
-= empty
* BITS_PER_MP_LIMB
;
1461 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1463 /* We make a difference here because the compiler
1464 cannot optimize the `else' case that good and
1465 this reflects all currently used FLOAT types
1466 and GMP implementations. */
1467 #if RETURN_LIMB_SIZE <= 2
1468 assert (empty
== 1);
1469 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1470 BITS_PER_MP_LIMB
, 0);
1472 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1473 retval
[i
] = retval
[i
- empty
];
1480 used
= MANT_DIG
- bits
;
1481 if (used
>= BITS_PER_MP_LIMB
)
1484 (void) __mpn_lshift (&retval
[used
1485 / BITS_PER_MP_LIMB
],
1486 retval
, RETURN_LIMB_SIZE
,
1487 used
% BITS_PER_MP_LIMB
);
1488 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1492 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1494 bits
+= empty
* BITS_PER_MP_LIMB
;
1496 for (i
= numsize
; i
> 0; --i
)
1497 num
[i
+ empty
] = num
[i
- 1];
1498 MPN_ZERO (num
, empty
+ 1);
1503 assert (numsize
== densize
);
1504 for (i
= numsize
; i
> 0; --i
)
1505 num
[i
] = num
[i
- 1];
1511 while (bits
<= MANT_DIG
)
1514 /* This might over-estimate QUOT, but it's probably not
1515 worth the extra code here to find out. */
1516 quot
= ~(mp_limb_t
) 0;
1521 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1522 umul_ppmm (n1
, n0
, d1
, quot
);
1524 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1528 if (r
< dX
) /* I.e. "carry in previous addition?" */
1535 /* Possible optimization: We already have (q * n0) and (1 * n1)
1536 after the calculation of QUOT. Taking advantage of this, we
1537 could make this loop make two iterations less. */
1539 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1541 if (num
[densize
] != cy
)
1543 cy
= __mpn_add_n (num
, num
, den
, densize
);
1547 n0
= num
[densize
] = num
[densize
- 1];
1548 for (i
= densize
- 1; i
> 0; --i
)
1549 num
[i
] = num
[i
- 1];
1554 for (i
= densize
; num
[i
] == 0 && i
>= 0; --i
)
1556 return round_and_return (retval
, exponent
- 1, negative
,
1557 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1558 more_bits
|| i
>= 0);
1565 #if defined _LIBC && !defined USE_WIDE_CHAR
1566 libc_hidden_def (____STRTOF_INTERNAL
)
1569 /* External user entry point. */
1572 #ifdef weak_function
1575 __STRTOF (nptr
, endptr
, loc
)
1576 const STRING_TYPE
*nptr
;
1577 STRING_TYPE
**endptr
;
1580 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1582 weak_alias (__STRTOF
, STRTOF
)
1584 #ifdef LONG_DOUBLE_COMPAT
1585 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1586 # ifdef USE_WIDE_CHAR
1587 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1589 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1592 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1593 # ifdef USE_WIDE_CHAR
1594 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1596 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);