Set up the data structures for vDSO in libc.a
[glibc.git] / stdlib / strtod_l.c
blob95f13e40a2b26cde1821ebb59f6f16ca5dba3ff5
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997-2012 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 if ((mant & 0xfffffffffffffULL) == 0) \
46 mant = 0x8000000000000ULL; \
47 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
48 u.ieee.mantissa1 = (mant) & 0xffffffff; \
49 (flt) = u.d; \
50 } while (0)
51 #endif
52 /* End of configuration part. */
54 #include <ctype.h>
55 #include <errno.h>
56 #include <float.h>
57 #include <ieee754.h>
58 #include "../locale/localeinfo.h"
59 #include <locale.h>
60 #include <math.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <stdint.h>
64 #include <rounding-mode.h>
66 /* The gmp headers need some configuration frobs. */
67 #define HAVE_ALLOCA 1
69 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
70 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
71 #include <gmp-mparam.h>
72 #include <gmp.h>
73 #include "gmp-impl.h"
74 #include "longlong.h"
75 #include "fpioconst.h"
77 #include <assert.h>
80 /* We use this code for the extended locale handling where the
81 function gets as an additional argument the locale which has to be
82 used. To access the values we have to redefine the _NL_CURRENT and
83 _NL_CURRENT_WORD macros. */
84 #undef _NL_CURRENT
85 #define _NL_CURRENT(category, item) \
86 (current->values[_NL_ITEM_INDEX (item)].string)
87 #undef _NL_CURRENT_WORD
88 #define _NL_CURRENT_WORD(category, item) \
89 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
91 #if defined _LIBC || defined HAVE_WCHAR_H
92 # include <wchar.h>
93 #endif
95 #ifdef USE_WIDE_CHAR
96 # include <wctype.h>
97 # define STRING_TYPE wchar_t
98 # define CHAR_TYPE wint_t
99 # define L_(Ch) L##Ch
100 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
101 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
102 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
103 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
104 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
105 # define STRNCASECMP(S1, S2, N) \
106 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
107 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
108 #else
109 # define STRING_TYPE char
110 # define CHAR_TYPE char
111 # define L_(Ch) Ch
112 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
113 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
114 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
115 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
116 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
117 # define STRNCASECMP(S1, S2, N) \
118 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
119 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
120 #endif
123 /* Constants we need from float.h; select the set for the FLOAT precision. */
124 #define MANT_DIG PASTE(FLT,_MANT_DIG)
125 #define DIG PASTE(FLT,_DIG)
126 #define MAX_EXP PASTE(FLT,_MAX_EXP)
127 #define MIN_EXP PASTE(FLT,_MIN_EXP)
128 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
129 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
130 #define MAX_VALUE PASTE(FLT,_MAX)
131 #define MIN_VALUE PASTE(FLT,_MIN)
133 /* Extra macros required to get FLT expanded before the pasting. */
134 #define PASTE(a,b) PASTE1(a,b)
135 #define PASTE1(a,b) a##b
137 /* Function to construct a floating point number from an MP integer
138 containing the fraction bits, a base 2 exponent, and a sign flag. */
139 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
141 /* Definitions according to limb size used. */
142 #if BITS_PER_MP_LIMB == 32
143 # define MAX_DIG_PER_LIMB 9
144 # define MAX_FAC_PER_LIMB 1000000000UL
145 #elif BITS_PER_MP_LIMB == 64
146 # define MAX_DIG_PER_LIMB 19
147 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
148 #else
149 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
150 #endif
152 extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];
154 #ifndef howmany
155 #define howmany(x,y) (((x)+((y)-1))/(y))
156 #endif
157 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
159 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
161 #define RETURN(val,end) \
162 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
163 return val; } while (0)
165 /* Maximum size necessary for mpn integers to hold floating point
166 numbers. The largest number we need to hold is 10^n where 2^-n is
167 1/4 ulp of the smallest representable value (that is, n = MANT_DIG
168 - MIN_EXP + 2). Approximate using 10^3 < 2^10. */
169 #define MPNSIZE (howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
170 BITS_PER_MP_LIMB) + 2)
171 /* Declare an mpn integer variable that big. */
172 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
173 /* Copy an mpn integer value. */
174 #define MPN_ASSIGN(dst, src) \
175 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
178 /* Set errno and return an overflowing value with sign specified by
179 NEGATIVE. */
180 static FLOAT
181 overflow_value (int negative)
183 __set_errno (ERANGE);
184 #if FLT_EVAL_METHOD != 0
185 volatile
186 #endif
187 FLOAT result = (negative ? -MAX_VALUE : MAX_VALUE) * MAX_VALUE;
188 return result;
192 /* Set errno and return an underflowing value with sign specified by
193 NEGATIVE. */
194 static FLOAT
195 underflow_value (int negative)
197 __set_errno (ERANGE);
198 #if FLT_EVAL_METHOD != 0
199 volatile
200 #endif
201 FLOAT result = (negative ? -MIN_VALUE : MIN_VALUE) * MIN_VALUE;
202 return result;
206 /* Return a floating point number of the needed type according to the given
207 multi-precision number after possible rounding. */
208 static FLOAT
209 round_and_return (mp_limb_t *retval, intmax_t exponent, int negative,
210 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
212 if (exponent < MIN_EXP - 1)
214 if (exponent < MIN_EXP - 1 - MANT_DIG)
215 return underflow_value (negative);
217 mp_size_t shift = MIN_EXP - 1 - exponent;
219 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
220 if (shift == MANT_DIG)
221 /* This is a special case to handle the very seldom case where
222 the mantissa will be empty after the shift. */
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; ++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 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
244 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
245 shift % BITS_PER_MP_LIMB);
246 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
247 shift / BITS_PER_MP_LIMB);
249 else if (shift > 0)
251 round_limb = retval[0];
252 round_bit = shift - 1;
253 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
255 /* This is a hook for the m68k long double format, where the
256 exponent bias is the same for normalized and denormalized
257 numbers. */
258 #ifndef DENORM_EXP
259 # define DENORM_EXP (MIN_EXP - 2)
260 #endif
261 exponent = DENORM_EXP;
262 __set_errno (ERANGE);
265 if (exponent > MAX_EXP)
266 goto overflow;
268 int mode = get_rounding_mode ();
270 if (round_away (negative,
271 (retval[0] & 1) != 0,
272 (round_limb & (((mp_limb_t) 1) << round_bit)) != 0,
273 (more_bits
274 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0),
275 mode))
277 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
279 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
280 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
281 (retval[RETURN_LIMB_SIZE - 1]
282 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
284 ++exponent;
285 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
286 retval[RETURN_LIMB_SIZE - 1]
287 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
289 else if (exponent == DENORM_EXP
290 && (retval[RETURN_LIMB_SIZE - 1]
291 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
292 != 0)
293 /* The number was denormalized but now normalized. */
294 exponent = MIN_EXP - 1;
297 if (exponent > MAX_EXP)
298 overflow:
299 return overflow_value (negative);
301 return MPN2FLOAT (retval, exponent, negative);
305 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
306 into N. Return the size of the number limbs in NSIZE at the first
307 character od the string that is not part of the integer as the function
308 value. If the EXPONENT is small enough to be taken as an additional
309 factor for the resulting number (see code) multiply by it. */
310 static const STRING_TYPE *
311 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
312 intmax_t *exponent
313 #ifndef USE_WIDE_CHAR
314 , const char *decimal, size_t decimal_len, const char *thousands
315 #endif
319 /* Number of digits for actual limb. */
320 int cnt = 0;
321 mp_limb_t low = 0;
322 mp_limb_t start;
324 *nsize = 0;
325 assert (digcnt > 0);
328 if (cnt == MAX_DIG_PER_LIMB)
330 if (*nsize == 0)
332 n[0] = low;
333 *nsize = 1;
335 else
337 mp_limb_t cy;
338 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
339 cy += __mpn_add_1 (n, n, *nsize, low);
340 if (cy != 0)
342 assert (*nsize < MPNSIZE);
343 n[*nsize] = cy;
344 ++(*nsize);
347 cnt = 0;
348 low = 0;
351 /* There might be thousands separators or radix characters in
352 the string. But these all can be ignored because we know the
353 format of the number is correct and we have an exact number
354 of characters to read. */
355 #ifdef USE_WIDE_CHAR
356 if (*str < L'0' || *str > L'9')
357 ++str;
358 #else
359 if (*str < '0' || *str > '9')
361 int inner = 0;
362 if (thousands != NULL && *str == *thousands
363 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
364 if (thousands[inner] != str[inner])
365 break;
366 thousands[inner] == '\0'; }))
367 str += inner;
368 else
369 str += decimal_len;
371 #endif
372 low = low * 10 + *str++ - L_('0');
373 ++cnt;
375 while (--digcnt > 0);
377 if (*exponent > 0 && *exponent <= MAX_DIG_PER_LIMB - cnt)
379 low *= _tens_in_limb[*exponent];
380 start = _tens_in_limb[cnt + *exponent];
381 *exponent = 0;
383 else
384 start = _tens_in_limb[cnt];
386 if (*nsize == 0)
388 n[0] = low;
389 *nsize = 1;
391 else
393 mp_limb_t cy;
394 cy = __mpn_mul_1 (n, n, *nsize, start);
395 cy += __mpn_add_1 (n, n, *nsize, low);
396 if (cy != 0)
398 assert (*nsize < MPNSIZE);
399 n[(*nsize)++] = cy;
403 return str;
407 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
408 with the COUNT most significant bits of LIMB.
410 Tege doesn't like this function so I have to write it here myself. :)
411 --drepper */
412 static inline void
413 __attribute ((always_inline))
414 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
415 mp_limb_t limb)
417 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
419 /* Optimize the case of shifting by exactly a word:
420 just copy words, with no actual bit-shifting. */
421 mp_size_t i;
422 for (i = size - 1; i > 0; --i)
423 ptr[i] = ptr[i - 1];
424 ptr[0] = limb;
426 else
428 (void) __mpn_lshift (ptr, ptr, size, count);
429 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
434 #define INTERNAL(x) INTERNAL1(x)
435 #define INTERNAL1(x) __##x##_internal
436 #ifndef ____STRTOF_INTERNAL
437 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
438 #endif
440 /* This file defines a function to check for correct grouping. */
441 #include "grouping.h"
444 /* Return a floating point number with the value of the given string NPTR.
445 Set *ENDPTR to the character after the last used one. If the number is
446 smaller than the smallest representable number, set `errno' to ERANGE and
447 return 0.0. If the number is too big to be represented, set `errno' to
448 ERANGE and return HUGE_VAL with the appropriate sign. */
449 FLOAT
450 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
451 const STRING_TYPE *nptr;
452 STRING_TYPE **endptr;
453 int group;
454 __locale_t loc;
456 int negative; /* The sign of the number. */
457 MPN_VAR (num); /* MP representation of the number. */
458 intmax_t exponent; /* Exponent of the number. */
460 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
461 int base = 10;
463 /* When we have to compute fractional digits we form a fraction with a
464 second multi-precision number (and we sometimes need a second for
465 temporary results). */
466 MPN_VAR (den);
468 /* Representation for the return value. */
469 mp_limb_t retval[RETURN_LIMB_SIZE];
470 /* Number of bits currently in result value. */
471 int bits;
473 /* Running pointer after the last character processed in the string. */
474 const STRING_TYPE *cp, *tp;
475 /* Start of significant part of the number. */
476 const STRING_TYPE *startp, *start_of_digits;
477 /* Points at the character following the integer and fractional digits. */
478 const STRING_TYPE *expp;
479 /* Total number of digit and number of digits in integer part. */
480 size_t dig_no, int_no, lead_zero;
481 /* Contains the last character read. */
482 CHAR_TYPE c;
484 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
485 there. So define it ourselves if it remains undefined. */
486 #ifndef _WINT_T
487 typedef unsigned int wint_t;
488 #endif
489 /* The radix character of the current locale. */
490 #ifdef USE_WIDE_CHAR
491 wchar_t decimal;
492 #else
493 const char *decimal;
494 size_t decimal_len;
495 #endif
496 /* The thousands character of the current locale. */
497 #ifdef USE_WIDE_CHAR
498 wchar_t thousands = L'\0';
499 #else
500 const char *thousands = NULL;
501 #endif
502 /* The numeric grouping specification of the current locale,
503 in the format described in <locale.h>. */
504 const char *grouping;
505 /* Used in several places. */
506 int cnt;
508 struct __locale_data *current = loc->__locales[LC_NUMERIC];
510 if (__builtin_expect (group, 0))
512 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
513 if (*grouping <= 0 || *grouping == CHAR_MAX)
514 grouping = NULL;
515 else
517 /* Figure out the thousands separator character. */
518 #ifdef USE_WIDE_CHAR
519 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
520 _NL_NUMERIC_THOUSANDS_SEP_WC);
521 if (thousands == L'\0')
522 grouping = NULL;
523 #else
524 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
525 if (*thousands == '\0')
527 thousands = NULL;
528 grouping = NULL;
530 #endif
533 else
534 grouping = NULL;
536 /* Find the locale's decimal point character. */
537 #ifdef USE_WIDE_CHAR
538 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
539 assert (decimal != L'\0');
540 # define decimal_len 1
541 #else
542 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
543 decimal_len = strlen (decimal);
544 assert (decimal_len > 0);
545 #endif
547 /* Prepare number representation. */
548 exponent = 0;
549 negative = 0;
550 bits = 0;
552 /* Parse string to get maximal legal prefix. We need the number of
553 characters of the integer part, the fractional part and the exponent. */
554 cp = nptr - 1;
555 /* Ignore leading white space. */
557 c = *++cp;
558 while (ISSPACE (c));
560 /* Get sign of the result. */
561 if (c == L_('-'))
563 negative = 1;
564 c = *++cp;
566 else if (c == L_('+'))
567 c = *++cp;
569 /* Return 0.0 if no legal string is found.
570 No character is used even if a sign was found. */
571 #ifdef USE_WIDE_CHAR
572 if (c == (wint_t) decimal
573 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
575 /* We accept it. This funny construct is here only to indent
576 the code correctly. */
578 #else
579 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
580 if (cp[cnt] != decimal[cnt])
581 break;
582 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
584 /* We accept it. This funny construct is here only to indent
585 the code correctly. */
587 #endif
588 else if (c < L_('0') || c > L_('9'))
590 /* Check for `INF' or `INFINITY'. */
591 CHAR_TYPE lowc = TOLOWER_C (c);
593 if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
595 /* Return +/- infinity. */
596 if (endptr != NULL)
597 *endptr = (STRING_TYPE *)
598 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
599 ? 8 : 3));
601 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
604 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
606 /* Return NaN. */
607 FLOAT retval = NAN;
609 cp += 3;
611 /* Match `(n-char-sequence-digit)'. */
612 if (*cp == L_('('))
614 const STRING_TYPE *startp = cp;
616 ++cp;
617 while ((*cp >= L_('0') && *cp <= L_('9'))
618 || ({ CHAR_TYPE lo = TOLOWER (*cp);
619 lo >= L_('a') && lo <= L_('z'); })
620 || *cp == L_('_'));
622 if (*cp != L_(')'))
623 /* The closing brace is missing. Only match the NAN
624 part. */
625 cp = startp;
626 else
628 /* This is a system-dependent way to specify the
629 bitmask used for the NaN. We expect it to be
630 a number which is put in the mantissa of the
631 number. */
632 STRING_TYPE *endp;
633 unsigned long long int mant;
635 mant = STRTOULL (startp + 1, &endp, 0);
636 if (endp == cp)
637 SET_MANTISSA (retval, mant);
639 /* Consume the closing brace. */
640 ++cp;
644 if (endptr != NULL)
645 *endptr = (STRING_TYPE *) cp;
647 return retval;
650 /* It is really a text we do not recognize. */
651 RETURN (0.0, nptr);
654 /* First look whether we are faced with a hexadecimal number. */
655 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
657 /* Okay, it is a hexa-decimal number. Remember this and skip
658 the characters. BTW: hexadecimal numbers must not be
659 grouped. */
660 base = 16;
661 cp += 2;
662 c = *cp;
663 grouping = NULL;
666 /* Record the start of the digits, in case we will check their grouping. */
667 start_of_digits = startp = cp;
669 /* Ignore leading zeroes. This helps us to avoid useless computations. */
670 #ifdef USE_WIDE_CHAR
671 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
672 c = *++cp;
673 #else
674 if (__builtin_expect (thousands == NULL, 1))
675 while (c == '0')
676 c = *++cp;
677 else
679 /* We also have the multibyte thousands string. */
680 while (1)
682 if (c != '0')
684 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
685 if (thousands[cnt] != cp[cnt])
686 break;
687 if (thousands[cnt] != '\0')
688 break;
689 cp += cnt - 1;
691 c = *++cp;
694 #endif
696 /* If no other digit but a '0' is found the result is 0.0.
697 Return current read pointer. */
698 CHAR_TYPE lowc = TOLOWER (c);
699 if (!((c >= L_('0') && c <= L_('9'))
700 || (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
701 || (
702 #ifdef USE_WIDE_CHAR
703 c == (wint_t) decimal
704 #else
705 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
706 if (decimal[cnt] != cp[cnt])
707 break;
708 decimal[cnt] == '\0'; })
709 #endif
710 /* '0x.' alone is not a valid hexadecimal number.
711 '.' alone is not valid either, but that has been checked
712 already earlier. */
713 && (base != 16
714 || cp != start_of_digits
715 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
716 || ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
717 lo >= L_('a') && lo <= L_('f'); })))
718 || (base == 16 && (cp != start_of_digits
719 && lowc == L_('p')))
720 || (base != 16 && lowc == L_('e'))))
722 #ifdef USE_WIDE_CHAR
723 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
724 grouping);
725 #else
726 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
727 grouping);
728 #endif
729 /* If TP is at the start of the digits, there was no correctly
730 grouped prefix of the string; so no number found. */
731 RETURN (negative ? -0.0 : 0.0,
732 tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
735 /* Remember first significant digit and read following characters until the
736 decimal point, exponent character or any non-FP number character. */
737 startp = cp;
738 dig_no = 0;
739 while (1)
741 if ((c >= L_('0') && c <= L_('9'))
742 || (base == 16
743 && ({ CHAR_TYPE lo = TOLOWER (c);
744 lo >= L_('a') && lo <= L_('f'); })))
745 ++dig_no;
746 else
748 #ifdef USE_WIDE_CHAR
749 if (__builtin_expect ((wint_t) thousands == L'\0', 1)
750 || c != (wint_t) thousands)
751 /* Not a digit or separator: end of the integer part. */
752 break;
753 #else
754 if (__builtin_expect (thousands == NULL, 1))
755 break;
756 else
758 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
759 if (thousands[cnt] != cp[cnt])
760 break;
761 if (thousands[cnt] != '\0')
762 break;
763 cp += cnt - 1;
765 #endif
767 c = *++cp;
770 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
772 /* Check the grouping of the digits. */
773 #ifdef USE_WIDE_CHAR
774 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
775 grouping);
776 #else
777 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
778 grouping);
779 #endif
780 if (cp != tp)
782 /* Less than the entire string was correctly grouped. */
784 if (tp == start_of_digits)
785 /* No valid group of numbers at all: no valid number. */
786 RETURN (0.0, nptr);
788 if (tp < startp)
789 /* The number is validly grouped, but consists
790 only of zeroes. The whole value is zero. */
791 RETURN (negative ? -0.0 : 0.0, tp);
793 /* Recompute DIG_NO so we won't read more digits than
794 are properly grouped. */
795 cp = tp;
796 dig_no = 0;
797 for (tp = startp; tp < cp; ++tp)
798 if (*tp >= L_('0') && *tp <= L_('9'))
799 ++dig_no;
801 int_no = dig_no;
802 lead_zero = 0;
804 goto number_parsed;
808 /* We have the number of digits in the integer part. Whether these
809 are all or any is really a fractional digit will be decided
810 later. */
811 int_no = dig_no;
812 lead_zero = int_no == 0 ? (size_t) -1 : 0;
814 /* Read the fractional digits. A special case are the 'american
815 style' numbers like `16.' i.e. with decimal point but without
816 trailing digits. */
817 if (
818 #ifdef USE_WIDE_CHAR
819 c == (wint_t) decimal
820 #else
821 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
822 if (decimal[cnt] != cp[cnt])
823 break;
824 decimal[cnt] == '\0'; })
825 #endif
828 cp += decimal_len;
829 c = *cp;
830 while ((c >= L_('0') && c <= L_('9')) ||
831 (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
832 lo >= L_('a') && lo <= L_('f'); })))
834 if (c != L_('0') && lead_zero == (size_t) -1)
835 lead_zero = dig_no - int_no;
836 ++dig_no;
837 c = *++cp;
840 assert (dig_no <= (uintmax_t) INTMAX_MAX);
842 /* Remember start of exponent (if any). */
843 expp = cp;
845 /* Read exponent. */
846 lowc = TOLOWER (c);
847 if ((base == 16 && lowc == L_('p'))
848 || (base != 16 && lowc == L_('e')))
850 int exp_negative = 0;
852 c = *++cp;
853 if (c == L_('-'))
855 exp_negative = 1;
856 c = *++cp;
858 else if (c == L_('+'))
859 c = *++cp;
861 if (c >= L_('0') && c <= L_('9'))
863 intmax_t exp_limit;
865 /* Get the exponent limit. */
866 if (base == 16)
868 if (exp_negative)
870 assert (int_no <= (uintmax_t) (INTMAX_MAX
871 + MIN_EXP - MANT_DIG) / 4);
872 exp_limit = -MIN_EXP + MANT_DIG + 4 * (intmax_t) int_no;
874 else
876 if (int_no)
878 assert (lead_zero == 0
879 && int_no <= (uintmax_t) INTMAX_MAX / 4);
880 exp_limit = MAX_EXP - 4 * (intmax_t) int_no + 3;
882 else if (lead_zero == (size_t) -1)
884 /* The number is zero and this limit is
885 arbitrary. */
886 exp_limit = MAX_EXP + 3;
888 else
890 assert (lead_zero
891 <= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
892 exp_limit = (MAX_EXP
893 + 4 * (intmax_t) lead_zero
894 + 3);
898 else
900 if (exp_negative)
902 assert (int_no
903 <= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
904 exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
906 else
908 if (int_no)
910 assert (lead_zero == 0
911 && int_no <= (uintmax_t) INTMAX_MAX);
912 exp_limit = MAX_10_EXP - (intmax_t) int_no + 1;
914 else if (lead_zero == (size_t) -1)
916 /* The number is zero and this limit is
917 arbitrary. */
918 exp_limit = MAX_10_EXP + 1;
920 else
922 assert (lead_zero
923 <= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
924 exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
929 if (exp_limit < 0)
930 exp_limit = 0;
934 if (__builtin_expect ((exponent > exp_limit / 10
935 || (exponent == exp_limit / 10
936 && c - L_('0') > exp_limit % 10)), 0))
937 /* The exponent is too large/small to represent a valid
938 number. */
940 FLOAT result;
942 /* We have to take care for special situation: a joker
943 might have written "0.0e100000" which is in fact
944 zero. */
945 if (lead_zero == (size_t) -1)
946 result = negative ? -0.0 : 0.0;
947 else
949 /* Overflow or underflow. */
950 result = (exp_negative
951 ? underflow_value (negative)
952 : overflow_value (negative));
955 /* Accept all following digits as part of the exponent. */
957 ++cp;
958 while (*cp >= L_('0') && *cp <= L_('9'));
960 RETURN (result, cp);
961 /* NOTREACHED */
964 exponent *= 10;
965 exponent += c - L_('0');
967 c = *++cp;
969 while (c >= L_('0') && c <= L_('9'));
971 if (exp_negative)
972 exponent = -exponent;
974 else
975 cp = expp;
978 /* We don't want to have to work with trailing zeroes after the radix. */
979 if (dig_no > int_no)
981 while (expp[-1] == L_('0'))
983 --expp;
984 --dig_no;
986 assert (dig_no >= int_no);
989 if (dig_no == int_no && dig_no > 0 && exponent < 0)
992 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
993 --expp;
995 if (expp[-1] != L_('0'))
996 break;
998 --expp;
999 --dig_no;
1000 --int_no;
1001 exponent += base == 16 ? 4 : 1;
1003 while (dig_no > 0 && exponent < 0);
1005 number_parsed:
1007 /* The whole string is parsed. Store the address of the next character. */
1008 if (endptr)
1009 *endptr = (STRING_TYPE *) cp;
1011 if (dig_no == 0)
1012 return negative ? -0.0 : 0.0;
1014 if (lead_zero)
1016 /* Find the decimal point */
1017 #ifdef USE_WIDE_CHAR
1018 while (*startp != decimal)
1019 ++startp;
1020 #else
1021 while (1)
1023 if (*startp == decimal[0])
1025 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
1026 if (decimal[cnt] != startp[cnt])
1027 break;
1028 if (decimal[cnt] == '\0')
1029 break;
1031 ++startp;
1033 #endif
1034 startp += lead_zero + decimal_len;
1035 assert (lead_zero <= (base == 16
1036 ? (uintmax_t) INTMAX_MAX / 4
1037 : (uintmax_t) INTMAX_MAX));
1038 assert (lead_zero <= (base == 16
1039 ? ((uintmax_t) exponent
1040 - (uintmax_t) INTMAX_MIN) / 4
1041 : ((uintmax_t) exponent - (uintmax_t) INTMAX_MIN)));
1042 exponent -= base == 16 ? 4 * (intmax_t) lead_zero : (intmax_t) lead_zero;
1043 dig_no -= lead_zero;
1046 /* If the BASE is 16 we can use a simpler algorithm. */
1047 if (base == 16)
1049 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
1050 4, 4, 4, 4, 4, 4, 4, 4 };
1051 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
1052 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1053 mp_limb_t val;
1055 while (!ISXDIGIT (*startp))
1056 ++startp;
1057 while (*startp == L_('0'))
1058 ++startp;
1059 if (ISDIGIT (*startp))
1060 val = *startp++ - L_('0');
1061 else
1062 val = 10 + TOLOWER (*startp++) - L_('a');
1063 bits = nbits[val];
1064 /* We cannot have a leading zero. */
1065 assert (bits != 0);
1067 if (pos + 1 >= 4 || pos + 1 >= bits)
1069 /* We don't have to care for wrapping. This is the normal
1070 case so we add the first clause in the `if' expression as
1071 an optimization. It is a compile-time constant and so does
1072 not cost anything. */
1073 retval[idx] = val << (pos - bits + 1);
1074 pos -= bits;
1076 else
1078 retval[idx--] = val >> (bits - pos - 1);
1079 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
1080 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
1083 /* Adjust the exponent for the bits we are shifting in. */
1084 assert (int_no <= (uintmax_t) (exponent < 0
1085 ? (INTMAX_MAX - bits + 1) / 4
1086 : (INTMAX_MAX - exponent - bits + 1) / 4));
1087 exponent += bits - 1 + ((intmax_t) int_no - 1) * 4;
1089 while (--dig_no > 0 && idx >= 0)
1091 if (!ISXDIGIT (*startp))
1092 startp += decimal_len;
1093 if (ISDIGIT (*startp))
1094 val = *startp++ - L_('0');
1095 else
1096 val = 10 + TOLOWER (*startp++) - L_('a');
1098 if (pos + 1 >= 4)
1100 retval[idx] |= val << (pos - 4 + 1);
1101 pos -= 4;
1103 else
1105 retval[idx--] |= val >> (4 - pos - 1);
1106 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1107 if (idx < 0)
1109 int rest_nonzero = 0;
1110 while (--dig_no > 0)
1112 if (*startp != L_('0'))
1114 rest_nonzero = 1;
1115 break;
1117 startp++;
1119 return round_and_return (retval, exponent, negative, val,
1120 BITS_PER_MP_LIMB - 1, rest_nonzero);
1123 retval[idx] = val;
1124 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1128 /* We ran out of digits. */
1129 MPN_ZERO (retval, idx);
1131 return round_and_return (retval, exponent, negative, 0, 0, 0);
1134 /* Now we have the number of digits in total and the integer digits as well
1135 as the exponent and its sign. We can decide whether the read digits are
1136 really integer digits or belong to the fractional part; i.e. we normalize
1137 123e-2 to 1.23. */
1139 register intmax_t incr = (exponent < 0
1140 ? MAX (-(intmax_t) int_no, exponent)
1141 : MIN ((intmax_t) dig_no - (intmax_t) int_no,
1142 exponent));
1143 int_no += incr;
1144 exponent -= incr;
1147 if (__builtin_expect (exponent > MAX_10_EXP + 1 - (intmax_t) int_no, 0))
1148 return overflow_value (negative);
1150 if (__builtin_expect (exponent < MIN_10_EXP - (DIG + 1), 0))
1151 return underflow_value (negative);
1153 if (int_no > 0)
1155 /* Read the integer part as a multi-precision number to NUM. */
1156 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1157 #ifndef USE_WIDE_CHAR
1158 , decimal, decimal_len, thousands
1159 #endif
1162 if (exponent > 0)
1164 /* We now multiply the gained number by the given power of ten. */
1165 mp_limb_t *psrc = num;
1166 mp_limb_t *pdest = den;
1167 int expbit = 1;
1168 const struct mp_power *ttab = &_fpioconst_pow10[0];
1172 if ((exponent & expbit) != 0)
1174 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1175 mp_limb_t cy;
1176 exponent ^= expbit;
1178 /* FIXME: not the whole multiplication has to be
1179 done. If we have the needed number of bits we
1180 only need the information whether more non-zero
1181 bits follow. */
1182 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1183 cy = __mpn_mul (pdest, psrc, numsize,
1184 &__tens[ttab->arrayoff
1185 + _FPIO_CONST_OFFSET],
1186 size);
1187 else
1188 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1189 + _FPIO_CONST_OFFSET],
1190 size, psrc, numsize);
1191 numsize += size;
1192 if (cy == 0)
1193 --numsize;
1194 (void) SWAP (psrc, pdest);
1196 expbit <<= 1;
1197 ++ttab;
1199 while (exponent != 0);
1201 if (psrc == den)
1202 memcpy (num, den, numsize * sizeof (mp_limb_t));
1205 /* Determine how many bits of the result we already have. */
1206 count_leading_zeros (bits, num[numsize - 1]);
1207 bits = numsize * BITS_PER_MP_LIMB - bits;
1209 /* Now we know the exponent of the number in base two.
1210 Check it against the maximum possible exponent. */
1211 if (__builtin_expect (bits > MAX_EXP, 0))
1212 return overflow_value (negative);
1214 /* We have already the first BITS bits of the result. Together with
1215 the information whether more non-zero bits follow this is enough
1216 to determine the result. */
1217 if (bits > MANT_DIG)
1219 int i;
1220 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1221 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1222 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1223 : least_idx;
1224 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1225 : least_bit - 1;
1227 if (least_bit == 0)
1228 memcpy (retval, &num[least_idx],
1229 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1230 else
1232 for (i = least_idx; i < numsize - 1; ++i)
1233 retval[i - least_idx] = (num[i] >> least_bit)
1234 | (num[i + 1]
1235 << (BITS_PER_MP_LIMB - least_bit));
1236 if (i - least_idx < RETURN_LIMB_SIZE)
1237 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1240 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1241 for (i = 0; num[i] == 0; ++i)
1244 return round_and_return (retval, bits - 1, negative,
1245 num[round_idx], round_bit,
1246 int_no < dig_no || i < round_idx);
1247 /* NOTREACHED */
1249 else if (dig_no == int_no)
1251 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1252 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1254 if (target_bit == is_bit)
1256 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1257 numsize * sizeof (mp_limb_t));
1258 /* FIXME: the following loop can be avoided if we assume a
1259 maximal MANT_DIG value. */
1260 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1262 else if (target_bit > is_bit)
1264 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1265 num, numsize, target_bit - is_bit);
1266 /* FIXME: the following loop can be avoided if we assume a
1267 maximal MANT_DIG value. */
1268 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1270 else
1272 mp_limb_t cy;
1273 assert (numsize < RETURN_LIMB_SIZE);
1275 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1276 num, numsize, is_bit - target_bit);
1277 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1278 /* FIXME: the following loop can be avoided if we assume a
1279 maximal MANT_DIG value. */
1280 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1283 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1284 /* NOTREACHED */
1287 /* Store the bits we already have. */
1288 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1289 #if RETURN_LIMB_SIZE > 1
1290 if (numsize < RETURN_LIMB_SIZE)
1291 # if RETURN_LIMB_SIZE == 2
1292 retval[numsize] = 0;
1293 # else
1294 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1295 # endif
1296 #endif
1299 /* We have to compute at least some of the fractional digits. */
1301 /* We construct a fraction and the result of the division gives us
1302 the needed digits. The denominator is 1.0 multiplied by the
1303 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1304 123e-6 gives 123 / 1000000. */
1306 int expbit;
1307 int neg_exp;
1308 int more_bits;
1309 int need_frac_digits;
1310 mp_limb_t cy;
1311 mp_limb_t *psrc = den;
1312 mp_limb_t *pdest = num;
1313 const struct mp_power *ttab = &_fpioconst_pow10[0];
1315 assert (dig_no > int_no
1316 && exponent <= 0
1317 && exponent >= MIN_10_EXP - (DIG + 1));
1319 /* We need to compute MANT_DIG - BITS fractional bits that lie
1320 within the mantissa of the result, the following bit for
1321 rounding, and to know whether any subsequent bit is 0.
1322 Computing a bit with value 2^-n means looking at n digits after
1323 the decimal point. */
1324 if (bits > 0)
1326 /* The bits required are those immediately after the point. */
1327 assert (int_no > 0 && exponent == 0);
1328 need_frac_digits = 1 + MANT_DIG - bits;
1330 else
1332 /* The number is in the form .123eEXPONENT. */
1333 assert (int_no == 0 && *startp != L_('0'));
1334 /* The number is at least 10^(EXPONENT-1), and 10^3 <
1335 2^10. */
1336 int neg_exp_2 = ((1 - exponent) * 10) / 3 + 1;
1337 /* The number is at least 2^-NEG_EXP_2. We need up to
1338 MANT_DIG bits following that bit. */
1339 need_frac_digits = neg_exp_2 + MANT_DIG;
1340 /* However, we never need bits beyond 1/4 ulp of the smallest
1341 representable value. (That 1/4 ulp bit is only needed to
1342 determine tinyness on machines where tinyness is determined
1343 after rounding.) */
1344 if (need_frac_digits > MANT_DIG - MIN_EXP + 2)
1345 need_frac_digits = MANT_DIG - MIN_EXP + 2;
1346 /* At this point, NEED_FRAC_DIGITS is the total number of
1347 digits needed after the point, but some of those may be
1348 leading 0s. */
1349 need_frac_digits += exponent;
1350 /* Any cases underflowing enough that none of the fractional
1351 digits are needed should have been caught earlier (such
1352 cases are on the order of 10^-n or smaller where 2^-n is
1353 the least subnormal). */
1354 assert (need_frac_digits > 0);
1357 if (need_frac_digits > (intmax_t) dig_no - (intmax_t) int_no)
1358 need_frac_digits = (intmax_t) dig_no - (intmax_t) int_no;
1360 if ((intmax_t) dig_no > (intmax_t) int_no + need_frac_digits)
1362 dig_no = int_no + need_frac_digits;
1363 more_bits = 1;
1365 else
1366 more_bits = 0;
1368 neg_exp = (intmax_t) dig_no - (intmax_t) int_no - exponent;
1370 /* Construct the denominator. */
1371 densize = 0;
1372 expbit = 1;
1375 if ((neg_exp & expbit) != 0)
1377 mp_limb_t cy;
1378 neg_exp ^= expbit;
1380 if (densize == 0)
1382 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1383 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1384 densize * sizeof (mp_limb_t));
1386 else
1388 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1389 + _FPIO_CONST_OFFSET],
1390 ttab->arraysize - _FPIO_CONST_OFFSET,
1391 psrc, densize);
1392 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1393 if (cy == 0)
1394 --densize;
1395 (void) SWAP (psrc, pdest);
1398 expbit <<= 1;
1399 ++ttab;
1401 while (neg_exp != 0);
1403 if (psrc == num)
1404 memcpy (den, num, densize * sizeof (mp_limb_t));
1406 /* Read the fractional digits from the string. */
1407 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1408 #ifndef USE_WIDE_CHAR
1409 , decimal, decimal_len, thousands
1410 #endif
1413 /* We now have to shift both numbers so that the highest bit in the
1414 denominator is set. In the same process we copy the numerator to
1415 a high place in the array so that the division constructs the wanted
1416 digits. This is done by a "quasi fix point" number representation.
1418 num: ddddddddddd . 0000000000000000000000
1419 |--- m ---|
1420 den: ddddddddddd n >= m
1421 |--- n ---|
1424 count_leading_zeros (cnt, den[densize - 1]);
1426 if (cnt > 0)
1428 /* Don't call `mpn_shift' with a count of zero since the specification
1429 does not allow this. */
1430 (void) __mpn_lshift (den, den, densize, cnt);
1431 cy = __mpn_lshift (num, num, numsize, cnt);
1432 if (cy != 0)
1433 num[numsize++] = cy;
1436 /* Now we are ready for the division. But it is not necessary to
1437 do a full multi-precision division because we only need a small
1438 number of bits for the result. So we do not use __mpn_divmod
1439 here but instead do the division here by hand and stop whenever
1440 the needed number of bits is reached. The code itself comes
1441 from the GNU MP Library by Torbj\"orn Granlund. */
1443 exponent = bits;
1445 switch (densize)
1447 case 1:
1449 mp_limb_t d, n, quot;
1450 int used = 0;
1452 n = num[0];
1453 d = den[0];
1454 assert (numsize == 1 && n < d);
1458 udiv_qrnnd (quot, n, n, 0, d);
1460 #define got_limb \
1461 if (bits == 0) \
1463 register int cnt; \
1464 if (quot == 0) \
1465 cnt = BITS_PER_MP_LIMB; \
1466 else \
1467 count_leading_zeros (cnt, quot); \
1468 exponent -= cnt; \
1469 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1471 used = MANT_DIG + cnt; \
1472 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1473 bits = MANT_DIG + 1; \
1475 else \
1477 /* Note that we only clear the second element. */ \
1478 /* The conditional is determined at compile time. */ \
1479 if (RETURN_LIMB_SIZE > 1) \
1480 retval[1] = 0; \
1481 retval[0] = quot; \
1482 bits = -cnt; \
1485 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1486 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1487 quot); \
1488 else \
1490 used = MANT_DIG - bits; \
1491 if (used > 0) \
1492 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1494 bits += BITS_PER_MP_LIMB
1496 got_limb;
1498 while (bits <= MANT_DIG);
1500 return round_and_return (retval, exponent - 1, negative,
1501 quot, BITS_PER_MP_LIMB - 1 - used,
1502 more_bits || n != 0);
1504 case 2:
1506 mp_limb_t d0, d1, n0, n1;
1507 mp_limb_t quot = 0;
1508 int used = 0;
1510 d0 = den[0];
1511 d1 = den[1];
1513 if (numsize < densize)
1515 if (num[0] >= d1)
1517 /* The numerator of the number occupies fewer bits than
1518 the denominator but the one limb is bigger than the
1519 high limb of the numerator. */
1520 n1 = 0;
1521 n0 = num[0];
1523 else
1525 if (bits <= 0)
1526 exponent -= BITS_PER_MP_LIMB;
1527 else
1529 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1530 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1531 BITS_PER_MP_LIMB, 0);
1532 else
1534 used = MANT_DIG - bits;
1535 if (used > 0)
1536 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1538 bits += BITS_PER_MP_LIMB;
1540 n1 = num[0];
1541 n0 = 0;
1544 else
1546 n1 = num[1];
1547 n0 = num[0];
1550 while (bits <= MANT_DIG)
1552 mp_limb_t r;
1554 if (n1 == d1)
1556 /* QUOT should be either 111..111 or 111..110. We need
1557 special treatment of this rare case as normal division
1558 would give overflow. */
1559 quot = ~(mp_limb_t) 0;
1561 r = n0 + d1;
1562 if (r < d1) /* Carry in the addition? */
1564 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1565 goto have_quot;
1567 n1 = d0 - (d0 != 0);
1568 n0 = -d0;
1570 else
1572 udiv_qrnnd (quot, r, n1, n0, d1);
1573 umul_ppmm (n1, n0, d0, quot);
1576 q_test:
1577 if (n1 > r || (n1 == r && n0 > 0))
1579 /* The estimated QUOT was too large. */
1580 --quot;
1582 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1583 r += d1;
1584 if (r >= d1) /* If not carry, test QUOT again. */
1585 goto q_test;
1587 sub_ddmmss (n1, n0, r, 0, n1, n0);
1589 have_quot:
1590 got_limb;
1593 return round_and_return (retval, exponent - 1, negative,
1594 quot, BITS_PER_MP_LIMB - 1 - used,
1595 more_bits || n1 != 0 || n0 != 0);
1597 default:
1599 int i;
1600 mp_limb_t cy, dX, d1, n0, n1;
1601 mp_limb_t quot = 0;
1602 int used = 0;
1604 dX = den[densize - 1];
1605 d1 = den[densize - 2];
1607 /* The division does not work if the upper limb of the two-limb
1608 numerator is greater than the denominator. */
1609 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1610 num[numsize++] = 0;
1612 if (numsize < densize)
1614 mp_size_t empty = densize - numsize;
1615 register int i;
1617 if (bits <= 0)
1618 exponent -= empty * BITS_PER_MP_LIMB;
1619 else
1621 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1623 /* We make a difference here because the compiler
1624 cannot optimize the `else' case that good and
1625 this reflects all currently used FLOAT types
1626 and GMP implementations. */
1627 #if RETURN_LIMB_SIZE <= 2
1628 assert (empty == 1);
1629 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1630 BITS_PER_MP_LIMB, 0);
1631 #else
1632 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1633 retval[i] = retval[i - empty];
1634 while (i >= 0)
1635 retval[i--] = 0;
1636 #endif
1638 else
1640 used = MANT_DIG - bits;
1641 if (used >= BITS_PER_MP_LIMB)
1643 register int i;
1644 (void) __mpn_lshift (&retval[used
1645 / BITS_PER_MP_LIMB],
1646 retval,
1647 (RETURN_LIMB_SIZE
1648 - used / BITS_PER_MP_LIMB),
1649 used % BITS_PER_MP_LIMB);
1650 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1651 retval[i] = 0;
1653 else if (used > 0)
1654 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1656 bits += empty * BITS_PER_MP_LIMB;
1658 for (i = numsize; i > 0; --i)
1659 num[i + empty] = num[i - 1];
1660 MPN_ZERO (num, empty + 1);
1662 else
1664 int i;
1665 assert (numsize == densize);
1666 for (i = numsize; i > 0; --i)
1667 num[i] = num[i - 1];
1668 num[0] = 0;
1671 den[densize] = 0;
1672 n0 = num[densize];
1674 while (bits <= MANT_DIG)
1676 if (n0 == dX)
1677 /* This might over-estimate QUOT, but it's probably not
1678 worth the extra code here to find out. */
1679 quot = ~(mp_limb_t) 0;
1680 else
1682 mp_limb_t r;
1684 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1685 umul_ppmm (n1, n0, d1, quot);
1687 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1689 --quot;
1690 r += dX;
1691 if (r < dX) /* I.e. "carry in previous addition?" */
1692 break;
1693 n1 -= n0 < d1;
1694 n0 -= d1;
1698 /* Possible optimization: We already have (q * n0) and (1 * n1)
1699 after the calculation of QUOT. Taking advantage of this, we
1700 could make this loop make two iterations less. */
1702 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1704 if (num[densize] != cy)
1706 cy = __mpn_add_n (num, num, den, densize);
1707 assert (cy != 0);
1708 --quot;
1710 n0 = num[densize] = num[densize - 1];
1711 for (i = densize - 1; i > 0; --i)
1712 num[i] = num[i - 1];
1713 num[0] = 0;
1715 got_limb;
1718 for (i = densize; num[i] == 0 && i >= 0; --i)
1720 return round_and_return (retval, exponent - 1, negative,
1721 quot, BITS_PER_MP_LIMB - 1 - used,
1722 more_bits || i >= 0);
1727 /* NOTREACHED */
1729 #if defined _LIBC && !defined USE_WIDE_CHAR
1730 libc_hidden_def (____STRTOF_INTERNAL)
1731 #endif
1733 /* External user entry point. */
1735 FLOAT
1736 #ifdef weak_function
1737 weak_function
1738 #endif
1739 __STRTOF (nptr, endptr, loc)
1740 const STRING_TYPE *nptr;
1741 STRING_TYPE **endptr;
1742 __locale_t loc;
1744 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1746 #if defined _LIBC
1747 libc_hidden_def (__STRTOF)
1748 libc_hidden_ver (__STRTOF, STRTOF)
1749 #endif
1750 weak_alias (__STRTOF, STRTOF)
1752 #ifdef LONG_DOUBLE_COMPAT
1753 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1754 # ifdef USE_WIDE_CHAR
1755 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1756 # else
1757 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1758 # endif
1759 # endif
1760 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1761 # ifdef USE_WIDE_CHAR
1762 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1763 # else
1764 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1765 # endif
1766 # endif
1767 #endif