1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2017 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"
50 #include <math_private.h>
54 #include <rounding-mode.h>
57 /* The gmp headers need some configuration frobs. */
60 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
61 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
62 #include <gmp-mparam.h>
66 #include "fpioconst.h"
71 /* We use this code for the extended locale handling where the
72 function gets as an additional argument the locale which has to be
73 used. To access the values we have to redefine the _NL_CURRENT and
74 _NL_CURRENT_WORD macros. */
76 #define _NL_CURRENT(category, item) \
77 (current->values[_NL_ITEM_INDEX (item)].string)
78 #undef _NL_CURRENT_WORD
79 #define _NL_CURRENT_WORD(category, item) \
80 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
82 #if defined _LIBC || defined HAVE_WCHAR_H
88 # define STRING_TYPE wchar_t
89 # define CHAR_TYPE wint_t
91 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
92 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
93 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
94 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
95 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
96 # define STRNCASECMP(S1, S2, N) \
97 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
99 # define STRING_TYPE char
100 # define CHAR_TYPE char
102 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
103 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
104 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
105 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
106 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
107 # define STRNCASECMP(S1, S2, N) \
108 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
112 /* Constants we need from float.h; select the set for the FLOAT precision. */
113 #define MANT_DIG PASTE(FLT,_MANT_DIG)
114 #define DIG PASTE(FLT,_DIG)
115 #define MAX_EXP PASTE(FLT,_MAX_EXP)
116 #define MIN_EXP PASTE(FLT,_MIN_EXP)
117 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
118 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
119 #define MAX_VALUE PASTE(FLT,_MAX)
120 #define MIN_VALUE PASTE(FLT,_MIN)
122 /* Extra macros required to get FLT expanded before the pasting. */
123 #define PASTE(a,b) PASTE1(a,b)
124 #define PASTE1(a,b) a##b
126 /* Function to construct a floating point number from an MP integer
127 containing the fraction bits, a base 2 exponent, and a sign flag. */
128 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
130 /* Definitions according to limb size used. */
131 #if BITS_PER_MP_LIMB == 32
132 # define MAX_DIG_PER_LIMB 9
133 # define MAX_FAC_PER_LIMB 1000000000UL
134 #elif BITS_PER_MP_LIMB == 64
135 # define MAX_DIG_PER_LIMB 19
136 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
138 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
141 extern const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1];
144 #define howmany(x,y) (((x)+((y)-1))/(y))
146 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
148 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
150 #define RETURN(val,end) \
151 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
152 return val; } while (0)
154 /* Maximum size necessary for mpn integers to hold floating point
155 numbers. The largest number we need to hold is 10^n where 2^-n is
156 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
157 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
158 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
159 BITS_PER_MP_LIMB) + 2)
160 /* Declare an mpn integer variable that big. */
161 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
162 /* Copy an mpn integer value. */
163 #define MPN_ASSIGN(dst, src) \
164 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
167 /* Set errno and return an overflowing value with sign specified by
170 overflow_value (int negative
)
172 __set_errno (ERANGE
);
173 FLOAT result
= math_narrow_eval ((negative
? -MAX_VALUE
: MAX_VALUE
)
179 /* Set errno and return an underflowing value with sign specified by
182 underflow_value (int negative
)
184 __set_errno (ERANGE
);
185 FLOAT result
= math_narrow_eval ((negative
? -MIN_VALUE
: MIN_VALUE
)
191 /* Return a floating point number of the needed type according to the given
192 multi-precision number after possible rounding. */
194 round_and_return (mp_limb_t
*retval
, intmax_t exponent
, int negative
,
195 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
197 int mode
= get_rounding_mode ();
199 if (exponent
< MIN_EXP
- 1)
201 if (exponent
< MIN_EXP
- 1 - MANT_DIG
)
202 return underflow_value (negative
);
204 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
207 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
208 if (shift
== MANT_DIG
)
209 /* This is a special case to handle the very seldom case where
210 the mantissa will be empty after the shift. */
214 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
215 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
216 for (i
= 0; i
< RETURN_LIMB_SIZE
- 1; ++i
)
217 more_bits
|= retval
[i
] != 0;
218 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
220 else if (shift
>= BITS_PER_MP_LIMB
)
224 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
225 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
226 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
227 more_bits
|= retval
[i
] != 0;
228 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
231 /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB. */
232 if ((shift
% BITS_PER_MP_LIMB
) != 0)
233 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
234 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
235 shift
% BITS_PER_MP_LIMB
);
237 for (i
= 0; i
< RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
); i
++)
238 retval
[i
] = retval
[i
+ (shift
/ BITS_PER_MP_LIMB
)];
239 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
240 shift
/ BITS_PER_MP_LIMB
);
244 if (TININESS_AFTER_ROUNDING
&& shift
== 1)
246 /* Whether the result counts as tiny depends on whether,
247 after rounding to the normal precision, it still has
248 a subnormal exponent. */
249 mp_limb_t retval_normal
[RETURN_LIMB_SIZE
];
250 if (round_away (negative
,
251 (retval
[0] & 1) != 0,
253 & (((mp_limb_t
) 1) << round_bit
)) != 0,
256 & ((((mp_limb_t
) 1) << round_bit
) - 1))
260 mp_limb_t cy
= __mpn_add_1 (retval_normal
, retval
,
261 RETURN_LIMB_SIZE
, 1);
263 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
264 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
265 ((retval_normal
[RETURN_LIMB_SIZE
- 1]
266 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
)))
271 round_limb
= retval
[0];
272 round_bit
= shift
- 1;
273 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
275 /* This is a hook for the m68k long double format, where the
276 exponent bias is the same for normalized and denormalized
279 # define DENORM_EXP (MIN_EXP - 2)
281 exponent
= DENORM_EXP
;
283 && ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
285 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
287 __set_errno (ERANGE
);
288 FLOAT force_underflow
= MIN_VALUE
* MIN_VALUE
;
289 math_force_eval (force_underflow
);
293 if (exponent
> MAX_EXP
)
296 bool half_bit
= (round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0;
297 bool more_bits_nonzero
299 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0);
300 if (round_away (negative
,
301 (retval
[0] & 1) != 0,
306 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
308 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
309 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
310 (retval
[RETURN_LIMB_SIZE
- 1]
311 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
314 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
315 retval
[RETURN_LIMB_SIZE
- 1]
316 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
318 else if (exponent
== DENORM_EXP
319 && (retval
[RETURN_LIMB_SIZE
- 1]
320 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
322 /* The number was denormalized but now normalized. */
323 exponent
= MIN_EXP
- 1;
326 if (exponent
> MAX_EXP
)
328 return overflow_value (negative
);
330 if (half_bit
|| more_bits_nonzero
)
332 FLOAT force_inexact
= (FLOAT
) 1 + MIN_VALUE
;
333 math_force_eval (force_inexact
);
335 return MPN2FLOAT (retval
, exponent
, negative
);
339 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
340 into N. Return the size of the number limbs in NSIZE at the first
341 character od the string that is not part of the integer as the function
342 value. If the EXPONENT is small enough to be taken as an additional
343 factor for the resulting number (see code) multiply by it. */
344 static const STRING_TYPE
*
345 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
347 #ifndef USE_WIDE_CHAR
348 , const char *decimal
, size_t decimal_len
, const char *thousands
353 /* Number of digits for actual limb. */
362 if (cnt
== MAX_DIG_PER_LIMB
)
372 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
373 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
376 assert (*nsize
< MPNSIZE
);
385 /* There might be thousands separators or radix characters in
386 the string. But these all can be ignored because we know the
387 format of the number is correct and we have an exact number
388 of characters to read. */
390 if (*str
< L
'0' || *str
> L
'9')
393 if (*str
< '0' || *str
> '9')
396 if (thousands
!= NULL
&& *str
== *thousands
397 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
398 if (thousands
[inner
] != str
[inner
])
400 thousands
[inner
] == '\0'; }))
406 low
= low
* 10 + *str
++ - L_('0');
409 while (--digcnt
> 0);
411 if (*exponent
> 0 && *exponent
<= MAX_DIG_PER_LIMB
- cnt
)
413 low
*= _tens_in_limb
[*exponent
];
414 start
= _tens_in_limb
[cnt
+ *exponent
];
418 start
= _tens_in_limb
[cnt
];
428 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
429 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
432 assert (*nsize
< MPNSIZE
);
441 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
442 with the COUNT most significant bits of LIMB.
444 Implemented as a macro, so that __builtin_constant_p works even at -O0.
446 Tege doesn't like this macro so I have to write it here myself. :)
448 #define __mpn_lshift_1(ptr, size, count, limb) \
451 mp_limb_t *__ptr = (ptr); \
452 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
455 for (i = (size) - 1; i > 0; --i) \
456 __ptr[i] = __ptr[i - 1]; \
461 /* We assume count > 0 && count < BITS_PER_MP_LIMB here. */ \
462 unsigned int __count = (count); \
463 (void) __mpn_lshift (__ptr, __ptr, size, __count); \
464 __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count); \
470 #define INTERNAL(x) INTERNAL1(x)
471 #define INTERNAL1(x) __##x##_internal
472 #ifndef ____STRTOF_INTERNAL
473 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
476 /* This file defines a function to check for correct grouping. */
477 #include "grouping.h"
480 /* Return a floating point number with the value of the given string NPTR.
481 Set *ENDPTR to the character after the last used one. If the number is
482 smaller than the smallest representable number, set `errno' to ERANGE and
483 return 0.0. If the number is too big to be represented, set `errno' to
484 ERANGE and return HUGE_VAL with the appropriate sign. */
486 ____STRTOF_INTERNAL (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int group
,
489 int negative
; /* The sign of the number. */
490 MPN_VAR (num
); /* MP representation of the number. */
491 intmax_t exponent
; /* Exponent of the number. */
493 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
496 /* When we have to compute fractional digits we form a fraction with a
497 second multi-precision number (and we sometimes need a second for
498 temporary results). */
501 /* Representation for the return value. */
502 mp_limb_t retval
[RETURN_LIMB_SIZE
];
503 /* Number of bits currently in result value. */
506 /* Running pointer after the last character processed in the string. */
507 const STRING_TYPE
*cp
, *tp
;
508 /* Start of significant part of the number. */
509 const STRING_TYPE
*startp
, *start_of_digits
;
510 /* Points at the character following the integer and fractional digits. */
511 const STRING_TYPE
*expp
;
512 /* Total number of digit and number of digits in integer part. */
513 size_t dig_no
, int_no
, lead_zero
;
514 /* Contains the last character read. */
517 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
518 there. So define it ourselves if it remains undefined. */
520 typedef unsigned int wint_t;
522 /* The radix character of the current locale. */
529 /* The thousands character of the current locale. */
531 wchar_t thousands
= L
'\0';
533 const char *thousands
= NULL
;
535 /* The numeric grouping specification of the current locale,
536 in the format described in <locale.h>. */
537 const char *grouping
;
538 /* Used in several places. */
541 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
543 if (__glibc_unlikely (group
))
545 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
546 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
550 /* Figure out the thousands separator character. */
552 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
553 _NL_NUMERIC_THOUSANDS_SEP_WC
);
554 if (thousands
== L
'\0')
557 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
558 if (*thousands
== '\0')
569 /* Find the locale's decimal point character. */
571 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
572 assert (decimal
!= L
'\0');
573 # define decimal_len 1
575 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
576 decimal_len
= strlen (decimal
);
577 assert (decimal_len
> 0);
580 /* Prepare number representation. */
585 /* Parse string to get maximal legal prefix. We need the number of
586 characters of the integer part, the fractional part and the exponent. */
588 /* Ignore leading white space. */
593 /* Get sign of the result. */
599 else if (c
== L_('+'))
602 /* Return 0.0 if no legal string is found.
603 No character is used even if a sign was found. */
605 if (c
== (wint_t) decimal
606 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
608 /* We accept it. This funny construct is here only to indent
609 the code correctly. */
612 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
613 if (cp
[cnt
] != decimal
[cnt
])
615 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
617 /* We accept it. This funny construct is here only to indent
618 the code correctly. */
621 else if (c
< L_('0') || c
> L_('9'))
623 /* Check for `INF' or `INFINITY'. */
624 CHAR_TYPE lowc
= TOLOWER_C (c
);
626 if (lowc
== L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
628 /* Return +/- infinity. */
630 *endptr
= (STRING_TYPE
*)
631 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
634 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
637 if (lowc
== L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
644 /* Match `(n-char-sequence-digit)'. */
647 const STRING_TYPE
*startp
= cp
;
649 retval
= STRTOF_NAN (cp
+ 1, &endp
, L_(')'));
650 if (*endp
== L_(')'))
651 /* Consume the closing parenthesis. */
654 /* Only match the NAN part. */
659 *endptr
= (STRING_TYPE
*) cp
;
664 /* It is really a text we do not recognize. */
668 /* First look whether we are faced with a hexadecimal number. */
669 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
671 /* Okay, it is a hexa-decimal number. Remember this and skip
672 the characters. BTW: hexadecimal numbers must not be
680 /* Record the start of the digits, in case we will check their grouping. */
681 start_of_digits
= startp
= cp
;
683 /* Ignore leading zeroes. This helps us to avoid useless computations. */
685 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
688 if (__glibc_likely (thousands
== NULL
))
693 /* We also have the multibyte thousands string. */
698 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
699 if (thousands
[cnt
] != cp
[cnt
])
701 if (thousands
[cnt
] != '\0')
710 /* If no other digit but a '0' is found the result is 0.0.
711 Return current read pointer. */
712 CHAR_TYPE lowc
= TOLOWER (c
);
713 if (!((c
>= L_('0') && c
<= L_('9'))
714 || (base
== 16 && lowc
>= L_('a') && lowc
<= L_('f'))
717 c
== (wint_t) decimal
719 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
720 if (decimal
[cnt
] != cp
[cnt
])
722 decimal
[cnt
] == '\0'; })
724 /* '0x.' alone is not a valid hexadecimal number.
725 '.' alone is not valid either, but that has been checked
728 || cp
!= start_of_digits
729 || (cp
[decimal_len
] >= L_('0') && cp
[decimal_len
] <= L_('9'))
730 || ({ CHAR_TYPE lo
= TOLOWER (cp
[decimal_len
]);
731 lo
>= L_('a') && lo
<= L_('f'); })))
732 || (base
== 16 && (cp
!= start_of_digits
734 || (base
!= 16 && lowc
== L_('e'))))
737 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
740 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
743 /* If TP is at the start of the digits, there was no correctly
744 grouped prefix of the string; so no number found. */
745 RETURN (negative
? -0.0 : 0.0,
746 tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
749 /* Remember first significant digit and read following characters until the
750 decimal point, exponent character or any non-FP number character. */
755 if ((c
>= L_('0') && c
<= L_('9'))
757 && ({ CHAR_TYPE lo
= TOLOWER (c
);
758 lo
>= L_('a') && lo
<= L_('f'); })))
763 if (__builtin_expect ((wint_t) thousands
== L
'\0', 1)
764 || c
!= (wint_t) thousands
)
765 /* Not a digit or separator: end of the integer part. */
768 if (__glibc_likely (thousands
== NULL
))
772 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
773 if (thousands
[cnt
] != cp
[cnt
])
775 if (thousands
[cnt
] != '\0')
784 if (__builtin_expect (grouping
!= NULL
, 0) && cp
> start_of_digits
)
786 /* Check the grouping of the digits. */
788 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
791 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
796 /* Less than the entire string was correctly grouped. */
798 if (tp
== start_of_digits
)
799 /* No valid group of numbers at all: no valid number. */
803 /* The number is validly grouped, but consists
804 only of zeroes. The whole value is zero. */
805 RETURN (negative
? -0.0 : 0.0, tp
);
807 /* Recompute DIG_NO so we won't read more digits than
808 are properly grouped. */
811 for (tp
= startp
; tp
< cp
; ++tp
)
812 if (*tp
>= L_('0') && *tp
<= L_('9'))
822 /* We have the number of digits in the integer part. Whether these
823 are all or any is really a fractional digit will be decided
826 lead_zero
= int_no
== 0 ? (size_t) -1 : 0;
828 /* Read the fractional digits. A special case are the 'american
829 style' numbers like `16.' i.e. with decimal point but without
833 c
== (wint_t) decimal
835 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
836 if (decimal
[cnt
] != cp
[cnt
])
838 decimal
[cnt
] == '\0'; })
844 while ((c
>= L_('0') && c
<= L_('9')) ||
845 (base
== 16 && ({ CHAR_TYPE lo
= TOLOWER (c
);
846 lo
>= L_('a') && lo
<= L_('f'); })))
848 if (c
!= L_('0') && lead_zero
== (size_t) -1)
849 lead_zero
= dig_no
- int_no
;
854 assert (dig_no
<= (uintmax_t) INTMAX_MAX
);
856 /* Remember start of exponent (if any). */
861 if ((base
== 16 && lowc
== L_('p'))
862 || (base
!= 16 && lowc
== L_('e')))
864 int exp_negative
= 0;
872 else if (c
== L_('+'))
875 if (c
>= L_('0') && c
<= L_('9'))
879 /* Get the exponent limit. */
884 assert (int_no
<= (uintmax_t) (INTMAX_MAX
885 + MIN_EXP
- MANT_DIG
) / 4);
886 exp_limit
= -MIN_EXP
+ MANT_DIG
+ 4 * (intmax_t) int_no
;
892 assert (lead_zero
== 0
893 && int_no
<= (uintmax_t) INTMAX_MAX
/ 4);
894 exp_limit
= MAX_EXP
- 4 * (intmax_t) int_no
+ 3;
896 else if (lead_zero
== (size_t) -1)
898 /* The number is zero and this limit is
900 exp_limit
= MAX_EXP
+ 3;
905 <= (uintmax_t) (INTMAX_MAX
- MAX_EXP
- 3) / 4);
907 + 4 * (intmax_t) lead_zero
917 <= (uintmax_t) (INTMAX_MAX
+ MIN_10_EXP
- MANT_DIG
));
918 exp_limit
= -MIN_10_EXP
+ MANT_DIG
+ (intmax_t) int_no
;
924 assert (lead_zero
== 0
925 && int_no
<= (uintmax_t) INTMAX_MAX
);
926 exp_limit
= MAX_10_EXP
- (intmax_t) int_no
+ 1;
928 else if (lead_zero
== (size_t) -1)
930 /* The number is zero and this limit is
932 exp_limit
= MAX_10_EXP
+ 1;
937 <= (uintmax_t) (INTMAX_MAX
- MAX_10_EXP
- 1));
938 exp_limit
= MAX_10_EXP
+ (intmax_t) lead_zero
+ 1;
948 if (__builtin_expect ((exponent
> exp_limit
/ 10
949 || (exponent
== exp_limit
/ 10
950 && c
- L_('0') > exp_limit
% 10)), 0))
951 /* The exponent is too large/small to represent a valid
956 /* We have to take care for special situation: a joker
957 might have written "0.0e100000" which is in fact
959 if (lead_zero
== (size_t) -1)
960 result
= negative
? -0.0 : 0.0;
963 /* Overflow or underflow. */
964 result
= (exp_negative
965 ? underflow_value (negative
)
966 : overflow_value (negative
));
969 /* Accept all following digits as part of the exponent. */
972 while (*cp
>= L_('0') && *cp
<= L_('9'));
979 exponent
+= c
- L_('0');
983 while (c
>= L_('0') && c
<= L_('9'));
986 exponent
= -exponent
;
992 /* We don't want to have to work with trailing zeroes after the radix. */
995 while (expp
[-1] == L_('0'))
1000 assert (dig_no
>= int_no
);
1003 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
1006 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
1009 if (expp
[-1] != L_('0'))
1015 exponent
+= base
== 16 ? 4 : 1;
1017 while (dig_no
> 0 && exponent
< 0);
1021 /* The whole string is parsed. Store the address of the next character. */
1023 *endptr
= (STRING_TYPE
*) cp
;
1026 return negative
? -0.0 : 0.0;
1030 /* Find the decimal point */
1031 #ifdef USE_WIDE_CHAR
1032 while (*startp
!= decimal
)
1037 if (*startp
== decimal
[0])
1039 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
1040 if (decimal
[cnt
] != startp
[cnt
])
1042 if (decimal
[cnt
] == '\0')
1048 startp
+= lead_zero
+ decimal_len
;
1049 assert (lead_zero
<= (base
== 16
1050 ? (uintmax_t) INTMAX_MAX
/ 4
1051 : (uintmax_t) INTMAX_MAX
));
1052 assert (lead_zero
<= (base
== 16
1053 ? ((uintmax_t) exponent
1054 - (uintmax_t) INTMAX_MIN
) / 4
1055 : ((uintmax_t) exponent
- (uintmax_t) INTMAX_MIN
)));
1056 exponent
-= base
== 16 ? 4 * (intmax_t) lead_zero
: (intmax_t) lead_zero
;
1057 dig_no
-= lead_zero
;
1060 /* If the BASE is 16 we can use a simpler algorithm. */
1063 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1064 4, 4, 4, 4, 4, 4, 4, 4 };
1065 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
1066 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1069 while (!ISXDIGIT (*startp
))
1071 while (*startp
== L_('0'))
1073 if (ISDIGIT (*startp
))
1074 val
= *startp
++ - L_('0');
1076 val
= 10 + TOLOWER (*startp
++) - L_('a');
1078 /* We cannot have a leading zero. */
1081 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
1083 /* We don't have to care for wrapping. This is the normal
1084 case so we add the first clause in the `if' expression as
1085 an optimization. It is a compile-time constant and so does
1086 not cost anything. */
1087 retval
[idx
] = val
<< (pos
- bits
+ 1);
1092 retval
[idx
--] = val
>> (bits
- pos
- 1);
1093 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
1094 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
1097 /* Adjust the exponent for the bits we are shifting in. */
1098 assert (int_no
<= (uintmax_t) (exponent
< 0
1099 ? (INTMAX_MAX
- bits
+ 1) / 4
1100 : (INTMAX_MAX
- exponent
- bits
+ 1) / 4));
1101 exponent
+= bits
- 1 + ((intmax_t) int_no
- 1) * 4;
1103 while (--dig_no
> 0 && idx
>= 0)
1105 if (!ISXDIGIT (*startp
))
1106 startp
+= decimal_len
;
1107 if (ISDIGIT (*startp
))
1108 val
= *startp
++ - L_('0');
1110 val
= 10 + TOLOWER (*startp
++) - L_('a');
1114 retval
[idx
] |= val
<< (pos
- 4 + 1);
1119 retval
[idx
--] |= val
>> (4 - pos
- 1);
1120 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
1123 int rest_nonzero
= 0;
1124 while (--dig_no
> 0)
1126 if (*startp
!= L_('0'))
1133 return round_and_return (retval
, exponent
, negative
, val
,
1134 BITS_PER_MP_LIMB
- 1, rest_nonzero
);
1138 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
1142 /* We ran out of digits. */
1143 MPN_ZERO (retval
, idx
);
1145 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1148 /* Now we have the number of digits in total and the integer digits as well
1149 as the exponent and its sign. We can decide whether the read digits are
1150 really integer digits or belong to the fractional part; i.e. we normalize
1153 intmax_t incr
= (exponent
< 0
1154 ? MAX (-(intmax_t) int_no
, exponent
)
1155 : MIN ((intmax_t) dig_no
- (intmax_t) int_no
, exponent
));
1160 if (__glibc_unlikely (exponent
> MAX_10_EXP
+ 1 - (intmax_t) int_no
))
1161 return overflow_value (negative
);
1163 /* 10^(MIN_10_EXP-1) is not normal. Thus, 10^(MIN_10_EXP-1) /
1164 2^MANT_DIG is below half the least subnormal, so anything with a
1165 base-10 exponent less than the base-10 exponent (which is
1166 MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
1167 underflows. DIG is floor((MANT_DIG-1)log10(2)), so an exponent
1168 below MIN_10_EXP - (DIG + 3) underflows. But EXPONENT is
1169 actually an exponent multiplied only by a fractional part, not an
1170 integer part, so an exponent below MIN_10_EXP - (DIG + 2)
1172 if (__glibc_unlikely (exponent
< MIN_10_EXP
- (DIG
+ 2)))
1173 return underflow_value (negative
);
1177 /* Read the integer part as a multi-precision number to NUM. */
1178 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1179 #ifndef USE_WIDE_CHAR
1180 , decimal
, decimal_len
, thousands
1186 /* We now multiply the gained number by the given power of ten. */
1187 mp_limb_t
*psrc
= num
;
1188 mp_limb_t
*pdest
= den
;
1190 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1194 if ((exponent
& expbit
) != 0)
1196 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1200 /* FIXME: not the whole multiplication has to be
1201 done. If we have the needed number of bits we
1202 only need the information whether more non-zero
1204 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1205 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1206 &__tens
[ttab
->arrayoff
1207 + _FPIO_CONST_OFFSET
],
1210 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1211 + _FPIO_CONST_OFFSET
],
1212 size
, psrc
, numsize
);
1216 (void) SWAP (psrc
, pdest
);
1221 while (exponent
!= 0);
1224 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1227 /* Determine how many bits of the result we already have. */
1228 count_leading_zeros (bits
, num
[numsize
- 1]);
1229 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1231 /* Now we know the exponent of the number in base two.
1232 Check it against the maximum possible exponent. */
1233 if (__glibc_unlikely (bits
> MAX_EXP
))
1234 return overflow_value (negative
);
1236 /* We have already the first BITS bits of the result. Together with
1237 the information whether more non-zero bits follow this is enough
1238 to determine the result. */
1239 if (bits
> MANT_DIG
)
1242 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1243 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1244 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1246 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1250 memcpy (retval
, &num
[least_idx
],
1251 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1254 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1255 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1257 << (BITS_PER_MP_LIMB
- least_bit
));
1258 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1259 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1262 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1263 for (i
= 0; num
[i
] == 0; ++i
)
1266 return round_and_return (retval
, bits
- 1, negative
,
1267 num
[round_idx
], round_bit
,
1268 int_no
< dig_no
|| i
< round_idx
);
1271 else if (dig_no
== int_no
)
1273 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1274 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1276 if (target_bit
== is_bit
)
1278 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1279 numsize
* sizeof (mp_limb_t
));
1280 /* FIXME: the following loop can be avoided if we assume a
1281 maximal MANT_DIG value. */
1282 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1284 else if (target_bit
> is_bit
)
1286 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1287 num
, numsize
, target_bit
- is_bit
);
1288 /* FIXME: the following loop can be avoided if we assume a
1289 maximal MANT_DIG value. */
1290 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1295 assert (numsize
< RETURN_LIMB_SIZE
);
1297 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1298 num
, numsize
, is_bit
- target_bit
);
1299 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1300 /* FIXME: the following loop can be avoided if we assume a
1301 maximal MANT_DIG value. */
1302 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1305 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1309 /* Store the bits we already have. */
1310 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1311 #if RETURN_LIMB_SIZE > 1
1312 if (numsize
< RETURN_LIMB_SIZE
)
1313 # if RETURN_LIMB_SIZE == 2
1314 retval
[numsize
] = 0;
1316 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1321 /* We have to compute at least some of the fractional digits. */
1323 /* We construct a fraction and the result of the division gives us
1324 the needed digits. The denominator is 1.0 multiplied by the
1325 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1326 123e-6 gives 123 / 1000000. */
1331 int need_frac_digits
;
1333 mp_limb_t
*psrc
= den
;
1334 mp_limb_t
*pdest
= num
;
1335 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1337 assert (dig_no
> int_no
1339 && exponent
>= MIN_10_EXP
- (DIG
+ 2));
1341 /* We need to compute MANT_DIG - BITS fractional bits that lie
1342 within the mantissa of the result, the following bit for
1343 rounding, and to know whether any subsequent bit is 0.
1344 Computing a bit with value 2^-n means looking at n digits after
1345 the decimal point. */
1348 /* The bits required are those immediately after the point. */
1349 assert (int_no
> 0 && exponent
== 0);
1350 need_frac_digits
= 1 + MANT_DIG
- bits
;
1354 /* The number is in the form .123eEXPONENT. */
1355 assert (int_no
== 0 && *startp
!= L_('0'));
1356 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1358 int neg_exp_2
= ((1 - exponent
) * 10) / 3 + 1;
1359 /* The number is at least 2^-NEG_EXP_2. We need up to
1360 MANT_DIG bits following that bit. */
1361 need_frac_digits
= neg_exp_2
+ MANT_DIG
;
1362 /* However, we never need bits beyond 1/4 ulp of the smallest
1363 representable value. (That 1/4 ulp bit is only needed to
1364 determine tinyness on machines where tinyness is determined
1366 if (need_frac_digits
> MANT_DIG
- MIN_EXP
+ 2)
1367 need_frac_digits
= MANT_DIG
- MIN_EXP
+ 2;
1368 /* At this point, NEED_FRAC_DIGITS is the total number of
1369 digits needed after the point, but some of those may be
1371 need_frac_digits
+= exponent
;
1372 /* Any cases underflowing enough that none of the fractional
1373 digits are needed should have been caught earlier (such
1374 cases are on the order of 10^-n or smaller where 2^-n is
1375 the least subnormal). */
1376 assert (need_frac_digits
> 0);
1379 if (need_frac_digits
> (intmax_t) dig_no
- (intmax_t) int_no
)
1380 need_frac_digits
= (intmax_t) dig_no
- (intmax_t) int_no
;
1382 if ((intmax_t) dig_no
> (intmax_t) int_no
+ need_frac_digits
)
1384 dig_no
= int_no
+ need_frac_digits
;
1390 neg_exp
= (intmax_t) dig_no
- (intmax_t) int_no
- exponent
;
1392 /* Construct the denominator. */
1397 if ((neg_exp
& expbit
) != 0)
1404 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1405 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1406 densize
* sizeof (mp_limb_t
));
1410 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1411 + _FPIO_CONST_OFFSET
],
1412 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1414 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1417 (void) SWAP (psrc
, pdest
);
1423 while (neg_exp
!= 0);
1426 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1428 /* Read the fractional digits from the string. */
1429 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1430 #ifndef USE_WIDE_CHAR
1431 , decimal
, decimal_len
, thousands
1435 /* We now have to shift both numbers so that the highest bit in the
1436 denominator is set. In the same process we copy the numerator to
1437 a high place in the array so that the division constructs the wanted
1438 digits. This is done by a "quasi fix point" number representation.
1440 num: ddddddddddd . 0000000000000000000000
1442 den: ddddddddddd n >= m
1446 count_leading_zeros (cnt
, den
[densize
- 1]);
1450 /* Don't call `mpn_shift' with a count of zero since the specification
1451 does not allow this. */
1452 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1453 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1455 num
[numsize
++] = cy
;
1458 /* Now we are ready for the division. But it is not necessary to
1459 do a full multi-precision division because we only need a small
1460 number of bits for the result. So we do not use __mpn_divmod
1461 here but instead do the division here by hand and stop whenever
1462 the needed number of bits is reached. The code itself comes
1463 from the GNU MP Library by Torbj\"orn Granlund. */
1471 mp_limb_t d
, n
, quot
;
1476 assert (numsize
== 1 && n
< d
);
1480 udiv_qrnnd (quot
, n
, n
, 0, d
);
1487 cnt = BITS_PER_MP_LIMB; \
1489 count_leading_zeros (cnt, quot); \
1491 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1493 used = MANT_DIG + cnt; \
1494 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1495 bits = MANT_DIG + 1; \
1499 /* Note that we only clear the second element. */ \
1500 /* The conditional is determined at compile time. */ \
1501 if (RETURN_LIMB_SIZE > 1) \
1507 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1508 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1512 used = MANT_DIG - bits; \
1514 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1516 bits += BITS_PER_MP_LIMB
1520 while (bits
<= MANT_DIG
);
1522 return round_and_return (retval
, exponent
- 1, negative
,
1523 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1524 more_bits
|| n
!= 0);
1528 mp_limb_t d0
, d1
, n0
, n1
;
1535 if (numsize
< densize
)
1539 /* The numerator of the number occupies fewer bits than
1540 the denominator but the one limb is bigger than the
1541 high limb of the numerator. */
1548 exponent
-= BITS_PER_MP_LIMB
;
1551 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1552 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1553 BITS_PER_MP_LIMB
, 0);
1556 used
= MANT_DIG
- bits
;
1558 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1560 bits
+= BITS_PER_MP_LIMB
;
1572 while (bits
<= MANT_DIG
)
1578 /* QUOT should be either 111..111 or 111..110. We need
1579 special treatment of this rare case as normal division
1580 would give overflow. */
1581 quot
= ~(mp_limb_t
) 0;
1584 if (r
< d1
) /* Carry in the addition? */
1586 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1589 n1
= d0
- (d0
!= 0);
1594 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1595 umul_ppmm (n1
, n0
, d0
, quot
);
1599 if (n1
> r
|| (n1
== r
&& n0
> 0))
1601 /* The estimated QUOT was too large. */
1604 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1606 if (r
>= d1
) /* If not carry, test QUOT again. */
1609 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1615 return round_and_return (retval
, exponent
- 1, negative
,
1616 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1617 more_bits
|| n1
!= 0 || n0
!= 0);
1622 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1626 dX
= den
[densize
- 1];
1627 d1
= den
[densize
- 2];
1629 /* The division does not work if the upper limb of the two-limb
1630 numerator is greater than the denominator. */
1631 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1634 if (numsize
< densize
)
1636 mp_size_t empty
= densize
- numsize
;
1640 exponent
-= empty
* BITS_PER_MP_LIMB
;
1643 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1645 /* We make a difference here because the compiler
1646 cannot optimize the `else' case that good and
1647 this reflects all currently used FLOAT types
1648 and GMP implementations. */
1649 #if RETURN_LIMB_SIZE <= 2
1650 assert (empty
== 1);
1651 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1652 BITS_PER_MP_LIMB
, 0);
1654 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1655 retval
[i
] = retval
[i
- empty
];
1662 used
= MANT_DIG
- bits
;
1663 if (used
>= BITS_PER_MP_LIMB
)
1666 (void) __mpn_lshift (&retval
[used
1667 / BITS_PER_MP_LIMB
],
1670 - used
/ BITS_PER_MP_LIMB
),
1671 used
% BITS_PER_MP_LIMB
);
1672 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1676 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1678 bits
+= empty
* BITS_PER_MP_LIMB
;
1680 for (i
= numsize
; i
> 0; --i
)
1681 num
[i
+ empty
] = num
[i
- 1];
1682 MPN_ZERO (num
, empty
+ 1);
1687 assert (numsize
== densize
);
1688 for (i
= numsize
; i
> 0; --i
)
1689 num
[i
] = num
[i
- 1];
1696 while (bits
<= MANT_DIG
)
1699 /* This might over-estimate QUOT, but it's probably not
1700 worth the extra code here to find out. */
1701 quot
= ~(mp_limb_t
) 0;
1706 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1707 umul_ppmm (n1
, n0
, d1
, quot
);
1709 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1713 if (r
< dX
) /* I.e. "carry in previous addition?" */
1720 /* Possible optimization: We already have (q * n0) and (1 * n1)
1721 after the calculation of QUOT. Taking advantage of this, we
1722 could make this loop make two iterations less. */
1724 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1726 if (num
[densize
] != cy
)
1728 cy
= __mpn_add_n (num
, num
, den
, densize
);
1732 n0
= num
[densize
] = num
[densize
- 1];
1733 for (i
= densize
- 1; i
> 0; --i
)
1734 num
[i
] = num
[i
- 1];
1740 for (i
= densize
; i
>= 0 && num
[i
] == 0; --i
)
1742 return round_and_return (retval
, exponent
- 1, negative
,
1743 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1744 more_bits
|| i
>= 0);
1751 #if defined _LIBC && !defined USE_WIDE_CHAR
1752 libc_hidden_def (____STRTOF_INTERNAL
)
1755 /* External user entry point. */
1758 #ifdef weak_function
1761 __STRTOF (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, locale_t loc
)
1763 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1766 libc_hidden_def (__STRTOF
)
1767 libc_hidden_ver (__STRTOF
, STRTOF
)
1769 weak_alias (__STRTOF
, STRTOF
)
1771 #ifdef LONG_DOUBLE_COMPAT
1772 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1773 # ifdef USE_WIDE_CHAR
1774 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1776 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1779 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1780 # ifdef USE_WIDE_CHAR
1781 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1783 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);