1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2015 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"
60 #include <math_private.h>
64 #include <rounding-mode.h>
67 /* The gmp headers need some configuration frobs. */
70 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
71 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
72 #include <gmp-mparam.h>
76 #include "fpioconst.h"
81 /* We use this code for the extended locale handling where the
82 function gets as an additional argument the locale which has to be
83 used. To access the values we have to redefine the _NL_CURRENT and
84 _NL_CURRENT_WORD macros. */
86 #define _NL_CURRENT(category, item) \
87 (current->values[_NL_ITEM_INDEX (item)].string)
88 #undef _NL_CURRENT_WORD
89 #define _NL_CURRENT_WORD(category, item) \
90 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
92 #if defined _LIBC || defined HAVE_WCHAR_H
98 # define STRING_TYPE wchar_t
99 # define CHAR_TYPE wint_t
100 # define L_(Ch) L##Ch
101 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
102 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
103 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
104 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
105 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
106 # define STRNCASECMP(S1, S2, N) \
107 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
108 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
110 # define STRING_TYPE char
111 # define CHAR_TYPE char
113 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
114 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
115 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
116 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
117 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
118 # define STRNCASECMP(S1, S2, N) \
119 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
120 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
124 /* Constants we need from float.h; select the set for the FLOAT precision. */
125 #define MANT_DIG PASTE(FLT,_MANT_DIG)
126 #define DIG PASTE(FLT,_DIG)
127 #define MAX_EXP PASTE(FLT,_MAX_EXP)
128 #define MIN_EXP PASTE(FLT,_MIN_EXP)
129 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
130 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
131 #define MAX_VALUE PASTE(FLT,_MAX)
132 #define MIN_VALUE PASTE(FLT,_MIN)
134 /* Extra macros required to get FLT expanded before the pasting. */
135 #define PASTE(a,b) PASTE1(a,b)
136 #define PASTE1(a,b) a##b
138 /* Function to construct a floating point number from an MP integer
139 containing the fraction bits, a base 2 exponent, and a sign flag. */
140 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
142 /* Definitions according to limb size used. */
143 #if BITS_PER_MP_LIMB == 32
144 # define MAX_DIG_PER_LIMB 9
145 # define MAX_FAC_PER_LIMB 1000000000UL
146 #elif BITS_PER_MP_LIMB == 64
147 # define MAX_DIG_PER_LIMB 19
148 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
150 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
153 extern const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1];
156 #define howmany(x,y) (((x)+((y)-1))/(y))
158 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
160 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
162 #define RETURN(val,end) \
163 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
164 return val; } while (0)
166 /* Maximum size necessary for mpn integers to hold floating point
167 numbers. The largest number we need to hold is 10^n where 2^-n is
168 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
169 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
170 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
171 BITS_PER_MP_LIMB) + 2)
172 /* Declare an mpn integer variable that big. */
173 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
174 /* Copy an mpn integer value. */
175 #define MPN_ASSIGN(dst, src) \
176 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
179 /* Set errno and return an overflowing value with sign specified by
182 overflow_value (int negative
)
184 __set_errno (ERANGE
);
185 FLOAT result
= math_narrow_eval ((negative
? -MAX_VALUE
: MAX_VALUE
)
191 /* Set errno and return an underflowing value with sign specified by
194 underflow_value (int negative
)
196 __set_errno (ERANGE
);
197 FLOAT result
= math_narrow_eval ((negative
? -MIN_VALUE
: MIN_VALUE
)
203 /* Return a floating point number of the needed type according to the given
204 multi-precision number after possible rounding. */
206 round_and_return (mp_limb_t
*retval
, intmax_t exponent
, int negative
,
207 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
209 int mode
= get_rounding_mode ();
211 if (exponent
< MIN_EXP
- 1)
213 if (exponent
< MIN_EXP
- 1 - MANT_DIG
)
214 return underflow_value (negative
);
216 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
219 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
220 if (shift
== MANT_DIG
)
221 /* This is a special case to handle the very seldom case where
222 the mantissa will be empty after the shift. */
226 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
227 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
228 for (i
= 0; i
< RETURN_LIMB_SIZE
- 1; ++i
)
229 more_bits
|= retval
[i
] != 0;
230 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
232 else if (shift
>= BITS_PER_MP_LIMB
)
236 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
237 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
238 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
239 more_bits
|= retval
[i
] != 0;
240 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
243 /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB. */
244 if ((shift
% BITS_PER_MP_LIMB
) != 0)
245 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
246 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
247 shift
% BITS_PER_MP_LIMB
);
249 for (i
= 0; i
< RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
); i
++)
250 retval
[i
] = retval
[i
+ (shift
/ BITS_PER_MP_LIMB
)];
251 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
252 shift
/ BITS_PER_MP_LIMB
);
256 if (TININESS_AFTER_ROUNDING
&& shift
== 1)
258 /* Whether the result counts as tiny depends on whether,
259 after rounding to the normal precision, it still has
260 a subnormal exponent. */
261 mp_limb_t retval_normal
[RETURN_LIMB_SIZE
];
262 if (round_away (negative
,
263 (retval
[0] & 1) != 0,
265 & (((mp_limb_t
) 1) << round_bit
)) != 0,
268 & ((((mp_limb_t
) 1) << round_bit
) - 1))
272 mp_limb_t cy
= __mpn_add_1 (retval_normal
, retval
,
273 RETURN_LIMB_SIZE
, 1);
275 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
276 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
277 ((retval_normal
[RETURN_LIMB_SIZE
- 1]
278 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
)))
283 round_limb
= retval
[0];
284 round_bit
= shift
- 1;
285 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
287 /* This is a hook for the m68k long double format, where the
288 exponent bias is the same for normalized and denormalized
291 # define DENORM_EXP (MIN_EXP - 2)
293 exponent
= DENORM_EXP
;
295 && ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
297 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
299 __set_errno (ERANGE
);
300 FLOAT force_underflow
= MIN_VALUE
* MIN_VALUE
;
301 math_force_eval (force_underflow
);
305 if (exponent
> MAX_EXP
)
308 if (round_away (negative
,
309 (retval
[0] & 1) != 0,
310 (round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0,
312 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0),
315 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
317 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
318 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
319 (retval
[RETURN_LIMB_SIZE
- 1]
320 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
323 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
324 retval
[RETURN_LIMB_SIZE
- 1]
325 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
327 else if (exponent
== DENORM_EXP
328 && (retval
[RETURN_LIMB_SIZE
- 1]
329 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
331 /* The number was denormalized but now normalized. */
332 exponent
= MIN_EXP
- 1;
335 if (exponent
> MAX_EXP
)
337 return overflow_value (negative
);
339 return MPN2FLOAT (retval
, exponent
, negative
);
343 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
344 into N. Return the size of the number limbs in NSIZE at the first
345 character od the string that is not part of the integer as the function
346 value. If the EXPONENT is small enough to be taken as an additional
347 factor for the resulting number (see code) multiply by it. */
348 static const STRING_TYPE
*
349 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
351 #ifndef USE_WIDE_CHAR
352 , const char *decimal
, size_t decimal_len
, const char *thousands
357 /* Number of digits for actual limb. */
366 if (cnt
== MAX_DIG_PER_LIMB
)
376 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
377 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
380 assert (*nsize
< MPNSIZE
);
389 /* There might be thousands separators or radix characters in
390 the string. But these all can be ignored because we know the
391 format of the number is correct and we have an exact number
392 of characters to read. */
394 if (*str
< L
'0' || *str
> L
'9')
397 if (*str
< '0' || *str
> '9')
400 if (thousands
!= NULL
&& *str
== *thousands
401 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
402 if (thousands
[inner
] != str
[inner
])
404 thousands
[inner
] == '\0'; }))
410 low
= low
* 10 + *str
++ - L_('0');
413 while (--digcnt
> 0);
415 if (*exponent
> 0 && *exponent
<= MAX_DIG_PER_LIMB
- cnt
)
417 low
*= _tens_in_limb
[*exponent
];
418 start
= _tens_in_limb
[cnt
+ *exponent
];
422 start
= _tens_in_limb
[cnt
];
432 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
433 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
436 assert (*nsize
< MPNSIZE
);
445 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
446 with the COUNT most significant bits of LIMB.
448 Implemented as a macro, so that __builtin_constant_p works even at -O0.
450 Tege doesn't like this macro so I have to write it here myself. :)
452 #define __mpn_lshift_1(ptr, size, count, limb) \
455 mp_limb_t *__ptr = (ptr); \
456 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
459 for (i = (size) - 1; i > 0; --i) \
460 __ptr[i] = __ptr[i - 1]; \
465 /* We assume count > 0 && count < BITS_PER_MP_LIMB here. */ \
466 unsigned int __count = (count); \
467 (void) __mpn_lshift (__ptr, __ptr, size, __count); \
468 __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count); \
474 #define INTERNAL(x) INTERNAL1(x)
475 #define INTERNAL1(x) __##x##_internal
476 #ifndef ____STRTOF_INTERNAL
477 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
480 /* This file defines a function to check for correct grouping. */
481 #include "grouping.h"
484 /* Return a floating point number with the value of the given string NPTR.
485 Set *ENDPTR to the character after the last used one. If the number is
486 smaller than the smallest representable number, set `errno' to ERANGE and
487 return 0.0. If the number is too big to be represented, set `errno' to
488 ERANGE and return HUGE_VAL with the appropriate sign. */
490 ____STRTOF_INTERNAL (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int group
,
493 int negative
; /* The sign of the number. */
494 MPN_VAR (num
); /* MP representation of the number. */
495 intmax_t exponent
; /* Exponent of the number. */
497 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
500 /* When we have to compute fractional digits we form a fraction with a
501 second multi-precision number (and we sometimes need a second for
502 temporary results). */
505 /* Representation for the return value. */
506 mp_limb_t retval
[RETURN_LIMB_SIZE
];
507 /* Number of bits currently in result value. */
510 /* Running pointer after the last character processed in the string. */
511 const STRING_TYPE
*cp
, *tp
;
512 /* Start of significant part of the number. */
513 const STRING_TYPE
*startp
, *start_of_digits
;
514 /* Points at the character following the integer and fractional digits. */
515 const STRING_TYPE
*expp
;
516 /* Total number of digit and number of digits in integer part. */
517 size_t dig_no
, int_no
, lead_zero
;
518 /* Contains the last character read. */
521 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
522 there. So define it ourselves if it remains undefined. */
524 typedef unsigned int wint_t;
526 /* The radix character of the current locale. */
533 /* The thousands character of the current locale. */
535 wchar_t thousands
= L
'\0';
537 const char *thousands
= NULL
;
539 /* The numeric grouping specification of the current locale,
540 in the format described in <locale.h>. */
541 const char *grouping
;
542 /* Used in several places. */
545 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
547 if (__glibc_unlikely (group
))
549 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
550 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
554 /* Figure out the thousands separator character. */
556 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
557 _NL_NUMERIC_THOUSANDS_SEP_WC
);
558 if (thousands
== L
'\0')
561 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
562 if (*thousands
== '\0')
573 /* Find the locale's decimal point character. */
575 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
576 assert (decimal
!= L
'\0');
577 # define decimal_len 1
579 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
580 decimal_len
= strlen (decimal
);
581 assert (decimal_len
> 0);
584 /* Prepare number representation. */
589 /* Parse string to get maximal legal prefix. We need the number of
590 characters of the integer part, the fractional part and the exponent. */
592 /* Ignore leading white space. */
597 /* Get sign of the result. */
603 else if (c
== L_('+'))
606 /* Return 0.0 if no legal string is found.
607 No character is used even if a sign was found. */
609 if (c
== (wint_t) decimal
610 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
612 /* We accept it. This funny construct is here only to indent
613 the code correctly. */
616 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
617 if (cp
[cnt
] != decimal
[cnt
])
619 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
621 /* We accept it. This funny construct is here only to indent
622 the code correctly. */
625 else if (c
< L_('0') || c
> L_('9'))
627 /* Check for `INF' or `INFINITY'. */
628 CHAR_TYPE lowc
= TOLOWER_C (c
);
630 if (lowc
== L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
632 /* Return +/- infinity. */
634 *endptr
= (STRING_TYPE
*)
635 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
638 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
641 if (lowc
== L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
648 /* Match `(n-char-sequence-digit)'. */
651 const STRING_TYPE
*startp
= cp
;
654 while ((*cp
>= L_('0') && *cp
<= L_('9'))
655 || (*cp
>= L_('A') && *cp
<= L_('Z'))
656 || (*cp
>= L_('a') && *cp
<= L_('z'))
660 /* The closing brace is missing. Only match the NAN
665 /* This is a system-dependent way to specify the
666 bitmask used for the NaN. We expect it to be
667 a number which is put in the mantissa of the
670 unsigned long long int mant
;
672 mant
= STRTOULL (startp
+ 1, &endp
, 0);
674 SET_MANTISSA (retval
, mant
);
676 /* Consume the closing brace. */
682 *endptr
= (STRING_TYPE
*) cp
;
687 /* It is really a text we do not recognize. */
691 /* First look whether we are faced with a hexadecimal number. */
692 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
694 /* Okay, it is a hexa-decimal number. Remember this and skip
695 the characters. BTW: hexadecimal numbers must not be
703 /* Record the start of the digits, in case we will check their grouping. */
704 start_of_digits
= startp
= cp
;
706 /* Ignore leading zeroes. This helps us to avoid useless computations. */
708 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
711 if (__glibc_likely (thousands
== NULL
))
716 /* We also have the multibyte thousands string. */
721 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
722 if (thousands
[cnt
] != cp
[cnt
])
724 if (thousands
[cnt
] != '\0')
733 /* If no other digit but a '0' is found the result is 0.0.
734 Return current read pointer. */
735 CHAR_TYPE lowc
= TOLOWER (c
);
736 if (!((c
>= L_('0') && c
<= L_('9'))
737 || (base
== 16 && lowc
>= L_('a') && lowc
<= L_('f'))
740 c
== (wint_t) decimal
742 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
743 if (decimal
[cnt
] != cp
[cnt
])
745 decimal
[cnt
] == '\0'; })
747 /* '0x.' alone is not a valid hexadecimal number.
748 '.' alone is not valid either, but that has been checked
751 || cp
!= start_of_digits
752 || (cp
[decimal_len
] >= L_('0') && cp
[decimal_len
] <= L_('9'))
753 || ({ CHAR_TYPE lo
= TOLOWER (cp
[decimal_len
]);
754 lo
>= L_('a') && lo
<= L_('f'); })))
755 || (base
== 16 && (cp
!= start_of_digits
757 || (base
!= 16 && lowc
== L_('e'))))
760 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
763 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
766 /* If TP is at the start of the digits, there was no correctly
767 grouped prefix of the string; so no number found. */
768 RETURN (negative
? -0.0 : 0.0,
769 tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
772 /* Remember first significant digit and read following characters until the
773 decimal point, exponent character or any non-FP number character. */
778 if ((c
>= L_('0') && c
<= L_('9'))
780 && ({ CHAR_TYPE lo
= TOLOWER (c
);
781 lo
>= L_('a') && lo
<= L_('f'); })))
786 if (__builtin_expect ((wint_t) thousands
== L
'\0', 1)
787 || c
!= (wint_t) thousands
)
788 /* Not a digit or separator: end of the integer part. */
791 if (__glibc_likely (thousands
== NULL
))
795 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
796 if (thousands
[cnt
] != cp
[cnt
])
798 if (thousands
[cnt
] != '\0')
807 if (__builtin_expect (grouping
!= NULL
, 0) && cp
> start_of_digits
)
809 /* Check the grouping of the digits. */
811 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
814 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
819 /* Less than the entire string was correctly grouped. */
821 if (tp
== start_of_digits
)
822 /* No valid group of numbers at all: no valid number. */
826 /* The number is validly grouped, but consists
827 only of zeroes. The whole value is zero. */
828 RETURN (negative
? -0.0 : 0.0, tp
);
830 /* Recompute DIG_NO so we won't read more digits than
831 are properly grouped. */
834 for (tp
= startp
; tp
< cp
; ++tp
)
835 if (*tp
>= L_('0') && *tp
<= L_('9'))
845 /* We have the number of digits in the integer part. Whether these
846 are all or any is really a fractional digit will be decided
849 lead_zero
= int_no
== 0 ? (size_t) -1 : 0;
851 /* Read the fractional digits. A special case are the 'american
852 style' numbers like `16.' i.e. with decimal point but without
856 c
== (wint_t) decimal
858 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
859 if (decimal
[cnt
] != cp
[cnt
])
861 decimal
[cnt
] == '\0'; })
867 while ((c
>= L_('0') && c
<= L_('9')) ||
868 (base
== 16 && ({ CHAR_TYPE lo
= TOLOWER (c
);
869 lo
>= L_('a') && lo
<= L_('f'); })))
871 if (c
!= L_('0') && lead_zero
== (size_t) -1)
872 lead_zero
= dig_no
- int_no
;
877 assert (dig_no
<= (uintmax_t) INTMAX_MAX
);
879 /* Remember start of exponent (if any). */
884 if ((base
== 16 && lowc
== L_('p'))
885 || (base
!= 16 && lowc
== L_('e')))
887 int exp_negative
= 0;
895 else if (c
== L_('+'))
898 if (c
>= L_('0') && c
<= L_('9'))
902 /* Get the exponent limit. */
907 assert (int_no
<= (uintmax_t) (INTMAX_MAX
908 + MIN_EXP
- MANT_DIG
) / 4);
909 exp_limit
= -MIN_EXP
+ MANT_DIG
+ 4 * (intmax_t) int_no
;
915 assert (lead_zero
== 0
916 && int_no
<= (uintmax_t) INTMAX_MAX
/ 4);
917 exp_limit
= MAX_EXP
- 4 * (intmax_t) int_no
+ 3;
919 else if (lead_zero
== (size_t) -1)
921 /* The number is zero and this limit is
923 exp_limit
= MAX_EXP
+ 3;
928 <= (uintmax_t) (INTMAX_MAX
- MAX_EXP
- 3) / 4);
930 + 4 * (intmax_t) lead_zero
940 <= (uintmax_t) (INTMAX_MAX
+ MIN_10_EXP
- MANT_DIG
));
941 exp_limit
= -MIN_10_EXP
+ MANT_DIG
+ (intmax_t) int_no
;
947 assert (lead_zero
== 0
948 && int_no
<= (uintmax_t) INTMAX_MAX
);
949 exp_limit
= MAX_10_EXP
- (intmax_t) int_no
+ 1;
951 else if (lead_zero
== (size_t) -1)
953 /* The number is zero and this limit is
955 exp_limit
= MAX_10_EXP
+ 1;
960 <= (uintmax_t) (INTMAX_MAX
- MAX_10_EXP
- 1));
961 exp_limit
= MAX_10_EXP
+ (intmax_t) lead_zero
+ 1;
971 if (__builtin_expect ((exponent
> exp_limit
/ 10
972 || (exponent
== exp_limit
/ 10
973 && c
- L_('0') > exp_limit
% 10)), 0))
974 /* The exponent is too large/small to represent a valid
979 /* We have to take care for special situation: a joker
980 might have written "0.0e100000" which is in fact
982 if (lead_zero
== (size_t) -1)
983 result
= negative
? -0.0 : 0.0;
986 /* Overflow or underflow. */
987 result
= (exp_negative
988 ? underflow_value (negative
)
989 : overflow_value (negative
));
992 /* Accept all following digits as part of the exponent. */
995 while (*cp
>= L_('0') && *cp
<= L_('9'));
1002 exponent
+= c
- L_('0');
1006 while (c
>= L_('0') && c
<= L_('9'));
1009 exponent
= -exponent
;
1015 /* We don't want to have to work with trailing zeroes after the radix. */
1016 if (dig_no
> int_no
)
1018 while (expp
[-1] == L_('0'))
1023 assert (dig_no
>= int_no
);
1026 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
1029 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
1032 if (expp
[-1] != L_('0'))
1038 exponent
+= base
== 16 ? 4 : 1;
1040 while (dig_no
> 0 && exponent
< 0);
1044 /* The whole string is parsed. Store the address of the next character. */
1046 *endptr
= (STRING_TYPE
*) cp
;
1049 return negative
? -0.0 : 0.0;
1053 /* Find the decimal point */
1054 #ifdef USE_WIDE_CHAR
1055 while (*startp
!= decimal
)
1060 if (*startp
== decimal
[0])
1062 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
1063 if (decimal
[cnt
] != startp
[cnt
])
1065 if (decimal
[cnt
] == '\0')
1071 startp
+= lead_zero
+ decimal_len
;
1072 assert (lead_zero
<= (base
== 16
1073 ? (uintmax_t) INTMAX_MAX
/ 4
1074 : (uintmax_t) INTMAX_MAX
));
1075 assert (lead_zero
<= (base
== 16
1076 ? ((uintmax_t) exponent
1077 - (uintmax_t) INTMAX_MIN
) / 4
1078 : ((uintmax_t) exponent
- (uintmax_t) INTMAX_MIN
)));
1079 exponent
-= base
== 16 ? 4 * (intmax_t) lead_zero
: (intmax_t) lead_zero
;
1080 dig_no
-= lead_zero
;
1083 /* If the BASE is 16 we can use a simpler algorithm. */
1086 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1087 4, 4, 4, 4, 4, 4, 4, 4 };
1088 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
1089 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1092 while (!ISXDIGIT (*startp
))
1094 while (*startp
== L_('0'))
1096 if (ISDIGIT (*startp
))
1097 val
= *startp
++ - L_('0');
1099 val
= 10 + TOLOWER (*startp
++) - L_('a');
1101 /* We cannot have a leading zero. */
1104 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
1106 /* We don't have to care for wrapping. This is the normal
1107 case so we add the first clause in the `if' expression as
1108 an optimization. It is a compile-time constant and so does
1109 not cost anything. */
1110 retval
[idx
] = val
<< (pos
- bits
+ 1);
1115 retval
[idx
--] = val
>> (bits
- pos
- 1);
1116 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
1117 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
1120 /* Adjust the exponent for the bits we are shifting in. */
1121 assert (int_no
<= (uintmax_t) (exponent
< 0
1122 ? (INTMAX_MAX
- bits
+ 1) / 4
1123 : (INTMAX_MAX
- exponent
- bits
+ 1) / 4));
1124 exponent
+= bits
- 1 + ((intmax_t) int_no
- 1) * 4;
1126 while (--dig_no
> 0 && idx
>= 0)
1128 if (!ISXDIGIT (*startp
))
1129 startp
+= decimal_len
;
1130 if (ISDIGIT (*startp
))
1131 val
= *startp
++ - L_('0');
1133 val
= 10 + TOLOWER (*startp
++) - L_('a');
1137 retval
[idx
] |= val
<< (pos
- 4 + 1);
1142 retval
[idx
--] |= val
>> (4 - pos
- 1);
1143 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
1146 int rest_nonzero
= 0;
1147 while (--dig_no
> 0)
1149 if (*startp
!= L_('0'))
1156 return round_and_return (retval
, exponent
, negative
, val
,
1157 BITS_PER_MP_LIMB
- 1, rest_nonzero
);
1161 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
1165 /* We ran out of digits. */
1166 MPN_ZERO (retval
, idx
);
1168 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1171 /* Now we have the number of digits in total and the integer digits as well
1172 as the exponent and its sign. We can decide whether the read digits are
1173 really integer digits or belong to the fractional part; i.e. we normalize
1176 intmax_t incr
= (exponent
< 0
1177 ? MAX (-(intmax_t) int_no
, exponent
)
1178 : MIN ((intmax_t) dig_no
- (intmax_t) int_no
, exponent
));
1183 if (__glibc_unlikely (exponent
> MAX_10_EXP
+ 1 - (intmax_t) int_no
))
1184 return overflow_value (negative
);
1186 /* 10^(MIN_10_EXP-1) is not normal. Thus, 10^(MIN_10_EXP-1) /
1187 2^MANT_DIG is below half the least subnormal, so anything with a
1188 base-10 exponent less than the base-10 exponent (which is
1189 MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
1190 underflows. DIG is floor((MANT_DIG-1)log10(2)), so an exponent
1191 below MIN_10_EXP - (DIG + 3) underflows. But EXPONENT is
1192 actually an exponent multiplied only by a fractional part, not an
1193 integer part, so an exponent below MIN_10_EXP - (DIG + 2)
1195 if (__glibc_unlikely (exponent
< MIN_10_EXP
- (DIG
+ 2)))
1196 return underflow_value (negative
);
1200 /* Read the integer part as a multi-precision number to NUM. */
1201 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1202 #ifndef USE_WIDE_CHAR
1203 , decimal
, decimal_len
, thousands
1209 /* We now multiply the gained number by the given power of ten. */
1210 mp_limb_t
*psrc
= num
;
1211 mp_limb_t
*pdest
= den
;
1213 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1217 if ((exponent
& expbit
) != 0)
1219 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1223 /* FIXME: not the whole multiplication has to be
1224 done. If we have the needed number of bits we
1225 only need the information whether more non-zero
1227 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1228 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1229 &__tens
[ttab
->arrayoff
1230 + _FPIO_CONST_OFFSET
],
1233 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1234 + _FPIO_CONST_OFFSET
],
1235 size
, psrc
, numsize
);
1239 (void) SWAP (psrc
, pdest
);
1244 while (exponent
!= 0);
1247 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1250 /* Determine how many bits of the result we already have. */
1251 count_leading_zeros (bits
, num
[numsize
- 1]);
1252 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1254 /* Now we know the exponent of the number in base two.
1255 Check it against the maximum possible exponent. */
1256 if (__glibc_unlikely (bits
> MAX_EXP
))
1257 return overflow_value (negative
);
1259 /* We have already the first BITS bits of the result. Together with
1260 the information whether more non-zero bits follow this is enough
1261 to determine the result. */
1262 if (bits
> MANT_DIG
)
1265 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1266 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1267 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1269 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1273 memcpy (retval
, &num
[least_idx
],
1274 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1277 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1278 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1280 << (BITS_PER_MP_LIMB
- least_bit
));
1281 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1282 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1285 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1286 for (i
= 0; num
[i
] == 0; ++i
)
1289 return round_and_return (retval
, bits
- 1, negative
,
1290 num
[round_idx
], round_bit
,
1291 int_no
< dig_no
|| i
< round_idx
);
1294 else if (dig_no
== int_no
)
1296 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1297 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1299 if (target_bit
== is_bit
)
1301 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1302 numsize
* sizeof (mp_limb_t
));
1303 /* FIXME: the following loop can be avoided if we assume a
1304 maximal MANT_DIG value. */
1305 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1307 else if (target_bit
> is_bit
)
1309 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1310 num
, numsize
, target_bit
- is_bit
);
1311 /* FIXME: the following loop can be avoided if we assume a
1312 maximal MANT_DIG value. */
1313 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1318 assert (numsize
< RETURN_LIMB_SIZE
);
1320 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1321 num
, numsize
, is_bit
- target_bit
);
1322 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1323 /* FIXME: the following loop can be avoided if we assume a
1324 maximal MANT_DIG value. */
1325 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1328 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1332 /* Store the bits we already have. */
1333 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1334 #if RETURN_LIMB_SIZE > 1
1335 if (numsize
< RETURN_LIMB_SIZE
)
1336 # if RETURN_LIMB_SIZE == 2
1337 retval
[numsize
] = 0;
1339 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1344 /* We have to compute at least some of the fractional digits. */
1346 /* We construct a fraction and the result of the division gives us
1347 the needed digits. The denominator is 1.0 multiplied by the
1348 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1349 123e-6 gives 123 / 1000000. */
1354 int need_frac_digits
;
1356 mp_limb_t
*psrc
= den
;
1357 mp_limb_t
*pdest
= num
;
1358 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1360 assert (dig_no
> int_no
1362 && exponent
>= MIN_10_EXP
- (DIG
+ 2));
1364 /* We need to compute MANT_DIG - BITS fractional bits that lie
1365 within the mantissa of the result, the following bit for
1366 rounding, and to know whether any subsequent bit is 0.
1367 Computing a bit with value 2^-n means looking at n digits after
1368 the decimal point. */
1371 /* The bits required are those immediately after the point. */
1372 assert (int_no
> 0 && exponent
== 0);
1373 need_frac_digits
= 1 + MANT_DIG
- bits
;
1377 /* The number is in the form .123eEXPONENT. */
1378 assert (int_no
== 0 && *startp
!= L_('0'));
1379 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1381 int neg_exp_2
= ((1 - exponent
) * 10) / 3 + 1;
1382 /* The number is at least 2^-NEG_EXP_2. We need up to
1383 MANT_DIG bits following that bit. */
1384 need_frac_digits
= neg_exp_2
+ MANT_DIG
;
1385 /* However, we never need bits beyond 1/4 ulp of the smallest
1386 representable value. (That 1/4 ulp bit is only needed to
1387 determine tinyness on machines where tinyness is determined
1389 if (need_frac_digits
> MANT_DIG
- MIN_EXP
+ 2)
1390 need_frac_digits
= MANT_DIG
- MIN_EXP
+ 2;
1391 /* At this point, NEED_FRAC_DIGITS is the total number of
1392 digits needed after the point, but some of those may be
1394 need_frac_digits
+= exponent
;
1395 /* Any cases underflowing enough that none of the fractional
1396 digits are needed should have been caught earlier (such
1397 cases are on the order of 10^-n or smaller where 2^-n is
1398 the least subnormal). */
1399 assert (need_frac_digits
> 0);
1402 if (need_frac_digits
> (intmax_t) dig_no
- (intmax_t) int_no
)
1403 need_frac_digits
= (intmax_t) dig_no
- (intmax_t) int_no
;
1405 if ((intmax_t) dig_no
> (intmax_t) int_no
+ need_frac_digits
)
1407 dig_no
= int_no
+ need_frac_digits
;
1413 neg_exp
= (intmax_t) dig_no
- (intmax_t) int_no
- exponent
;
1415 /* Construct the denominator. */
1420 if ((neg_exp
& expbit
) != 0)
1427 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1428 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1429 densize
* sizeof (mp_limb_t
));
1433 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1434 + _FPIO_CONST_OFFSET
],
1435 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1437 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1440 (void) SWAP (psrc
, pdest
);
1446 while (neg_exp
!= 0);
1449 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1451 /* Read the fractional digits from the string. */
1452 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1453 #ifndef USE_WIDE_CHAR
1454 , decimal
, decimal_len
, thousands
1458 /* We now have to shift both numbers so that the highest bit in the
1459 denominator is set. In the same process we copy the numerator to
1460 a high place in the array so that the division constructs the wanted
1461 digits. This is done by a "quasi fix point" number representation.
1463 num: ddddddddddd . 0000000000000000000000
1465 den: ddddddddddd n >= m
1469 count_leading_zeros (cnt
, den
[densize
- 1]);
1473 /* Don't call `mpn_shift' with a count of zero since the specification
1474 does not allow this. */
1475 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1476 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1478 num
[numsize
++] = cy
;
1481 /* Now we are ready for the division. But it is not necessary to
1482 do a full multi-precision division because we only need a small
1483 number of bits for the result. So we do not use __mpn_divmod
1484 here but instead do the division here by hand and stop whenever
1485 the needed number of bits is reached. The code itself comes
1486 from the GNU MP Library by Torbj\"orn Granlund. */
1494 mp_limb_t d
, n
, quot
;
1499 assert (numsize
== 1 && n
< d
);
1503 udiv_qrnnd (quot
, n
, n
, 0, d
);
1510 cnt = BITS_PER_MP_LIMB; \
1512 count_leading_zeros (cnt, quot); \
1514 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1516 used = MANT_DIG + cnt; \
1517 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1518 bits = MANT_DIG + 1; \
1522 /* Note that we only clear the second element. */ \
1523 /* The conditional is determined at compile time. */ \
1524 if (RETURN_LIMB_SIZE > 1) \
1530 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1531 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1535 used = MANT_DIG - bits; \
1537 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1539 bits += BITS_PER_MP_LIMB
1543 while (bits
<= MANT_DIG
);
1545 return round_and_return (retval
, exponent
- 1, negative
,
1546 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1547 more_bits
|| n
!= 0);
1551 mp_limb_t d0
, d1
, n0
, n1
;
1558 if (numsize
< densize
)
1562 /* The numerator of the number occupies fewer bits than
1563 the denominator but the one limb is bigger than the
1564 high limb of the numerator. */
1571 exponent
-= BITS_PER_MP_LIMB
;
1574 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1575 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1576 BITS_PER_MP_LIMB
, 0);
1579 used
= MANT_DIG
- bits
;
1581 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1583 bits
+= BITS_PER_MP_LIMB
;
1595 while (bits
<= MANT_DIG
)
1601 /* QUOT should be either 111..111 or 111..110. We need
1602 special treatment of this rare case as normal division
1603 would give overflow. */
1604 quot
= ~(mp_limb_t
) 0;
1607 if (r
< d1
) /* Carry in the addition? */
1609 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1612 n1
= d0
- (d0
!= 0);
1617 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1618 umul_ppmm (n1
, n0
, d0
, quot
);
1622 if (n1
> r
|| (n1
== r
&& n0
> 0))
1624 /* The estimated QUOT was too large. */
1627 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1629 if (r
>= d1
) /* If not carry, test QUOT again. */
1632 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1638 return round_and_return (retval
, exponent
- 1, negative
,
1639 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1640 more_bits
|| n1
!= 0 || n0
!= 0);
1645 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1649 dX
= den
[densize
- 1];
1650 d1
= den
[densize
- 2];
1652 /* The division does not work if the upper limb of the two-limb
1653 numerator is greater than the denominator. */
1654 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1657 if (numsize
< densize
)
1659 mp_size_t empty
= densize
- numsize
;
1663 exponent
-= empty
* BITS_PER_MP_LIMB
;
1666 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1668 /* We make a difference here because the compiler
1669 cannot optimize the `else' case that good and
1670 this reflects all currently used FLOAT types
1671 and GMP implementations. */
1672 #if RETURN_LIMB_SIZE <= 2
1673 assert (empty
== 1);
1674 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1675 BITS_PER_MP_LIMB
, 0);
1677 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1678 retval
[i
] = retval
[i
- empty
];
1685 used
= MANT_DIG
- bits
;
1686 if (used
>= BITS_PER_MP_LIMB
)
1689 (void) __mpn_lshift (&retval
[used
1690 / BITS_PER_MP_LIMB
],
1693 - used
/ BITS_PER_MP_LIMB
),
1694 used
% BITS_PER_MP_LIMB
);
1695 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1699 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1701 bits
+= empty
* BITS_PER_MP_LIMB
;
1703 for (i
= numsize
; i
> 0; --i
)
1704 num
[i
+ empty
] = num
[i
- 1];
1705 MPN_ZERO (num
, empty
+ 1);
1710 assert (numsize
== densize
);
1711 for (i
= numsize
; i
> 0; --i
)
1712 num
[i
] = num
[i
- 1];
1719 while (bits
<= MANT_DIG
)
1722 /* This might over-estimate QUOT, but it's probably not
1723 worth the extra code here to find out. */
1724 quot
= ~(mp_limb_t
) 0;
1729 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1730 umul_ppmm (n1
, n0
, d1
, quot
);
1732 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1736 if (r
< dX
) /* I.e. "carry in previous addition?" */
1743 /* Possible optimization: We already have (q * n0) and (1 * n1)
1744 after the calculation of QUOT. Taking advantage of this, we
1745 could make this loop make two iterations less. */
1747 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1749 if (num
[densize
] != cy
)
1751 cy
= __mpn_add_n (num
, num
, den
, densize
);
1755 n0
= num
[densize
] = num
[densize
- 1];
1756 for (i
= densize
- 1; i
> 0; --i
)
1757 num
[i
] = num
[i
- 1];
1763 for (i
= densize
; i
>= 0 && num
[i
] == 0; --i
)
1765 return round_and_return (retval
, exponent
- 1, negative
,
1766 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1767 more_bits
|| i
>= 0);
1774 #if defined _LIBC && !defined USE_WIDE_CHAR
1775 libc_hidden_def (____STRTOF_INTERNAL
)
1778 /* External user entry point. */
1781 #ifdef weak_function
1784 __STRTOF (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, __locale_t loc
)
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
);