Add dependencies on needed locales in each subdir tests (bug 18969)
[glibc.git] / stdlib / strtod_l.c
blobda8e4df1cea540d3623cd549275fe66eb3bcefd5
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/>. */
20 #include <xlocale.h>
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. */
29 #ifndef FLOAT
30 # include <math_ldbl_opt.h>
31 # define FLOAT double
32 # define FLT DBL
33 # ifdef USE_WIDE_CHAR
34 # define STRTOF wcstod_l
35 # define __STRTOF __wcstod_l
36 # else
37 # define STRTOF strtod_l
38 # define __STRTOF __strtod_l
39 # endif
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; \
44 u.d = (flt); \
45 u.ieee_nan.mantissa0 = (mant) >> 32; \
46 u.ieee_nan.mantissa1 = (mant); \
47 if ((u.ieee.mantissa0 | u.ieee.mantissa1) != 0) \
48 (flt) = u.d; \
49 } while (0)
50 #endif
51 /* End of configuration part. */
53 #include <ctype.h>
54 #include <errno.h>
55 #include <float.h>
56 #include <ieee754.h>
57 #include "../locale/localeinfo.h"
58 #include <locale.h>
59 #include <math.h>
60 #include <math_private.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <stdint.h>
64 #include <rounding-mode.h>
65 #include <tininess.h>
67 /* The gmp headers need some configuration frobs. */
68 #define HAVE_ALLOCA 1
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>
73 #include <gmp.h>
74 #include "gmp-impl.h"
75 #include "longlong.h"
76 #include "fpioconst.h"
78 #include <assert.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. */
85 #undef _NL_CURRENT
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
93 # include <wchar.h>
94 #endif
96 #ifdef USE_WIDE_CHAR
97 # include <wctype.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)
109 #else
110 # define STRING_TYPE char
111 # define CHAR_TYPE char
112 # define L_(Ch) Ch
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)
121 #endif
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
149 #else
150 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
151 #endif
153 extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];
155 #ifndef howmany
156 #define howmany(x,y) (((x)+((y)-1))/(y))
157 #endif
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
180 NEGATIVE. */
181 static FLOAT
182 overflow_value (int negative)
184 __set_errno (ERANGE);
185 FLOAT result = math_narrow_eval ((negative ? -MAX_VALUE : MAX_VALUE)
186 * MAX_VALUE);
187 return result;
191 /* Set errno and return an underflowing value with sign specified by
192 NEGATIVE. */
193 static FLOAT
194 underflow_value (int negative)
196 __set_errno (ERANGE);
197 FLOAT result = math_narrow_eval ((negative ? -MIN_VALUE : MIN_VALUE)
198 * MIN_VALUE);
199 return result;
203 /* Return a floating point number of the needed type according to the given
204 multi-precision number after possible rounding. */
205 static FLOAT
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;
217 bool is_tiny = true;
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. */
224 int i;
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)
234 int i;
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))
241 != 0);
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);
248 else
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);
254 else if (shift > 0)
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,
264 (round_limb
265 & (((mp_limb_t) 1) << round_bit)) != 0,
266 (more_bits
267 || ((round_limb
268 & ((((mp_limb_t) 1) << round_bit) - 1))
269 != 0)),
270 mode))
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)))
279 != 0)))
280 is_tiny = false;
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
289 numbers. */
290 #ifndef DENORM_EXP
291 # define DENORM_EXP (MIN_EXP - 2)
292 #endif
293 exponent = DENORM_EXP;
294 if (is_tiny
295 && ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
296 || more_bits
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)
306 goto overflow;
308 if (round_away (negative,
309 (retval[0] & 1) != 0,
310 (round_limb & (((mp_limb_t) 1) << round_bit)) != 0,
311 (more_bits
312 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0),
313 mode))
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))
322 ++exponent;
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)))
330 != 0)
331 /* The number was denormalized but now normalized. */
332 exponent = MIN_EXP - 1;
335 if (exponent > MAX_EXP)
336 overflow:
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,
350 intmax_t *exponent
351 #ifndef USE_WIDE_CHAR
352 , const char *decimal, size_t decimal_len, const char *thousands
353 #endif
357 /* Number of digits for actual limb. */
358 int cnt = 0;
359 mp_limb_t low = 0;
360 mp_limb_t start;
362 *nsize = 0;
363 assert (digcnt > 0);
366 if (cnt == MAX_DIG_PER_LIMB)
368 if (*nsize == 0)
370 n[0] = low;
371 *nsize = 1;
373 else
375 mp_limb_t cy;
376 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
377 cy += __mpn_add_1 (n, n, *nsize, low);
378 if (cy != 0)
380 assert (*nsize < MPNSIZE);
381 n[*nsize] = cy;
382 ++(*nsize);
385 cnt = 0;
386 low = 0;
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. */
393 #ifdef USE_WIDE_CHAR
394 if (*str < L'0' || *str > L'9')
395 ++str;
396 #else
397 if (*str < '0' || *str > '9')
399 int inner = 0;
400 if (thousands != NULL && *str == *thousands
401 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
402 if (thousands[inner] != str[inner])
403 break;
404 thousands[inner] == '\0'; }))
405 str += inner;
406 else
407 str += decimal_len;
409 #endif
410 low = low * 10 + *str++ - L_('0');
411 ++cnt;
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];
419 *exponent = 0;
421 else
422 start = _tens_in_limb[cnt];
424 if (*nsize == 0)
426 n[0] = low;
427 *nsize = 1;
429 else
431 mp_limb_t cy;
432 cy = __mpn_mul_1 (n, n, *nsize, start);
433 cy += __mpn_add_1 (n, n, *nsize, low);
434 if (cy != 0)
436 assert (*nsize < MPNSIZE);
437 n[(*nsize)++] = cy;
441 return str;
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. :)
451 --drepper */
452 #define __mpn_lshift_1(ptr, size, count, limb) \
453 do \
455 mp_limb_t *__ptr = (ptr); \
456 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB) \
458 mp_size_t i; \
459 for (i = (size) - 1; i > 0; --i) \
460 __ptr[i] = __ptr[i - 1]; \
461 __ptr[0] = (limb); \
463 else \
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); \
471 while (0)
474 #define INTERNAL(x) INTERNAL1(x)
475 #define INTERNAL1(x) __##x##_internal
476 #ifndef ____STRTOF_INTERNAL
477 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
478 #endif
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. */
489 FLOAT
490 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
491 const STRING_TYPE *nptr;
492 STRING_TYPE **endptr;
493 int group;
494 __locale_t loc;
496 int negative; /* The sign of the number. */
497 MPN_VAR (num); /* MP representation of the number. */
498 intmax_t exponent; /* Exponent of the number. */
500 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
501 int base = 10;
503 /* When we have to compute fractional digits we form a fraction with a
504 second multi-precision number (and we sometimes need a second for
505 temporary results). */
506 MPN_VAR (den);
508 /* Representation for the return value. */
509 mp_limb_t retval[RETURN_LIMB_SIZE];
510 /* Number of bits currently in result value. */
511 int bits;
513 /* Running pointer after the last character processed in the string. */
514 const STRING_TYPE *cp, *tp;
515 /* Start of significant part of the number. */
516 const STRING_TYPE *startp, *start_of_digits;
517 /* Points at the character following the integer and fractional digits. */
518 const STRING_TYPE *expp;
519 /* Total number of digit and number of digits in integer part. */
520 size_t dig_no, int_no, lead_zero;
521 /* Contains the last character read. */
522 CHAR_TYPE c;
524 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
525 there. So define it ourselves if it remains undefined. */
526 #ifndef _WINT_T
527 typedef unsigned int wint_t;
528 #endif
529 /* The radix character of the current locale. */
530 #ifdef USE_WIDE_CHAR
531 wchar_t decimal;
532 #else
533 const char *decimal;
534 size_t decimal_len;
535 #endif
536 /* The thousands character of the current locale. */
537 #ifdef USE_WIDE_CHAR
538 wchar_t thousands = L'\0';
539 #else
540 const char *thousands = NULL;
541 #endif
542 /* The numeric grouping specification of the current locale,
543 in the format described in <locale.h>. */
544 const char *grouping;
545 /* Used in several places. */
546 int cnt;
548 struct __locale_data *current = loc->__locales[LC_NUMERIC];
550 if (__glibc_unlikely (group))
552 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
553 if (*grouping <= 0 || *grouping == CHAR_MAX)
554 grouping = NULL;
555 else
557 /* Figure out the thousands separator character. */
558 #ifdef USE_WIDE_CHAR
559 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
560 _NL_NUMERIC_THOUSANDS_SEP_WC);
561 if (thousands == L'\0')
562 grouping = NULL;
563 #else
564 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
565 if (*thousands == '\0')
567 thousands = NULL;
568 grouping = NULL;
570 #endif
573 else
574 grouping = NULL;
576 /* Find the locale's decimal point character. */
577 #ifdef USE_WIDE_CHAR
578 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
579 assert (decimal != L'\0');
580 # define decimal_len 1
581 #else
582 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
583 decimal_len = strlen (decimal);
584 assert (decimal_len > 0);
585 #endif
587 /* Prepare number representation. */
588 exponent = 0;
589 negative = 0;
590 bits = 0;
592 /* Parse string to get maximal legal prefix. We need the number of
593 characters of the integer part, the fractional part and the exponent. */
594 cp = nptr - 1;
595 /* Ignore leading white space. */
597 c = *++cp;
598 while (ISSPACE (c));
600 /* Get sign of the result. */
601 if (c == L_('-'))
603 negative = 1;
604 c = *++cp;
606 else if (c == L_('+'))
607 c = *++cp;
609 /* Return 0.0 if no legal string is found.
610 No character is used even if a sign was found. */
611 #ifdef USE_WIDE_CHAR
612 if (c == (wint_t) decimal
613 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
615 /* We accept it. This funny construct is here only to indent
616 the code correctly. */
618 #else
619 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
620 if (cp[cnt] != decimal[cnt])
621 break;
622 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
624 /* We accept it. This funny construct is here only to indent
625 the code correctly. */
627 #endif
628 else if (c < L_('0') || c > L_('9'))
630 /* Check for `INF' or `INFINITY'. */
631 CHAR_TYPE lowc = TOLOWER_C (c);
633 if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
635 /* Return +/- infinity. */
636 if (endptr != NULL)
637 *endptr = (STRING_TYPE *)
638 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
639 ? 8 : 3));
641 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
644 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
646 /* Return NaN. */
647 FLOAT retval = NAN;
649 cp += 3;
651 /* Match `(n-char-sequence-digit)'. */
652 if (*cp == L_('('))
654 const STRING_TYPE *startp = cp;
656 ++cp;
657 while ((*cp >= L_('0') && *cp <= L_('9'))
658 || ({ CHAR_TYPE lo = TOLOWER (*cp);
659 lo >= L_('a') && lo <= L_('z'); })
660 || *cp == L_('_'));
662 if (*cp != L_(')'))
663 /* The closing brace is missing. Only match the NAN
664 part. */
665 cp = startp;
666 else
668 /* This is a system-dependent way to specify the
669 bitmask used for the NaN. We expect it to be
670 a number which is put in the mantissa of the
671 number. */
672 STRING_TYPE *endp;
673 unsigned long long int mant;
675 mant = STRTOULL (startp + 1, &endp, 0);
676 if (endp == cp)
677 SET_MANTISSA (retval, mant);
679 /* Consume the closing brace. */
680 ++cp;
684 if (endptr != NULL)
685 *endptr = (STRING_TYPE *) cp;
687 return retval;
690 /* It is really a text we do not recognize. */
691 RETURN (0.0, nptr);
694 /* First look whether we are faced with a hexadecimal number. */
695 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
697 /* Okay, it is a hexa-decimal number. Remember this and skip
698 the characters. BTW: hexadecimal numbers must not be
699 grouped. */
700 base = 16;
701 cp += 2;
702 c = *cp;
703 grouping = NULL;
706 /* Record the start of the digits, in case we will check their grouping. */
707 start_of_digits = startp = cp;
709 /* Ignore leading zeroes. This helps us to avoid useless computations. */
710 #ifdef USE_WIDE_CHAR
711 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
712 c = *++cp;
713 #else
714 if (__glibc_likely (thousands == NULL))
715 while (c == '0')
716 c = *++cp;
717 else
719 /* We also have the multibyte thousands string. */
720 while (1)
722 if (c != '0')
724 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
725 if (thousands[cnt] != cp[cnt])
726 break;
727 if (thousands[cnt] != '\0')
728 break;
729 cp += cnt - 1;
731 c = *++cp;
734 #endif
736 /* If no other digit but a '0' is found the result is 0.0.
737 Return current read pointer. */
738 CHAR_TYPE lowc = TOLOWER (c);
739 if (!((c >= L_('0') && c <= L_('9'))
740 || (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
741 || (
742 #ifdef USE_WIDE_CHAR
743 c == (wint_t) decimal
744 #else
745 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
746 if (decimal[cnt] != cp[cnt])
747 break;
748 decimal[cnt] == '\0'; })
749 #endif
750 /* '0x.' alone is not a valid hexadecimal number.
751 '.' alone is not valid either, but that has been checked
752 already earlier. */
753 && (base != 16
754 || cp != start_of_digits
755 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
756 || ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
757 lo >= L_('a') && lo <= L_('f'); })))
758 || (base == 16 && (cp != start_of_digits
759 && lowc == L_('p')))
760 || (base != 16 && lowc == L_('e'))))
762 #ifdef USE_WIDE_CHAR
763 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
764 grouping);
765 #else
766 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
767 grouping);
768 #endif
769 /* If TP is at the start of the digits, there was no correctly
770 grouped prefix of the string; so no number found. */
771 RETURN (negative ? -0.0 : 0.0,
772 tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
775 /* Remember first significant digit and read following characters until the
776 decimal point, exponent character or any non-FP number character. */
777 startp = cp;
778 dig_no = 0;
779 while (1)
781 if ((c >= L_('0') && c <= L_('9'))
782 || (base == 16
783 && ({ CHAR_TYPE lo = TOLOWER (c);
784 lo >= L_('a') && lo <= L_('f'); })))
785 ++dig_no;
786 else
788 #ifdef USE_WIDE_CHAR
789 if (__builtin_expect ((wint_t) thousands == L'\0', 1)
790 || c != (wint_t) thousands)
791 /* Not a digit or separator: end of the integer part. */
792 break;
793 #else
794 if (__glibc_likely (thousands == NULL))
795 break;
796 else
798 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
799 if (thousands[cnt] != cp[cnt])
800 break;
801 if (thousands[cnt] != '\0')
802 break;
803 cp += cnt - 1;
805 #endif
807 c = *++cp;
810 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
812 /* Check the grouping of the digits. */
813 #ifdef USE_WIDE_CHAR
814 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
815 grouping);
816 #else
817 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
818 grouping);
819 #endif
820 if (cp != tp)
822 /* Less than the entire string was correctly grouped. */
824 if (tp == start_of_digits)
825 /* No valid group of numbers at all: no valid number. */
826 RETURN (0.0, nptr);
828 if (tp < startp)
829 /* The number is validly grouped, but consists
830 only of zeroes. The whole value is zero. */
831 RETURN (negative ? -0.0 : 0.0, tp);
833 /* Recompute DIG_NO so we won't read more digits than
834 are properly grouped. */
835 cp = tp;
836 dig_no = 0;
837 for (tp = startp; tp < cp; ++tp)
838 if (*tp >= L_('0') && *tp <= L_('9'))
839 ++dig_no;
841 int_no = dig_no;
842 lead_zero = 0;
844 goto number_parsed;
848 /* We have the number of digits in the integer part. Whether these
849 are all or any is really a fractional digit will be decided
850 later. */
851 int_no = dig_no;
852 lead_zero = int_no == 0 ? (size_t) -1 : 0;
854 /* Read the fractional digits. A special case are the 'american
855 style' numbers like `16.' i.e. with decimal point but without
856 trailing digits. */
857 if (
858 #ifdef USE_WIDE_CHAR
859 c == (wint_t) decimal
860 #else
861 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
862 if (decimal[cnt] != cp[cnt])
863 break;
864 decimal[cnt] == '\0'; })
865 #endif
868 cp += decimal_len;
869 c = *cp;
870 while ((c >= L_('0') && c <= L_('9')) ||
871 (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
872 lo >= L_('a') && lo <= L_('f'); })))
874 if (c != L_('0') && lead_zero == (size_t) -1)
875 lead_zero = dig_no - int_no;
876 ++dig_no;
877 c = *++cp;
880 assert (dig_no <= (uintmax_t) INTMAX_MAX);
882 /* Remember start of exponent (if any). */
883 expp = cp;
885 /* Read exponent. */
886 lowc = TOLOWER (c);
887 if ((base == 16 && lowc == L_('p'))
888 || (base != 16 && lowc == L_('e')))
890 int exp_negative = 0;
892 c = *++cp;
893 if (c == L_('-'))
895 exp_negative = 1;
896 c = *++cp;
898 else if (c == L_('+'))
899 c = *++cp;
901 if (c >= L_('0') && c <= L_('9'))
903 intmax_t exp_limit;
905 /* Get the exponent limit. */
906 if (base == 16)
908 if (exp_negative)
910 assert (int_no <= (uintmax_t) (INTMAX_MAX
911 + MIN_EXP - MANT_DIG) / 4);
912 exp_limit = -MIN_EXP + MANT_DIG + 4 * (intmax_t) int_no;
914 else
916 if (int_no)
918 assert (lead_zero == 0
919 && int_no <= (uintmax_t) INTMAX_MAX / 4);
920 exp_limit = MAX_EXP - 4 * (intmax_t) int_no + 3;
922 else if (lead_zero == (size_t) -1)
924 /* The number is zero and this limit is
925 arbitrary. */
926 exp_limit = MAX_EXP + 3;
928 else
930 assert (lead_zero
931 <= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
932 exp_limit = (MAX_EXP
933 + 4 * (intmax_t) lead_zero
934 + 3);
938 else
940 if (exp_negative)
942 assert (int_no
943 <= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
944 exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
946 else
948 if (int_no)
950 assert (lead_zero == 0
951 && int_no <= (uintmax_t) INTMAX_MAX);
952 exp_limit = MAX_10_EXP - (intmax_t) int_no + 1;
954 else if (lead_zero == (size_t) -1)
956 /* The number is zero and this limit is
957 arbitrary. */
958 exp_limit = MAX_10_EXP + 1;
960 else
962 assert (lead_zero
963 <= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
964 exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
969 if (exp_limit < 0)
970 exp_limit = 0;
974 if (__builtin_expect ((exponent > exp_limit / 10
975 || (exponent == exp_limit / 10
976 && c - L_('0') > exp_limit % 10)), 0))
977 /* The exponent is too large/small to represent a valid
978 number. */
980 FLOAT result;
982 /* We have to take care for special situation: a joker
983 might have written "0.0e100000" which is in fact
984 zero. */
985 if (lead_zero == (size_t) -1)
986 result = negative ? -0.0 : 0.0;
987 else
989 /* Overflow or underflow. */
990 result = (exp_negative
991 ? underflow_value (negative)
992 : overflow_value (negative));
995 /* Accept all following digits as part of the exponent. */
997 ++cp;
998 while (*cp >= L_('0') && *cp <= L_('9'));
1000 RETURN (result, cp);
1001 /* NOTREACHED */
1004 exponent *= 10;
1005 exponent += c - L_('0');
1007 c = *++cp;
1009 while (c >= L_('0') && c <= L_('9'));
1011 if (exp_negative)
1012 exponent = -exponent;
1014 else
1015 cp = expp;
1018 /* We don't want to have to work with trailing zeroes after the radix. */
1019 if (dig_no > int_no)
1021 while (expp[-1] == L_('0'))
1023 --expp;
1024 --dig_no;
1026 assert (dig_no >= int_no);
1029 if (dig_no == int_no && dig_no > 0 && exponent < 0)
1032 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
1033 --expp;
1035 if (expp[-1] != L_('0'))
1036 break;
1038 --expp;
1039 --dig_no;
1040 --int_no;
1041 exponent += base == 16 ? 4 : 1;
1043 while (dig_no > 0 && exponent < 0);
1045 number_parsed:
1047 /* The whole string is parsed. Store the address of the next character. */
1048 if (endptr)
1049 *endptr = (STRING_TYPE *) cp;
1051 if (dig_no == 0)
1052 return negative ? -0.0 : 0.0;
1054 if (lead_zero)
1056 /* Find the decimal point */
1057 #ifdef USE_WIDE_CHAR
1058 while (*startp != decimal)
1059 ++startp;
1060 #else
1061 while (1)
1063 if (*startp == decimal[0])
1065 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
1066 if (decimal[cnt] != startp[cnt])
1067 break;
1068 if (decimal[cnt] == '\0')
1069 break;
1071 ++startp;
1073 #endif
1074 startp += lead_zero + decimal_len;
1075 assert (lead_zero <= (base == 16
1076 ? (uintmax_t) INTMAX_MAX / 4
1077 : (uintmax_t) INTMAX_MAX));
1078 assert (lead_zero <= (base == 16
1079 ? ((uintmax_t) exponent
1080 - (uintmax_t) INTMAX_MIN) / 4
1081 : ((uintmax_t) exponent - (uintmax_t) INTMAX_MIN)));
1082 exponent -= base == 16 ? 4 * (intmax_t) lead_zero : (intmax_t) lead_zero;
1083 dig_no -= lead_zero;
1086 /* If the BASE is 16 we can use a simpler algorithm. */
1087 if (base == 16)
1089 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1090 4, 4, 4, 4, 4, 4, 4, 4 };
1091 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
1092 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1093 mp_limb_t val;
1095 while (!ISXDIGIT (*startp))
1096 ++startp;
1097 while (*startp == L_('0'))
1098 ++startp;
1099 if (ISDIGIT (*startp))
1100 val = *startp++ - L_('0');
1101 else
1102 val = 10 + TOLOWER (*startp++) - L_('a');
1103 bits = nbits[val];
1104 /* We cannot have a leading zero. */
1105 assert (bits != 0);
1107 if (pos + 1 >= 4 || pos + 1 >= bits)
1109 /* We don't have to care for wrapping. This is the normal
1110 case so we add the first clause in the `if' expression as
1111 an optimization. It is a compile-time constant and so does
1112 not cost anything. */
1113 retval[idx] = val << (pos - bits + 1);
1114 pos -= bits;
1116 else
1118 retval[idx--] = val >> (bits - pos - 1);
1119 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
1120 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
1123 /* Adjust the exponent for the bits we are shifting in. */
1124 assert (int_no <= (uintmax_t) (exponent < 0
1125 ? (INTMAX_MAX - bits + 1) / 4
1126 : (INTMAX_MAX - exponent - bits + 1) / 4));
1127 exponent += bits - 1 + ((intmax_t) int_no - 1) * 4;
1129 while (--dig_no > 0 && idx >= 0)
1131 if (!ISXDIGIT (*startp))
1132 startp += decimal_len;
1133 if (ISDIGIT (*startp))
1134 val = *startp++ - L_('0');
1135 else
1136 val = 10 + TOLOWER (*startp++) - L_('a');
1138 if (pos + 1 >= 4)
1140 retval[idx] |= val << (pos - 4 + 1);
1141 pos -= 4;
1143 else
1145 retval[idx--] |= val >> (4 - pos - 1);
1146 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1147 if (idx < 0)
1149 int rest_nonzero = 0;
1150 while (--dig_no > 0)
1152 if (*startp != L_('0'))
1154 rest_nonzero = 1;
1155 break;
1157 startp++;
1159 return round_and_return (retval, exponent, negative, val,
1160 BITS_PER_MP_LIMB - 1, rest_nonzero);
1163 retval[idx] = val;
1164 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1168 /* We ran out of digits. */
1169 MPN_ZERO (retval, idx);
1171 return round_and_return (retval, exponent, negative, 0, 0, 0);
1174 /* Now we have the number of digits in total and the integer digits as well
1175 as the exponent and its sign. We can decide whether the read digits are
1176 really integer digits or belong to the fractional part; i.e. we normalize
1177 123e-2 to 1.23. */
1179 intmax_t incr = (exponent < 0
1180 ? MAX (-(intmax_t) int_no, exponent)
1181 : MIN ((intmax_t) dig_no - (intmax_t) int_no, exponent));
1182 int_no += incr;
1183 exponent -= incr;
1186 if (__glibc_unlikely (exponent > MAX_10_EXP + 1 - (intmax_t) int_no))
1187 return overflow_value (negative);
1189 /* 10^(MIN_10_EXP-1) is not normal. Thus, 10^(MIN_10_EXP-1) /
1190 2^MANT_DIG is below half the least subnormal, so anything with a
1191 base-10 exponent less than the base-10 exponent (which is
1192 MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
1193 underflows. DIG is floor((MANT_DIG-1)log10(2)), so an exponent
1194 below MIN_10_EXP - (DIG + 3) underflows. But EXPONENT is
1195 actually an exponent multiplied only by a fractional part, not an
1196 integer part, so an exponent below MIN_10_EXP - (DIG + 2)
1197 underflows. */
1198 if (__glibc_unlikely (exponent < MIN_10_EXP - (DIG + 2)))
1199 return underflow_value (negative);
1201 if (int_no > 0)
1203 /* Read the integer part as a multi-precision number to NUM. */
1204 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1205 #ifndef USE_WIDE_CHAR
1206 , decimal, decimal_len, thousands
1207 #endif
1210 if (exponent > 0)
1212 /* We now multiply the gained number by the given power of ten. */
1213 mp_limb_t *psrc = num;
1214 mp_limb_t *pdest = den;
1215 int expbit = 1;
1216 const struct mp_power *ttab = &_fpioconst_pow10[0];
1220 if ((exponent & expbit) != 0)
1222 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1223 mp_limb_t cy;
1224 exponent ^= expbit;
1226 /* FIXME: not the whole multiplication has to be
1227 done. If we have the needed number of bits we
1228 only need the information whether more non-zero
1229 bits follow. */
1230 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1231 cy = __mpn_mul (pdest, psrc, numsize,
1232 &__tens[ttab->arrayoff
1233 + _FPIO_CONST_OFFSET],
1234 size);
1235 else
1236 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1237 + _FPIO_CONST_OFFSET],
1238 size, psrc, numsize);
1239 numsize += size;
1240 if (cy == 0)
1241 --numsize;
1242 (void) SWAP (psrc, pdest);
1244 expbit <<= 1;
1245 ++ttab;
1247 while (exponent != 0);
1249 if (psrc == den)
1250 memcpy (num, den, numsize * sizeof (mp_limb_t));
1253 /* Determine how many bits of the result we already have. */
1254 count_leading_zeros (bits, num[numsize - 1]);
1255 bits = numsize * BITS_PER_MP_LIMB - bits;
1257 /* Now we know the exponent of the number in base two.
1258 Check it against the maximum possible exponent. */
1259 if (__glibc_unlikely (bits > MAX_EXP))
1260 return overflow_value (negative);
1262 /* We have already the first BITS bits of the result. Together with
1263 the information whether more non-zero bits follow this is enough
1264 to determine the result. */
1265 if (bits > MANT_DIG)
1267 int i;
1268 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1269 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1270 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1271 : least_idx;
1272 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1273 : least_bit - 1;
1275 if (least_bit == 0)
1276 memcpy (retval, &num[least_idx],
1277 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1278 else
1280 for (i = least_idx; i < numsize - 1; ++i)
1281 retval[i - least_idx] = (num[i] >> least_bit)
1282 | (num[i + 1]
1283 << (BITS_PER_MP_LIMB - least_bit));
1284 if (i - least_idx < RETURN_LIMB_SIZE)
1285 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1288 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1289 for (i = 0; num[i] == 0; ++i)
1292 return round_and_return (retval, bits - 1, negative,
1293 num[round_idx], round_bit,
1294 int_no < dig_no || i < round_idx);
1295 /* NOTREACHED */
1297 else if (dig_no == int_no)
1299 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1300 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1302 if (target_bit == is_bit)
1304 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1305 numsize * sizeof (mp_limb_t));
1306 /* FIXME: the following loop can be avoided if we assume a
1307 maximal MANT_DIG value. */
1308 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1310 else if (target_bit > is_bit)
1312 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1313 num, numsize, target_bit - is_bit);
1314 /* FIXME: the following loop can be avoided if we assume a
1315 maximal MANT_DIG value. */
1316 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1318 else
1320 mp_limb_t cy;
1321 assert (numsize < RETURN_LIMB_SIZE);
1323 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1324 num, numsize, is_bit - target_bit);
1325 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1326 /* FIXME: the following loop can be avoided if we assume a
1327 maximal MANT_DIG value. */
1328 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1331 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1332 /* NOTREACHED */
1335 /* Store the bits we already have. */
1336 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1337 #if RETURN_LIMB_SIZE > 1
1338 if (numsize < RETURN_LIMB_SIZE)
1339 # if RETURN_LIMB_SIZE == 2
1340 retval[numsize] = 0;
1341 # else
1342 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1343 # endif
1344 #endif
1347 /* We have to compute at least some of the fractional digits. */
1349 /* We construct a fraction and the result of the division gives us
1350 the needed digits. The denominator is 1.0 multiplied by the
1351 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1352 123e-6 gives 123 / 1000000. */
1354 int expbit;
1355 int neg_exp;
1356 int more_bits;
1357 int need_frac_digits;
1358 mp_limb_t cy;
1359 mp_limb_t *psrc = den;
1360 mp_limb_t *pdest = num;
1361 const struct mp_power *ttab = &_fpioconst_pow10[0];
1363 assert (dig_no > int_no
1364 && exponent <= 0
1365 && exponent >= MIN_10_EXP - (DIG + 2));
1367 /* We need to compute MANT_DIG - BITS fractional bits that lie
1368 within the mantissa of the result, the following bit for
1369 rounding, and to know whether any subsequent bit is 0.
1370 Computing a bit with value 2^-n means looking at n digits after
1371 the decimal point. */
1372 if (bits > 0)
1374 /* The bits required are those immediately after the point. */
1375 assert (int_no > 0 && exponent == 0);
1376 need_frac_digits = 1 + MANT_DIG - bits;
1378 else
1380 /* The number is in the form .123eEXPONENT. */
1381 assert (int_no == 0 && *startp != L_('0'));
1382 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1383 2^10. */
1384 int neg_exp_2 = ((1 - exponent) * 10) / 3 + 1;
1385 /* The number is at least 2^-NEG_EXP_2. We need up to
1386 MANT_DIG bits following that bit. */
1387 need_frac_digits = neg_exp_2 + MANT_DIG;
1388 /* However, we never need bits beyond 1/4 ulp of the smallest
1389 representable value. (That 1/4 ulp bit is only needed to
1390 determine tinyness on machines where tinyness is determined
1391 after rounding.) */
1392 if (need_frac_digits > MANT_DIG - MIN_EXP + 2)
1393 need_frac_digits = MANT_DIG - MIN_EXP + 2;
1394 /* At this point, NEED_FRAC_DIGITS is the total number of
1395 digits needed after the point, but some of those may be
1396 leading 0s. */
1397 need_frac_digits += exponent;
1398 /* Any cases underflowing enough that none of the fractional
1399 digits are needed should have been caught earlier (such
1400 cases are on the order of 10^-n or smaller where 2^-n is
1401 the least subnormal). */
1402 assert (need_frac_digits > 0);
1405 if (need_frac_digits > (intmax_t) dig_no - (intmax_t) int_no)
1406 need_frac_digits = (intmax_t) dig_no - (intmax_t) int_no;
1408 if ((intmax_t) dig_no > (intmax_t) int_no + need_frac_digits)
1410 dig_no = int_no + need_frac_digits;
1411 more_bits = 1;
1413 else
1414 more_bits = 0;
1416 neg_exp = (intmax_t) dig_no - (intmax_t) int_no - exponent;
1418 /* Construct the denominator. */
1419 densize = 0;
1420 expbit = 1;
1423 if ((neg_exp & expbit) != 0)
1425 mp_limb_t cy;
1426 neg_exp ^= expbit;
1428 if (densize == 0)
1430 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1431 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1432 densize * sizeof (mp_limb_t));
1434 else
1436 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1437 + _FPIO_CONST_OFFSET],
1438 ttab->arraysize - _FPIO_CONST_OFFSET,
1439 psrc, densize);
1440 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1441 if (cy == 0)
1442 --densize;
1443 (void) SWAP (psrc, pdest);
1446 expbit <<= 1;
1447 ++ttab;
1449 while (neg_exp != 0);
1451 if (psrc == num)
1452 memcpy (den, num, densize * sizeof (mp_limb_t));
1454 /* Read the fractional digits from the string. */
1455 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1456 #ifndef USE_WIDE_CHAR
1457 , decimal, decimal_len, thousands
1458 #endif
1461 /* We now have to shift both numbers so that the highest bit in the
1462 denominator is set. In the same process we copy the numerator to
1463 a high place in the array so that the division constructs the wanted
1464 digits. This is done by a "quasi fix point" number representation.
1466 num: ddddddddddd . 0000000000000000000000
1467 |--- m ---|
1468 den: ddddddddddd n >= m
1469 |--- n ---|
1472 count_leading_zeros (cnt, den[densize - 1]);
1474 if (cnt > 0)
1476 /* Don't call `mpn_shift' with a count of zero since the specification
1477 does not allow this. */
1478 (void) __mpn_lshift (den, den, densize, cnt);
1479 cy = __mpn_lshift (num, num, numsize, cnt);
1480 if (cy != 0)
1481 num[numsize++] = cy;
1484 /* Now we are ready for the division. But it is not necessary to
1485 do a full multi-precision division because we only need a small
1486 number of bits for the result. So we do not use __mpn_divmod
1487 here but instead do the division here by hand and stop whenever
1488 the needed number of bits is reached. The code itself comes
1489 from the GNU MP Library by Torbj\"orn Granlund. */
1491 exponent = bits;
1493 switch (densize)
1495 case 1:
1497 mp_limb_t d, n, quot;
1498 int used = 0;
1500 n = num[0];
1501 d = den[0];
1502 assert (numsize == 1 && n < d);
1506 udiv_qrnnd (quot, n, n, 0, d);
1508 #define got_limb \
1509 if (bits == 0) \
1511 int cnt; \
1512 if (quot == 0) \
1513 cnt = BITS_PER_MP_LIMB; \
1514 else \
1515 count_leading_zeros (cnt, quot); \
1516 exponent -= cnt; \
1517 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1519 used = MANT_DIG + cnt; \
1520 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1521 bits = MANT_DIG + 1; \
1523 else \
1525 /* Note that we only clear the second element. */ \
1526 /* The conditional is determined at compile time. */ \
1527 if (RETURN_LIMB_SIZE > 1) \
1528 retval[1] = 0; \
1529 retval[0] = quot; \
1530 bits = -cnt; \
1533 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1534 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1535 quot); \
1536 else \
1538 used = MANT_DIG - bits; \
1539 if (used > 0) \
1540 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1542 bits += BITS_PER_MP_LIMB
1544 got_limb;
1546 while (bits <= MANT_DIG);
1548 return round_and_return (retval, exponent - 1, negative,
1549 quot, BITS_PER_MP_LIMB - 1 - used,
1550 more_bits || n != 0);
1552 case 2:
1554 mp_limb_t d0, d1, n0, n1;
1555 mp_limb_t quot = 0;
1556 int used = 0;
1558 d0 = den[0];
1559 d1 = den[1];
1561 if (numsize < densize)
1563 if (num[0] >= d1)
1565 /* The numerator of the number occupies fewer bits than
1566 the denominator but the one limb is bigger than the
1567 high limb of the numerator. */
1568 n1 = 0;
1569 n0 = num[0];
1571 else
1573 if (bits <= 0)
1574 exponent -= BITS_PER_MP_LIMB;
1575 else
1577 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1578 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1579 BITS_PER_MP_LIMB, 0);
1580 else
1582 used = MANT_DIG - bits;
1583 if (used > 0)
1584 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1586 bits += BITS_PER_MP_LIMB;
1588 n1 = num[0];
1589 n0 = 0;
1592 else
1594 n1 = num[1];
1595 n0 = num[0];
1598 while (bits <= MANT_DIG)
1600 mp_limb_t r;
1602 if (n1 == d1)
1604 /* QUOT should be either 111..111 or 111..110. We need
1605 special treatment of this rare case as normal division
1606 would give overflow. */
1607 quot = ~(mp_limb_t) 0;
1609 r = n0 + d1;
1610 if (r < d1) /* Carry in the addition? */
1612 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1613 goto have_quot;
1615 n1 = d0 - (d0 != 0);
1616 n0 = -d0;
1618 else
1620 udiv_qrnnd (quot, r, n1, n0, d1);
1621 umul_ppmm (n1, n0, d0, quot);
1624 q_test:
1625 if (n1 > r || (n1 == r && n0 > 0))
1627 /* The estimated QUOT was too large. */
1628 --quot;
1630 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1631 r += d1;
1632 if (r >= d1) /* If not carry, test QUOT again. */
1633 goto q_test;
1635 sub_ddmmss (n1, n0, r, 0, n1, n0);
1637 have_quot:
1638 got_limb;
1641 return round_and_return (retval, exponent - 1, negative,
1642 quot, BITS_PER_MP_LIMB - 1 - used,
1643 more_bits || n1 != 0 || n0 != 0);
1645 default:
1647 int i;
1648 mp_limb_t cy, dX, d1, n0, n1;
1649 mp_limb_t quot = 0;
1650 int used = 0;
1652 dX = den[densize - 1];
1653 d1 = den[densize - 2];
1655 /* The division does not work if the upper limb of the two-limb
1656 numerator is greater than the denominator. */
1657 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1658 num[numsize++] = 0;
1660 if (numsize < densize)
1662 mp_size_t empty = densize - numsize;
1663 int i;
1665 if (bits <= 0)
1666 exponent -= empty * BITS_PER_MP_LIMB;
1667 else
1669 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1671 /* We make a difference here because the compiler
1672 cannot optimize the `else' case that good and
1673 this reflects all currently used FLOAT types
1674 and GMP implementations. */
1675 #if RETURN_LIMB_SIZE <= 2
1676 assert (empty == 1);
1677 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1678 BITS_PER_MP_LIMB, 0);
1679 #else
1680 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1681 retval[i] = retval[i - empty];
1682 while (i >= 0)
1683 retval[i--] = 0;
1684 #endif
1686 else
1688 used = MANT_DIG - bits;
1689 if (used >= BITS_PER_MP_LIMB)
1691 int i;
1692 (void) __mpn_lshift (&retval[used
1693 / BITS_PER_MP_LIMB],
1694 retval,
1695 (RETURN_LIMB_SIZE
1696 - used / BITS_PER_MP_LIMB),
1697 used % BITS_PER_MP_LIMB);
1698 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1699 retval[i] = 0;
1701 else if (used > 0)
1702 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1704 bits += empty * BITS_PER_MP_LIMB;
1706 for (i = numsize; i > 0; --i)
1707 num[i + empty] = num[i - 1];
1708 MPN_ZERO (num, empty + 1);
1710 else
1712 int i;
1713 assert (numsize == densize);
1714 for (i = numsize; i > 0; --i)
1715 num[i] = num[i - 1];
1716 num[0] = 0;
1719 den[densize] = 0;
1720 n0 = num[densize];
1722 while (bits <= MANT_DIG)
1724 if (n0 == dX)
1725 /* This might over-estimate QUOT, but it's probably not
1726 worth the extra code here to find out. */
1727 quot = ~(mp_limb_t) 0;
1728 else
1730 mp_limb_t r;
1732 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1733 umul_ppmm (n1, n0, d1, quot);
1735 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1737 --quot;
1738 r += dX;
1739 if (r < dX) /* I.e. "carry in previous addition?" */
1740 break;
1741 n1 -= n0 < d1;
1742 n0 -= d1;
1746 /* Possible optimization: We already have (q * n0) and (1 * n1)
1747 after the calculation of QUOT. Taking advantage of this, we
1748 could make this loop make two iterations less. */
1750 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1752 if (num[densize] != cy)
1754 cy = __mpn_add_n (num, num, den, densize);
1755 assert (cy != 0);
1756 --quot;
1758 n0 = num[densize] = num[densize - 1];
1759 for (i = densize - 1; i > 0; --i)
1760 num[i] = num[i - 1];
1761 num[0] = 0;
1763 got_limb;
1766 for (i = densize; i >= 0 && num[i] == 0; --i)
1768 return round_and_return (retval, exponent - 1, negative,
1769 quot, BITS_PER_MP_LIMB - 1 - used,
1770 more_bits || i >= 0);
1775 /* NOTREACHED */
1777 #if defined _LIBC && !defined USE_WIDE_CHAR
1778 libc_hidden_def (____STRTOF_INTERNAL)
1779 #endif
1781 /* External user entry point. */
1783 FLOAT
1784 #ifdef weak_function
1785 weak_function
1786 #endif
1787 __STRTOF (nptr, endptr, loc)
1788 const STRING_TYPE *nptr;
1789 STRING_TYPE **endptr;
1790 __locale_t loc;
1792 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1794 #if defined _LIBC
1795 libc_hidden_def (__STRTOF)
1796 libc_hidden_ver (__STRTOF, STRTOF)
1797 #endif
1798 weak_alias (__STRTOF, STRTOF)
1800 #ifdef LONG_DOUBLE_COMPAT
1801 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1802 # ifdef USE_WIDE_CHAR
1803 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1804 # else
1805 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1806 # endif
1807 # endif
1808 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1809 # ifdef USE_WIDE_CHAR
1810 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1811 # else
1812 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1813 # endif
1814 # endif
1815 #endif