1 /* Read decimal floating point numbers.
2 This file is part of the GNU C Library.
3 Copyright (C) 1995,96,97,98,99,2000,01,02 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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
21 /* Configuration part. These macros are defined by `strtold.c',
22 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
23 `long double' and `float' versions of the reader. */
28 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
29 # define STRTOF __wcstod_l
31 # define STRTOF wcstod
34 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
35 # define STRTOF __strtod_l
37 # define STRTOF strtod
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. */
69 #include <gmp-mparam.h>
71 #include "fpioconst.h"
77 /* We use this code also for the extended locale handling where the
78 function gets as an additional argument the locale which has to be
79 used. To access the values we have to redefine the _NL_CURRENT
81 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
83 # define _NL_CURRENT(category, item) \
84 (current->values[_NL_ITEM_INDEX (item)].string)
85 # define LOCALE_PARAM , loc
86 # define LOCALE_PARAM_DECL __locale_t loc;
89 # define LOCALE_PARAM_DECL
92 #if defined _LIBC || defined HAVE_WCHAR_H
98 # define STRING_TYPE wchar_t
99 # define CHAR_TYPE wint_t
100 # define L_(Ch) L##Ch
101 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
102 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
103 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
104 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
105 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
106 # define STRNCASECMP(S1, S2, N) __wcsncasecmp_l ((S1), (S2), (N), loc)
107 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
109 # define ISSPACE(Ch) iswspace (Ch)
110 # define ISDIGIT(Ch) iswdigit (Ch)
111 # define ISXDIGIT(Ch) iswxdigit (Ch)
112 # define TOLOWER(Ch) towlower (Ch)
113 # define STRNCASECMP(S1, S2, N) __wcsncasecmp ((S1), (S2), (N))
114 # define STRTOULL(S, E, B) __wcstoull_internal ((S), (E), (B), 0)
117 # define STRING_TYPE char
118 # define CHAR_TYPE char
120 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
121 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
122 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
123 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
124 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
125 # define STRNCASECMP(S1, S2, N) __strncasecmp_l ((S1), (S2), (N), loc)
126 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
128 # define ISSPACE(Ch) isspace (Ch)
129 # define ISDIGIT(Ch) isdigit (Ch)
130 # define ISXDIGIT(Ch) isxdigit (Ch)
131 # define TOLOWER(Ch) tolower (Ch)
132 # define STRNCASECMP(S1, S2, N) __strncasecmp ((S1), (S2), (N))
133 # define STRTOULL(S, E, B) __strtoull_internal ((S), (E), 0, (B))
138 /* Constants we need from float.h; select the set for the FLOAT precision. */
139 #define MANT_DIG PASTE(FLT,_MANT_DIG)
140 #define DIG PASTE(FLT,_DIG)
141 #define MAX_EXP PASTE(FLT,_MAX_EXP)
142 #define MIN_EXP PASTE(FLT,_MIN_EXP)
143 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
144 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
146 /* Extra macros required to get FLT expanded before the pasting. */
147 #define PASTE(a,b) PASTE1(a,b)
148 #define PASTE1(a,b) a##b
150 /* Function to construct a floating point number from an MP integer
151 containing the fraction bits, a base 2 exponent, and a sign flag. */
152 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
154 /* Definitions according to limb size used. */
155 #if BITS_PER_MP_LIMB == 32
156 # define MAX_DIG_PER_LIMB 9
157 # define MAX_FAC_PER_LIMB 1000000000UL
158 #elif BITS_PER_MP_LIMB == 64
159 # define MAX_DIG_PER_LIMB 19
160 # define MAX_FAC_PER_LIMB 10000000000000000000UL
162 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
166 /* Local data structure. */
167 static const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1] =
170 1000000, 10000000, 100000000,
172 #if BITS_PER_MP_LIMB > 32
173 , 10000000000U, 100000000000U,
174 1000000000000U, 10000000000000U, 100000000000000U,
175 1000000000000000U, 10000000000000000U, 100000000000000000U,
176 1000000000000000000U, 10000000000000000000U
178 #if BITS_PER_MP_LIMB > 64
179 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
184 #define howmany(x,y) (((x)+((y)-1))/(y))
186 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
188 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
189 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
190 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
192 #define RETURN(val,end) \
193 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
194 return val; } while (0)
196 /* Maximum size necessary for mpn integers to hold floating point numbers. */
197 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
199 /* Declare an mpn integer variable that big. */
200 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
201 /* Copy an mpn integer value. */
202 #define MPN_ASSIGN(dst, src) \
203 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
206 /* Return a floating point number of the needed type according to the given
207 multi-precision number after possible rounding. */
209 round_and_return (mp_limb_t
*retval
, int exponent
, int negative
,
210 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
212 if (exponent
< MIN_EXP
- 1)
214 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
216 if (shift
> MANT_DIG
)
222 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
223 if (shift
== MANT_DIG
)
224 /* This is a special case to handle the very seldom case where
225 the mantissa will be empty after the shift. */
229 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
230 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
231 for (i
= 0; i
< RETURN_LIMB_SIZE
; ++i
)
232 more_bits
|= retval
[i
] != 0;
233 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
235 else if (shift
>= BITS_PER_MP_LIMB
)
239 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
240 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
241 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
242 more_bits
|= retval
[i
] != 0;
243 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
246 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
247 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
248 shift
% BITS_PER_MP_LIMB
);
249 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
250 shift
/ BITS_PER_MP_LIMB
);
254 round_limb
= retval
[0];
255 round_bit
= shift
- 1;
256 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
258 /* This is a hook for the m68k long double format, where the
259 exponent bias is the same for normalized and denormalized
262 # define DENORM_EXP (MIN_EXP - 2)
264 exponent
= DENORM_EXP
;
267 if ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
268 && (more_bits
|| (retval
[0] & 1) != 0
269 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
271 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
273 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
274 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
275 (retval
[RETURN_LIMB_SIZE
- 1]
276 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
279 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
280 retval
[RETURN_LIMB_SIZE
- 1]
281 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
283 else if (exponent
== DENORM_EXP
284 && (retval
[RETURN_LIMB_SIZE
- 1]
285 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
287 /* The number was denormalized but now normalized. */
288 exponent
= MIN_EXP
- 1;
291 if (exponent
> MAX_EXP
)
292 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
294 return MPN2FLOAT (retval
, exponent
, negative
);
298 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
299 into N. Return the size of the number limbs in NSIZE at the first
300 character od the string that is not part of the integer as the function
301 value. If the EXPONENT is small enough to be taken as an additional
302 factor for the resulting number (see code) multiply by it. */
303 static inline const STRING_TYPE
*
304 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
306 #ifndef USE_WIDE_CHAR
307 , const char *decimal
, size_t decimal_len
, const char *thousands
312 /* Number of digits for actual limb. */
321 if (cnt
== MAX_DIG_PER_LIMB
)
331 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
332 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
343 /* There might be thousands separators or radix characters in
344 the string. But these all can be ignored because we know the
345 format of the number is correct and we have an exact number
346 of characters to read. */
348 if (*str
< L
'0' || *str
> L
'9')
351 if (*str
< '0' || *str
> '9')
354 if (thousands
!= NULL
&& *str
== *thousands
355 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
356 if (thousands
[inner
] != str
[inner
])
358 thousands
[inner
] == '\0'; }))
364 low
= low
* 10 + *str
++ - L_('0');
367 while (--digcnt
> 0);
369 if (*exponent
> 0 && cnt
+ *exponent
<= MAX_DIG_PER_LIMB
)
371 low
*= _tens_in_limb
[*exponent
];
372 start
= _tens_in_limb
[cnt
+ *exponent
];
376 start
= _tens_in_limb
[cnt
];
386 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
387 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
396 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
397 with the COUNT most significant bits of LIMB.
399 Tege doesn't like this function so I have to write it here myself. :)
402 __mpn_lshift_1 (mp_limb_t
*ptr
, mp_size_t size
, unsigned int count
,
405 if (count
== BITS_PER_MP_LIMB
)
407 /* Optimize the case of shifting by exactly a word:
408 just copy words, with no actual bit-shifting. */
410 for (i
= size
- 1; i
> 0; --i
)
416 (void) __mpn_lshift (ptr
, ptr
, size
, count
);
417 ptr
[0] |= limb
>> (BITS_PER_MP_LIMB
- count
);
422 #define INTERNAL(x) INTERNAL1(x)
423 #define INTERNAL1(x) __##x##_internal
425 /* This file defines a function to check for correct grouping. */
426 #include "grouping.h"
429 /* Return a floating point number with the value of the given string NPTR.
430 Set *ENDPTR to the character after the last used one. If the number is
431 smaller than the smallest representable number, set `errno' to ERANGE and
432 return 0.0. If the number is too big to be represented, set `errno' to
433 ERANGE and return HUGE_VAL with the appropriate sign. */
435 INTERNAL (STRTOF
) (nptr
, endptr
, group LOCALE_PARAM
)
436 const STRING_TYPE
*nptr
;
437 STRING_TYPE
**endptr
;
441 int negative
; /* The sign of the number. */
442 MPN_VAR (num
); /* MP representation of the number. */
443 int exponent
; /* Exponent of the number. */
445 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
448 /* When we have to compute fractional digits we form a fraction with a
449 second multi-precision number (and we sometimes need a second for
450 temporary results). */
453 /* Representation for the return value. */
454 mp_limb_t retval
[RETURN_LIMB_SIZE
];
455 /* Number of bits currently in result value. */
458 /* Running pointer after the last character processed in the string. */
459 const STRING_TYPE
*cp
, *tp
;
460 /* Start of significant part of the number. */
461 const STRING_TYPE
*startp
, *start_of_digits
;
462 /* Points at the character following the integer and fractional digits. */
463 const STRING_TYPE
*expp
;
464 /* Total number of digit and number of digits in integer part. */
465 int dig_no
, int_no
, lead_zero
;
466 /* Contains the last character read. */
469 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
470 there. So define it ourselves if it remains undefined. */
472 typedef unsigned int wint_t;
474 /* The radix character of the current locale. */
481 /* The thousands character of the current locale. */
483 wchar_t thousands
= L
'\0';
485 const char *thousands
= NULL
;
487 /* The numeric grouping specification of the current locale,
488 in the format described in <locale.h>. */
489 const char *grouping
;
490 /* Used in several places. */
493 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
494 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
499 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
500 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
504 /* Figure out the thousands separator character. */
506 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
507 _NL_NUMERIC_THOUSANDS_SEP_WC
);
508 if (thousands
== L
'\0')
511 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
512 if (*thousands
== '\0')
523 /* Find the locale's decimal point character. */
525 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
526 assert (decimal
!= L
'\0');
527 # define decimal_len 1
529 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
530 decimal_len
= strlen (decimal
);
531 assert (decimal_len
> 0);
534 /* Prepare number representation. */
539 /* Parse string to get maximal legal prefix. We need the number of
540 characters of the integer part, the fractional part and the exponent. */
542 /* Ignore leading white space. */
547 /* Get sign of the result. */
553 else if (c
== L_('+'))
556 /* Return 0.0 if no legal string is found.
557 No character is used even if a sign was found. */
559 if (c
== (wint_t) decimal
560 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
562 /* We accept it. This funny construct is here only to indent
563 the code directly. */
566 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
567 if (cp
[cnt
] != decimal
[cnt
])
569 if (decimal
[cnt
] == '\0' && cp
[1] >= '0' && cp
[1] <= '9')
571 /* We accept it. This funny construct is here only to indent
572 the code directly. */
575 else if (c
< L_('0') || c
> L_('9'))
577 /* Check for `INF' or `INFINITY'. */
578 if (TOLOWER (c
) == L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
580 /* Return +/- infinity. */
582 *endptr
= (STRING_TYPE
*)
583 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
586 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
589 if (TOLOWER (c
) == L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
596 /* Match `(n-char-sequence-digit)'. */
599 const STRING_TYPE
*startp
= cp
;
602 while ((*cp
>= L_('0') && *cp
<= L_('9'))
603 || (TOLOWER (*cp
) >= L_('a') && TOLOWER (*cp
) <= L_('z'))
607 /* The closing brace is missing. Only match the NAN
612 /* This is a system-dependent way to specify the
613 bitmask used for the NaN. We expect it to be
614 a number which is put in the mantissa of the
617 unsigned long long int mant
;
619 mant
= STRTOULL (startp
+ 1, &endp
, 0);
621 SET_MANTISSA (retval
, mant
);
626 *endptr
= (STRING_TYPE
*) cp
;
631 /* It is really a text we do not recognize. */
635 /* First look whether we are faced with a hexadecimal number. */
636 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
638 /* Okay, it is a hexa-decimal number. Remember this and skip
639 the characters. BTW: hexadecimal numbers must not be
647 /* Record the start of the digits, in case we will check their grouping. */
648 start_of_digits
= startp
= cp
;
650 /* Ignore leading zeroes. This helps us to avoid useless computations. */
652 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
655 if (thousands
== NULL
)
660 /* We also have the multibyte thousands string. */
665 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
666 if (c
!= thousands
[cnt
])
668 if (thousands
[cnt
] != '\0')
676 /* If no other digit but a '0' is found the result is 0.0.
677 Return current read pointer. */
678 if ((c
< L_('0') || c
> L_('9'))
679 && (base
== 16 && (c
< (CHAR_TYPE
) TOLOWER (L_('a'))
680 || c
> (CHAR_TYPE
) TOLOWER (L_('f'))))
682 && c
!= (wint_t) decimal
684 && ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
685 if (decimal
[cnt
] != cp
[cnt
])
687 decimal
[cnt
] != '\0'; })
689 && (base
== 16 && (cp
== start_of_digits
690 || (CHAR_TYPE
) TOLOWER (c
) != L_('p')))
691 && (base
!= 16 && (CHAR_TYPE
) TOLOWER (c
) != L_('e')))
693 tp
= correctly_grouped_prefix (start_of_digits
, cp
, thousands
, grouping
);
694 /* If TP is at the start of the digits, there was no correctly
695 grouped prefix of the string; so no number found. */
696 RETURN (0.0, tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
699 /* Remember first significant digit and read following characters until the
700 decimal point, exponent character or any non-FP number character. */
705 if ((c
>= L_('0') && c
<= L_('9'))
706 || (base
== 16 && (wint_t) TOLOWER (c
) >= L_('a')
707 && (wint_t) TOLOWER (c
) <= L_('f')))
712 if ((wint_t) thousands
== L
'\0' || c
!= (wint_t) thousands
)
713 /* Not a digit or separator: end of the integer part. */
716 if (thousands
== NULL
)
720 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
721 if (thousands
[cnt
] != cp
[cnt
])
723 if (thousands
[cnt
] != '\0')
731 if (grouping
&& dig_no
> 0)
733 /* Check the grouping of the digits. */
734 tp
= correctly_grouped_prefix (start_of_digits
, cp
, thousands
, grouping
);
737 /* Less than the entire string was correctly grouped. */
739 if (tp
== start_of_digits
)
740 /* No valid group of numbers at all: no valid number. */
744 /* The number is validly grouped, but consists
745 only of zeroes. The whole value is zero. */
748 /* Recompute DIG_NO so we won't read more digits than
749 are properly grouped. */
752 for (tp
= startp
; tp
< cp
; ++tp
)
753 if (*tp
>= L_('0') && *tp
<= L_('9'))
763 /* We have the number digits in the integer part. Whether these are all or
764 any is really a fractional digit will be decided later. */
766 lead_zero
= int_no
== 0 ? -1 : 0;
768 /* Read the fractional digits. A special case are the 'american style'
769 numbers like `16.' i.e. with decimal but without trailing digits. */
772 c
== (wint_t) decimal
774 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
775 if (decimal
[cnt
] != cp
[cnt
])
777 decimal
[cnt
] == '\0'; })
783 while ((c
>= L_('0') && c
<= L_('9')) ||
784 (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
786 if (c
!= L_('0') && lead_zero
== -1)
787 lead_zero
= dig_no
- int_no
;
793 /* Remember start of exponent (if any). */
797 if ((base
== 16 && TOLOWER (c
) == L_('p'))
798 || (base
!= 16 && TOLOWER (c
) == L_('e')))
800 int exp_negative
= 0;
808 else if (c
== L_('+'))
811 if (c
>= L_('0') && c
<= L_('9'))
815 /* Get the exponent limit. */
817 exp_limit
= (exp_negative
?
818 -MIN_EXP
+ MANT_DIG
+ 4 * int_no
:
819 MAX_EXP
- 4 * int_no
+ lead_zero
);
821 exp_limit
= (exp_negative
?
822 -MIN_10_EXP
+ MANT_DIG
+ int_no
:
823 MAX_10_EXP
- int_no
+ lead_zero
);
829 if (exponent
> exp_limit
)
830 /* The exponent is too large/small to represent a valid
835 /* We have to take care for special situation: a joker
836 might have written "0.0e100000" which is in fact
839 result
= negative
? -0.0 : 0.0;
842 /* Overflow or underflow. */
843 __set_errno (ERANGE
);
844 result
= (exp_negative
? 0.0 :
845 negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
);
848 /* Accept all following digits as part of the exponent. */
851 while (*cp
>= L_('0') && *cp
<= L_('9'));
857 exponent
+= c
- L_('0');
860 while (c
>= L_('0') && c
<= L_('9'));
863 exponent
= -exponent
;
869 /* We don't want to have to work with trailing zeroes after the radix. */
872 while (expp
[-1] == L_('0'))
877 assert (dig_no
>= int_no
);
880 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
883 while (expp
[-1] < L_('0') || expp
[-1] > L_('9'))
886 if (expp
[-1] != L_('0'))
894 while (dig_no
> 0 && exponent
< 0);
898 /* The whole string is parsed. Store the address of the next character. */
900 *endptr
= (STRING_TYPE
*) cp
;
903 return negative
? -0.0 : 0.0;
907 /* Find the decimal point */
909 while (*startp
!= decimal
)
914 if (*startp
== decimal
[0])
916 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
917 if (decimal
[cnt
] != startp
[cnt
])
919 if (decimal
[cnt
] == '\0')
925 startp
+= lead_zero
+ decimal_len
;
926 exponent
-= base
== 16 ? 4 * lead_zero
: lead_zero
;
930 /* If the BASE is 16 we can use a simpler algorithm. */
933 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
934 4, 4, 4, 4, 4, 4, 4, 4 };
935 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
936 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
939 while (!ISXDIGIT (*startp
))
941 while (*startp
== L_('0'))
943 if (ISDIGIT (*startp
))
944 val
= *startp
++ - L_('0');
946 val
= 10 + TOLOWER (*startp
++) - L_('a');
948 /* We cannot have a leading zero. */
951 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
953 /* We don't have to care for wrapping. This is the normal
954 case so we add the first clause in the `if' expression as
955 an optimization. It is a compile-time constant and so does
956 not cost anything. */
957 retval
[idx
] = val
<< (pos
- bits
+ 1);
962 retval
[idx
--] = val
>> (bits
- pos
- 1);
963 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
964 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
967 /* Adjust the exponent for the bits we are shifting in. */
968 exponent
+= bits
- 1 + (int_no
- 1) * 4;
970 while (--dig_no
> 0 && idx
>= 0)
972 if (!ISXDIGIT (*startp
))
973 startp
+= decimal_len
;
974 if (ISDIGIT (*startp
))
975 val
= *startp
++ - L_('0');
977 val
= 10 + TOLOWER (*startp
++) - L_('a');
981 retval
[idx
] |= val
<< (pos
- 4 + 1);
986 retval
[idx
--] |= val
>> (4 - pos
- 1);
987 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
989 return round_and_return (retval
, exponent
, negative
, val
,
990 BITS_PER_MP_LIMB
- 1, dig_no
> 0);
993 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
997 /* We ran out of digits. */
998 MPN_ZERO (retval
, idx
);
1000 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1003 /* Now we have the number of digits in total and the integer digits as well
1004 as the exponent and its sign. We can decide whether the read digits are
1005 really integer digits or belong to the fractional part; i.e. we normalize
1008 register int incr
= (exponent
< 0 ? MAX (-int_no
, exponent
)
1009 : MIN (dig_no
- int_no
, exponent
));
1014 if (int_no
+ exponent
> MAX_10_EXP
+ 1)
1016 __set_errno (ERANGE
);
1017 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1020 if (exponent
< MIN_10_EXP
- (DIG
+ 1))
1022 __set_errno (ERANGE
);
1028 /* Read the integer part as a multi-precision number to NUM. */
1029 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1030 #ifndef USE_WIDE_CHAR
1031 , decimal
, decimal_len
, thousands
1037 /* We now multiply the gained number by the given power of ten. */
1038 mp_limb_t
*psrc
= num
;
1039 mp_limb_t
*pdest
= den
;
1041 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1045 if ((exponent
& expbit
) != 0)
1047 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1051 /* FIXME: not the whole multiplication has to be
1052 done. If we have the needed number of bits we
1053 only need the information whether more non-zero
1055 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1056 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1057 &__tens
[ttab
->arrayoff
1058 + _FPIO_CONST_OFFSET
],
1061 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1062 + _FPIO_CONST_OFFSET
],
1063 size
, psrc
, numsize
);
1067 (void) SWAP (psrc
, pdest
);
1072 while (exponent
!= 0);
1075 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1078 /* Determine how many bits of the result we already have. */
1079 count_leading_zeros (bits
, num
[numsize
- 1]);
1080 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1082 /* Now we know the exponent of the number in base two.
1083 Check it against the maximum possible exponent. */
1086 __set_errno (ERANGE
);
1087 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
1090 /* We have already the first BITS bits of the result. Together with
1091 the information whether more non-zero bits follow this is enough
1092 to determine the result. */
1093 if (bits
> MANT_DIG
)
1096 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1097 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1098 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1100 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1104 memcpy (retval
, &num
[least_idx
],
1105 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1108 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1109 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1111 << (BITS_PER_MP_LIMB
- least_bit
));
1112 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1113 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1116 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1117 for (i
= 0; num
[i
] == 0; ++i
)
1120 return round_and_return (retval
, bits
- 1, negative
,
1121 num
[round_idx
], round_bit
,
1122 int_no
< dig_no
|| i
< round_idx
);
1125 else if (dig_no
== int_no
)
1127 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1128 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1130 if (target_bit
== is_bit
)
1132 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1133 numsize
* sizeof (mp_limb_t
));
1134 /* FIXME: the following loop can be avoided if we assume a
1135 maximal MANT_DIG value. */
1136 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1138 else if (target_bit
> is_bit
)
1140 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1141 num
, numsize
, target_bit
- is_bit
);
1142 /* FIXME: the following loop can be avoided if we assume a
1143 maximal MANT_DIG value. */
1144 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1149 assert (numsize
< RETURN_LIMB_SIZE
);
1151 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1152 num
, numsize
, is_bit
- target_bit
);
1153 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1154 /* FIXME: the following loop can be avoided if we assume a
1155 maximal MANT_DIG value. */
1156 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1159 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1163 /* Store the bits we already have. */
1164 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1165 #if RETURN_LIMB_SIZE > 1
1166 if (numsize
< RETURN_LIMB_SIZE
)
1167 retval
[numsize
] = 0;
1171 /* We have to compute at least some of the fractional digits. */
1173 /* We construct a fraction and the result of the division gives us
1174 the needed digits. The denominator is 1.0 multiplied by the
1175 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1176 123e-6 gives 123 / 1000000. */
1182 mp_limb_t
*psrc
= den
;
1183 mp_limb_t
*pdest
= num
;
1184 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1186 assert (dig_no
> int_no
&& exponent
<= 0);
1189 /* For the fractional part we need not process too many digits. One
1190 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1192 digits we should have enough bits for the result. The remaining
1193 decimal digits give us the information that more bits are following.
1194 This can be used while rounding. (One added as a safety margin.) */
1195 if (dig_no
- int_no
> (MANT_DIG
- bits
+ 2) / 3 + 1)
1197 dig_no
= int_no
+ (MANT_DIG
- bits
+ 2) / 3 + 1;
1203 neg_exp
= dig_no
- int_no
- exponent
;
1205 /* Construct the denominator. */
1210 if ((neg_exp
& expbit
) != 0)
1217 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1218 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1219 densize
* sizeof (mp_limb_t
));
1223 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1224 + _FPIO_CONST_OFFSET
],
1225 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1227 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1230 (void) SWAP (psrc
, pdest
);
1236 while (neg_exp
!= 0);
1239 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1241 /* Read the fractional digits from the string. */
1242 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1243 #ifndef USE_WIDE_CHAR
1244 , decimal
, decimal_len
, thousands
1248 /* We now have to shift both numbers so that the highest bit in the
1249 denominator is set. In the same process we copy the numerator to
1250 a high place in the array so that the division constructs the wanted
1251 digits. This is done by a "quasi fix point" number representation.
1253 num: ddddddddddd . 0000000000000000000000
1255 den: ddddddddddd n >= m
1259 count_leading_zeros (cnt
, den
[densize
- 1]);
1263 /* Don't call `mpn_shift' with a count of zero since the specification
1264 does not allow this. */
1265 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1266 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1268 num
[numsize
++] = cy
;
1271 /* Now we are ready for the division. But it is not necessary to
1272 do a full multi-precision division because we only need a small
1273 number of bits for the result. So we do not use __mpn_divmod
1274 here but instead do the division here by hand and stop whenever
1275 the needed number of bits is reached. The code itself comes
1276 from the GNU MP Library by Torbj\"orn Granlund. */
1284 mp_limb_t d
, n
, quot
;
1289 assert (numsize
== 1 && n
< d
);
1293 udiv_qrnnd (quot
, n
, n
, 0, d
);
1300 cnt = BITS_PER_MP_LIMB; \
1302 count_leading_zeros (cnt, quot); \
1304 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1306 used = MANT_DIG + cnt; \
1307 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1308 bits = MANT_DIG + 1; \
1312 /* Note that we only clear the second element. */ \
1313 /* The conditional is determined at compile time. */ \
1314 if (RETURN_LIMB_SIZE > 1) \
1320 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1321 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1325 used = MANT_DIG - bits; \
1327 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1329 bits += BITS_PER_MP_LIMB
1333 while (bits
<= MANT_DIG
);
1335 return round_and_return (retval
, exponent
- 1, negative
,
1336 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1337 more_bits
|| n
!= 0);
1341 mp_limb_t d0
, d1
, n0
, n1
;
1348 if (numsize
< densize
)
1352 /* The numerator of the number occupies fewer bits than
1353 the denominator but the one limb is bigger than the
1354 high limb of the numerator. */
1361 exponent
-= BITS_PER_MP_LIMB
;
1364 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1365 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1366 BITS_PER_MP_LIMB
, 0);
1369 used
= MANT_DIG
- bits
;
1371 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1373 bits
+= BITS_PER_MP_LIMB
;
1385 while (bits
<= MANT_DIG
)
1391 /* QUOT should be either 111..111 or 111..110. We need
1392 special treatment of this rare case as normal division
1393 would give overflow. */
1394 quot
= ~(mp_limb_t
) 0;
1397 if (r
< d1
) /* Carry in the addition? */
1399 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1402 n1
= d0
- (d0
!= 0);
1407 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1408 umul_ppmm (n1
, n0
, d0
, quot
);
1412 if (n1
> r
|| (n1
== r
&& n0
> 0))
1414 /* The estimated QUOT was too large. */
1417 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1419 if (r
>= d1
) /* If not carry, test QUOT again. */
1422 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1428 return round_and_return (retval
, exponent
- 1, negative
,
1429 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1430 more_bits
|| n1
!= 0 || n0
!= 0);
1435 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1439 dX
= den
[densize
- 1];
1440 d1
= den
[densize
- 2];
1442 /* The division does not work if the upper limb of the two-limb
1443 numerator is greater than the denominator. */
1444 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1447 if (numsize
< densize
)
1449 mp_size_t empty
= densize
- numsize
;
1454 for (i
= numsize
; i
> 0; --i
)
1455 num
[i
+ empty
] = num
[i
- 1];
1456 MPN_ZERO (num
, empty
+ 1);
1457 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. */
1468 #if RETURN_LIMB_SIZE <= 2
1469 assert (empty
== 1);
1470 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1471 BITS_PER_MP_LIMB
, 0);
1473 for (i
= RETURN_LIMB_SIZE
; i
> empty
; --i
)
1474 retval
[i
] = retval
[i
- empty
];
1476 #if RETURN_LIMB_SIZE > 1
1479 for (i
= numsize
; i
> 0; --i
)
1480 num
[i
+ empty
] = num
[i
- 1];
1481 MPN_ZERO (num
, empty
+ 1);
1485 used
= MANT_DIG
- bits
;
1486 if (used
>= BITS_PER_MP_LIMB
)
1489 (void) __mpn_lshift (&retval
[used
1490 / BITS_PER_MP_LIMB
],
1491 retval
, RETURN_LIMB_SIZE
,
1492 used
% BITS_PER_MP_LIMB
);
1493 for (i
= used
/ BITS_PER_MP_LIMB
; i
>= 0; --i
)
1497 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1499 bits
+= empty
* BITS_PER_MP_LIMB
;
1505 assert (numsize
== densize
);
1506 for (i
= numsize
; i
> 0; --i
)
1507 num
[i
] = num
[i
- 1];
1513 while (bits
<= MANT_DIG
)
1516 /* This might over-estimate QUOT, but it's probably not
1517 worth the extra code here to find out. */
1518 quot
= ~(mp_limb_t
) 0;
1523 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1524 umul_ppmm (n1
, n0
, d1
, quot
);
1526 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1530 if (r
< dX
) /* I.e. "carry in previous addition?" */
1537 /* Possible optimization: We already have (q * n0) and (1 * n1)
1538 after the calculation of QUOT. Taking advantage of this, we
1539 could make this loop make two iterations less. */
1541 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1543 if (num
[densize
] != cy
)
1545 cy
= __mpn_add_n (num
, num
, den
, densize
);
1549 n0
= num
[densize
] = num
[densize
- 1];
1550 for (i
= densize
- 1; i
> 0; --i
)
1551 num
[i
] = num
[i
- 1];
1556 for (i
= densize
; num
[i
] == 0 && i
>= 0; --i
)
1558 return round_and_return (retval
, exponent
- 1, negative
,
1559 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1560 more_bits
|| i
>= 0);
1568 && !(defined USE_IN_EXTENDED_LOCALE_MODEL && defined USE_WIDE_CHAR)
1569 libc_hidden_def (INTERNAL (STRTOF
))
1572 /* External user entry point. */
1575 #ifdef weak_function
1578 STRTOF (nptr
, endptr LOCALE_PARAM
)
1579 const STRING_TYPE
*nptr
;
1580 STRING_TYPE
**endptr
;
1583 return INTERNAL (STRTOF
) (nptr
, endptr
, 0 LOCALE_PARAM
);