1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2016 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, see
18 <http://www.gnu.org/licenses/>. */
22 extern double ____strtod_l_internal (const char *, char **, int, __locale_t
);
24 /* Configuration part. These macros are defined by `strtold.c',
25 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
26 `long double' and `float' versions of the reader. */
28 # include <math_ldbl_opt.h>
32 # define STRTOF wcstod_l
33 # define __STRTOF __wcstod_l
34 # define STRTOF_NAN __wcstod_nan
36 # define STRTOF strtod_l
37 # define __STRTOF __strtod_l
38 # define STRTOF_NAN __strtod_nan
40 # define MPN2FLOAT __mpn_construct_double
41 # define FLOAT_HUGE_VAL HUGE_VAL
43 /* End of configuration part. */
48 #include "../locale/localeinfo.h"
51 #include <math_private.h>
55 #include <rounding-mode.h>
58 /* The gmp headers need some configuration frobs. */
61 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
62 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
63 #include <gmp-mparam.h>
67 #include "fpioconst.h"
72 /* We use this code for the extended locale handling where the
73 function gets as an additional argument the locale which has to be
74 used. To access the values we have to redefine the _NL_CURRENT and
75 _NL_CURRENT_WORD macros. */
77 #define _NL_CURRENT(category, item) \
78 (current->values[_NL_ITEM_INDEX (item)].string)
79 #undef _NL_CURRENT_WORD
80 #define _NL_CURRENT_WORD(category, item) \
81 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
83 #if defined _LIBC || defined HAVE_WCHAR_H
89 # define STRING_TYPE wchar_t
90 # define CHAR_TYPE wint_t
92 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
93 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
94 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
95 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
96 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
97 # define STRNCASECMP(S1, S2, N) \
98 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
100 # define STRING_TYPE char
101 # define CHAR_TYPE char
103 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
104 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
105 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
106 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
107 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
108 # define STRNCASECMP(S1, S2, N) \
109 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
113 /* Constants we need from float.h; select the set for the FLOAT precision. */
114 #define MANT_DIG PASTE(FLT,_MANT_DIG)
115 #define DIG PASTE(FLT,_DIG)
116 #define MAX_EXP PASTE(FLT,_MAX_EXP)
117 #define MIN_EXP PASTE(FLT,_MIN_EXP)
118 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
119 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
120 #define MAX_VALUE PASTE(FLT,_MAX)
121 #define MIN_VALUE PASTE(FLT,_MIN)
123 /* Extra macros required to get FLT expanded before the pasting. */
124 #define PASTE(a,b) PASTE1(a,b)
125 #define PASTE1(a,b) a##b
127 /* Function to construct a floating point number from an MP integer
128 containing the fraction bits, a base 2 exponent, and a sign flag. */
129 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
131 /* Definitions according to limb size used. */
132 #if BITS_PER_MP_LIMB == 32
133 # define MAX_DIG_PER_LIMB 9
134 # define MAX_FAC_PER_LIMB 1000000000UL
135 #elif BITS_PER_MP_LIMB == 64
136 # define MAX_DIG_PER_LIMB 19
137 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
139 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
142 extern const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1];
145 #define howmany(x,y) (((x)+((y)-1))/(y))
147 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
149 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
151 #define RETURN(val,end) \
152 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
153 return val; } while (0)
155 /* Maximum size necessary for mpn integers to hold floating point
156 numbers. The largest number we need to hold is 10^n where 2^-n is
157 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
158 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
159 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
160 BITS_PER_MP_LIMB) + 2)
161 /* Declare an mpn integer variable that big. */
162 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
163 /* Copy an mpn integer value. */
164 #define MPN_ASSIGN(dst, src) \
165 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
168 /* Set errno and return an overflowing value with sign specified by
171 overflow_value (int negative
)
173 __set_errno (ERANGE
);
174 FLOAT result
= math_narrow_eval ((negative
? -MAX_VALUE
: MAX_VALUE
)
180 /* Set errno and return an underflowing value with sign specified by
183 underflow_value (int negative
)
185 __set_errno (ERANGE
);
186 FLOAT result
= math_narrow_eval ((negative
? -MIN_VALUE
: MIN_VALUE
)
192 /* Return a floating point number of the needed type according to the given
193 multi-precision number after possible rounding. */
195 round_and_return (mp_limb_t
*retval
, intmax_t exponent
, int negative
,
196 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
198 int mode
= get_rounding_mode ();
200 if (exponent
< MIN_EXP
- 1)
202 if (exponent
< MIN_EXP
- 1 - MANT_DIG
)
203 return underflow_value (negative
);
205 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
208 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
209 if (shift
== MANT_DIG
)
210 /* This is a special case to handle the very seldom case where
211 the mantissa will be empty after the shift. */
215 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
216 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
217 for (i
= 0; i
< RETURN_LIMB_SIZE
- 1; ++i
)
218 more_bits
|= retval
[i
] != 0;
219 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
221 else if (shift
>= BITS_PER_MP_LIMB
)
225 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
226 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
227 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
228 more_bits
|= retval
[i
] != 0;
229 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
232 /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB. */
233 if ((shift
% BITS_PER_MP_LIMB
) != 0)
234 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
235 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
236 shift
% BITS_PER_MP_LIMB
);
238 for (i
= 0; i
< RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
); i
++)
239 retval
[i
] = retval
[i
+ (shift
/ BITS_PER_MP_LIMB
)];
240 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
241 shift
/ BITS_PER_MP_LIMB
);
245 if (TININESS_AFTER_ROUNDING
&& shift
== 1)
247 /* Whether the result counts as tiny depends on whether,
248 after rounding to the normal precision, it still has
249 a subnormal exponent. */
250 mp_limb_t retval_normal
[RETURN_LIMB_SIZE
];
251 if (round_away (negative
,
252 (retval
[0] & 1) != 0,
254 & (((mp_limb_t
) 1) << round_bit
)) != 0,
257 & ((((mp_limb_t
) 1) << round_bit
) - 1))
261 mp_limb_t cy
= __mpn_add_1 (retval_normal
, retval
,
262 RETURN_LIMB_SIZE
, 1);
264 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
265 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
266 ((retval_normal
[RETURN_LIMB_SIZE
- 1]
267 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
)))
272 round_limb
= retval
[0];
273 round_bit
= shift
- 1;
274 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
276 /* This is a hook for the m68k long double format, where the
277 exponent bias is the same for normalized and denormalized
280 # define DENORM_EXP (MIN_EXP - 2)
282 exponent
= DENORM_EXP
;
284 && ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
286 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
288 __set_errno (ERANGE
);
289 FLOAT force_underflow
= MIN_VALUE
* MIN_VALUE
;
290 math_force_eval (force_underflow
);
294 if (exponent
> MAX_EXP
)
297 if (round_away (negative
,
298 (retval
[0] & 1) != 0,
299 (round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0,
301 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0),
304 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
306 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
307 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
308 (retval
[RETURN_LIMB_SIZE
- 1]
309 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
312 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
313 retval
[RETURN_LIMB_SIZE
- 1]
314 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
316 else if (exponent
== DENORM_EXP
317 && (retval
[RETURN_LIMB_SIZE
- 1]
318 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
320 /* The number was denormalized but now normalized. */
321 exponent
= MIN_EXP
- 1;
324 if (exponent
> MAX_EXP
)
326 return overflow_value (negative
);
328 return MPN2FLOAT (retval
, exponent
, negative
);
332 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
333 into N. Return the size of the number limbs in NSIZE at the first
334 character od the string that is not part of the integer as the function
335 value. If the EXPONENT is small enough to be taken as an additional
336 factor for the resulting number (see code) multiply by it. */
337 static const STRING_TYPE
*
338 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
340 #ifndef USE_WIDE_CHAR
341 , const char *decimal
, size_t decimal_len
, const char *thousands
346 /* Number of digits for actual limb. */
355 if (cnt
== MAX_DIG_PER_LIMB
)
365 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
366 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
369 assert (*nsize
< MPNSIZE
);
378 /* There might be thousands separators or radix characters in
379 the string. But these all can be ignored because we know the
380 format of the number is correct and we have an exact number
381 of characters to read. */
383 if (*str
< L
'0' || *str
> L
'9')
386 if (*str
< '0' || *str
> '9')
389 if (thousands
!= NULL
&& *str
== *thousands
390 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
391 if (thousands
[inner
] != str
[inner
])
393 thousands
[inner
] == '\0'; }))
399 low
= low
* 10 + *str
++ - L_('0');
402 while (--digcnt
> 0);
404 if (*exponent
> 0 && *exponent
<= MAX_DIG_PER_LIMB
- cnt
)
406 low
*= _tens_in_limb
[*exponent
];
407 start
= _tens_in_limb
[cnt
+ *exponent
];
411 start
= _tens_in_limb
[cnt
];
421 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
422 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
425 assert (*nsize
< MPNSIZE
);
434 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
435 with the COUNT most significant bits of LIMB.
437 Implemented as a macro, so that __builtin_constant_p works even at -O0.
439 Tege doesn't like this macro so I have to write it here myself. :)
441 #define __mpn_lshift_1(ptr, size, count, limb) \
444 mp_limb_t *__ptr = (ptr); \
445 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
448 for (i = (size) - 1; i > 0; --i) \
449 __ptr[i] = __ptr[i - 1]; \
454 /* We assume count > 0 && count < BITS_PER_MP_LIMB here. */ \
455 unsigned int __count = (count); \
456 (void) __mpn_lshift (__ptr, __ptr, size, __count); \
457 __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count); \
463 #define INTERNAL(x) INTERNAL1(x)
464 #define INTERNAL1(x) __##x##_internal
465 #ifndef ____STRTOF_INTERNAL
466 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
469 /* This file defines a function to check for correct grouping. */
470 #include "grouping.h"
473 /* Return a floating point number with the value of the given string NPTR.
474 Set *ENDPTR to the character after the last used one. If the number is
475 smaller than the smallest representable number, set `errno' to ERANGE and
476 return 0.0. If the number is too big to be represented, set `errno' to
477 ERANGE and return HUGE_VAL with the appropriate sign. */
479 ____STRTOF_INTERNAL (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int group
,
482 int negative
; /* The sign of the number. */
483 MPN_VAR (num
); /* MP representation of the number. */
484 intmax_t exponent
; /* Exponent of the number. */
486 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
489 /* When we have to compute fractional digits we form a fraction with a
490 second multi-precision number (and we sometimes need a second for
491 temporary results). */
494 /* Representation for the return value. */
495 mp_limb_t retval
[RETURN_LIMB_SIZE
];
496 /* Number of bits currently in result value. */
499 /* Running pointer after the last character processed in the string. */
500 const STRING_TYPE
*cp
, *tp
;
501 /* Start of significant part of the number. */
502 const STRING_TYPE
*startp
, *start_of_digits
;
503 /* Points at the character following the integer and fractional digits. */
504 const STRING_TYPE
*expp
;
505 /* Total number of digit and number of digits in integer part. */
506 size_t dig_no
, int_no
, lead_zero
;
507 /* Contains the last character read. */
510 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
511 there. So define it ourselves if it remains undefined. */
513 typedef unsigned int wint_t;
515 /* The radix character of the current locale. */
522 /* The thousands character of the current locale. */
524 wchar_t thousands
= L
'\0';
526 const char *thousands
= NULL
;
528 /* The numeric grouping specification of the current locale,
529 in the format described in <locale.h>. */
530 const char *grouping
;
531 /* Used in several places. */
534 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
536 if (__glibc_unlikely (group
))
538 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
539 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
543 /* Figure out the thousands separator character. */
545 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
546 _NL_NUMERIC_THOUSANDS_SEP_WC
);
547 if (thousands
== L
'\0')
550 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
551 if (*thousands
== '\0')
562 /* Find the locale's decimal point character. */
564 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
565 assert (decimal
!= L
'\0');
566 # define decimal_len 1
568 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
569 decimal_len
= strlen (decimal
);
570 assert (decimal_len
> 0);
573 /* Prepare number representation. */
578 /* Parse string to get maximal legal prefix. We need the number of
579 characters of the integer part, the fractional part and the exponent. */
581 /* Ignore leading white space. */
586 /* Get sign of the result. */
592 else if (c
== L_('+'))
595 /* Return 0.0 if no legal string is found.
596 No character is used even if a sign was found. */
598 if (c
== (wint_t) decimal
599 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
601 /* We accept it. This funny construct is here only to indent
602 the code correctly. */
605 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
606 if (cp
[cnt
] != decimal
[cnt
])
608 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
610 /* We accept it. This funny construct is here only to indent
611 the code correctly. */
614 else if (c
< L_('0') || c
> L_('9'))
616 /* Check for `INF' or `INFINITY'. */
617 CHAR_TYPE lowc
= TOLOWER_C (c
);
619 if (lowc
== L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
621 /* Return +/- infinity. */
623 *endptr
= (STRING_TYPE
*)
624 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
627 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
630 if (lowc
== L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
637 /* Match `(n-char-sequence-digit)'. */
640 const STRING_TYPE
*startp
= cp
;
642 retval
= STRTOF_NAN (cp
+ 1, &endp
, L_(')'));
643 if (*endp
== L_(')'))
644 /* Consume the closing parenthesis. */
647 /* Only match the NAN part. */
652 *endptr
= (STRING_TYPE
*) cp
;
657 /* It is really a text we do not recognize. */
661 /* First look whether we are faced with a hexadecimal number. */
662 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
664 /* Okay, it is a hexa-decimal number. Remember this and skip
665 the characters. BTW: hexadecimal numbers must not be
673 /* Record the start of the digits, in case we will check their grouping. */
674 start_of_digits
= startp
= cp
;
676 /* Ignore leading zeroes. This helps us to avoid useless computations. */
678 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
681 if (__glibc_likely (thousands
== NULL
))
686 /* We also have the multibyte thousands string. */
691 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
692 if (thousands
[cnt
] != cp
[cnt
])
694 if (thousands
[cnt
] != '\0')
703 /* If no other digit but a '0' is found the result is 0.0.
704 Return current read pointer. */
705 CHAR_TYPE lowc
= TOLOWER (c
);
706 if (!((c
>= L_('0') && c
<= L_('9'))
707 || (base
== 16 && lowc
>= L_('a') && lowc
<= L_('f'))
710 c
== (wint_t) decimal
712 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
713 if (decimal
[cnt
] != cp
[cnt
])
715 decimal
[cnt
] == '\0'; })
717 /* '0x.' alone is not a valid hexadecimal number.
718 '.' alone is not valid either, but that has been checked
721 || cp
!= start_of_digits
722 || (cp
[decimal_len
] >= L_('0') && cp
[decimal_len
] <= L_('9'))
723 || ({ CHAR_TYPE lo
= TOLOWER (cp
[decimal_len
]);
724 lo
>= L_('a') && lo
<= L_('f'); })))
725 || (base
== 16 && (cp
!= start_of_digits
727 || (base
!= 16 && lowc
== L_('e'))))
730 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
733 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
736 /* If TP is at the start of the digits, there was no correctly
737 grouped prefix of the string; so no number found. */
738 RETURN (negative
? -0.0 : 0.0,
739 tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
742 /* Remember first significant digit and read following characters until the
743 decimal point, exponent character or any non-FP number character. */
748 if ((c
>= L_('0') && c
<= L_('9'))
750 && ({ CHAR_TYPE lo
= TOLOWER (c
);
751 lo
>= L_('a') && lo
<= L_('f'); })))
756 if (__builtin_expect ((wint_t) thousands
== L
'\0', 1)
757 || c
!= (wint_t) thousands
)
758 /* Not a digit or separator: end of the integer part. */
761 if (__glibc_likely (thousands
== NULL
))
765 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
766 if (thousands
[cnt
] != cp
[cnt
])
768 if (thousands
[cnt
] != '\0')
777 if (__builtin_expect (grouping
!= NULL
, 0) && cp
> start_of_digits
)
779 /* Check the grouping of the digits. */
781 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
784 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
789 /* Less than the entire string was correctly grouped. */
791 if (tp
== start_of_digits
)
792 /* No valid group of numbers at all: no valid number. */
796 /* The number is validly grouped, but consists
797 only of zeroes. The whole value is zero. */
798 RETURN (negative
? -0.0 : 0.0, tp
);
800 /* Recompute DIG_NO so we won't read more digits than
801 are properly grouped. */
804 for (tp
= startp
; tp
< cp
; ++tp
)
805 if (*tp
>= L_('0') && *tp
<= L_('9'))
815 /* We have the number of digits in the integer part. Whether these
816 are all or any is really a fractional digit will be decided
819 lead_zero
= int_no
== 0 ? (size_t) -1 : 0;
821 /* Read the fractional digits. A special case are the 'american
822 style' numbers like `16.' i.e. with decimal point but without
826 c
== (wint_t) decimal
828 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
829 if (decimal
[cnt
] != cp
[cnt
])
831 decimal
[cnt
] == '\0'; })
837 while ((c
>= L_('0') && c
<= L_('9')) ||
838 (base
== 16 && ({ CHAR_TYPE lo
= TOLOWER (c
);
839 lo
>= L_('a') && lo
<= L_('f'); })))
841 if (c
!= L_('0') && lead_zero
== (size_t) -1)
842 lead_zero
= dig_no
- int_no
;
847 assert (dig_no
<= (uintmax_t) INTMAX_MAX
);
849 /* Remember start of exponent (if any). */
854 if ((base
== 16 && lowc
== L_('p'))
855 || (base
!= 16 && lowc
== L_('e')))
857 int exp_negative
= 0;
865 else if (c
== L_('+'))
868 if (c
>= L_('0') && c
<= L_('9'))
872 /* Get the exponent limit. */
877 assert (int_no
<= (uintmax_t) (INTMAX_MAX
878 + MIN_EXP
- MANT_DIG
) / 4);
879 exp_limit
= -MIN_EXP
+ MANT_DIG
+ 4 * (intmax_t) int_no
;
885 assert (lead_zero
== 0
886 && int_no
<= (uintmax_t) INTMAX_MAX
/ 4);
887 exp_limit
= MAX_EXP
- 4 * (intmax_t) int_no
+ 3;
889 else if (lead_zero
== (size_t) -1)
891 /* The number is zero and this limit is
893 exp_limit
= MAX_EXP
+ 3;
898 <= (uintmax_t) (INTMAX_MAX
- MAX_EXP
- 3) / 4);
900 + 4 * (intmax_t) lead_zero
910 <= (uintmax_t) (INTMAX_MAX
+ MIN_10_EXP
- MANT_DIG
));
911 exp_limit
= -MIN_10_EXP
+ MANT_DIG
+ (intmax_t) int_no
;
917 assert (lead_zero
== 0
918 && int_no
<= (uintmax_t) INTMAX_MAX
);
919 exp_limit
= MAX_10_EXP
- (intmax_t) int_no
+ 1;
921 else if (lead_zero
== (size_t) -1)
923 /* The number is zero and this limit is
925 exp_limit
= MAX_10_EXP
+ 1;
930 <= (uintmax_t) (INTMAX_MAX
- MAX_10_EXP
- 1));
931 exp_limit
= MAX_10_EXP
+ (intmax_t) lead_zero
+ 1;
941 if (__builtin_expect ((exponent
> exp_limit
/ 10
942 || (exponent
== exp_limit
/ 10
943 && c
- L_('0') > exp_limit
% 10)), 0))
944 /* The exponent is too large/small to represent a valid
949 /* We have to take care for special situation: a joker
950 might have written "0.0e100000" which is in fact
952 if (lead_zero
== (size_t) -1)
953 result
= negative
? -0.0 : 0.0;
956 /* Overflow or underflow. */
957 result
= (exp_negative
958 ? underflow_value (negative
)
959 : overflow_value (negative
));
962 /* Accept all following digits as part of the exponent. */
965 while (*cp
>= L_('0') && *cp
<= L_('9'));
972 exponent
+= c
- L_('0');
976 while (c
>= L_('0') && c
<= L_('9'));
979 exponent
= -exponent
;
985 /* We don't want to have to work with trailing zeroes after the radix. */
988 while (expp
[-1] == L_('0'))
993 assert (dig_no
>= int_no
);
996 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
999 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
1002 if (expp
[-1] != L_('0'))
1008 exponent
+= base
== 16 ? 4 : 1;
1010 while (dig_no
> 0 && exponent
< 0);
1014 /* The whole string is parsed. Store the address of the next character. */
1016 *endptr
= (STRING_TYPE
*) cp
;
1019 return negative
? -0.0 : 0.0;
1023 /* Find the decimal point */
1024 #ifdef USE_WIDE_CHAR
1025 while (*startp
!= decimal
)
1030 if (*startp
== decimal
[0])
1032 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
1033 if (decimal
[cnt
] != startp
[cnt
])
1035 if (decimal
[cnt
] == '\0')
1041 startp
+= lead_zero
+ decimal_len
;
1042 assert (lead_zero
<= (base
== 16
1043 ? (uintmax_t) INTMAX_MAX
/ 4
1044 : (uintmax_t) INTMAX_MAX
));
1045 assert (lead_zero
<= (base
== 16
1046 ? ((uintmax_t) exponent
1047 - (uintmax_t) INTMAX_MIN
) / 4
1048 : ((uintmax_t) exponent
- (uintmax_t) INTMAX_MIN
)));
1049 exponent
-= base
== 16 ? 4 * (intmax_t) lead_zero
: (intmax_t) lead_zero
;
1050 dig_no
-= lead_zero
;
1053 /* If the BASE is 16 we can use a simpler algorithm. */
1056 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1057 4, 4, 4, 4, 4, 4, 4, 4 };
1058 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
1059 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1062 while (!ISXDIGIT (*startp
))
1064 while (*startp
== L_('0'))
1066 if (ISDIGIT (*startp
))
1067 val
= *startp
++ - L_('0');
1069 val
= 10 + TOLOWER (*startp
++) - L_('a');
1071 /* We cannot have a leading zero. */
1074 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
1076 /* We don't have to care for wrapping. This is the normal
1077 case so we add the first clause in the `if' expression as
1078 an optimization. It is a compile-time constant and so does
1079 not cost anything. */
1080 retval
[idx
] = val
<< (pos
- bits
+ 1);
1085 retval
[idx
--] = val
>> (bits
- pos
- 1);
1086 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
1087 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
1090 /* Adjust the exponent for the bits we are shifting in. */
1091 assert (int_no
<= (uintmax_t) (exponent
< 0
1092 ? (INTMAX_MAX
- bits
+ 1) / 4
1093 : (INTMAX_MAX
- exponent
- bits
+ 1) / 4));
1094 exponent
+= bits
- 1 + ((intmax_t) int_no
- 1) * 4;
1096 while (--dig_no
> 0 && idx
>= 0)
1098 if (!ISXDIGIT (*startp
))
1099 startp
+= decimal_len
;
1100 if (ISDIGIT (*startp
))
1101 val
= *startp
++ - L_('0');
1103 val
= 10 + TOLOWER (*startp
++) - L_('a');
1107 retval
[idx
] |= val
<< (pos
- 4 + 1);
1112 retval
[idx
--] |= val
>> (4 - pos
- 1);
1113 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
1116 int rest_nonzero
= 0;
1117 while (--dig_no
> 0)
1119 if (*startp
!= L_('0'))
1126 return round_and_return (retval
, exponent
, negative
, val
,
1127 BITS_PER_MP_LIMB
- 1, rest_nonzero
);
1131 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
1135 /* We ran out of digits. */
1136 MPN_ZERO (retval
, idx
);
1138 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1141 /* Now we have the number of digits in total and the integer digits as well
1142 as the exponent and its sign. We can decide whether the read digits are
1143 really integer digits or belong to the fractional part; i.e. we normalize
1146 intmax_t incr
= (exponent
< 0
1147 ? MAX (-(intmax_t) int_no
, exponent
)
1148 : MIN ((intmax_t) dig_no
- (intmax_t) int_no
, exponent
));
1153 if (__glibc_unlikely (exponent
> MAX_10_EXP
+ 1 - (intmax_t) int_no
))
1154 return overflow_value (negative
);
1156 /* 10^(MIN_10_EXP-1) is not normal. Thus, 10^(MIN_10_EXP-1) /
1157 2^MANT_DIG is below half the least subnormal, so anything with a
1158 base-10 exponent less than the base-10 exponent (which is
1159 MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
1160 underflows. DIG is floor((MANT_DIG-1)log10(2)), so an exponent
1161 below MIN_10_EXP - (DIG + 3) underflows. But EXPONENT is
1162 actually an exponent multiplied only by a fractional part, not an
1163 integer part, so an exponent below MIN_10_EXP - (DIG + 2)
1165 if (__glibc_unlikely (exponent
< MIN_10_EXP
- (DIG
+ 2)))
1166 return underflow_value (negative
);
1170 /* Read the integer part as a multi-precision number to NUM. */
1171 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1172 #ifndef USE_WIDE_CHAR
1173 , decimal
, decimal_len
, thousands
1179 /* We now multiply the gained number by the given power of ten. */
1180 mp_limb_t
*psrc
= num
;
1181 mp_limb_t
*pdest
= den
;
1183 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1187 if ((exponent
& expbit
) != 0)
1189 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1193 /* FIXME: not the whole multiplication has to be
1194 done. If we have the needed number of bits we
1195 only need the information whether more non-zero
1197 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1198 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1199 &__tens
[ttab
->arrayoff
1200 + _FPIO_CONST_OFFSET
],
1203 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1204 + _FPIO_CONST_OFFSET
],
1205 size
, psrc
, numsize
);
1209 (void) SWAP (psrc
, pdest
);
1214 while (exponent
!= 0);
1217 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1220 /* Determine how many bits of the result we already have. */
1221 count_leading_zeros (bits
, num
[numsize
- 1]);
1222 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1224 /* Now we know the exponent of the number in base two.
1225 Check it against the maximum possible exponent. */
1226 if (__glibc_unlikely (bits
> MAX_EXP
))
1227 return overflow_value (negative
);
1229 /* We have already the first BITS bits of the result. Together with
1230 the information whether more non-zero bits follow this is enough
1231 to determine the result. */
1232 if (bits
> MANT_DIG
)
1235 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1236 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1237 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1239 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1243 memcpy (retval
, &num
[least_idx
],
1244 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1247 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1248 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1250 << (BITS_PER_MP_LIMB
- least_bit
));
1251 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1252 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1255 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1256 for (i
= 0; num
[i
] == 0; ++i
)
1259 return round_and_return (retval
, bits
- 1, negative
,
1260 num
[round_idx
], round_bit
,
1261 int_no
< dig_no
|| i
< round_idx
);
1264 else if (dig_no
== int_no
)
1266 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1267 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1269 if (target_bit
== is_bit
)
1271 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1272 numsize
* sizeof (mp_limb_t
));
1273 /* FIXME: the following loop can be avoided if we assume a
1274 maximal MANT_DIG value. */
1275 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1277 else if (target_bit
> is_bit
)
1279 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1280 num
, numsize
, target_bit
- is_bit
);
1281 /* FIXME: the following loop can be avoided if we assume a
1282 maximal MANT_DIG value. */
1283 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1288 assert (numsize
< RETURN_LIMB_SIZE
);
1290 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1291 num
, numsize
, is_bit
- target_bit
);
1292 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1293 /* FIXME: the following loop can be avoided if we assume a
1294 maximal MANT_DIG value. */
1295 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1298 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1302 /* Store the bits we already have. */
1303 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1304 #if RETURN_LIMB_SIZE > 1
1305 if (numsize
< RETURN_LIMB_SIZE
)
1306 # if RETURN_LIMB_SIZE == 2
1307 retval
[numsize
] = 0;
1309 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1314 /* We have to compute at least some of the fractional digits. */
1316 /* We construct a fraction and the result of the division gives us
1317 the needed digits. The denominator is 1.0 multiplied by the
1318 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1319 123e-6 gives 123 / 1000000. */
1324 int need_frac_digits
;
1326 mp_limb_t
*psrc
= den
;
1327 mp_limb_t
*pdest
= num
;
1328 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1330 assert (dig_no
> int_no
1332 && exponent
>= MIN_10_EXP
- (DIG
+ 2));
1334 /* We need to compute MANT_DIG - BITS fractional bits that lie
1335 within the mantissa of the result, the following bit for
1336 rounding, and to know whether any subsequent bit is 0.
1337 Computing a bit with value 2^-n means looking at n digits after
1338 the decimal point. */
1341 /* The bits required are those immediately after the point. */
1342 assert (int_no
> 0 && exponent
== 0);
1343 need_frac_digits
= 1 + MANT_DIG
- bits
;
1347 /* The number is in the form .123eEXPONENT. */
1348 assert (int_no
== 0 && *startp
!= L_('0'));
1349 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1351 int neg_exp_2
= ((1 - exponent
) * 10) / 3 + 1;
1352 /* The number is at least 2^-NEG_EXP_2. We need up to
1353 MANT_DIG bits following that bit. */
1354 need_frac_digits
= neg_exp_2
+ MANT_DIG
;
1355 /* However, we never need bits beyond 1/4 ulp of the smallest
1356 representable value. (That 1/4 ulp bit is only needed to
1357 determine tinyness on machines where tinyness is determined
1359 if (need_frac_digits
> MANT_DIG
- MIN_EXP
+ 2)
1360 need_frac_digits
= MANT_DIG
- MIN_EXP
+ 2;
1361 /* At this point, NEED_FRAC_DIGITS is the total number of
1362 digits needed after the point, but some of those may be
1364 need_frac_digits
+= exponent
;
1365 /* Any cases underflowing enough that none of the fractional
1366 digits are needed should have been caught earlier (such
1367 cases are on the order of 10^-n or smaller where 2^-n is
1368 the least subnormal). */
1369 assert (need_frac_digits
> 0);
1372 if (need_frac_digits
> (intmax_t) dig_no
- (intmax_t) int_no
)
1373 need_frac_digits
= (intmax_t) dig_no
- (intmax_t) int_no
;
1375 if ((intmax_t) dig_no
> (intmax_t) int_no
+ need_frac_digits
)
1377 dig_no
= int_no
+ need_frac_digits
;
1383 neg_exp
= (intmax_t) dig_no
- (intmax_t) int_no
- exponent
;
1385 /* Construct the denominator. */
1390 if ((neg_exp
& expbit
) != 0)
1397 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1398 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1399 densize
* sizeof (mp_limb_t
));
1403 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1404 + _FPIO_CONST_OFFSET
],
1405 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1407 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1410 (void) SWAP (psrc
, pdest
);
1416 while (neg_exp
!= 0);
1419 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1421 /* Read the fractional digits from the string. */
1422 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1423 #ifndef USE_WIDE_CHAR
1424 , decimal
, decimal_len
, thousands
1428 /* We now have to shift both numbers so that the highest bit in the
1429 denominator is set. In the same process we copy the numerator to
1430 a high place in the array so that the division constructs the wanted
1431 digits. This is done by a "quasi fix point" number representation.
1433 num: ddddddddddd . 0000000000000000000000
1435 den: ddddddddddd n >= m
1439 count_leading_zeros (cnt
, den
[densize
- 1]);
1443 /* Don't call `mpn_shift' with a count of zero since the specification
1444 does not allow this. */
1445 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1446 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1448 num
[numsize
++] = cy
;
1451 /* Now we are ready for the division. But it is not necessary to
1452 do a full multi-precision division because we only need a small
1453 number of bits for the result. So we do not use __mpn_divmod
1454 here but instead do the division here by hand and stop whenever
1455 the needed number of bits is reached. The code itself comes
1456 from the GNU MP Library by Torbj\"orn Granlund. */
1464 mp_limb_t d
, n
, quot
;
1469 assert (numsize
== 1 && n
< d
);
1473 udiv_qrnnd (quot
, n
, n
, 0, d
);
1480 cnt = BITS_PER_MP_LIMB; \
1482 count_leading_zeros (cnt, quot); \
1484 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1486 used = MANT_DIG + cnt; \
1487 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1488 bits = MANT_DIG + 1; \
1492 /* Note that we only clear the second element. */ \
1493 /* The conditional is determined at compile time. */ \
1494 if (RETURN_LIMB_SIZE > 1) \
1500 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1501 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1505 used = MANT_DIG - bits; \
1507 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1509 bits += BITS_PER_MP_LIMB
1513 while (bits
<= MANT_DIG
);
1515 return round_and_return (retval
, exponent
- 1, negative
,
1516 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1517 more_bits
|| n
!= 0);
1521 mp_limb_t d0
, d1
, n0
, n1
;
1528 if (numsize
< densize
)
1532 /* The numerator of the number occupies fewer bits than
1533 the denominator but the one limb is bigger than the
1534 high limb of the numerator. */
1541 exponent
-= BITS_PER_MP_LIMB
;
1544 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1545 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1546 BITS_PER_MP_LIMB
, 0);
1549 used
= MANT_DIG
- bits
;
1551 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1553 bits
+= BITS_PER_MP_LIMB
;
1565 while (bits
<= MANT_DIG
)
1571 /* QUOT should be either 111..111 or 111..110. We need
1572 special treatment of this rare case as normal division
1573 would give overflow. */
1574 quot
= ~(mp_limb_t
) 0;
1577 if (r
< d1
) /* Carry in the addition? */
1579 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1582 n1
= d0
- (d0
!= 0);
1587 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1588 umul_ppmm (n1
, n0
, d0
, quot
);
1592 if (n1
> r
|| (n1
== r
&& n0
> 0))
1594 /* The estimated QUOT was too large. */
1597 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1599 if (r
>= d1
) /* If not carry, test QUOT again. */
1602 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1608 return round_and_return (retval
, exponent
- 1, negative
,
1609 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1610 more_bits
|| n1
!= 0 || n0
!= 0);
1615 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1619 dX
= den
[densize
- 1];
1620 d1
= den
[densize
- 2];
1622 /* The division does not work if the upper limb of the two-limb
1623 numerator is greater than the denominator. */
1624 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1627 if (numsize
< densize
)
1629 mp_size_t empty
= densize
- numsize
;
1633 exponent
-= empty
* BITS_PER_MP_LIMB
;
1636 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1638 /* We make a difference here because the compiler
1639 cannot optimize the `else' case that good and
1640 this reflects all currently used FLOAT types
1641 and GMP implementations. */
1642 #if RETURN_LIMB_SIZE <= 2
1643 assert (empty
== 1);
1644 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1645 BITS_PER_MP_LIMB
, 0);
1647 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1648 retval
[i
] = retval
[i
- empty
];
1655 used
= MANT_DIG
- bits
;
1656 if (used
>= BITS_PER_MP_LIMB
)
1659 (void) __mpn_lshift (&retval
[used
1660 / BITS_PER_MP_LIMB
],
1663 - used
/ BITS_PER_MP_LIMB
),
1664 used
% BITS_PER_MP_LIMB
);
1665 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1669 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1671 bits
+= empty
* BITS_PER_MP_LIMB
;
1673 for (i
= numsize
; i
> 0; --i
)
1674 num
[i
+ empty
] = num
[i
- 1];
1675 MPN_ZERO (num
, empty
+ 1);
1680 assert (numsize
== densize
);
1681 for (i
= numsize
; i
> 0; --i
)
1682 num
[i
] = num
[i
- 1];
1689 while (bits
<= MANT_DIG
)
1692 /* This might over-estimate QUOT, but it's probably not
1693 worth the extra code here to find out. */
1694 quot
= ~(mp_limb_t
) 0;
1699 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1700 umul_ppmm (n1
, n0
, d1
, quot
);
1702 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1706 if (r
< dX
) /* I.e. "carry in previous addition?" */
1713 /* Possible optimization: We already have (q * n0) and (1 * n1)
1714 after the calculation of QUOT. Taking advantage of this, we
1715 could make this loop make two iterations less. */
1717 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1719 if (num
[densize
] != cy
)
1721 cy
= __mpn_add_n (num
, num
, den
, densize
);
1725 n0
= num
[densize
] = num
[densize
- 1];
1726 for (i
= densize
- 1; i
> 0; --i
)
1727 num
[i
] = num
[i
- 1];
1733 for (i
= densize
; i
>= 0 && num
[i
] == 0; --i
)
1735 return round_and_return (retval
, exponent
- 1, negative
,
1736 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1737 more_bits
|| i
>= 0);
1744 #if defined _LIBC && !defined USE_WIDE_CHAR
1745 libc_hidden_def (____STRTOF_INTERNAL
)
1748 /* External user entry point. */
1751 #ifdef weak_function
1754 __STRTOF (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, __locale_t loc
)
1756 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1759 libc_hidden_def (__STRTOF
)
1760 libc_hidden_ver (__STRTOF
, STRTOF
)
1762 weak_alias (__STRTOF
, STRTOF
)
1764 #ifdef LONG_DOUBLE_COMPAT
1765 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1766 # ifdef USE_WIDE_CHAR
1767 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1769 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1772 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1773 # ifdef USE_WIDE_CHAR
1774 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1776 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);