Fix build of nptl/tst-thread_local1.cc with GCC 12
[glibc.git] / stdlib / strtod_l.c
blob5a54d99ae866364118f8dfe0492c79a555464684
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>
22 #ifdef FLOAT
23 # define BUILD_DOUBLE 0
24 #else
25 # define BUILD_DOUBLE 1
26 #endif
28 #if BUILD_DOUBLE
29 # if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
30 # define strtof64_l __hide_strtof64_l
31 # define wcstof64_l __hide_wcstof64_l
32 # endif
33 # if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
34 # define strtof32x_l __hide_strtof32x_l
35 # define wcstof32x_l __hide_wcstof32x_l
36 # endif
37 #endif
39 #include <locale.h>
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. */
46 #ifndef FLOAT
47 # include <math_ldbl_opt.h>
48 # define FLOAT double
49 # define FLT DBL
50 # ifdef USE_WIDE_CHAR
51 # define STRTOF wcstod_l
52 # define __STRTOF __wcstod_l
53 # define STRTOF_NAN __wcstod_nan
54 # else
55 # define STRTOF strtod_l
56 # define __STRTOF __strtod_l
57 # define STRTOF_NAN __strtod_nan
58 # endif
59 # define MPN2FLOAT __mpn_construct_double
60 # define FLOAT_HUGE_VAL HUGE_VAL
61 #endif
62 /* End of configuration part. */
64 #include <ctype.h>
65 #include <errno.h>
66 #include <float.h>
67 #include "../locale/localeinfo.h"
68 #include <math.h>
69 #include <math-barriers.h>
70 #include <math-narrow-eval.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <stdint.h>
74 #include <rounding-mode.h>
75 #include <tininess.h>
77 /* The gmp headers need some configuration frobs. */
78 #define HAVE_ALLOCA 1
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>
83 #include <gmp.h>
84 #include "gmp-impl.h"
85 #include "longlong.h"
86 #include "fpioconst.h"
88 #include <assert.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. */
95 #undef _NL_CURRENT
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
103 # include <wchar.h>
104 #endif
106 #ifdef USE_WIDE_CHAR
107 # include <wctype.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)
118 #else
119 # define STRING_TYPE char
120 # define CHAR_TYPE char
121 # define L_(Ch) Ch
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)
129 #endif
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
157 #else
158 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
159 #endif
161 extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];
163 #ifndef howmany
164 #define howmany(x,y) (((x)+((y)-1))/(y))
165 #endif
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
188 NEGATIVE. */
189 static FLOAT
190 overflow_value (int negative)
192 __set_errno (ERANGE);
193 FLOAT result = math_narrow_eval ((negative ? -MAX_VALUE : MAX_VALUE)
194 * MAX_VALUE);
195 return result;
199 /* Set errno and return an underflowing value with sign specified by
200 NEGATIVE. */
201 static FLOAT
202 underflow_value (int negative)
204 __set_errno (ERANGE);
205 FLOAT result = math_narrow_eval ((negative ? -MIN_VALUE : MIN_VALUE)
206 * MIN_VALUE);
207 return result;
211 /* Return a floating point number of the needed type according to the given
212 multi-precision number after possible rounding. */
213 static FLOAT
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;
225 bool is_tiny = true;
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. */
232 int i;
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)
242 int i;
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))
249 != 0);
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);
256 else
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);
262 else if (shift > 0)
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,
272 (round_limb
273 & (((mp_limb_t) 1) << round_bit)) != 0,
274 (more_bits
275 || ((round_limb
276 & ((((mp_limb_t) 1) << round_bit) - 1))
277 != 0)),
278 mode))
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]
286 & (((mp_limb_t) 1)
287 << (MANT_DIG % BITS_PER_MP_LIMB)))
288 != 0)))
289 is_tiny = false;
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
298 numbers. */
299 #ifndef DENORM_EXP
300 # define DENORM_EXP (MIN_EXP - 2)
301 #endif
302 exponent = DENORM_EXP;
303 if (is_tiny
304 && ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
305 || more_bits
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)
315 goto overflow;
317 bool half_bit = (round_limb & (((mp_limb_t) 1) << round_bit)) != 0;
318 bool more_bits_nonzero
319 = (more_bits
320 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0);
321 if (round_away (negative,
322 (retval[0] & 1) != 0,
323 half_bit,
324 more_bits_nonzero,
325 mode))
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))
334 ++exponent;
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)))
342 != 0)
343 /* The number was denormalized but now normalized. */
344 exponent = MIN_EXP - 1;
347 if (exponent >= MAX_EXP)
348 overflow:
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,
367 intmax_t *exponent
368 #ifndef USE_WIDE_CHAR
369 , const char *decimal, size_t decimal_len, const char *thousands
370 #endif
374 /* Number of digits for actual limb. */
375 int cnt = 0;
376 mp_limb_t low = 0;
377 mp_limb_t start;
379 *nsize = 0;
380 assert (digcnt > 0);
383 if (cnt == MAX_DIG_PER_LIMB)
385 if (*nsize == 0)
387 n[0] = low;
388 *nsize = 1;
390 else
392 mp_limb_t cy;
393 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
394 cy += __mpn_add_1 (n, n, *nsize, low);
395 if (cy != 0)
397 assert (*nsize < MPNSIZE);
398 n[*nsize] = cy;
399 ++(*nsize);
402 cnt = 0;
403 low = 0;
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. */
410 #ifdef USE_WIDE_CHAR
411 if (*str < L'0' || *str > L'9')
412 ++str;
413 #else
414 if (*str < '0' || *str > '9')
416 int inner = 0;
417 if (thousands != NULL && *str == *thousands
418 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
419 if (thousands[inner] != str[inner])
420 break;
421 thousands[inner] == '\0'; }))
422 str += inner;
423 else
424 str += decimal_len;
426 #endif
427 low = low * 10 + *str++ - L_('0');
428 ++cnt;
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];
436 *exponent = 0;
438 else
439 start = _tens_in_limb[cnt];
441 if (*nsize == 0)
443 n[0] = low;
444 *nsize = 1;
446 else
448 mp_limb_t cy;
449 cy = __mpn_mul_1 (n, n, *nsize, start);
450 cy += __mpn_add_1 (n, n, *nsize, low);
451 if (cy != 0)
453 assert (*nsize < MPNSIZE);
454 n[(*nsize)++] = cy;
458 return str;
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. :)
468 --drepper */
469 #define __mpn_lshift_1(ptr, size, count, limb) \
470 do \
472 mp_limb_t *__ptr = (ptr); \
473 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
475 mp_size_t i; \
476 for (i = (size) - 1; i > 0; --i) \
477 __ptr[i] = __ptr[i - 1]; \
478 __ptr[0] = (limb); \
480 else \
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); \
488 while (0)
491 #define INTERNAL(x) INTERNAL1(x)
492 #define INTERNAL1(x) __##x##_internal
493 #ifndef ____STRTOF_INTERNAL
494 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
495 #endif
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. */
506 FLOAT
507 ____STRTOF_INTERNAL (const STRING_TYPE *nptr, STRING_TYPE **endptr, int group,
508 locale_t loc)
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. */
515 int base = 10;
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). */
520 MPN_VAR (den);
522 /* Representation for the return value. */
523 mp_limb_t retval[RETURN_LIMB_SIZE];
524 /* Number of bits currently in result value. */
525 int bits;
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. */
536 CHAR_TYPE c;
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. */
540 #ifndef _WINT_T
541 typedef unsigned int wint_t;
542 #endif
543 /* The radix character of the current locale. */
544 #ifdef USE_WIDE_CHAR
545 wchar_t decimal;
546 #else
547 const char *decimal;
548 size_t decimal_len;
549 #endif
550 /* The thousands character of the current locale. */
551 #ifdef USE_WIDE_CHAR
552 wchar_t thousands = L'\0';
553 #else
554 const char *thousands = NULL;
555 #endif
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. */
560 int cnt;
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)
568 grouping = NULL;
569 else
571 /* Figure out the thousands separator character. */
572 #ifdef USE_WIDE_CHAR
573 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
574 _NL_NUMERIC_THOUSANDS_SEP_WC);
575 if (thousands == L'\0')
576 grouping = NULL;
577 #else
578 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
579 if (*thousands == '\0')
581 thousands = NULL;
582 grouping = NULL;
584 #endif
587 else
588 grouping = NULL;
590 /* Find the locale's decimal point character. */
591 #ifdef USE_WIDE_CHAR
592 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
593 assert (decimal != L'\0');
594 # define decimal_len 1
595 #else
596 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
597 decimal_len = strlen (decimal);
598 assert (decimal_len > 0);
599 #endif
601 /* Prepare number representation. */
602 exponent = 0;
603 negative = 0;
604 bits = 0;
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. */
608 cp = nptr - 1;
609 /* Ignore leading white space. */
611 c = *++cp;
612 while (ISSPACE (c));
614 /* Get sign of the result. */
615 if (c == L_('-'))
617 negative = 1;
618 c = *++cp;
620 else if (c == L_('+'))
621 c = *++cp;
623 /* Return 0.0 if no legal string is found.
624 No character is used even if a sign was found. */
625 #ifdef USE_WIDE_CHAR
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. */
632 #else
633 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
634 if (cp[cnt] != decimal[cnt])
635 break;
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. */
641 #endif
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. */
650 if (endptr != NULL)
651 *endptr = (STRING_TYPE *)
652 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
653 ? 8 : 3));
655 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
658 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
660 /* Return NaN. */
661 FLOAT retval = NAN;
663 cp += 3;
665 /* Match `(n-char-sequence-digit)'. */
666 if (*cp == L_('('))
668 const STRING_TYPE *startp = cp;
669 STRING_TYPE *endp;
670 retval = STRTOF_NAN (cp + 1, &endp, L_(')'));
671 if (*endp == L_(')'))
672 /* Consume the closing parenthesis. */
673 cp = endp + 1;
674 else
675 /* Only match the NAN part. */
676 cp = startp;
679 if (endptr != NULL)
680 *endptr = (STRING_TYPE *) cp;
682 return negative ? -retval : retval;
685 /* It is really a text we do not recognize. */
686 RETURN (0.0, nptr);
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
694 grouped. */
695 base = 16;
696 cp += 2;
697 c = *cp;
698 grouping = NULL;
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. */
705 #ifdef USE_WIDE_CHAR
706 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
707 c = *++cp;
708 #else
709 if (__glibc_likely (thousands == NULL))
710 while (c == '0')
711 c = *++cp;
712 else
714 /* We also have the multibyte thousands string. */
715 while (1)
717 if (c != '0')
719 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
720 if (thousands[cnt] != cp[cnt])
721 break;
722 if (thousands[cnt] != '\0')
723 break;
724 cp += cnt - 1;
726 c = *++cp;
729 #endif
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'))
736 || (
737 #ifdef USE_WIDE_CHAR
738 c == (wint_t) decimal
739 #else
740 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
741 if (decimal[cnt] != cp[cnt])
742 break;
743 decimal[cnt] == '\0'; })
744 #endif
745 /* '0x.' alone is not a valid hexadecimal number.
746 '.' alone is not valid either, but that has been checked
747 already earlier. */
748 && (base != 16
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
754 && lowc == L_('p')))
755 || (base != 16 && lowc == L_('e'))))
757 #ifdef USE_WIDE_CHAR
758 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
759 grouping);
760 #else
761 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
762 grouping);
763 #endif
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. */
772 startp = cp;
773 dig_no = 0;
774 while (1)
776 if ((c >= L_('0') && c <= L_('9'))
777 || (base == 16
778 && ({ CHAR_TYPE lo = TOLOWER (c);
779 lo >= L_('a') && lo <= L_('f'); })))
780 ++dig_no;
781 else
783 #ifdef USE_WIDE_CHAR
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. */
787 break;
788 #else
789 if (__glibc_likely (thousands == NULL))
790 break;
791 else
793 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
794 if (thousands[cnt] != cp[cnt])
795 break;
796 if (thousands[cnt] != '\0')
797 break;
798 cp += cnt - 1;
800 #endif
802 c = *++cp;
805 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
807 /* Check the grouping of the digits. */
808 #ifdef USE_WIDE_CHAR
809 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
810 grouping);
811 #else
812 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
813 grouping);
814 #endif
815 if (cp != tp)
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. */
821 RETURN (0.0, nptr);
823 if (tp < startp)
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. */
830 cp = tp;
831 dig_no = 0;
832 for (tp = startp; tp < cp; ++tp)
833 if (*tp >= L_('0') && *tp <= L_('9'))
834 ++dig_no;
836 int_no = dig_no;
837 lead_zero = 0;
839 goto number_parsed;
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
845 later. */
846 int_no = dig_no;
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
851 trailing digits. */
852 if (
853 #ifdef USE_WIDE_CHAR
854 c == (wint_t) decimal
855 #else
856 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
857 if (decimal[cnt] != cp[cnt])
858 break;
859 decimal[cnt] == '\0'; })
860 #endif
863 cp += decimal_len;
864 c = *cp;
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;
871 ++dig_no;
872 c = *++cp;
875 assert (dig_no <= (uintmax_t) INTMAX_MAX);
877 /* Remember start of exponent (if any). */
878 expp = cp;
880 /* Read exponent. */
881 lowc = TOLOWER (c);
882 if ((base == 16 && lowc == L_('p'))
883 || (base != 16 && lowc == L_('e')))
885 int exp_negative = 0;
887 c = *++cp;
888 if (c == L_('-'))
890 exp_negative = 1;
891 c = *++cp;
893 else if (c == L_('+'))
894 c = *++cp;
896 if (c >= L_('0') && c <= L_('9'))
898 intmax_t exp_limit;
900 /* Get the exponent limit. */
901 if (base == 16)
903 if (exp_negative)
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;
909 else
911 if (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
920 arbitrary. */
921 exp_limit = MAX_EXP + 3;
923 else
925 assert (lead_zero
926 <= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
927 exp_limit = (MAX_EXP
928 + 4 * (intmax_t) lead_zero
929 + 3);
933 else
935 if (exp_negative)
937 assert (int_no
938 <= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
939 exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
941 else
943 if (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
952 arbitrary. */
953 exp_limit = MAX_10_EXP + 1;
955 else
957 assert (lead_zero
958 <= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
959 exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
964 if (exp_limit < 0)
965 exp_limit = 0;
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
973 number. */
975 FLOAT result;
977 /* We have to take care for special situation: a joker
978 might have written "0.0e100000" which is in fact
979 zero. */
980 if (lead_zero == (size_t) -1)
981 result = negative ? -0.0 : 0.0;
982 else
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. */
992 ++cp;
993 while (*cp >= L_('0') && *cp <= L_('9'));
995 RETURN (result, cp);
996 /* NOTREACHED */
999 exponent *= 10;
1000 exponent += c - L_('0');
1002 c = *++cp;
1004 while (c >= L_('0') && c <= L_('9'));
1006 if (exp_negative)
1007 exponent = -exponent;
1009 else
1010 cp = expp;
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'))
1018 --expp;
1019 --dig_no;
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])))
1028 --expp;
1030 if (expp[-1] != L_('0'))
1031 break;
1033 --expp;
1034 --dig_no;
1035 --int_no;
1036 exponent += base == 16 ? 4 : 1;
1038 while (dig_no > 0 && exponent < 0);
1040 number_parsed:
1042 /* The whole string is parsed. Store the address of the next character. */
1043 if (endptr)
1044 *endptr = (STRING_TYPE *) cp;
1046 if (dig_no == 0)
1047 return negative ? -0.0 : 0.0;
1049 if (lead_zero)
1051 /* Find the decimal point */
1052 #ifdef USE_WIDE_CHAR
1053 while (*startp != decimal)
1054 ++startp;
1055 #else
1056 while (1)
1058 if (*startp == decimal[0])
1060 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
1061 if (decimal[cnt] != startp[cnt])
1062 break;
1063 if (decimal[cnt] == '\0')
1064 break;
1066 ++startp;
1068 #endif
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. */
1082 if (base == 16)
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;
1088 mp_limb_t val;
1090 while (!ISXDIGIT (*startp))
1091 ++startp;
1092 while (*startp == L_('0'))
1093 ++startp;
1094 if (ISDIGIT (*startp))
1095 val = *startp++ - L_('0');
1096 else
1097 val = 10 + TOLOWER (*startp++) - L_('a');
1098 bits = nbits[val];
1099 /* We cannot have a leading zero. */
1100 assert (bits != 0);
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);
1109 pos -= bits;
1111 else
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');
1130 else
1131 val = 10 + TOLOWER (*startp++) - L_('a');
1133 if (pos + 1 >= 4)
1135 retval[idx] |= val << (pos - 4 + 1);
1136 pos -= 4;
1138 else
1140 retval[idx--] |= val >> (4 - pos - 1);
1141 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1142 if (idx < 0)
1144 int rest_nonzero = 0;
1145 while (--dig_no > 0)
1147 if (*startp != L_('0'))
1149 rest_nonzero = 1;
1150 break;
1152 startp++;
1154 return round_and_return (retval, exponent, negative, val,
1155 BITS_PER_MP_LIMB - 1, rest_nonzero);
1158 retval[idx] = val;
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
1172 123e-2 to 1.23. */
1174 intmax_t incr = (exponent < 0
1175 ? MAX (-(intmax_t) int_no, exponent)
1176 : MIN ((intmax_t) dig_no - (intmax_t) int_no, exponent));
1177 int_no += incr;
1178 exponent -= incr;
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)
1192 underflows. */
1193 if (__glibc_unlikely (exponent < MIN_10_EXP - (DIG + 2)))
1194 return underflow_value (negative);
1196 if (int_no > 0)
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
1202 #endif
1205 if (exponent > 0)
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;
1210 int expbit = 1;
1211 const struct mp_power *ttab = &_fpioconst_pow10[0];
1215 if ((exponent & expbit) != 0)
1217 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1218 mp_limb_t cy;
1219 exponent ^= expbit;
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
1224 bits follow. */
1225 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1226 cy = __mpn_mul (pdest, psrc, numsize,
1227 &__tens[ttab->arrayoff
1228 + _FPIO_CONST_OFFSET],
1229 size);
1230 else
1231 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1232 + _FPIO_CONST_OFFSET],
1233 size, psrc, numsize);
1234 numsize += size;
1235 if (cy == 0)
1236 --numsize;
1237 (void) SWAP (psrc, pdest);
1239 expbit <<= 1;
1240 ++ttab;
1242 while (exponent != 0);
1244 if (psrc == den)
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)
1262 int i;
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
1266 : least_idx;
1267 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1268 : least_bit - 1;
1270 if (least_bit == 0)
1271 memcpy (retval, &num[least_idx],
1272 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1273 else
1275 for (i = least_idx; i < numsize - 1; ++i)
1276 retval[i - least_idx] = (num[i] >> least_bit)
1277 | (num[i + 1]
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);
1290 /* NOTREACHED */
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);
1313 else
1315 mp_limb_t cy;
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);
1327 /* NOTREACHED */
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;
1336 # else
1337 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1338 # endif
1339 #endif
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. */
1349 int expbit;
1350 int neg_exp;
1351 int more_bits;
1352 int need_frac_digits;
1353 mp_limb_t cy;
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
1359 && exponent <= 0
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. */
1367 if (bits > 0)
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;
1373 else
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 <
1378 2^10. */
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
1386 after rounding.) */
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
1391 leading 0s. */
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;
1406 more_bits = 1;
1408 else
1409 more_bits = 0;
1411 neg_exp = (intmax_t) dig_no - (intmax_t) int_no - exponent;
1413 /* Construct the denominator. */
1414 densize = 0;
1415 expbit = 1;
1418 if ((neg_exp & expbit) != 0)
1420 mp_limb_t cy;
1421 neg_exp ^= expbit;
1423 if (densize == 0)
1425 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1426 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1427 densize * sizeof (mp_limb_t));
1429 else
1431 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1432 + _FPIO_CONST_OFFSET],
1433 ttab->arraysize - _FPIO_CONST_OFFSET,
1434 psrc, densize);
1435 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1436 if (cy == 0)
1437 --densize;
1438 (void) SWAP (psrc, pdest);
1441 expbit <<= 1;
1442 ++ttab;
1444 while (neg_exp != 0);
1446 if (psrc == num)
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
1453 #endif
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
1462 |--- m ---|
1463 den: ddddddddddd n >= m
1464 |--- n ---|
1467 count_leading_zeros (cnt, den[densize - 1]);
1469 if (cnt > 0)
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);
1475 if (cy != 0)
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. */
1486 exponent = bits;
1488 switch (densize)
1490 case 1:
1492 mp_limb_t d, n, quot;
1493 int used = 0;
1495 n = num[0];
1496 d = den[0];
1497 assert (numsize == 1 && n < d);
1501 udiv_qrnnd (quot, n, n, 0, d);
1503 #define got_limb \
1504 if (bits == 0) \
1506 int cnt; \
1507 if (quot == 0) \
1508 cnt = BITS_PER_MP_LIMB; \
1509 else \
1510 count_leading_zeros (cnt, quot); \
1511 exponent -= cnt; \
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; \
1518 else \
1520 /* Note that we only clear the second element. */ \
1521 /* The conditional is determined at compile time. */ \
1522 if (RETURN_LIMB_SIZE > 1) \
1523 retval[1] = 0; \
1524 retval[0] = quot; \
1525 bits = -cnt; \
1528 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1529 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1530 quot); \
1531 else \
1533 used = MANT_DIG - bits; \
1534 if (used > 0) \
1535 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1537 bits += BITS_PER_MP_LIMB
1539 got_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);
1547 case 2:
1549 mp_limb_t d0, d1, n0, n1;
1550 mp_limb_t quot = 0;
1551 int used = 0;
1553 d0 = den[0];
1554 d1 = den[1];
1556 if (numsize < densize)
1558 if (num[0] >= d1)
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. */
1563 n1 = 0;
1564 n0 = num[0];
1566 else
1568 if (bits <= 0)
1569 exponent -= BITS_PER_MP_LIMB;
1570 else
1572 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1573 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1574 BITS_PER_MP_LIMB, 0);
1575 else
1577 used = MANT_DIG - bits;
1578 if (used > 0)
1579 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1581 bits += BITS_PER_MP_LIMB;
1583 n1 = num[0];
1584 n0 = 0;
1587 else
1589 n1 = num[1];
1590 n0 = num[0];
1593 while (bits <= MANT_DIG)
1595 mp_limb_t r;
1597 if (n1 == d1)
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;
1604 r = n0 + d1;
1605 if (r < d1) /* Carry in the addition? */
1607 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1608 goto have_quot;
1610 n1 = d0 - (d0 != 0);
1611 n0 = -d0;
1613 else
1615 udiv_qrnnd (quot, r, n1, n0, d1);
1616 umul_ppmm (n1, n0, d0, quot);
1619 q_test:
1620 if (n1 > r || (n1 == r && n0 > 0))
1622 /* The estimated QUOT was too large. */
1623 --quot;
1625 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1626 r += d1;
1627 if (r >= d1) /* If not carry, test QUOT again. */
1628 goto q_test;
1630 sub_ddmmss (n1, n0, r, 0, n1, n0);
1632 have_quot:
1633 got_limb;
1636 return round_and_return (retval, exponent - 1, negative,
1637 quot, BITS_PER_MP_LIMB - 1 - used,
1638 more_bits || n1 != 0 || n0 != 0);
1640 default:
1642 int i;
1643 mp_limb_t cy, dX, d1, n0, n1;
1644 mp_limb_t quot = 0;
1645 int used = 0;
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)
1653 num[numsize++] = 0;
1655 if (numsize < densize)
1657 mp_size_t empty = densize - numsize;
1658 int i;
1660 if (bits <= 0)
1661 exponent -= empty * BITS_PER_MP_LIMB;
1662 else
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);
1674 #else
1675 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1676 retval[i] = retval[i - empty];
1677 while (i >= 0)
1678 retval[i--] = 0;
1679 #endif
1681 else
1683 used = MANT_DIG - bits;
1684 if (used >= BITS_PER_MP_LIMB)
1686 int i;
1687 (void) __mpn_lshift (&retval[used
1688 / BITS_PER_MP_LIMB],
1689 retval,
1690 (RETURN_LIMB_SIZE
1691 - used / BITS_PER_MP_LIMB),
1692 used % BITS_PER_MP_LIMB);
1693 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1694 retval[i] = 0;
1696 else if (used > 0)
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);
1705 else
1707 int i;
1708 assert (numsize == densize);
1709 for (i = numsize; i > 0; --i)
1710 num[i] = num[i - 1];
1711 num[0] = 0;
1714 den[densize] = 0;
1715 n0 = num[densize];
1717 while (bits <= MANT_DIG)
1719 if (n0 == dX)
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;
1723 else
1725 mp_limb_t r;
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]))
1732 --quot;
1733 r += dX;
1734 if (r < dX) /* I.e. "carry in previous addition?" */
1735 break;
1736 n1 -= n0 < d1;
1737 n0 -= d1;
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);
1750 assert (cy != 0);
1751 --quot;
1753 n0 = num[densize] = num[densize - 1];
1754 for (i = densize - 1; i > 0; --i)
1755 num[i] = num[i - 1];
1756 num[0] = 0;
1758 got_limb;
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);
1770 /* NOTREACHED */
1772 #if defined _LIBC && !defined USE_WIDE_CHAR
1773 libc_hidden_def (____STRTOF_INTERNAL)
1774 #endif
1776 /* External user entry point. */
1778 FLOAT
1779 #ifdef weak_function
1780 weak_function
1781 #endif
1782 __STRTOF (const STRING_TYPE *nptr, STRING_TYPE **endptr, locale_t loc)
1784 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1786 #if defined _LIBC
1787 libc_hidden_def (__STRTOF)
1788 libc_hidden_ver (__STRTOF, STRTOF)
1789 #endif
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);
1796 # else
1797 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1798 # endif
1799 # endif
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);
1803 # else
1804 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1805 # endif
1806 # endif
1807 #endif
1809 #if BUILD_DOUBLE
1810 # if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
1811 # undef strtof64_l
1812 # undef wcstof64_l
1813 # ifdef USE_WIDE_CHAR
1814 weak_alias (wcstod_l, wcstof64_l)
1815 # else
1816 weak_alias (strtod_l, strtof64_l)
1817 # endif
1818 # endif
1819 # if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
1820 # undef strtof32x_l
1821 # undef wcstof32x_l
1822 # ifdef USE_WIDE_CHAR
1823 weak_alias (wcstod_l, wcstof32x_l)
1824 # else
1825 weak_alias (strtod_l, strtof32x_l)
1826 # endif
1827 # endif
1828 #endif