1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2021 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 <https://www.gnu.org/licenses/>. */
20 #include <bits/floatn.h>
23 # define BUILD_DOUBLE 0
25 # define BUILD_DOUBLE 1
29 # if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
30 # define strtof64_l __hide_strtof64_l
31 # define wcstof64_l __hide_wcstof64_l
33 # if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
34 # define strtof32x_l __hide_strtof32x_l
35 # define wcstof32x_l __hide_wcstof32x_l
41 extern double ____strtod_l_internal (const char *, char **, int, locale_t
);
43 /* Configuration part. These macros are defined by `strtold.c',
44 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
45 `long double' and `float' versions of the reader. */
47 # include <math_ldbl_opt.h>
51 # define STRTOF wcstod_l
52 # define __STRTOF __wcstod_l
53 # define STRTOF_NAN __wcstod_nan
55 # define STRTOF strtod_l
56 # define __STRTOF __strtod_l
57 # define STRTOF_NAN __strtod_nan
59 # define MPN2FLOAT __mpn_construct_double
60 # define FLOAT_HUGE_VAL HUGE_VAL
62 /* End of configuration part. */
67 #include "../locale/localeinfo.h"
69 #include <math-barriers.h>
70 #include <math-narrow-eval.h>
74 #include <rounding-mode.h>
77 /* The gmp headers need some configuration frobs. */
80 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
81 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
82 #include <gmp-mparam.h>
86 #include "fpioconst.h"
91 /* We use this code for the extended locale handling where the
92 function gets as an additional argument the locale which has to be
93 used. To access the values we have to redefine the _NL_CURRENT and
94 _NL_CURRENT_WORD macros. */
96 #define _NL_CURRENT(category, item) \
97 (current->values[_NL_ITEM_INDEX (item)].string)
98 #undef _NL_CURRENT_WORD
99 #define _NL_CURRENT_WORD(category, item) \
100 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
102 #if defined _LIBC || defined HAVE_WCHAR_H
108 # define STRING_TYPE wchar_t
109 # define CHAR_TYPE wint_t
110 # define L_(Ch) L##Ch
111 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
112 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
113 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
114 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
115 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
116 # define STRNCASECMP(S1, S2, N) \
117 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
119 # define STRING_TYPE char
120 # define CHAR_TYPE char
122 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
123 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
124 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
125 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
126 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
127 # define STRNCASECMP(S1, S2, N) \
128 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
132 /* Constants we need from float.h; select the set for the FLOAT precision. */
133 #define MANT_DIG PASTE(FLT,_MANT_DIG)
134 #define DIG PASTE(FLT,_DIG)
135 #define MAX_EXP PASTE(FLT,_MAX_EXP)
136 #define MIN_EXP PASTE(FLT,_MIN_EXP)
137 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
138 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
139 #define MAX_VALUE PASTE(FLT,_MAX)
140 #define MIN_VALUE PASTE(FLT,_MIN)
142 /* Extra macros required to get FLT expanded before the pasting. */
143 #define PASTE(a,b) PASTE1(a,b)
144 #define PASTE1(a,b) a##b
146 /* Function to construct a floating point number from an MP integer
147 containing the fraction bits, a base 2 exponent, and a sign flag. */
148 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
150 /* Definitions according to limb size used. */
151 #if BITS_PER_MP_LIMB == 32
152 # define MAX_DIG_PER_LIMB 9
153 # define MAX_FAC_PER_LIMB 1000000000UL
154 #elif BITS_PER_MP_LIMB == 64
155 # define MAX_DIG_PER_LIMB 19
156 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
158 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
161 extern const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1];
164 #define howmany(x,y) (((x)+((y)-1))/(y))
166 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
168 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
170 #define RETURN(val,end) \
171 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
172 return val; } while (0)
174 /* Maximum size necessary for mpn integers to hold floating point
175 numbers. The largest number we need to hold is 10^n where 2^-n is
176 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
177 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
178 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
179 BITS_PER_MP_LIMB) + 2)
180 /* Declare an mpn integer variable that big. */
181 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
182 /* Copy an mpn integer value. */
183 #define MPN_ASSIGN(dst, src) \
184 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
187 /* Set errno and return an overflowing value with sign specified by
190 overflow_value (int negative
)
192 __set_errno (ERANGE
);
193 FLOAT result
= math_narrow_eval ((negative
? -MAX_VALUE
: MAX_VALUE
)
199 /* Set errno and return an underflowing value with sign specified by
202 underflow_value (int negative
)
204 __set_errno (ERANGE
);
205 FLOAT result
= math_narrow_eval ((negative
? -MIN_VALUE
: MIN_VALUE
)
211 /* Return a floating point number of the needed type according to the given
212 multi-precision number after possible rounding. */
214 round_and_return (mp_limb_t
*retval
, intmax_t exponent
, int negative
,
215 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
217 int mode
= get_rounding_mode ();
219 if (exponent
< MIN_EXP
- 1)
221 if (exponent
< MIN_EXP
- 1 - MANT_DIG
)
222 return underflow_value (negative
);
224 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
227 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
228 if (shift
== MANT_DIG
)
229 /* This is a special case to handle the very seldom case where
230 the mantissa will be empty after the shift. */
234 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
235 round_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
236 for (i
= 0; i
< RETURN_LIMB_SIZE
- 1; ++i
)
237 more_bits
|= retval
[i
] != 0;
238 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
240 else if (shift
>= BITS_PER_MP_LIMB
)
244 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
245 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
246 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
247 more_bits
|= retval
[i
] != 0;
248 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
251 /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB. */
252 if ((shift
% BITS_PER_MP_LIMB
) != 0)
253 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
254 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
255 shift
% BITS_PER_MP_LIMB
);
257 for (i
= 0; i
< RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
); i
++)
258 retval
[i
] = retval
[i
+ (shift
/ BITS_PER_MP_LIMB
)];
259 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
260 shift
/ BITS_PER_MP_LIMB
);
264 if (TININESS_AFTER_ROUNDING
&& shift
== 1)
266 /* Whether the result counts as tiny depends on whether,
267 after rounding to the normal precision, it still has
268 a subnormal exponent. */
269 mp_limb_t retval_normal
[RETURN_LIMB_SIZE
];
270 if (round_away (negative
,
271 (retval
[0] & 1) != 0,
273 & (((mp_limb_t
) 1) << round_bit
)) != 0,
276 & ((((mp_limb_t
) 1) << round_bit
) - 1))
280 mp_limb_t cy
= __mpn_add_1 (retval_normal
, retval
,
281 RETURN_LIMB_SIZE
, 1);
283 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
)
284 || ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0
285 && ((retval_normal
[RETURN_LIMB_SIZE
- 1]
287 << (MANT_DIG
% BITS_PER_MP_LIMB
)))
292 round_limb
= retval
[0];
293 round_bit
= shift
- 1;
294 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
296 /* This is a hook for the m68k long double format, where the
297 exponent bias is the same for normalized and denormalized
300 # define DENORM_EXP (MIN_EXP - 2)
302 exponent
= DENORM_EXP
;
304 && ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
306 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
308 __set_errno (ERANGE
);
309 FLOAT force_underflow
= MIN_VALUE
* MIN_VALUE
;
310 math_force_eval (force_underflow
);
314 if (exponent
>= MAX_EXP
)
317 bool half_bit
= (round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0;
318 bool more_bits_nonzero
320 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0);
321 if (round_away (negative
,
322 (retval
[0] & 1) != 0,
327 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
329 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
)
330 || ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0
331 && (retval
[RETURN_LIMB_SIZE
- 1]
332 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
335 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
336 retval
[RETURN_LIMB_SIZE
- 1]
337 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
339 else if (exponent
== DENORM_EXP
340 && (retval
[RETURN_LIMB_SIZE
- 1]
341 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
343 /* The number was denormalized but now normalized. */
344 exponent
= MIN_EXP
- 1;
347 if (exponent
>= MAX_EXP
)
349 return overflow_value (negative
);
351 if (half_bit
|| more_bits_nonzero
)
353 FLOAT force_inexact
= (FLOAT
) 1 + MIN_VALUE
;
354 math_force_eval (force_inexact
);
356 return MPN2FLOAT (retval
, exponent
, negative
);
360 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
361 into N. Return the size of the number limbs in NSIZE at the first
362 character od the string that is not part of the integer as the function
363 value. If the EXPONENT is small enough to be taken as an additional
364 factor for the resulting number (see code) multiply by it. */
365 static const STRING_TYPE
*
366 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
368 #ifndef USE_WIDE_CHAR
369 , const char *decimal
, size_t decimal_len
, const char *thousands
374 /* Number of digits for actual limb. */
383 if (cnt
== MAX_DIG_PER_LIMB
)
393 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
394 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
397 assert (*nsize
< MPNSIZE
);
406 /* There might be thousands separators or radix characters in
407 the string. But these all can be ignored because we know the
408 format of the number is correct and we have an exact number
409 of characters to read. */
411 if (*str
< L
'0' || *str
> L
'9')
414 if (*str
< '0' || *str
> '9')
417 if (thousands
!= NULL
&& *str
== *thousands
418 && ({ for (inner
= 1; thousands
[inner
] != '\0'; ++inner
)
419 if (thousands
[inner
] != str
[inner
])
421 thousands
[inner
] == '\0'; }))
427 low
= low
* 10 + *str
++ - L_('0');
430 while (--digcnt
> 0);
432 if (*exponent
> 0 && *exponent
<= MAX_DIG_PER_LIMB
- cnt
)
434 low
*= _tens_in_limb
[*exponent
];
435 start
= _tens_in_limb
[cnt
+ *exponent
];
439 start
= _tens_in_limb
[cnt
];
449 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
450 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
453 assert (*nsize
< MPNSIZE
);
462 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
463 with the COUNT most significant bits of LIMB.
465 Implemented as a macro, so that __builtin_constant_p works even at -O0.
467 Tege doesn't like this macro so I have to write it here myself. :)
469 #define __mpn_lshift_1(ptr, size, count, limb) \
472 mp_limb_t *__ptr = (ptr); \
473 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
476 for (i = (size) - 1; i > 0; --i) \
477 __ptr[i] = __ptr[i - 1]; \
482 /* We assume count > 0 && count < BITS_PER_MP_LIMB here. */ \
483 unsigned int __count = (count); \
484 (void) __mpn_lshift (__ptr, __ptr, size, __count); \
485 __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count); \
491 #define INTERNAL(x) INTERNAL1(x)
492 #define INTERNAL1(x) __##x##_internal
493 #ifndef ____STRTOF_INTERNAL
494 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
497 /* This file defines a function to check for correct grouping. */
498 #include "grouping.h"
501 /* Return a floating point number with the value of the given string NPTR.
502 Set *ENDPTR to the character after the last used one. If the number is
503 smaller than the smallest representable number, set `errno' to ERANGE and
504 return 0.0. If the number is too big to be represented, set `errno' to
505 ERANGE and return HUGE_VAL with the appropriate sign. */
507 ____STRTOF_INTERNAL (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int group
,
510 int negative
; /* The sign of the number. */
511 MPN_VAR (num
); /* MP representation of the number. */
512 intmax_t exponent
; /* Exponent of the number. */
514 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
517 /* When we have to compute fractional digits we form a fraction with a
518 second multi-precision number (and we sometimes need a second for
519 temporary results). */
522 /* Representation for the return value. */
523 mp_limb_t retval
[RETURN_LIMB_SIZE
];
524 /* Number of bits currently in result value. */
527 /* Running pointer after the last character processed in the string. */
528 const STRING_TYPE
*cp
, *tp
;
529 /* Start of significant part of the number. */
530 const STRING_TYPE
*startp
, *start_of_digits
;
531 /* Points at the character following the integer and fractional digits. */
532 const STRING_TYPE
*expp
;
533 /* Total number of digit and number of digits in integer part. */
534 size_t dig_no
, int_no
, lead_zero
;
535 /* Contains the last character read. */
538 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
539 there. So define it ourselves if it remains undefined. */
541 typedef unsigned int wint_t;
543 /* The radix character of the current locale. */
550 /* The thousands character of the current locale. */
552 wchar_t thousands
= L
'\0';
554 const char *thousands
= NULL
;
556 /* The numeric grouping specification of the current locale,
557 in the format described in <locale.h>. */
558 const char *grouping
;
559 /* Used in several places. */
562 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
564 if (__glibc_unlikely (group
))
566 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
567 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
571 /* Figure out the thousands separator character. */
573 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
574 _NL_NUMERIC_THOUSANDS_SEP_WC
);
575 if (thousands
== L
'\0')
578 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
579 if (*thousands
== '\0')
590 /* Find the locale's decimal point character. */
592 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
593 assert (decimal
!= L
'\0');
594 # define decimal_len 1
596 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
597 decimal_len
= strlen (decimal
);
598 assert (decimal_len
> 0);
601 /* Prepare number representation. */
606 /* Parse string to get maximal legal prefix. We need the number of
607 characters of the integer part, the fractional part and the exponent. */
609 /* Ignore leading white space. */
614 /* Get sign of the result. */
620 else if (c
== L_('+'))
623 /* Return 0.0 if no legal string is found.
624 No character is used even if a sign was found. */
626 if (c
== (wint_t) decimal
627 && (wint_t) cp
[1] >= L
'0' && (wint_t) cp
[1] <= L
'9')
629 /* We accept it. This funny construct is here only to indent
630 the code correctly. */
633 for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
634 if (cp
[cnt
] != decimal
[cnt
])
636 if (decimal
[cnt
] == '\0' && cp
[cnt
] >= '0' && cp
[cnt
] <= '9')
638 /* We accept it. This funny construct is here only to indent
639 the code correctly. */
642 else if (c
< L_('0') || c
> L_('9'))
644 /* Check for `INF' or `INFINITY'. */
645 CHAR_TYPE lowc
= TOLOWER_C (c
);
647 if (lowc
== L_('i') && STRNCASECMP (cp
, L_("inf"), 3) == 0)
649 /* Return +/- infinity. */
651 *endptr
= (STRING_TYPE
*)
652 (cp
+ (STRNCASECMP (cp
+ 3, L_("inity"), 5) == 0
655 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
658 if (lowc
== L_('n') && STRNCASECMP (cp
, L_("nan"), 3) == 0)
665 /* Match `(n-char-sequence-digit)'. */
668 const STRING_TYPE
*startp
= cp
;
670 retval
= STRTOF_NAN (cp
+ 1, &endp
, L_(')'));
671 if (*endp
== L_(')'))
672 /* Consume the closing parenthesis. */
675 /* Only match the NAN part. */
680 *endptr
= (STRING_TYPE
*) cp
;
682 return negative
? -retval
: retval
;
685 /* It is really a text we do not recognize. */
689 /* First look whether we are faced with a hexadecimal number. */
690 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
692 /* Okay, it is a hexa-decimal number. Remember this and skip
693 the characters. BTW: hexadecimal numbers must not be
701 /* Record the start of the digits, in case we will check their grouping. */
702 start_of_digits
= startp
= cp
;
704 /* Ignore leading zeroes. This helps us to avoid useless computations. */
706 while (c
== L
'0' || ((wint_t) thousands
!= L
'\0' && c
== (wint_t) thousands
))
709 if (__glibc_likely (thousands
== NULL
))
714 /* We also have the multibyte thousands string. */
719 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
720 if (thousands
[cnt
] != cp
[cnt
])
722 if (thousands
[cnt
] != '\0')
731 /* If no other digit but a '0' is found the result is 0.0.
732 Return current read pointer. */
733 CHAR_TYPE lowc
= TOLOWER (c
);
734 if (!((c
>= L_('0') && c
<= L_('9'))
735 || (base
== 16 && lowc
>= L_('a') && lowc
<= L_('f'))
738 c
== (wint_t) decimal
740 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
741 if (decimal
[cnt
] != cp
[cnt
])
743 decimal
[cnt
] == '\0'; })
745 /* '0x.' alone is not a valid hexadecimal number.
746 '.' alone is not valid either, but that has been checked
749 || cp
!= start_of_digits
750 || (cp
[decimal_len
] >= L_('0') && cp
[decimal_len
] <= L_('9'))
751 || ({ CHAR_TYPE lo
= TOLOWER (cp
[decimal_len
]);
752 lo
>= L_('a') && lo
<= L_('f'); })))
753 || (base
== 16 && (cp
!= start_of_digits
755 || (base
!= 16 && lowc
== L_('e'))))
758 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
761 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
764 /* If TP is at the start of the digits, there was no correctly
765 grouped prefix of the string; so no number found. */
766 RETURN (negative
? -0.0 : 0.0,
767 tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
770 /* Remember first significant digit and read following characters until the
771 decimal point, exponent character or any non-FP number character. */
776 if ((c
>= L_('0') && c
<= L_('9'))
778 && ({ CHAR_TYPE lo
= TOLOWER (c
);
779 lo
>= L_('a') && lo
<= L_('f'); })))
784 if (__builtin_expect ((wint_t) thousands
== L
'\0', 1)
785 || c
!= (wint_t) thousands
)
786 /* Not a digit or separator: end of the integer part. */
789 if (__glibc_likely (thousands
== NULL
))
793 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
794 if (thousands
[cnt
] != cp
[cnt
])
796 if (thousands
[cnt
] != '\0')
805 if (__builtin_expect (grouping
!= NULL
, 0) && cp
> start_of_digits
)
807 /* Check the grouping of the digits. */
809 tp
= __correctly_grouped_prefixwc (start_of_digits
, cp
, thousands
,
812 tp
= __correctly_grouped_prefixmb (start_of_digits
, cp
, thousands
,
817 /* Less than the entire string was correctly grouped. */
819 if (tp
== start_of_digits
)
820 /* No valid group of numbers at all: no valid number. */
824 /* The number is validly grouped, but consists
825 only of zeroes. The whole value is zero. */
826 RETURN (negative
? -0.0 : 0.0, tp
);
828 /* Recompute DIG_NO so we won't read more digits than
829 are properly grouped. */
832 for (tp
= startp
; tp
< cp
; ++tp
)
833 if (*tp
>= L_('0') && *tp
<= L_('9'))
843 /* We have the number of digits in the integer part. Whether these
844 are all or any is really a fractional digit will be decided
847 lead_zero
= int_no
== 0 ? (size_t) -1 : 0;
849 /* Read the fractional digits. A special case are the 'american
850 style' numbers like `16.' i.e. with decimal point but without
854 c
== (wint_t) decimal
856 ({ for (cnt
= 0; decimal
[cnt
] != '\0'; ++cnt
)
857 if (decimal
[cnt
] != cp
[cnt
])
859 decimal
[cnt
] == '\0'; })
865 while ((c
>= L_('0') && c
<= L_('9'))
866 || (base
== 16 && ({ CHAR_TYPE lo
= TOLOWER (c
);
867 lo
>= L_('a') && lo
<= L_('f'); })))
869 if (c
!= L_('0') && lead_zero
== (size_t) -1)
870 lead_zero
= dig_no
- int_no
;
875 assert (dig_no
<= (uintmax_t) INTMAX_MAX
);
877 /* Remember start of exponent (if any). */
882 if ((base
== 16 && lowc
== L_('p'))
883 || (base
!= 16 && lowc
== L_('e')))
885 int exp_negative
= 0;
893 else if (c
== L_('+'))
896 if (c
>= L_('0') && c
<= L_('9'))
900 /* Get the exponent limit. */
905 assert (int_no
<= (uintmax_t) (INTMAX_MAX
906 + MIN_EXP
- MANT_DIG
) / 4);
907 exp_limit
= -MIN_EXP
+ MANT_DIG
+ 4 * (intmax_t) int_no
;
913 assert (lead_zero
== 0
914 && int_no
<= (uintmax_t) INTMAX_MAX
/ 4);
915 exp_limit
= MAX_EXP
- 4 * (intmax_t) int_no
+ 3;
917 else if (lead_zero
== (size_t) -1)
919 /* The number is zero and this limit is
921 exp_limit
= MAX_EXP
+ 3;
926 <= (uintmax_t) (INTMAX_MAX
- MAX_EXP
- 3) / 4);
928 + 4 * (intmax_t) lead_zero
938 <= (uintmax_t) (INTMAX_MAX
+ MIN_10_EXP
- MANT_DIG
));
939 exp_limit
= -MIN_10_EXP
+ MANT_DIG
+ (intmax_t) int_no
;
945 assert (lead_zero
== 0
946 && int_no
<= (uintmax_t) INTMAX_MAX
);
947 exp_limit
= MAX_10_EXP
- (intmax_t) int_no
+ 1;
949 else if (lead_zero
== (size_t) -1)
951 /* The number is zero and this limit is
953 exp_limit
= MAX_10_EXP
+ 1;
958 <= (uintmax_t) (INTMAX_MAX
- MAX_10_EXP
- 1));
959 exp_limit
= MAX_10_EXP
+ (intmax_t) lead_zero
+ 1;
969 if (__builtin_expect ((exponent
> exp_limit
/ 10
970 || (exponent
== exp_limit
/ 10
971 && c
- L_('0') > exp_limit
% 10)), 0))
972 /* The exponent is too large/small to represent a valid
977 /* We have to take care for special situation: a joker
978 might have written "0.0e100000" which is in fact
980 if (lead_zero
== (size_t) -1)
981 result
= negative
? -0.0 : 0.0;
984 /* Overflow or underflow. */
985 result
= (exp_negative
986 ? underflow_value (negative
)
987 : overflow_value (negative
));
990 /* Accept all following digits as part of the exponent. */
993 while (*cp
>= L_('0') && *cp
<= L_('9'));
1000 exponent
+= c
- L_('0');
1004 while (c
>= L_('0') && c
<= L_('9'));
1007 exponent
= -exponent
;
1013 /* We don't want to have to work with trailing zeroes after the radix. */
1014 if (dig_no
> int_no
)
1016 while (expp
[-1] == L_('0'))
1021 assert (dig_no
>= int_no
);
1024 if (dig_no
== int_no
&& dig_no
> 0 && exponent
< 0)
1027 while (! (base
== 16 ? ISXDIGIT (expp
[-1]) : ISDIGIT (expp
[-1])))
1030 if (expp
[-1] != L_('0'))
1036 exponent
+= base
== 16 ? 4 : 1;
1038 while (dig_no
> 0 && exponent
< 0);
1042 /* The whole string is parsed. Store the address of the next character. */
1044 *endptr
= (STRING_TYPE
*) cp
;
1047 return negative
? -0.0 : 0.0;
1051 /* Find the decimal point */
1052 #ifdef USE_WIDE_CHAR
1053 while (*startp
!= decimal
)
1058 if (*startp
== decimal
[0])
1060 for (cnt
= 1; decimal
[cnt
] != '\0'; ++cnt
)
1061 if (decimal
[cnt
] != startp
[cnt
])
1063 if (decimal
[cnt
] == '\0')
1069 startp
+= lead_zero
+ decimal_len
;
1070 assert (lead_zero
<= (base
== 16
1071 ? (uintmax_t) INTMAX_MAX
/ 4
1072 : (uintmax_t) INTMAX_MAX
));
1073 assert (lead_zero
<= (base
== 16
1074 ? ((uintmax_t) exponent
1075 - (uintmax_t) INTMAX_MIN
) / 4
1076 : ((uintmax_t) exponent
- (uintmax_t) INTMAX_MIN
)));
1077 exponent
-= base
== 16 ? 4 * (intmax_t) lead_zero
: (intmax_t) lead_zero
;
1078 dig_no
-= lead_zero
;
1081 /* If the BASE is 16 we can use a simpler algorithm. */
1084 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1085 4, 4, 4, 4, 4, 4, 4, 4 };
1086 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
1087 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1090 while (!ISXDIGIT (*startp
))
1092 while (*startp
== L_('0'))
1094 if (ISDIGIT (*startp
))
1095 val
= *startp
++ - L_('0');
1097 val
= 10 + TOLOWER (*startp
++) - L_('a');
1099 /* We cannot have a leading zero. */
1102 if (pos
+ 1 >= 4 || pos
+ 1 >= bits
)
1104 /* We don't have to care for wrapping. This is the normal
1105 case so we add the first clause in the `if' expression as
1106 an optimization. It is a compile-time constant and so does
1107 not cost anything. */
1108 retval
[idx
] = val
<< (pos
- bits
+ 1);
1113 retval
[idx
--] = val
>> (bits
- pos
- 1);
1114 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
1115 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
1118 /* Adjust the exponent for the bits we are shifting in. */
1119 assert (int_no
<= (uintmax_t) (exponent
< 0
1120 ? (INTMAX_MAX
- bits
+ 1) / 4
1121 : (INTMAX_MAX
- exponent
- bits
+ 1) / 4));
1122 exponent
+= bits
- 1 + ((intmax_t) int_no
- 1) * 4;
1124 while (--dig_no
> 0 && idx
>= 0)
1126 if (!ISXDIGIT (*startp
))
1127 startp
+= decimal_len
;
1128 if (ISDIGIT (*startp
))
1129 val
= *startp
++ - L_('0');
1131 val
= 10 + TOLOWER (*startp
++) - L_('a');
1135 retval
[idx
] |= val
<< (pos
- 4 + 1);
1140 retval
[idx
--] |= val
>> (4 - pos
- 1);
1141 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
1144 int rest_nonzero
= 0;
1145 while (--dig_no
> 0)
1147 if (*startp
!= L_('0'))
1154 return round_and_return (retval
, exponent
, negative
, val
,
1155 BITS_PER_MP_LIMB
- 1, rest_nonzero
);
1159 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
1163 /* We ran out of digits. */
1164 MPN_ZERO (retval
, idx
);
1166 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
1169 /* Now we have the number of digits in total and the integer digits as well
1170 as the exponent and its sign. We can decide whether the read digits are
1171 really integer digits or belong to the fractional part; i.e. we normalize
1174 intmax_t incr
= (exponent
< 0
1175 ? MAX (-(intmax_t) int_no
, exponent
)
1176 : MIN ((intmax_t) dig_no
- (intmax_t) int_no
, exponent
));
1181 if (__glibc_unlikely (exponent
> MAX_10_EXP
+ 1 - (intmax_t) int_no
))
1182 return overflow_value (negative
);
1184 /* 10^(MIN_10_EXP-1) is not normal. Thus, 10^(MIN_10_EXP-1) /
1185 2^MANT_DIG is below half the least subnormal, so anything with a
1186 base-10 exponent less than the base-10 exponent (which is
1187 MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
1188 underflows. DIG is floor((MANT_DIG-1)log10(2)), so an exponent
1189 below MIN_10_EXP - (DIG + 3) underflows. But EXPONENT is
1190 actually an exponent multiplied only by a fractional part, not an
1191 integer part, so an exponent below MIN_10_EXP - (DIG + 2)
1193 if (__glibc_unlikely (exponent
< MIN_10_EXP
- (DIG
+ 2)))
1194 return underflow_value (negative
);
1198 /* Read the integer part as a multi-precision number to NUM. */
1199 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
1200 #ifndef USE_WIDE_CHAR
1201 , decimal
, decimal_len
, thousands
1207 /* We now multiply the gained number by the given power of ten. */
1208 mp_limb_t
*psrc
= num
;
1209 mp_limb_t
*pdest
= den
;
1211 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1215 if ((exponent
& expbit
) != 0)
1217 size_t size
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1221 /* FIXME: not the whole multiplication has to be
1222 done. If we have the needed number of bits we
1223 only need the information whether more non-zero
1225 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
1226 cy
= __mpn_mul (pdest
, psrc
, numsize
,
1227 &__tens
[ttab
->arrayoff
1228 + _FPIO_CONST_OFFSET
],
1231 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1232 + _FPIO_CONST_OFFSET
],
1233 size
, psrc
, numsize
);
1237 (void) SWAP (psrc
, pdest
);
1242 while (exponent
!= 0);
1245 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
1248 /* Determine how many bits of the result we already have. */
1249 count_leading_zeros (bits
, num
[numsize
- 1]);
1250 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
1252 /* Now we know the exponent of the number in base two.
1253 Check it against the maximum possible exponent. */
1254 if (__glibc_unlikely (bits
> MAX_EXP
))
1255 return overflow_value (negative
);
1257 /* We have already the first BITS bits of the result. Together with
1258 the information whether more non-zero bits follow this is enough
1259 to determine the result. */
1260 if (bits
> MANT_DIG
)
1263 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
1264 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
1265 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
1267 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
1271 memcpy (retval
, &num
[least_idx
],
1272 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
1275 for (i
= least_idx
; i
< numsize
- 1; ++i
)
1276 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
1278 << (BITS_PER_MP_LIMB
- least_bit
));
1279 if (i
- least_idx
< RETURN_LIMB_SIZE
)
1280 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
1283 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1284 for (i
= 0; num
[i
] == 0; ++i
)
1287 return round_and_return (retval
, bits
- 1, negative
,
1288 num
[round_idx
], round_bit
,
1289 int_no
< dig_no
|| i
< round_idx
);
1292 else if (dig_no
== int_no
)
1294 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
1295 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
1297 if (target_bit
== is_bit
)
1299 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
1300 numsize
* sizeof (mp_limb_t
));
1301 /* FIXME: the following loop can be avoided if we assume a
1302 maximal MANT_DIG value. */
1303 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1305 else if (target_bit
> is_bit
)
1307 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1308 num
, numsize
, target_bit
- is_bit
);
1309 /* FIXME: the following loop can be avoided if we assume a
1310 maximal MANT_DIG value. */
1311 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
1316 assert (numsize
< RETURN_LIMB_SIZE
);
1318 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
1319 num
, numsize
, is_bit
- target_bit
);
1320 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
1321 /* FIXME: the following loop can be avoided if we assume a
1322 maximal MANT_DIG value. */
1323 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
1326 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1330 /* Store the bits we already have. */
1331 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1332 #if RETURN_LIMB_SIZE > 1
1333 if (numsize
< RETURN_LIMB_SIZE
)
1334 # if RETURN_LIMB_SIZE == 2
1335 retval
[numsize
] = 0;
1337 MPN_ZERO (retval
+ numsize
, RETURN_LIMB_SIZE
- numsize
);
1342 /* We have to compute at least some of the fractional digits. */
1344 /* We construct a fraction and the result of the division gives us
1345 the needed digits. The denominator is 1.0 multiplied by the
1346 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1347 123e-6 gives 123 / 1000000. */
1352 int need_frac_digits
;
1354 mp_limb_t
*psrc
= den
;
1355 mp_limb_t
*pdest
= num
;
1356 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1358 assert (dig_no
> int_no
1360 && exponent
>= MIN_10_EXP
- (DIG
+ 2));
1362 /* We need to compute MANT_DIG - BITS fractional bits that lie
1363 within the mantissa of the result, the following bit for
1364 rounding, and to know whether any subsequent bit is 0.
1365 Computing a bit with value 2^-n means looking at n digits after
1366 the decimal point. */
1369 /* The bits required are those immediately after the point. */
1370 assert (int_no
> 0 && exponent
== 0);
1371 need_frac_digits
= 1 + MANT_DIG
- bits
;
1375 /* The number is in the form .123eEXPONENT. */
1376 assert (int_no
== 0 && *startp
!= L_('0'));
1377 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1379 int neg_exp_2
= ((1 - exponent
) * 10) / 3 + 1;
1380 /* The number is at least 2^-NEG_EXP_2. We need up to
1381 MANT_DIG bits following that bit. */
1382 need_frac_digits
= neg_exp_2
+ MANT_DIG
;
1383 /* However, we never need bits beyond 1/4 ulp of the smallest
1384 representable value. (That 1/4 ulp bit is only needed to
1385 determine tinyness on machines where tinyness is determined
1387 if (need_frac_digits
> MANT_DIG
- MIN_EXP
+ 2)
1388 need_frac_digits
= MANT_DIG
- MIN_EXP
+ 2;
1389 /* At this point, NEED_FRAC_DIGITS is the total number of
1390 digits needed after the point, but some of those may be
1392 need_frac_digits
+= exponent
;
1393 /* Any cases underflowing enough that none of the fractional
1394 digits are needed should have been caught earlier (such
1395 cases are on the order of 10^-n or smaller where 2^-n is
1396 the least subnormal). */
1397 assert (need_frac_digits
> 0);
1400 if (need_frac_digits
> (intmax_t) dig_no
- (intmax_t) int_no
)
1401 need_frac_digits
= (intmax_t) dig_no
- (intmax_t) int_no
;
1403 if ((intmax_t) dig_no
> (intmax_t) int_no
+ need_frac_digits
)
1405 dig_no
= int_no
+ need_frac_digits
;
1411 neg_exp
= (intmax_t) dig_no
- (intmax_t) int_no
- exponent
;
1413 /* Construct the denominator. */
1418 if ((neg_exp
& expbit
) != 0)
1425 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1426 memcpy (psrc
, &__tens
[ttab
->arrayoff
+ _FPIO_CONST_OFFSET
],
1427 densize
* sizeof (mp_limb_t
));
1431 cy
= __mpn_mul (pdest
, &__tens
[ttab
->arrayoff
1432 + _FPIO_CONST_OFFSET
],
1433 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1435 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1438 (void) SWAP (psrc
, pdest
);
1444 while (neg_exp
!= 0);
1447 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1449 /* Read the fractional digits from the string. */
1450 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
1451 #ifndef USE_WIDE_CHAR
1452 , decimal
, decimal_len
, thousands
1456 /* We now have to shift both numbers so that the highest bit in the
1457 denominator is set. In the same process we copy the numerator to
1458 a high place in the array so that the division constructs the wanted
1459 digits. This is done by a "quasi fix point" number representation.
1461 num: ddddddddddd . 0000000000000000000000
1463 den: ddddddddddd n >= m
1467 count_leading_zeros (cnt
, den
[densize
- 1]);
1471 /* Don't call `mpn_shift' with a count of zero since the specification
1472 does not allow this. */
1473 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1474 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1476 num
[numsize
++] = cy
;
1479 /* Now we are ready for the division. But it is not necessary to
1480 do a full multi-precision division because we only need a small
1481 number of bits for the result. So we do not use __mpn_divmod
1482 here but instead do the division here by hand and stop whenever
1483 the needed number of bits is reached. The code itself comes
1484 from the GNU MP Library by Torbj\"orn Granlund. */
1492 mp_limb_t d
, n
, quot
;
1497 assert (numsize
== 1 && n
< d
);
1501 udiv_qrnnd (quot
, n
, n
, 0, d
);
1508 cnt = BITS_PER_MP_LIMB; \
1510 count_leading_zeros (cnt, quot); \
1512 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1514 used = MANT_DIG + cnt; \
1515 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1516 bits = MANT_DIG + 1; \
1520 /* Note that we only clear the second element. */ \
1521 /* The conditional is determined at compile time. */ \
1522 if (RETURN_LIMB_SIZE > 1) \
1528 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1529 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1533 used = MANT_DIG - bits; \
1535 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1537 bits += BITS_PER_MP_LIMB
1541 while (bits
<= MANT_DIG
);
1543 return round_and_return (retval
, exponent
- 1, negative
,
1544 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1545 more_bits
|| n
!= 0);
1549 mp_limb_t d0
, d1
, n0
, n1
;
1556 if (numsize
< densize
)
1560 /* The numerator of the number occupies fewer bits than
1561 the denominator but the one limb is bigger than the
1562 high limb of the numerator. */
1569 exponent
-= BITS_PER_MP_LIMB
;
1572 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1573 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1574 BITS_PER_MP_LIMB
, 0);
1577 used
= MANT_DIG
- bits
;
1579 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1581 bits
+= BITS_PER_MP_LIMB
;
1593 while (bits
<= MANT_DIG
)
1599 /* QUOT should be either 111..111 or 111..110. We need
1600 special treatment of this rare case as normal division
1601 would give overflow. */
1602 quot
= ~(mp_limb_t
) 0;
1605 if (r
< d1
) /* Carry in the addition? */
1607 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1610 n1
= d0
- (d0
!= 0);
1615 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1616 umul_ppmm (n1
, n0
, d0
, quot
);
1620 if (n1
> r
|| (n1
== r
&& n0
> 0))
1622 /* The estimated QUOT was too large. */
1625 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1627 if (r
>= d1
) /* If not carry, test QUOT again. */
1630 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1636 return round_and_return (retval
, exponent
- 1, negative
,
1637 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1638 more_bits
|| n1
!= 0 || n0
!= 0);
1643 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1647 dX
= den
[densize
- 1];
1648 d1
= den
[densize
- 2];
1650 /* The division does not work if the upper limb of the two-limb
1651 numerator is greater than or equal to the denominator. */
1652 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) >= 0)
1655 if (numsize
< densize
)
1657 mp_size_t empty
= densize
- numsize
;
1661 exponent
-= empty
* BITS_PER_MP_LIMB
;
1664 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1666 /* We make a difference here because the compiler
1667 cannot optimize the `else' case that good and
1668 this reflects all currently used FLOAT types
1669 and GMP implementations. */
1670 #if RETURN_LIMB_SIZE <= 2
1671 assert (empty
== 1);
1672 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1673 BITS_PER_MP_LIMB
, 0);
1675 for (i
= RETURN_LIMB_SIZE
- 1; i
>= empty
; --i
)
1676 retval
[i
] = retval
[i
- empty
];
1683 used
= MANT_DIG
- bits
;
1684 if (used
>= BITS_PER_MP_LIMB
)
1687 (void) __mpn_lshift (&retval
[used
1688 / BITS_PER_MP_LIMB
],
1691 - used
/ BITS_PER_MP_LIMB
),
1692 used
% BITS_PER_MP_LIMB
);
1693 for (i
= used
/ BITS_PER_MP_LIMB
- 1; i
>= 0; --i
)
1697 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1699 bits
+= empty
* BITS_PER_MP_LIMB
;
1701 for (i
= numsize
; i
> 0; --i
)
1702 num
[i
+ empty
] = num
[i
- 1];
1703 MPN_ZERO (num
, empty
+ 1);
1708 assert (numsize
== densize
);
1709 for (i
= numsize
; i
> 0; --i
)
1710 num
[i
] = num
[i
- 1];
1717 while (bits
<= MANT_DIG
)
1720 /* This might over-estimate QUOT, but it's probably not
1721 worth the extra code here to find out. */
1722 quot
= ~(mp_limb_t
) 0;
1727 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1728 umul_ppmm (n1
, n0
, d1
, quot
);
1730 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1734 if (r
< dX
) /* I.e. "carry in previous addition?" */
1741 /* Possible optimization: We already have (q * n0) and (1 * n1)
1742 after the calculation of QUOT. Taking advantage of this, we
1743 could make this loop make two iterations less. */
1745 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1747 if (num
[densize
] != cy
)
1749 cy
= __mpn_add_n (num
, num
, den
, densize
);
1753 n0
= num
[densize
] = num
[densize
- 1];
1754 for (i
= densize
- 1; i
> 0; --i
)
1755 num
[i
] = num
[i
- 1];
1761 for (i
= densize
; i
>= 0 && num
[i
] == 0; --i
)
1763 return round_and_return (retval
, exponent
- 1, negative
,
1764 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1765 more_bits
|| i
>= 0);
1772 #if defined _LIBC && !defined USE_WIDE_CHAR
1773 libc_hidden_def (____STRTOF_INTERNAL
)
1776 /* External user entry point. */
1779 #ifdef weak_function
1782 __STRTOF (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, locale_t loc
)
1784 return ____STRTOF_INTERNAL (nptr
, endptr
, 0, loc
);
1787 libc_hidden_def (__STRTOF
)
1788 libc_hidden_ver (__STRTOF
, STRTOF
)
1790 weak_alias (__STRTOF
, STRTOF
)
1792 #ifdef LONG_DOUBLE_COMPAT
1793 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1794 # ifdef USE_WIDE_CHAR
1795 compat_symbol (libc
, __wcstod_l
, __wcstold_l
, GLIBC_2_1
);
1797 compat_symbol (libc
, __strtod_l
, __strtold_l
, GLIBC_2_1
);
1800 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1801 # ifdef USE_WIDE_CHAR
1802 compat_symbol (libc
, wcstod_l
, wcstold_l
, GLIBC_2_3
);
1804 compat_symbol (libc
, strtod_l
, strtold_l
, GLIBC_2_3
);
1810 # if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
1813 # ifdef USE_WIDE_CHAR
1814 weak_alias (wcstod_l
, wcstof64_l
)
1816 weak_alias (strtod_l
, strtof64_l
)
1819 # if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
1822 # ifdef USE_WIDE_CHAR
1823 weak_alias (wcstod_l
, wcstof32x_l
)
1825 weak_alias (strtod_l
, strtof32x_l
)