1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2014 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
);
23 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
24 int, int, __locale_t
);
26 /* Configuration part. These macros are defined by `strtold.c',
27 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
28 `long double' and `float' versions of the reader. */
30 # include <math_ldbl_opt.h>
34 # define STRTOF wcstod_l
35 # define __STRTOF __wcstod_l
37 # define STRTOF strtod_l
38 # define __STRTOF __strtod_l
40 # define MPN2FLOAT __mpn_construct_double
41 # define FLOAT_HUGE_VAL HUGE_VAL
42 # define SET_MANTISSA(flt, mant) \
43 do { union ieee754_double u; \
45 u.ieee_nan.mantissa0 = (mant) >> 32; \
46 u.ieee_nan.mantissa1 = (mant); \
47 if ((u.ieee.mantissa0 | u.ieee.mantissa1) != 0) \
51 /* End of configuration part. */
57 #include "../locale/localeinfo.h"
63 #include <rounding-mode.h>
66 /* The gmp headers need some configuration frobs. */
69 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
70 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
71 #include <gmp-mparam.h>
75 #include "fpioconst.h"
80 /* We use this code for the extended locale handling where the
81 function gets as an additional argument the locale which has to be
82 used. To access the values we have to redefine the _NL_CURRENT and
83 _NL_CURRENT_WORD macros. */
85 #define _NL_CURRENT(category, item) \
86 (current->values[_NL_ITEM_INDEX (item)].string)
87 #undef _NL_CURRENT_WORD
88 #define _NL_CURRENT_WORD(category, item) \
89 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
91 #if defined _LIBC || defined HAVE_WCHAR_H
97 # define STRING_TYPE wchar_t
98 # define CHAR_TYPE wint_t
100 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
101 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
102 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
103 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
104 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
105 # define STRNCASECMP(S1, S2, N) \
106 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
107 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
109 # define STRING_TYPE char
110 # define CHAR_TYPE char
112 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
113 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
114 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
115 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
116 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
117 # define STRNCASECMP(S1, S2, N) \
118 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
119 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
123 /* Constants we need from float.h; select the set for the FLOAT precision. */
124 #define MANT_DIG PASTE(FLT,_MANT_DIG)
125 #define DIG PASTE(FLT,_DIG)
126 #define MAX_EXP PASTE(FLT,_MAX_EXP)
127 #define MIN_EXP PASTE(FLT,_MIN_EXP)
128 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
129 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
130 #define MAX_VALUE PASTE(FLT,_MAX)
131 #define MIN_VALUE PASTE(FLT,_MIN)
133 /* Extra macros required to get FLT expanded before the pasting. */
134 #define PASTE(a,b) PASTE1(a,b)
135 #define PASTE1(a,b) a##b
137 /* Function to construct a floating point number from an MP integer
138 containing the fraction bits, a base 2 exponent, and a sign flag. */
139 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
141 /* Definitions according to limb size used. */
142 #if BITS_PER_MP_LIMB == 32
143 # define MAX_DIG_PER_LIMB 9
144 # define MAX_FAC_PER_LIMB 1000000000UL
145 #elif BITS_PER_MP_LIMB == 64
146 # define MAX_DIG_PER_LIMB 19
147 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
149 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
152 extern const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1];
155 #define howmany(x,y) (((x)+((y)-1))/(y))
157 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
159 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
161 #define RETURN(val,end) \
162 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
163 return val; } while (0)
165 /* Maximum size necessary for mpn integers to hold floating point
166 numbers. The largest number we need to hold is 10^n where 2^-n is
167 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
168 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
169 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
170 BITS_PER_MP_LIMB) + 2)
171 /* Declare an mpn integer variable that big. */
172 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
173 /* Copy an mpn integer value. */
174 #define MPN_ASSIGN(dst, src) \
175 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
178 /* Set errno and return an overflowing value with sign specified by
181 overflow_value (int negative
)
183 __set_errno (ERANGE
);
184 #if FLT_EVAL_METHOD != 0
187 FLOAT result
= (negative
? -MAX_VALUE
: MAX_VALUE
) * MAX_VALUE
;
192 /* Set errno and return an underflowing value with sign specified by
195 underflow_value (int negative
)
197 __set_errno (ERANGE
);
198 #if FLT_EVAL_METHOD != 0
201 FLOAT result
= (negative
? -MIN_VALUE
: MIN_VALUE
) * MIN_VALUE
;
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
, intmax_t exponent
, int negative
,
210 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
212 int mode
= get_rounding_mode ();
214 if (exponent
< MIN_EXP
- 1)
216 if (exponent
< MIN_EXP
- 1 - MANT_DIG
)
217 return underflow_value (negative
);
219 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
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
- 1; ++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 /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB. */
247 if ((shift
% BITS_PER_MP_LIMB
) != 0)
248 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
249 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
250 shift
% BITS_PER_MP_LIMB
);
252 for (i
= 0; i
< RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
); i
++)
253 retval
[i
] = retval
[i
+ (shift
/ BITS_PER_MP_LIMB
)];
254 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
255 shift
/ BITS_PER_MP_LIMB
);
259 if (TININESS_AFTER_ROUNDING
&& shift
== 1)
261 /* Whether the result counts as tiny depends on whether,
262 after rounding to the normal precision, it still has
263 a subnormal exponent. */
264 mp_limb_t retval_normal
[RETURN_LIMB_SIZE
];
265 if (round_away (negative
,
266 (retval
[0] & 1) != 0,
268 & (((mp_limb_t
) 1) << round_bit
)) != 0,
271 & ((((mp_limb_t
) 1) << round_bit
) - 1))
275 mp_limb_t cy
= __mpn_add_1 (retval_normal
, retval
,
276 RETURN_LIMB_SIZE
, 1);
278 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
279 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
280 ((retval_normal
[RETURN_LIMB_SIZE
- 1]
281 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
)))
286 round_limb
= retval
[0];
287 round_bit
= shift
- 1;
288 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
290 /* This is a hook for the m68k long double format, where the
291 exponent bias is the same for normalized and denormalized
294 # define DENORM_EXP (MIN_EXP - 2)
296 exponent
= DENORM_EXP
;
298 && ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
300 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
302 __set_errno (ERANGE
);
303 volatile FLOAT force_underflow_exception
= MIN_VALUE
* MIN_VALUE
;
304 (void) force_underflow_exception
;
308 if (exponent
> MAX_EXP
)
311 if (round_away (negative
,
312 (retval
[0] & 1) != 0,
313 (round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0,
315 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0),
318 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
320 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
321 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
322 (retval
[RETURN_LIMB_SIZE
- 1]
323 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
326 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
327 retval
[RETURN_LIMB_SIZE
- 1]
328 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
330 else if (exponent
== DENORM_EXP
331 && (retval
[RETURN_LIMB_SIZE
- 1]
332 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
334 /* The number was denormalized but now normalized. */
335 exponent
= MIN_EXP
- 1;
338 if (exponent
> MAX_EXP
)
340 return overflow_value (negative
);
342 return MPN2FLOAT (retval
, exponent
, negative
);
346 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
347 into N. Return the size of the number limbs in NSIZE at the first
348 character od the string that is not part of the integer as the function
349 value. If the EXPONENT is small enough to be taken as an additional
350 factor for the resulting number (see code) multiply by it. */
351 static const STRING_TYPE
*
352 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
354 #ifndef USE_WIDE_CHAR
355 , const char *decimal
, size_t decimal_len
, const char *thousands
360 /* Number of digits for actual limb. */
369 if (cnt
== MAX_DIG_PER_LIMB
)
379 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
380 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
383 assert (*nsize
< MPNSIZE
);
392 /* There might be thousands separators or radix characters in
393 the string. But these all can be ignored because we know the
394 format of the number is correct and we have an exact number
395 of characters to read. */
397 if (*str
< L
'0' || *str
> L
'9')
400 if (*str
< '0' || *str
> '9')
403 if (thousands
!= NULL
&& *str
== *thousands
404 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
405 if (thousands
[inner
] != str
[inner
])
407 thousands
[inner
] == '\0'; }))
413 low
= low
* 10 + *str
++ - L_('0');
416 while (--digcnt
> 0);
418 if (*exponent
> 0 && *exponent
<= MAX_DIG_PER_LIMB
- cnt
)
420 low
*= _tens_in_limb
[*exponent
];
421 start
= _tens_in_limb
[cnt
+ *exponent
];
425 start
= _tens_in_limb
[cnt
];
435 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
436 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
439 assert (*nsize
< MPNSIZE
);
448 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
449 with the COUNT most significant bits of LIMB.
451 Implemented as a macro, so that __builtin_constant_p works even at -O0.
453 Tege doesn't like this macro so I have to write it here myself. :)
455 #define __mpn_lshift_1(ptr, size, count, limb) \
458 mp_limb_t *__ptr = (ptr); \
459 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
462 for (i = (size) - 1; i > 0; --i) \
463 __ptr[i] = __ptr[i - 1]; \
468 /* We assume count > 0 && count < BITS_PER_MP_LIMB here. */ \
469 unsigned int __count = (count); \
470 (void) __mpn_lshift (__ptr, __ptr, size, __count); \
471 __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count); \
477 #define INTERNAL(x) INTERNAL1(x)
478 #define INTERNAL1(x) __##x##_internal
479 #ifndef ____STRTOF_INTERNAL
480 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
483 /* This file defines a function to check for correct grouping. */
484 #include "grouping.h"
487 /* Return a floating point number with the value of the given string NPTR.
488 Set *ENDPTR to the character after the last used one. If the number is
489 smaller than the smallest representable number, set `errno' to ERANGE and
490 return 0.0. If the number is too big to be represented, set `errno' to
491 ERANGE and return HUGE_VAL with the appropriate sign. */
493 ____STRTOF_INTERNAL (nptr
, endptr
, group
, loc
)
494 const STRING_TYPE
*nptr
;
495 STRING_TYPE
**endptr
;
499 int negative
; /* The sign of the number. */
500 MPN_VAR (num
); /* MP representation of the number. */
501 intmax_t exponent
; /* Exponent of the number. */
503 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
506 /* When we have to compute fractional digits we form a fraction with a
507 second multi-precision number (and we sometimes need a second for
508 temporary results). */
511 /* Representation for the return value. */
512 mp_limb_t retval
[RETURN_LIMB_SIZE
];
513 /* Number of bits currently in result value. */
516 /* Running pointer after the last character processed in the string. */
517 const STRING_TYPE
*cp
, *tp
;
518 /* Start of significant part of the number. */
519 const STRING_TYPE
*startp
, *start_of_digits
;
520 /* Points at the character following the integer and fractional digits. */
521 const STRING_TYPE
*expp
;
522 /* Total number of digit and number of digits in integer part. */
523 size_t dig_no
, int_no
, lead_zero
;
524 /* Contains the last character read. */
527 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
528 there. So define it ourselves if it remains undefined. */
530 typedef unsigned int wint_t;
532 /* The radix character of the current locale. */
539 /* The thousands character of the current locale. */
541 wchar_t thousands
= L
'\0';
543 const char *thousands
= NULL
;
545 /* The numeric grouping specification of the current locale,
546 in the format described in <locale.h>. */
547 const char *grouping
;
548 /* Used in several places. */
551 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
553 if (__glibc_unlikely (group
))
555 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
556 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
560 /* Figure out the thousands separator character. */
562 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
563 _NL_NUMERIC_THOUSANDS_SEP_WC
);
564 if (thousands
== L
'\0')
567 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
568 if (*thousands
== '\0')
579 /* Find the locale's decimal point character. */
581 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
582 assert (decimal
!= L
'\0');
583 # define decimal_len 1
585 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
586 decimal_len
= strlen (decimal
);
587 assert (decimal_len
> 0);
590 /* Prepare number representation. */
595 /* Parse string to get maximal legal prefix. We need the number of
596 characters of the integer part, the fractional part and the exponent. */
598 /* Ignore leading white space. */
603 /* Get sign of the result. */
609 else if (c
== L_('+'))
612 /* Return 0.0 if no legal string is found.
613 No character is used even if a sign was found. */
615 if (c
== (wint_t) decimal
616 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
618 /* We accept it. This funny construct is here only to indent
619 the code correctly. */
622 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
623 if (cp
[cnt
] != decimal
[cnt
])
625 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
627 /* We accept it. This funny construct is here only to indent
628 the code correctly. */
631 else if (c
< L_('0') || c
> L_('9'))
633 /* Check for `INF' or `INFINITY'. */
634 CHAR_TYPE lowc
= TOLOWER_C (c
);
636 if (lowc
== L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
638 /* Return +/- infinity. */
640 *endptr
= (STRING_TYPE
*)
641 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
644 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
647 if (lowc
== L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
654 /* Match `(n-char-sequence-digit)'. */
657 const STRING_TYPE
*startp
= cp
;
660 while ((*cp
>= L_('0') && *cp
<= L_('9'))
661 || ({ CHAR_TYPE lo
= TOLOWER (*cp
);
662 lo
>= L_('a') && lo
<= L_('z'); })
666 /* The closing brace is missing. Only match the NAN
671 /* This is a system-dependent way to specify the
672 bitmask used for the NaN. We expect it to be
673 a number which is put in the mantissa of the
676 unsigned long long int mant
;
678 mant
= STRTOULL (startp
+ 1, &endp
, 0);
680 SET_MANTISSA (retval
, mant
);
682 /* Consume the closing brace. */
688 *endptr
= (STRING_TYPE
*) cp
;
693 /* It is really a text we do not recognize. */
697 /* First look whether we are faced with a hexadecimal number. */
698 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
700 /* Okay, it is a hexa-decimal number. Remember this and skip
701 the characters. BTW: hexadecimal numbers must not be
709 /* Record the start of the digits, in case we will check their grouping. */
710 start_of_digits
= startp
= cp
;
712 /* Ignore leading zeroes. This helps us to avoid useless computations. */
714 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
717 if (__glibc_likely (thousands
== NULL
))
722 /* We also have the multibyte thousands string. */
727 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
728 if (thousands
[cnt
] != cp
[cnt
])
730 if (thousands
[cnt
] != '\0')
739 /* If no other digit but a '0' is found the result is 0.0.
740 Return current read pointer. */
741 CHAR_TYPE lowc
= TOLOWER (c
);
742 if (!((c
>= L_('0') && c
<= L_('9'))
743 || (base
== 16 && lowc
>= L_('a') && lowc
<= L_('f'))
746 c
== (wint_t) decimal
748 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
749 if (decimal
[cnt
] != cp
[cnt
])
751 decimal
[cnt
] == '\0'; })
753 /* '0x.' alone is not a valid hexadecimal number.
754 '.' alone is not valid either, but that has been checked
757 || cp
!= start_of_digits
758 || (cp
[decimal_len
] >= L_('0') && cp
[decimal_len
] <= L_('9'))
759 || ({ CHAR_TYPE lo
= TOLOWER (cp
[decimal_len
]);
760 lo
>= L_('a') && lo
<= L_('f'); })))
761 || (base
== 16 && (cp
!= start_of_digits
763 || (base
!= 16 && lowc
== L_('e'))))
766 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
769 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
772 /* If TP is at the start of the digits, there was no correctly
773 grouped prefix of the string; so no number found. */
774 RETURN (negative
? -0.0 : 0.0,
775 tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
778 /* Remember first significant digit and read following characters until the
779 decimal point, exponent character or any non-FP number character. */
784 if ((c
>= L_('0') && c
<= L_('9'))
786 && ({ CHAR_TYPE lo
= TOLOWER (c
);
787 lo
>= L_('a') && lo
<= L_('f'); })))
792 if (__builtin_expect ((wint_t) thousands
== L
'\0', 1)
793 || c
!= (wint_t) thousands
)
794 /* Not a digit or separator: end of the integer part. */
797 if (__glibc_likely (thousands
== NULL
))
801 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
802 if (thousands
[cnt
] != cp
[cnt
])
804 if (thousands
[cnt
] != '\0')
813 if (__builtin_expect (grouping
!= NULL
, 0) && cp
> start_of_digits
)
815 /* Check the grouping of the digits. */
817 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
820 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
825 /* Less than the entire string was correctly grouped. */
827 if (tp
== start_of_digits
)
828 /* No valid group of numbers at all: no valid number. */
832 /* The number is validly grouped, but consists
833 only of zeroes. The whole value is zero. */
834 RETURN (negative
? -0.0 : 0.0, tp
);
836 /* Recompute DIG_NO so we won't read more digits than
837 are properly grouped. */
840 for (tp
= startp
; tp
< cp
; ++tp
)
841 if (*tp
>= L_('0') && *tp
<= L_('9'))
851 /* We have the number of digits in the integer part. Whether these
852 are all or any is really a fractional digit will be decided
855 lead_zero
= int_no
== 0 ? (size_t) -1 : 0;
857 /* Read the fractional digits. A special case are the 'american
858 style' numbers like `16.' i.e. with decimal point but without
862 c
== (wint_t) decimal
864 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
865 if (decimal
[cnt
] != cp
[cnt
])
867 decimal
[cnt
] == '\0'; })
873 while ((c
>= L_('0') && c
<= L_('9')) ||
874 (base
== 16 && ({ CHAR_TYPE lo
= TOLOWER (c
);
875 lo
>= L_('a') && lo
<= L_('f'); })))
877 if (c
!= L_('0') && lead_zero
== (size_t) -1)
878 lead_zero
= dig_no
- int_no
;
883 assert (dig_no
<= (uintmax_t) INTMAX_MAX
);
885 /* Remember start of exponent (if any). */
890 if ((base
== 16 && lowc
== L_('p'))
891 || (base
!= 16 && lowc
== L_('e')))
893 int exp_negative
= 0;
901 else if (c
== L_('+'))
904 if (c
>= L_('0') && c
<= L_('9'))
908 /* Get the exponent limit. */
913 assert (int_no
<= (uintmax_t) (INTMAX_MAX
914 + MIN_EXP
- MANT_DIG
) / 4);
915 exp_limit
= -MIN_EXP
+ MANT_DIG
+ 4 * (intmax_t) int_no
;
921 assert (lead_zero
== 0
922 && int_no
<= (uintmax_t) INTMAX_MAX
/ 4);
923 exp_limit
= MAX_EXP
- 4 * (intmax_t) int_no
+ 3;
925 else if (lead_zero
== (size_t) -1)
927 /* The number is zero and this limit is
929 exp_limit
= MAX_EXP
+ 3;
934 <= (uintmax_t) (INTMAX_MAX
- MAX_EXP
- 3) / 4);
936 + 4 * (intmax_t) lead_zero
946 <= (uintmax_t) (INTMAX_MAX
+ MIN_10_EXP
- MANT_DIG
));
947 exp_limit
= -MIN_10_EXP
+ MANT_DIG
+ (intmax_t) int_no
;
953 assert (lead_zero
== 0
954 && int_no
<= (uintmax_t) INTMAX_MAX
);
955 exp_limit
= MAX_10_EXP
- (intmax_t) int_no
+ 1;
957 else if (lead_zero
== (size_t) -1)
959 /* The number is zero and this limit is
961 exp_limit
= MAX_10_EXP
+ 1;
966 <= (uintmax_t) (INTMAX_MAX
- MAX_10_EXP
- 1));
967 exp_limit
= MAX_10_EXP
+ (intmax_t) lead_zero
+ 1;
977 if (__builtin_expect ((exponent
> exp_limit
/ 10
978 || (exponent
== exp_limit
/ 10
979 && c
- L_('0') > exp_limit
% 10)), 0))
980 /* The exponent is too large/small to represent a valid
985 /* We have to take care for special situation: a joker
986 might have written "0.0e100000" which is in fact
988 if (lead_zero
== (size_t) -1)
989 result
= negative
? -0.0 : 0.0;
992 /* Overflow or underflow. */
993 result
= (exp_negative
994 ? underflow_value (negative
)
995 : overflow_value (negative
));
998 /* Accept all following digits as part of the exponent. */
1001 while (*cp
>= L_('0') && *cp
<= L_('9'));
1003 RETURN (result
, cp
);
1008 exponent
+= c
- L_('0');
1012 while (c
>= L_('0') && c
<= L_('9'));
1015 exponent
= -exponent
;
1021 /* We don't want to have to work with trailing zeroes after the radix. */
1022 if (dig_no
> int_no
)
1024 while (expp
[-1] == L_('0'))
1029 assert (dig_no
>= int_no
);
1032 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
1035 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
1038 if (expp
[-1] != L_('0'))
1044 exponent
+= base
== 16 ? 4 : 1;
1046 while (dig_no
> 0 && exponent
< 0);
1050 /* The whole string is parsed. Store the address of the next character. */
1052 *endptr
= (STRING_TYPE
*) cp
;
1055 return negative
? -0.0 : 0.0;
1059 /* Find the decimal point */
1060 #ifdef USE_WIDE_CHAR
1061 while (*startp
!= decimal
)
1066 if (*startp
== decimal
[0])
1068 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
1069 if (decimal
[cnt
] != startp
[cnt
])
1071 if (decimal
[cnt
] == '\0')
1077 startp
+= lead_zero
+ decimal_len
;
1078 assert (lead_zero
<= (base
== 16
1079 ? (uintmax_t) INTMAX_MAX
/ 4
1080 : (uintmax_t) INTMAX_MAX
));
1081 assert (lead_zero
<= (base
== 16
1082 ? ((uintmax_t) exponent
1083 - (uintmax_t) INTMAX_MIN
) / 4
1084 : ((uintmax_t) exponent
- (uintmax_t) INTMAX_MIN
)));
1085 exponent
-= base
== 16 ? 4 * (intmax_t) lead_zero
: (intmax_t) lead_zero
;
1086 dig_no
-= lead_zero
;
1089 /* If the BASE is 16 we can use a simpler algorithm. */
1092 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1093 4, 4, 4, 4, 4, 4, 4, 4 };
1094 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
1095 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1098 while (!ISXDIGIT (*startp
))
1100 while (*startp
== L_('0'))
1102 if (ISDIGIT (*startp
))
1103 val
= *startp
++ - L_('0');
1105 val
= 10 + TOLOWER (*startp
++) - L_('a');
1107 /* We cannot have a leading zero. */
1110 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
1112 /* We don't have to care for wrapping. This is the normal
1113 case so we add the first clause in the `if' expression as
1114 an optimization. It is a compile-time constant and so does
1115 not cost anything. */
1116 retval
[idx
] = val
<< (pos
- bits
+ 1);
1121 retval
[idx
--] = val
>> (bits
- pos
- 1);
1122 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
1123 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
1126 /* Adjust the exponent for the bits we are shifting in. */
1127 assert (int_no
<= (uintmax_t) (exponent
< 0
1128 ? (INTMAX_MAX
- bits
+ 1) / 4
1129 : (INTMAX_MAX
- exponent
- bits
+ 1) / 4));
1130 exponent
+= bits
- 1 + ((intmax_t) int_no
- 1) * 4;
1132 while (--dig_no
> 0 && idx
>= 0)
1134 if (!ISXDIGIT (*startp
))
1135 startp
+= decimal_len
;
1136 if (ISDIGIT (*startp
))
1137 val
= *startp
++ - L_('0');
1139 val
= 10 + TOLOWER (*startp
++) - L_('a');
1143 retval
[idx
] |= val
<< (pos
- 4 + 1);
1148 retval
[idx
--] |= val
>> (4 - pos
- 1);
1149 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
1152 int rest_nonzero
= 0;
1153 while (--dig_no
> 0)
1155 if (*startp
!= L_('0'))
1162 return round_and_return (retval
, exponent
, negative
, val
,
1163 BITS_PER_MP_LIMB
- 1, rest_nonzero
);
1167 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
1171 /* We ran out of digits. */
1172 MPN_ZERO (retval
, idx
);
1174 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1177 /* Now we have the number of digits in total and the integer digits as well
1178 as the exponent and its sign. We can decide whether the read digits are
1179 really integer digits or belong to the fractional part; i.e. we normalize
1182 intmax_t incr
= (exponent
< 0
1183 ? MAX (-(intmax_t) int_no
, exponent
)
1184 : MIN ((intmax_t) dig_no
- (intmax_t) int_no
, exponent
));
1189 if (__glibc_unlikely (exponent
> MAX_10_EXP
+ 1 - (intmax_t) int_no
))
1190 return overflow_value (negative
);
1192 if (__glibc_unlikely (exponent
< MIN_10_EXP
- (DIG
+ 1)))
1193 return underflow_value (negative
);
1197 /* Read the integer part as a multi-precision number to NUM. */
1198 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1199 #ifndef USE_WIDE_CHAR
1200 , decimal
, decimal_len
, thousands
1206 /* We now multiply the gained number by the given power of ten. */
1207 mp_limb_t
*psrc
= num
;
1208 mp_limb_t
*pdest
= den
;
1210 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1214 if ((exponent
& expbit
) != 0)
1216 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1220 /* FIXME: not the whole multiplication has to be
1221 done. If we have the needed number of bits we
1222 only need the information whether more non-zero
1224 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1225 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1226 &__tens
[ttab
->arrayoff
1227 + _FPIO_CONST_OFFSET
],
1230 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1231 + _FPIO_CONST_OFFSET
],
1232 size
, psrc
, numsize
);
1236 (void) SWAP (psrc
, pdest
);
1241 while (exponent
!= 0);
1244 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1247 /* Determine how many bits of the result we already have. */
1248 count_leading_zeros (bits
, num
[numsize
- 1]);
1249 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1251 /* Now we know the exponent of the number in base two.
1252 Check it against the maximum possible exponent. */
1253 if (__glibc_unlikely (bits
> MAX_EXP
))
1254 return overflow_value (negative
);
1256 /* We have already the first BITS bits of the result. Together with
1257 the information whether more non-zero bits follow this is enough
1258 to determine the result. */
1259 if (bits
> MANT_DIG
)
1262 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1263 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1264 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1266 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1270 memcpy (retval
, &num
[least_idx
],
1271 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1274 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1275 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1277 << (BITS_PER_MP_LIMB
- least_bit
));
1278 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1279 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1282 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1283 for (i
= 0; num
[i
] == 0; ++i
)
1286 return round_and_return (retval
, bits
- 1, negative
,
1287 num
[round_idx
], round_bit
,
1288 int_no
< dig_no
|| i
< round_idx
);
1291 else if (dig_no
== int_no
)
1293 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1294 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1296 if (target_bit
== is_bit
)
1298 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1299 numsize
* sizeof (mp_limb_t
));
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
);
1304 else if (target_bit
> is_bit
)
1306 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1307 num
, numsize
, target_bit
- is_bit
);
1308 /* FIXME: the following loop can be avoided if we assume a
1309 maximal MANT_DIG value. */
1310 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1315 assert (numsize
< RETURN_LIMB_SIZE
);
1317 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1318 num
, numsize
, is_bit
- target_bit
);
1319 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1320 /* FIXME: the following loop can be avoided if we assume a
1321 maximal MANT_DIG value. */
1322 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1325 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1329 /* Store the bits we already have. */
1330 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1331 #if RETURN_LIMB_SIZE > 1
1332 if (numsize
< RETURN_LIMB_SIZE
)
1333 # if RETURN_LIMB_SIZE == 2
1334 retval
[numsize
] = 0;
1336 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1341 /* We have to compute at least some of the fractional digits. */
1343 /* We construct a fraction and the result of the division gives us
1344 the needed digits. The denominator is 1.0 multiplied by the
1345 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1346 123e-6 gives 123 / 1000000. */
1351 int need_frac_digits
;
1353 mp_limb_t
*psrc
= den
;
1354 mp_limb_t
*pdest
= num
;
1355 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1357 assert (dig_no
> int_no
1359 && exponent
>= MIN_10_EXP
- (DIG
+ 1));
1361 /* We need to compute MANT_DIG - BITS fractional bits that lie
1362 within the mantissa of the result, the following bit for
1363 rounding, and to know whether any subsequent bit is 0.
1364 Computing a bit with value 2^-n means looking at n digits after
1365 the decimal point. */
1368 /* The bits required are those immediately after the point. */
1369 assert (int_no
> 0 && exponent
== 0);
1370 need_frac_digits
= 1 + MANT_DIG
- bits
;
1374 /* The number is in the form .123eEXPONENT. */
1375 assert (int_no
== 0 && *startp
!= L_('0'));
1376 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1378 int neg_exp_2
= ((1 - exponent
) * 10) / 3 + 1;
1379 /* The number is at least 2^-NEG_EXP_2. We need up to
1380 MANT_DIG bits following that bit. */
1381 need_frac_digits
= neg_exp_2
+ MANT_DIG
;
1382 /* However, we never need bits beyond 1/4 ulp of the smallest
1383 representable value. (That 1/4 ulp bit is only needed to
1384 determine tinyness on machines where tinyness is determined
1386 if (need_frac_digits
> MANT_DIG
- MIN_EXP
+ 2)
1387 need_frac_digits
= MANT_DIG
- MIN_EXP
+ 2;
1388 /* At this point, NEED_FRAC_DIGITS is the total number of
1389 digits needed after the point, but some of those may be
1391 need_frac_digits
+= exponent
;
1392 /* Any cases underflowing enough that none of the fractional
1393 digits are needed should have been caught earlier (such
1394 cases are on the order of 10^-n or smaller where 2^-n is
1395 the least subnormal). */
1396 assert (need_frac_digits
> 0);
1399 if (need_frac_digits
> (intmax_t) dig_no
- (intmax_t) int_no
)
1400 need_frac_digits
= (intmax_t) dig_no
- (intmax_t) int_no
;
1402 if ((intmax_t) dig_no
> (intmax_t) int_no
+ need_frac_digits
)
1404 dig_no
= int_no
+ need_frac_digits
;
1410 neg_exp
= (intmax_t) dig_no
- (intmax_t) int_no
- exponent
;
1412 /* Construct the denominator. */
1417 if ((neg_exp
& expbit
) != 0)
1424 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1425 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1426 densize
* sizeof (mp_limb_t
));
1430 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1431 + _FPIO_CONST_OFFSET
],
1432 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1434 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1437 (void) SWAP (psrc
, pdest
);
1443 while (neg_exp
!= 0);
1446 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1448 /* Read the fractional digits from the string. */
1449 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1450 #ifndef USE_WIDE_CHAR
1451 , decimal
, decimal_len
, thousands
1455 /* We now have to shift both numbers so that the highest bit in the
1456 denominator is set. In the same process we copy the numerator to
1457 a high place in the array so that the division constructs the wanted
1458 digits. This is done by a "quasi fix point" number representation.
1460 num: ddddddddddd . 0000000000000000000000
1462 den: ddddddddddd n >= m
1466 count_leading_zeros (cnt
, den
[densize
- 1]);
1470 /* Don't call `mpn_shift' with a count of zero since the specification
1471 does not allow this. */
1472 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1473 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1475 num
[numsize
++] = cy
;
1478 /* Now we are ready for the division. But it is not necessary to
1479 do a full multi-precision division because we only need a small
1480 number of bits for the result. So we do not use __mpn_divmod
1481 here but instead do the division here by hand and stop whenever
1482 the needed number of bits is reached. The code itself comes
1483 from the GNU MP Library by Torbj\"orn Granlund. */
1491 mp_limb_t d
, n
, quot
;
1496 assert (numsize
== 1 && n
< d
);
1500 udiv_qrnnd (quot
, n
, n
, 0, d
);
1507 cnt = BITS_PER_MP_LIMB; \
1509 count_leading_zeros (cnt, quot); \
1511 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1513 used = MANT_DIG + cnt; \
1514 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1515 bits = MANT_DIG + 1; \
1519 /* Note that we only clear the second element. */ \
1520 /* The conditional is determined at compile time. */ \
1521 if (RETURN_LIMB_SIZE > 1) \
1527 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1528 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1532 used = MANT_DIG - bits; \
1534 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1536 bits += BITS_PER_MP_LIMB
1540 while (bits
<= MANT_DIG
);
1542 return round_and_return (retval
, exponent
- 1, negative
,
1543 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1544 more_bits
|| n
!= 0);
1548 mp_limb_t d0
, d1
, n0
, n1
;
1555 if (numsize
< densize
)
1559 /* The numerator of the number occupies fewer bits than
1560 the denominator but the one limb is bigger than the
1561 high limb of the numerator. */
1568 exponent
-= BITS_PER_MP_LIMB
;
1571 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1572 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1573 BITS_PER_MP_LIMB
, 0);
1576 used
= MANT_DIG
- bits
;
1578 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1580 bits
+= BITS_PER_MP_LIMB
;
1592 while (bits
<= MANT_DIG
)
1598 /* QUOT should be either 111..111 or 111..110. We need
1599 special treatment of this rare case as normal division
1600 would give overflow. */
1601 quot
= ~(mp_limb_t
) 0;
1604 if (r
< d1
) /* Carry in the addition? */
1606 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1609 n1
= d0
- (d0
!= 0);
1614 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1615 umul_ppmm (n1
, n0
, d0
, quot
);
1619 if (n1
> r
|| (n1
== r
&& n0
> 0))
1621 /* The estimated QUOT was too large. */
1624 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1626 if (r
>= d1
) /* If not carry, test QUOT again. */
1629 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1635 return round_and_return (retval
, exponent
- 1, negative
,
1636 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1637 more_bits
|| n1
!= 0 || n0
!= 0);
1642 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1646 dX
= den
[densize
- 1];
1647 d1
= den
[densize
- 2];
1649 /* The division does not work if the upper limb of the two-limb
1650 numerator is greater than the denominator. */
1651 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1654 if (numsize
< densize
)
1656 mp_size_t empty
= densize
- numsize
;
1660 exponent
-= empty
* BITS_PER_MP_LIMB
;
1663 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1665 /* We make a difference here because the compiler
1666 cannot optimize the `else' case that good and
1667 this reflects all currently used FLOAT types
1668 and GMP implementations. */
1669 #if RETURN_LIMB_SIZE <= 2
1670 assert (empty
== 1);
1671 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1672 BITS_PER_MP_LIMB
, 0);
1674 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1675 retval
[i
] = retval
[i
- empty
];
1682 used
= MANT_DIG
- bits
;
1683 if (used
>= BITS_PER_MP_LIMB
)
1686 (void) __mpn_lshift (&retval
[used
1687 / BITS_PER_MP_LIMB
],
1690 - used
/ BITS_PER_MP_LIMB
),
1691 used
% BITS_PER_MP_LIMB
);
1692 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1696 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1698 bits
+= empty
* BITS_PER_MP_LIMB
;
1700 for (i
= numsize
; i
> 0; --i
)
1701 num
[i
+ empty
] = num
[i
- 1];
1702 MPN_ZERO (num
, empty
+ 1);
1707 assert (numsize
== densize
);
1708 for (i
= numsize
; i
> 0; --i
)
1709 num
[i
] = num
[i
- 1];
1716 while (bits
<= MANT_DIG
)
1719 /* This might over-estimate QUOT, but it's probably not
1720 worth the extra code here to find out. */
1721 quot
= ~(mp_limb_t
) 0;
1726 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1727 umul_ppmm (n1
, n0
, d1
, quot
);
1729 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1733 if (r
< dX
) /* I.e. "carry in previous addition?" */
1740 /* Possible optimization: We already have (q * n0) and (1 * n1)
1741 after the calculation of QUOT. Taking advantage of this, we
1742 could make this loop make two iterations less. */
1744 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1746 if (num
[densize
] != cy
)
1748 cy
= __mpn_add_n (num
, num
, den
, densize
);
1752 n0
= num
[densize
] = num
[densize
- 1];
1753 for (i
= densize
- 1; i
> 0; --i
)
1754 num
[i
] = num
[i
- 1];
1760 for (i
= densize
; i
>= 0 && num
[i
] == 0; --i
)
1762 return round_and_return (retval
, exponent
- 1, negative
,
1763 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1764 more_bits
|| i
>= 0);
1771 #if defined _LIBC && !defined USE_WIDE_CHAR
1772 libc_hidden_def (____STRTOF_INTERNAL
)
1775 /* External user entry point. */
1778 #ifdef weak_function
1781 __STRTOF (nptr
, endptr
, loc
)
1782 const STRING_TYPE
*nptr
;
1783 STRING_TYPE
**endptr
;
1786 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1789 libc_hidden_def (__STRTOF
)
1790 libc_hidden_ver (__STRTOF
, STRTOF
)
1792 weak_alias (__STRTOF
, STRTOF
)
1794 #ifdef LONG_DOUBLE_COMPAT
1795 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1796 # ifdef USE_WIDE_CHAR
1797 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1799 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1802 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1803 # ifdef USE_WIDE_CHAR
1804 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1806 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);