1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,98,2002, 2004 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. */
34 # define STRTOF wcstod_l
35 # define __STRTOF __wcstod_l
37 # define STRTOF strtod_l
38 # define __STRTOF __strtod_l
40 # define MPN2FLOAT __mpn_construct_double
41 # define FLOAT_HUGE_VAL HUGE_VAL
42 # define SET_MANTISSA(flt, mant) \
43 do { union ieee754_double u; \
45 if ((mant & 0xfffffffffffffULL) == 0) \
46 mant = 0x8000000000000ULL; \
47 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
48 u.ieee.mantissa1 = (mant) & 0xffffffff; \
52 /* End of configuration part. */
58 #include "../locale/localeinfo.h"
64 /* The gmp headers need some configuration frobs. */
67 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
68 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
69 #include <gmp-mparam.h>
73 #include "fpioconst.h"
79 /* We use this code for the extended locale handling where the
80 function gets as an additional argument the locale which has to be
81 used. To access the values we have to redefine the _NL_CURRENT and
82 _NL_CURRENT_WORD macros. */
84 #define _NL_CURRENT(category, item) \
85 (current->values[_NL_ITEM_INDEX (item)].string)
86 #undef _NL_CURRENT_WORD
87 #define _NL_CURRENT_WORD(category, item) \
88 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
90 #if defined _LIBC || defined HAVE_WCHAR_H
96 # define STRING_TYPE wchar_t
97 # define CHAR_TYPE wint_t
99 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
100 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
101 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
102 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
103 # define STRNCASECMP(S1, S2, N) __wcsncasecmp_l ((S1), (S2), (N), loc)
104 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
106 # define STRING_TYPE char
107 # define CHAR_TYPE char
109 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
110 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
111 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
112 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
113 # define STRNCASECMP(S1, S2, N) __strncasecmp_l ((S1), (S2), (N), loc)
114 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
118 /* Constants we need from float.h; select the set for the FLOAT precision. */
119 #define MANT_DIG PASTE(FLT,_MANT_DIG)
120 #define DIG PASTE(FLT,_DIG)
121 #define MAX_EXP PASTE(FLT,_MAX_EXP)
122 #define MIN_EXP PASTE(FLT,_MIN_EXP)
123 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
124 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
126 /* Extra macros required to get FLT expanded before the pasting. */
127 #define PASTE(a,b) PASTE1(a,b)
128 #define PASTE1(a,b) a##b
130 /* Function to construct a floating point number from an MP integer
131 containing the fraction bits, a base 2 exponent, and a sign flag. */
132 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
134 /* Definitions according to limb size used. */
135 #if BITS_PER_MP_LIMB == 32
136 # define MAX_DIG_PER_LIMB 9
137 # define MAX_FAC_PER_LIMB 1000000000UL
138 #elif BITS_PER_MP_LIMB == 64
139 # define MAX_DIG_PER_LIMB 19
140 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
142 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
146 /* Local data structure. */
147 static const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1] =
149 1000, 10000, 100000L,
150 1000000L, 10000000L, 100000000L,
152 #if BITS_PER_MP_LIMB > 32
153 , 10000000000ULL, 100000000000ULL,
154 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
155 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
156 1000000000000000000ULL, 10000000000000000000ULL
158 #if BITS_PER_MP_LIMB > 64
159 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
164 #define howmany(x,y) (((x)+((y)-1))/(y))
166 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
168 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
169 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
170 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
172 #define RETURN(val,end) \
173 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
174 return val; } while (0)
176 /* Maximum size necessary for mpn integers to hold floating point numbers. */
177 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
179 /* Declare an mpn integer variable that big. */
180 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
181 /* Copy an mpn integer value. */
182 #define MPN_ASSIGN(dst, src) \
183 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
186 /* Return a floating point number of the needed type according to the given
187 multi-precision number after possible rounding. */
189 round_and_return (mp_limb_t
*retval
, int exponent
, int negative
,
190 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
192 if (exponent
< MIN_EXP
- 1)
194 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
196 if (shift
> MANT_DIG
)
202 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
203 if (shift
== MANT_DIG
)
204 /* This is a special case to handle the very seldom case where
205 the mantissa will be empty after the shift. */
209 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
210 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
211 for (i
= 0; i
< RETURN_LIMB_SIZE
; ++i
)
212 more_bits
|= retval
[i
] != 0;
213 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
215 else if (shift
>= BITS_PER_MP_LIMB
)
219 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
220 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
221 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
222 more_bits
|= retval
[i
] != 0;
223 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
226 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
227 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
228 shift
% BITS_PER_MP_LIMB
);
229 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
230 shift
/ BITS_PER_MP_LIMB
);
234 round_limb
= retval
[0];
235 round_bit
= shift
- 1;
236 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
238 /* This is a hook for the m68k long double format, where the
239 exponent bias is the same for normalized and denormalized
242 # define DENORM_EXP (MIN_EXP - 2)
244 exponent
= DENORM_EXP
;
247 if ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
248 && (more_bits
|| (retval
[0] & 1) != 0
249 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
251 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
253 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
254 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
255 (retval
[RETURN_LIMB_SIZE
- 1]
256 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
259 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
260 retval
[RETURN_LIMB_SIZE
- 1]
261 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
263 else if (exponent
== DENORM_EXP
264 && (retval
[RETURN_LIMB_SIZE
- 1]
265 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
267 /* The number was denormalized but now normalized. */
268 exponent
= MIN_EXP
- 1;
271 if (exponent
> MAX_EXP
)
272 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
274 return MPN2FLOAT (retval
, exponent
, negative
);
278 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
279 into N. Return the size of the number limbs in NSIZE at the first
280 character od the string that is not part of the integer as the function
281 value. If the EXPONENT is small enough to be taken as an additional
282 factor for the resulting number (see code) multiply by it. */
283 static const STRING_TYPE
*
284 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
286 #ifndef USE_WIDE_CHAR
287 , const char *decimal
, size_t decimal_len
, const char *thousands
292 /* Number of digits for actual limb. */
301 if (cnt
== MAX_DIG_PER_LIMB
)
311 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
312 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
323 /* There might be thousands separators or radix characters in
324 the string. But these all can be ignored because we know the
325 format of the number is correct and we have an exact number
326 of characters to read. */
328 if (*str
< L
'0' || *str
> L
'9')
331 if (*str
< '0' || *str
> '9')
334 if (thousands
!= NULL
&& *str
== *thousands
335 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
336 if (thousands
[inner
] != str
[inner
])
338 thousands
[inner
] == '\0'; }))
344 low
= low
* 10 + *str
++ - L_('0');
347 while (--digcnt
> 0);
349 if (*exponent
> 0 && cnt
+ *exponent
<= MAX_DIG_PER_LIMB
)
351 low
*= _tens_in_limb
[*exponent
];
352 start
= _tens_in_limb
[cnt
+ *exponent
];
356 start
= _tens_in_limb
[cnt
];
366 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
367 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
376 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
377 with the COUNT most significant bits of LIMB.
379 Tege doesn't like this function so I have to write it here myself. :)
382 __attribute ((always_inline
))
383 __mpn_lshift_1 (mp_limb_t
*ptr
, mp_size_t size
, unsigned int count
,
386 if (__builtin_constant_p (count
) && count
== BITS_PER_MP_LIMB
)
388 /* Optimize the case of shifting by exactly a word:
389 just copy words, with no actual bit-shifting. */
391 for (i
= size
- 1; i
> 0; --i
)
397 (void) __mpn_lshift (ptr
, ptr
, size
, count
);
398 ptr
[0] |= limb
>> (BITS_PER_MP_LIMB
- count
);
403 #define INTERNAL(x) INTERNAL1(x)
404 #define INTERNAL1(x) __##x##_internal
406 /* This file defines a function to check for correct grouping. */
407 #include "grouping.h"
410 /* Return a floating point number with the value of the given string NPTR.
411 Set *ENDPTR to the character after the last used one. If the number is
412 smaller than the smallest representable number, set `errno' to ERANGE and
413 return 0.0. If the number is too big to be represented, set `errno' to
414 ERANGE and return HUGE_VAL with the appropriate sign. */
416 INTERNAL (__STRTOF
) (nptr
, endptr
, group
, loc
)
417 const STRING_TYPE
*nptr
;
418 STRING_TYPE
**endptr
;
422 int negative
; /* The sign of the number. */
423 MPN_VAR (num
); /* MP representation of the number. */
424 int exponent
; /* Exponent of the number. */
426 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
429 /* When we have to compute fractional digits we form a fraction with a
430 second multi-precision number (and we sometimes need a second for
431 temporary results). */
434 /* Representation for the return value. */
435 mp_limb_t retval
[RETURN_LIMB_SIZE
];
436 /* Number of bits currently in result value. */
439 /* Running pointer after the last character processed in the string. */
440 const STRING_TYPE
*cp
, *tp
;
441 /* Start of significant part of the number. */
442 const STRING_TYPE
*startp
, *start_of_digits
;
443 /* Points at the character following the integer and fractional digits. */
444 const STRING_TYPE
*expp
;
445 /* Total number of digit and number of digits in integer part. */
446 int dig_no
, int_no
, lead_zero
;
447 /* Contains the last character read. */
450 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
451 there. So define it ourselves if it remains undefined. */
453 typedef unsigned int wint_t;
455 /* The radix character of the current locale. */
462 /* The thousands character of the current locale. */
464 wchar_t thousands
= L
'\0';
466 const char *thousands
= NULL
;
468 /* The numeric grouping specification of the current locale,
469 in the format described in <locale.h>. */
470 const char *grouping
;
471 /* Used in several places. */
474 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
478 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
479 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
483 /* Figure out the thousands separator character. */
485 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
486 _NL_NUMERIC_THOUSANDS_SEP_WC
);
487 if (thousands
== L
'\0')
490 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
491 if (*thousands
== '\0')
502 /* Find the locale's decimal point character. */
504 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
505 assert (decimal
!= L
'\0');
506 # define decimal_len 1
508 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
509 decimal_len
= strlen (decimal
);
510 assert (decimal_len
> 0);
513 /* Prepare number representation. */
518 /* Parse string to get maximal legal prefix. We need the number of
519 characters of the integer part, the fractional part and the exponent. */
521 /* Ignore leading white space. */
526 /* Get sign of the result. */
532 else if (c
== L_('+'))
535 /* Return 0.0 if no legal string is found.
536 No character is used even if a sign was found. */
538 if (c
== (wint_t) decimal
539 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
541 /* We accept it. This funny construct is here only to indent
542 the code directly. */
545 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
546 if (cp
[cnt
] != decimal
[cnt
])
548 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
550 /* We accept it. This funny construct is here only to indent
551 the code directly. */
554 else if (c
< L_('0') || c
> L_('9'))
556 /* Check for `INF' or `INFINITY'. */
557 if (TOLOWER (c
) == L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
559 /* Return +/- infinity. */
561 *endptr
= (STRING_TYPE
*)
562 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
565 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
568 if (TOLOWER (c
) == L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
575 /* Match `(n-char-sequence-digit)'. */
578 const STRING_TYPE
*startp
= cp
;
581 while ((*cp
>= L_('0') && *cp
<= L_('9'))
582 || (TOLOWER (*cp
) >= L_('a') && TOLOWER (*cp
) <= L_('z'))
586 /* The closing brace is missing. Only match the NAN
591 /* This is a system-dependent way to specify the
592 bitmask used for the NaN. We expect it to be
593 a number which is put in the mantissa of the
596 unsigned long long int mant
;
598 mant
= STRTOULL (startp
+ 1, &endp
, 0);
600 SET_MANTISSA (retval
, mant
);
605 *endptr
= (STRING_TYPE
*) cp
;
610 /* It is really a text we do not recognize. */
614 /* First look whether we are faced with a hexadecimal number. */
615 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
617 /* Okay, it is a hexa-decimal number. Remember this and skip
618 the characters. BTW: hexadecimal numbers must not be
626 /* Record the start of the digits, in case we will check their grouping. */
627 start_of_digits
= startp
= cp
;
629 /* Ignore leading zeroes. This helps us to avoid useless computations. */
631 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
634 if (thousands
== NULL
)
639 /* We also have the multibyte thousands string. */
644 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
645 if (c
!= thousands
[cnt
])
647 if (thousands
[cnt
] != '\0')
655 /* If no other digit but a '0' is found the result is 0.0.
656 Return current read pointer. */
657 if ((c
< L_('0') || c
> L_('9'))
658 && (base
== 16 && (c
< (CHAR_TYPE
) TOLOWER (L_('a'))
659 || c
> (CHAR_TYPE
) TOLOWER (L_('f'))))
661 && c
!= (wint_t) decimal
663 && ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
664 if (decimal
[cnt
] != cp
[cnt
])
666 decimal
[cnt
] != '\0'; })
668 && (base
== 16 && (cp
== start_of_digits
669 || (CHAR_TYPE
) TOLOWER (c
) != L_('p')))
670 && (base
!= 16 && (CHAR_TYPE
) TOLOWER (c
) != L_('e')))
673 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
676 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
679 /* If TP is at the start of the digits, there was no correctly
680 grouped prefix of the string; so no number found. */
681 RETURN (0.0, tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
684 /* Remember first significant digit and read following characters until the
685 decimal point, exponent character or any non-FP number character. */
690 if ((c
>= L_('0') && c
<= L_('9'))
691 || (base
== 16 && (wint_t) TOLOWER (c
) >= L_('a')
692 && (wint_t) TOLOWER (c
) <= L_('f')))
697 if ((wint_t) thousands
== L
'\0' || c
!= (wint_t) thousands
)
698 /* Not a digit or separator: end of the integer part. */
701 if (thousands
== NULL
)
705 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
706 if (thousands
[cnt
] != cp
[cnt
])
708 if (thousands
[cnt
] != '\0')
716 if (grouping
&& dig_no
> 0)
718 /* Check the grouping of the digits. */
720 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
723 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
728 /* Less than the entire string was correctly grouped. */
730 if (tp
== start_of_digits
)
731 /* No valid group of numbers at all: no valid number. */
735 /* The number is validly grouped, but consists
736 only of zeroes. The whole value is zero. */
739 /* Recompute DIG_NO so we won't read more digits than
740 are properly grouped. */
743 for (tp
= startp
; tp
< cp
; ++tp
)
744 if (*tp
>= L_('0') && *tp
<= L_('9'))
754 /* We have the number digits in the integer part. Whether these are all or
755 any is really a fractional digit will be decided later. */
757 lead_zero
= int_no
== 0 ? -1 : 0;
759 /* Read the fractional digits. A special case are the 'american style'
760 numbers like `16.' i.e. with decimal but without trailing digits. */
763 c
== (wint_t) decimal
765 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
766 if (decimal
[cnt
] != cp
[cnt
])
768 decimal
[cnt
] == '\0'; })
774 while ((c
>= L_('0') && c
<= L_('9')) ||
775 (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
777 if (c
!= L_('0') && lead_zero
== -1)
778 lead_zero
= dig_no
- int_no
;
784 /* Remember start of exponent (if any). */
788 if ((base
== 16 && TOLOWER (c
) == L_('p'))
789 || (base
!= 16 && TOLOWER (c
) == L_('e')))
791 int exp_negative
= 0;
799 else if (c
== L_('+'))
802 if (c
>= L_('0') && c
<= L_('9'))
806 /* Get the exponent limit. */
808 exp_limit
= (exp_negative
?
809 -MIN_EXP
+ MANT_DIG
+ 4 * int_no
:
810 MAX_EXP
- 4 * int_no
+ lead_zero
);
812 exp_limit
= (exp_negative
?
813 -MIN_10_EXP
+ MANT_DIG
+ int_no
:
814 MAX_10_EXP
- int_no
+ lead_zero
);
820 if (exponent
> exp_limit
)
821 /* The exponent is too large/small to represent a valid
826 /* We have to take care for special situation: a joker
827 might have written "0.0e100000" which is in fact
830 result
= negative
? -0.0 : 0.0;
833 /* Overflow or underflow. */
834 __set_errno (ERANGE
);
835 result
= (exp_negative
? 0.0 :
836 negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
);
839 /* Accept all following digits as part of the exponent. */
842 while (*cp
>= L_('0') && *cp
<= L_('9'));
848 exponent
+= c
- L_('0');
851 while (c
>= L_('0') && c
<= L_('9'));
854 exponent
= -exponent
;
860 /* We don't want to have to work with trailing zeroes after the radix. */
863 while (expp
[-1] == L_('0'))
868 assert (dig_no
>= int_no
);
871 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
874 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
877 if (expp
[-1] != L_('0'))
885 while (dig_no
> 0 && exponent
< 0);
889 /* The whole string is parsed. Store the address of the next character. */
891 *endptr
= (STRING_TYPE
*) cp
;
894 return negative
? -0.0 : 0.0;
898 /* Find the decimal point */
900 while (*startp
!= decimal
)
905 if (*startp
== decimal
[0])
907 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
908 if (decimal
[cnt
] != startp
[cnt
])
910 if (decimal
[cnt
] == '\0')
916 startp
+= lead_zero
+ decimal_len
;
917 exponent
-= base
== 16 ? 4 * lead_zero
: lead_zero
;
921 /* If the BASE is 16 we can use a simpler algorithm. */
924 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
925 4, 4, 4, 4, 4, 4, 4, 4 };
926 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
927 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
930 while (!ISXDIGIT (*startp
))
932 while (*startp
== L_('0'))
934 if (ISDIGIT (*startp
))
935 val
= *startp
++ - L_('0');
937 val
= 10 + TOLOWER (*startp
++) - L_('a');
939 /* We cannot have a leading zero. */
942 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
944 /* We don't have to care for wrapping. This is the normal
945 case so we add the first clause in the `if' expression as
946 an optimization. It is a compile-time constant and so does
947 not cost anything. */
948 retval
[idx
] = val
<< (pos
- bits
+ 1);
953 retval
[idx
--] = val
>> (bits
- pos
- 1);
954 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
955 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
958 /* Adjust the exponent for the bits we are shifting in. */
959 exponent
+= bits
- 1 + (int_no
- 1) * 4;
961 while (--dig_no
> 0 && idx
>= 0)
963 if (!ISXDIGIT (*startp
))
964 startp
+= decimal_len
;
965 if (ISDIGIT (*startp
))
966 val
= *startp
++ - L_('0');
968 val
= 10 + TOLOWER (*startp
++) - L_('a');
972 retval
[idx
] |= val
<< (pos
- 4 + 1);
977 retval
[idx
--] |= val
>> (4 - pos
- 1);
978 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
980 return round_and_return (retval
, exponent
, negative
, val
,
981 BITS_PER_MP_LIMB
- 1, dig_no
> 0);
984 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
988 /* We ran out of digits. */
989 MPN_ZERO (retval
, idx
);
991 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
994 /* Now we have the number of digits in total and the integer digits as well
995 as the exponent and its sign. We can decide whether the read digits are
996 really integer digits or belong to the fractional part; i.e. we normalize
999 register int incr
= (exponent
< 0 ? MAX (-int_no
, exponent
)
1000 : MIN (dig_no
- int_no
, exponent
));
1005 if (int_no
+ exponent
> MAX_10_EXP
+ 1)
1007 __set_errno (ERANGE
);
1008 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1011 if (exponent
< MIN_10_EXP
- (DIG
+ 1))
1013 __set_errno (ERANGE
);
1019 /* Read the integer part as a multi-precision number to NUM. */
1020 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1021 #ifndef USE_WIDE_CHAR
1022 , decimal
, decimal_len
, thousands
1028 /* We now multiply the gained number by the given power of ten. */
1029 mp_limb_t
*psrc
= num
;
1030 mp_limb_t
*pdest
= den
;
1032 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1036 if ((exponent
& expbit
) != 0)
1038 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1042 /* FIXME: not the whole multiplication has to be
1043 done. If we have the needed number of bits we
1044 only need the information whether more non-zero
1046 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1047 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1048 &__tens
[ttab
->arrayoff
1049 + _FPIO_CONST_OFFSET
],
1052 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1053 + _FPIO_CONST_OFFSET
],
1054 size
, psrc
, numsize
);
1058 (void) SWAP (psrc
, pdest
);
1063 while (exponent
!= 0);
1066 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1069 /* Determine how many bits of the result we already have. */
1070 count_leading_zeros (bits
, num
[numsize
- 1]);
1071 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1073 /* Now we know the exponent of the number in base two.
1074 Check it against the maximum possible exponent. */
1077 __set_errno (ERANGE
);
1078 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1081 /* We have already the first BITS bits of the result. Together with
1082 the information whether more non-zero bits follow this is enough
1083 to determine the result. */
1084 if (bits
> MANT_DIG
)
1087 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1088 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1089 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1091 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1095 memcpy (retval
, &num
[least_idx
],
1096 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1099 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1100 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1102 << (BITS_PER_MP_LIMB
- least_bit
));
1103 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1104 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1107 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1108 for (i
= 0; num
[i
] == 0; ++i
)
1111 return round_and_return (retval
, bits
- 1, negative
,
1112 num
[round_idx
], round_bit
,
1113 int_no
< dig_no
|| i
< round_idx
);
1116 else if (dig_no
== int_no
)
1118 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1119 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1121 if (target_bit
== is_bit
)
1123 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1124 numsize
* sizeof (mp_limb_t
));
1125 /* FIXME: the following loop can be avoided if we assume a
1126 maximal MANT_DIG value. */
1127 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1129 else if (target_bit
> is_bit
)
1131 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1132 num
, numsize
, target_bit
- is_bit
);
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
);
1140 assert (numsize
< RETURN_LIMB_SIZE
);
1142 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1143 num
, numsize
, is_bit
- target_bit
);
1144 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1145 /* FIXME: the following loop can be avoided if we assume a
1146 maximal MANT_DIG value. */
1147 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1150 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1154 /* Store the bits we already have. */
1155 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1156 #if RETURN_LIMB_SIZE > 1
1157 if (numsize
< RETURN_LIMB_SIZE
)
1158 # if RETURN_LIMB_SIZE == 2
1159 retval
[numsize
] = 0;
1161 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1166 /* We have to compute at least some of the fractional digits. */
1168 /* We construct a fraction and the result of the division gives us
1169 the needed digits. The denominator is 1.0 multiplied by the
1170 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1171 123e-6 gives 123 / 1000000. */
1177 mp_limb_t
*psrc
= den
;
1178 mp_limb_t
*pdest
= num
;
1179 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1181 assert (dig_no
> int_no
&& exponent
<= 0);
1184 /* For the fractional part we need not process too many digits. One
1185 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1187 digits we should have enough bits for the result. The remaining
1188 decimal digits give us the information that more bits are following.
1189 This can be used while rounding. (Two added as a safety margin.) */
1190 if (dig_no
- int_no
> (MANT_DIG
- bits
+ 2) / 3 + 2)
1192 dig_no
= int_no
+ (MANT_DIG
- bits
+ 2) / 3 + 2;
1198 neg_exp
= dig_no
- int_no
- exponent
;
1200 /* Construct the denominator. */
1205 if ((neg_exp
& expbit
) != 0)
1212 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1213 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1214 densize
* sizeof (mp_limb_t
));
1218 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1219 + _FPIO_CONST_OFFSET
],
1220 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1222 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1225 (void) SWAP (psrc
, pdest
);
1231 while (neg_exp
!= 0);
1234 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1236 /* Read the fractional digits from the string. */
1237 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1238 #ifndef USE_WIDE_CHAR
1239 , decimal
, decimal_len
, thousands
1243 /* We now have to shift both numbers so that the highest bit in the
1244 denominator is set. In the same process we copy the numerator to
1245 a high place in the array so that the division constructs the wanted
1246 digits. This is done by a "quasi fix point" number representation.
1248 num: ddddddddddd . 0000000000000000000000
1250 den: ddddddddddd n >= m
1254 count_leading_zeros (cnt
, den
[densize
- 1]);
1258 /* Don't call `mpn_shift' with a count of zero since the specification
1259 does not allow this. */
1260 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1261 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1263 num
[numsize
++] = cy
;
1266 /* Now we are ready for the division. But it is not necessary to
1267 do a full multi-precision division because we only need a small
1268 number of bits for the result. So we do not use __mpn_divmod
1269 here but instead do the division here by hand and stop whenever
1270 the needed number of bits is reached. The code itself comes
1271 from the GNU MP Library by Torbj\"orn Granlund. */
1279 mp_limb_t d
, n
, quot
;
1284 assert (numsize
== 1 && n
< d
);
1288 udiv_qrnnd (quot
, n
, n
, 0, d
);
1295 cnt = BITS_PER_MP_LIMB; \
1297 count_leading_zeros (cnt, quot); \
1299 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1301 used = MANT_DIG + cnt; \
1302 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1303 bits = MANT_DIG + 1; \
1307 /* Note that we only clear the second element. */ \
1308 /* The conditional is determined at compile time. */ \
1309 if (RETURN_LIMB_SIZE > 1) \
1315 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1316 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1320 used = MANT_DIG - bits; \
1322 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1324 bits += BITS_PER_MP_LIMB
1328 while (bits
<= MANT_DIG
);
1330 return round_and_return (retval
, exponent
- 1, negative
,
1331 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1332 more_bits
|| n
!= 0);
1336 mp_limb_t d0
, d1
, n0
, n1
;
1343 if (numsize
< densize
)
1347 /* The numerator of the number occupies fewer bits than
1348 the denominator but the one limb is bigger than the
1349 high limb of the numerator. */
1356 exponent
-= BITS_PER_MP_LIMB
;
1359 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1360 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1361 BITS_PER_MP_LIMB
, 0);
1364 used
= MANT_DIG
- bits
;
1366 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1368 bits
+= BITS_PER_MP_LIMB
;
1380 while (bits
<= MANT_DIG
)
1386 /* QUOT should be either 111..111 or 111..110. We need
1387 special treatment of this rare case as normal division
1388 would give overflow. */
1389 quot
= ~(mp_limb_t
) 0;
1392 if (r
< d1
) /* Carry in the addition? */
1394 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1397 n1
= d0
- (d0
!= 0);
1402 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1403 umul_ppmm (n1
, n0
, d0
, quot
);
1407 if (n1
> r
|| (n1
== r
&& n0
> 0))
1409 /* The estimated QUOT was too large. */
1412 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1414 if (r
>= d1
) /* If not carry, test QUOT again. */
1417 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1423 return round_and_return (retval
, exponent
- 1, negative
,
1424 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1425 more_bits
|| n1
!= 0 || n0
!= 0);
1430 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1434 dX
= den
[densize
- 1];
1435 d1
= den
[densize
- 2];
1437 /* The division does not work if the upper limb of the two-limb
1438 numerator is greater than the denominator. */
1439 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1442 if (numsize
< densize
)
1444 mp_size_t empty
= densize
- numsize
;
1448 exponent
-= empty
* BITS_PER_MP_LIMB
;
1451 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1453 /* We make a difference here because the compiler
1454 cannot optimize the `else' case that good and
1455 this reflects all currently used FLOAT types
1456 and GMP implementations. */
1457 #if RETURN_LIMB_SIZE <= 2
1458 assert (empty
== 1);
1459 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1460 BITS_PER_MP_LIMB
, 0);
1462 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1463 retval
[i
] = retval
[i
- empty
];
1470 used
= MANT_DIG
- bits
;
1471 if (used
>= BITS_PER_MP_LIMB
)
1474 (void) __mpn_lshift (&retval
[used
1475 / BITS_PER_MP_LIMB
],
1476 retval
, RETURN_LIMB_SIZE
,
1477 used
% BITS_PER_MP_LIMB
);
1478 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1482 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1484 bits
+= empty
* BITS_PER_MP_LIMB
;
1486 for (i
= numsize
; i
> 0; --i
)
1487 num
[i
+ empty
] = num
[i
- 1];
1488 MPN_ZERO (num
, empty
+ 1);
1493 assert (numsize
== densize
);
1494 for (i
= numsize
; i
> 0; --i
)
1495 num
[i
] = num
[i
- 1];
1501 while (bits
<= MANT_DIG
)
1504 /* This might over-estimate QUOT, but it's probably not
1505 worth the extra code here to find out. */
1506 quot
= ~(mp_limb_t
) 0;
1511 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1512 umul_ppmm (n1
, n0
, d1
, quot
);
1514 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1518 if (r
< dX
) /* I.e. "carry in previous addition?" */
1525 /* Possible optimization: We already have (q * n0) and (1 * n1)
1526 after the calculation of QUOT. Taking advantage of this, we
1527 could make this loop make two iterations less. */
1529 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1531 if (num
[densize
] != cy
)
1533 cy
= __mpn_add_n (num
, num
, den
, densize
);
1537 n0
= num
[densize
] = num
[densize
- 1];
1538 for (i
= densize
- 1; i
> 0; --i
)
1539 num
[i
] = num
[i
- 1];
1544 for (i
= densize
; num
[i
] == 0 && i
>= 0; --i
)
1546 return round_and_return (retval
, exponent
- 1, negative
,
1547 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1548 more_bits
|| i
>= 0);
1555 #if defined _LIBC && !defined USE_WIDE_CHAR
1556 libc_hidden_def (INTERNAL (__STRTOF
))
1559 /* External user entry point. */
1562 #ifdef weak_function
1565 __STRTOF (nptr
, endptr
, loc
)
1566 const STRING_TYPE
*nptr
;
1567 STRING_TYPE
**endptr
;
1570 return INTERNAL (__STRTOF
) (nptr
, endptr
, 0, loc
);
1572 weak_alias (__STRTOF
, STRTOF
)