2.9
[glibc/nacl-glibc.git] / stdlib / strtod_l.c
blob09a8f9bb3b766cfaabe7c3878c3da824a46513ae
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006,2007,2008
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <xlocale.h>
24 extern double ____strtod_l_internal (const char *, char **, int, __locale_t);
25 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
26 int, int, __locale_t);
28 /* Configuration part. These macros are defined by `strtold.c',
29 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
30 `long double' and `float' versions of the reader. */
31 #ifndef FLOAT
32 # include <math_ldbl_opt.h>
33 # define FLOAT double
34 # define FLT DBL
35 # ifdef USE_WIDE_CHAR
36 # define STRTOF wcstod_l
37 # define __STRTOF __wcstod_l
38 # else
39 # define STRTOF strtod_l
40 # define __STRTOF __strtod_l
41 # endif
42 # define MPN2FLOAT __mpn_construct_double
43 # define FLOAT_HUGE_VAL HUGE_VAL
44 # define SET_MANTISSA(flt, mant) \
45 do { union ieee754_double u; \
46 u.d = (flt); \
47 if ((mant & 0xfffffffffffffULL) == 0) \
48 mant = 0x8000000000000ULL; \
49 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
50 u.ieee.mantissa1 = (mant) & 0xffffffff; \
51 (flt) = u.d; \
52 } while (0)
53 #endif
54 /* End of configuration part. */
56 #include <ctype.h>
57 #include <errno.h>
58 #include <float.h>
59 #include <ieee754.h>
60 #include "../locale/localeinfo.h"
61 #include <locale.h>
62 #include <math.h>
63 #include <stdlib.h>
64 #include <string.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 #define NDEBUG 1
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)
132 /* Extra macros required to get FLT expanded before the pasting. */
133 #define PASTE(a,b) PASTE1(a,b)
134 #define PASTE1(a,b) a##b
136 /* Function to construct a floating point number from an MP integer
137 containing the fraction bits, a base 2 exponent, and a sign flag. */
138 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
140 /* Definitions according to limb size used. */
141 #if BITS_PER_MP_LIMB == 32
142 # define MAX_DIG_PER_LIMB 9
143 # define MAX_FAC_PER_LIMB 1000000000UL
144 #elif BITS_PER_MP_LIMB == 64
145 # define MAX_DIG_PER_LIMB 19
146 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
147 #else
148 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
149 #endif
151 extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];
153 #ifndef howmany
154 #define howmany(x,y) (((x)+((y)-1))/(y))
155 #endif
156 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
158 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
159 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
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 numbers. */
167 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
168 + 2)
169 /* Declare an mpn integer variable that big. */
170 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
171 /* Copy an mpn integer value. */
172 #define MPN_ASSIGN(dst, src) \
173 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
176 /* Return a floating point number of the needed type according to the given
177 multi-precision number after possible rounding. */
178 static FLOAT
179 round_and_return (mp_limb_t *retval, int exponent, int negative,
180 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
182 if (exponent < MIN_EXP - 1)
184 mp_size_t shift = MIN_EXP - 1 - exponent;
186 if (shift > MANT_DIG)
188 __set_errno (EDOM);
189 return 0.0;
192 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
193 if (shift == MANT_DIG)
194 /* This is a special case to handle the very seldom case where
195 the mantissa will be empty after the shift. */
197 int i;
199 round_limb = retval[RETURN_LIMB_SIZE - 1];
200 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
201 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
202 more_bits |= retval[i] != 0;
203 MPN_ZERO (retval, RETURN_LIMB_SIZE);
205 else if (shift >= BITS_PER_MP_LIMB)
207 int i;
209 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
210 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
211 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
212 more_bits |= retval[i] != 0;
213 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
214 != 0);
216 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
217 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
218 shift % BITS_PER_MP_LIMB);
219 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
220 shift / BITS_PER_MP_LIMB);
222 else if (shift > 0)
224 round_limb = retval[0];
225 round_bit = shift - 1;
226 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
228 /* This is a hook for the m68k long double format, where the
229 exponent bias is the same for normalized and denormalized
230 numbers. */
231 #ifndef DENORM_EXP
232 # define DENORM_EXP (MIN_EXP - 2)
233 #endif
234 exponent = DENORM_EXP;
237 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
238 && (more_bits || (retval[0] & 1) != 0
239 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
241 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
243 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
244 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
245 (retval[RETURN_LIMB_SIZE - 1]
246 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
248 ++exponent;
249 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
250 retval[RETURN_LIMB_SIZE - 1]
251 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
253 else if (exponent == DENORM_EXP
254 && (retval[RETURN_LIMB_SIZE - 1]
255 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
256 != 0)
257 /* The number was denormalized but now normalized. */
258 exponent = MIN_EXP - 1;
261 if (exponent > MAX_EXP)
262 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
264 return MPN2FLOAT (retval, exponent, negative);
268 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
269 into N. Return the size of the number limbs in NSIZE at the first
270 character od the string that is not part of the integer as the function
271 value. If the EXPONENT is small enough to be taken as an additional
272 factor for the resulting number (see code) multiply by it. */
273 static const STRING_TYPE *
274 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
275 int *exponent
276 #ifndef USE_WIDE_CHAR
277 , const char *decimal, size_t decimal_len, const char *thousands
278 #endif
282 /* Number of digits for actual limb. */
283 int cnt = 0;
284 mp_limb_t low = 0;
285 mp_limb_t start;
287 *nsize = 0;
288 assert (digcnt > 0);
291 if (cnt == MAX_DIG_PER_LIMB)
293 if (*nsize == 0)
295 n[0] = low;
296 *nsize = 1;
298 else
300 mp_limb_t cy;
301 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
302 cy += __mpn_add_1 (n, n, *nsize, low);
303 if (cy != 0)
305 n[*nsize] = cy;
306 ++(*nsize);
309 cnt = 0;
310 low = 0;
313 /* There might be thousands separators or radix characters in
314 the string. But these all can be ignored because we know the
315 format of the number is correct and we have an exact number
316 of characters to read. */
317 #ifdef USE_WIDE_CHAR
318 if (*str < L'0' || *str > L'9')
319 ++str;
320 #else
321 if (*str < '0' || *str > '9')
323 int inner = 0;
324 if (thousands != NULL && *str == *thousands
325 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
326 if (thousands[inner] != str[inner])
327 break;
328 thousands[inner] == '\0'; }))
329 str += inner;
330 else
331 str += decimal_len;
333 #endif
334 low = low * 10 + *str++ - L_('0');
335 ++cnt;
337 while (--digcnt > 0);
339 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
341 low *= _tens_in_limb[*exponent];
342 start = _tens_in_limb[cnt + *exponent];
343 *exponent = 0;
345 else
346 start = _tens_in_limb[cnt];
348 if (*nsize == 0)
350 n[0] = low;
351 *nsize = 1;
353 else
355 mp_limb_t cy;
356 cy = __mpn_mul_1 (n, n, *nsize, start);
357 cy += __mpn_add_1 (n, n, *nsize, low);
358 if (cy != 0)
359 n[(*nsize)++] = cy;
362 return str;
366 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
367 with the COUNT most significant bits of LIMB.
369 Tege doesn't like this function so I have to write it here myself. :)
370 --drepper */
371 static inline void
372 __attribute ((always_inline))
373 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
374 mp_limb_t limb)
376 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
378 /* Optimize the case of shifting by exactly a word:
379 just copy words, with no actual bit-shifting. */
380 mp_size_t i;
381 for (i = size - 1; i > 0; --i)
382 ptr[i] = ptr[i - 1];
383 ptr[0] = limb;
385 else
387 (void) __mpn_lshift (ptr, ptr, size, count);
388 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
393 #define INTERNAL(x) INTERNAL1(x)
394 #define INTERNAL1(x) __##x##_internal
395 #ifndef ____STRTOF_INTERNAL
396 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
397 #endif
399 /* This file defines a function to check for correct grouping. */
400 #include "grouping.h"
403 /* Return a floating point number with the value of the given string NPTR.
404 Set *ENDPTR to the character after the last used one. If the number is
405 smaller than the smallest representable number, set `errno' to ERANGE and
406 return 0.0. If the number is too big to be represented, set `errno' to
407 ERANGE and return HUGE_VAL with the appropriate sign. */
408 FLOAT
409 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
410 const STRING_TYPE *nptr;
411 STRING_TYPE **endptr;
412 int group;
413 __locale_t loc;
415 int negative; /* The sign of the number. */
416 MPN_VAR (num); /* MP representation of the number. */
417 int exponent; /* Exponent of the number. */
419 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
420 int base = 10;
422 /* When we have to compute fractional digits we form a fraction with a
423 second multi-precision number (and we sometimes need a second for
424 temporary results). */
425 MPN_VAR (den);
427 /* Representation for the return value. */
428 mp_limb_t retval[RETURN_LIMB_SIZE];
429 /* Number of bits currently in result value. */
430 int bits;
432 /* Running pointer after the last character processed in the string. */
433 const STRING_TYPE *cp, *tp;
434 /* Start of significant part of the number. */
435 const STRING_TYPE *startp, *start_of_digits;
436 /* Points at the character following the integer and fractional digits. */
437 const STRING_TYPE *expp;
438 /* Total number of digit and number of digits in integer part. */
439 int dig_no, int_no, lead_zero;
440 /* Contains the last character read. */
441 CHAR_TYPE c;
443 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
444 there. So define it ourselves if it remains undefined. */
445 #ifndef _WINT_T
446 typedef unsigned int wint_t;
447 #endif
448 /* The radix character of the current locale. */
449 #ifdef USE_WIDE_CHAR
450 wchar_t decimal;
451 #else
452 const char *decimal;
453 size_t decimal_len;
454 #endif
455 /* The thousands character of the current locale. */
456 #ifdef USE_WIDE_CHAR
457 wchar_t thousands = L'\0';
458 #else
459 const char *thousands = NULL;
460 #endif
461 /* The numeric grouping specification of the current locale,
462 in the format described in <locale.h>. */
463 const char *grouping;
464 /* Used in several places. */
465 int cnt;
467 struct locale_data *current = loc->__locales[LC_NUMERIC];
469 if (__builtin_expect (group, 0))
471 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
472 if (*grouping <= 0 || *grouping == CHAR_MAX)
473 grouping = NULL;
474 else
476 /* Figure out the thousands separator character. */
477 #ifdef USE_WIDE_CHAR
478 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
479 _NL_NUMERIC_THOUSANDS_SEP_WC);
480 if (thousands == L'\0')
481 grouping = NULL;
482 #else
483 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
484 if (*thousands == '\0')
486 thousands = NULL;
487 grouping = NULL;
489 #endif
492 else
493 grouping = NULL;
495 /* Find the locale's decimal point character. */
496 #ifdef USE_WIDE_CHAR
497 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
498 assert (decimal != L'\0');
499 # define decimal_len 1
500 #else
501 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
502 decimal_len = strlen (decimal);
503 assert (decimal_len > 0);
504 #endif
506 /* Prepare number representation. */
507 exponent = 0;
508 negative = 0;
509 bits = 0;
511 /* Parse string to get maximal legal prefix. We need the number of
512 characters of the integer part, the fractional part and the exponent. */
513 cp = nptr - 1;
514 /* Ignore leading white space. */
516 c = *++cp;
517 while (ISSPACE (c));
519 /* Get sign of the result. */
520 if (c == L_('-'))
522 negative = 1;
523 c = *++cp;
525 else if (c == L_('+'))
526 c = *++cp;
528 /* Return 0.0 if no legal string is found.
529 No character is used even if a sign was found. */
530 #ifdef USE_WIDE_CHAR
531 if (c == (wint_t) decimal
532 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
534 /* We accept it. This funny construct is here only to indent
535 the code correctly. */
537 #else
538 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
539 if (cp[cnt] != decimal[cnt])
540 break;
541 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
543 /* We accept it. This funny construct is here only to indent
544 the code correctly. */
546 #endif
547 else if (c < L_('0') || c > L_('9'))
549 /* Check for `INF' or `INFINITY'. */
550 CHAR_TYPE lowc = TOLOWER_C (c);
552 if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
554 /* Return +/- infinity. */
555 if (endptr != NULL)
556 *endptr = (STRING_TYPE *)
557 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
558 ? 8 : 3));
560 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
563 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
565 /* Return NaN. */
566 FLOAT retval = NAN;
568 cp += 3;
570 /* Match `(n-char-sequence-digit)'. */
571 if (*cp == L_('('))
573 const STRING_TYPE *startp = cp;
575 ++cp;
576 while ((*cp >= L_('0') && *cp <= L_('9'))
577 || ({ CHAR_TYPE lo = TOLOWER (*cp);
578 lo >= L_('a') && lo <= L_('z'); })
579 || *cp == L_('_'));
581 if (*cp != L_(')'))
582 /* The closing brace is missing. Only match the NAN
583 part. */
584 cp = startp;
585 else
587 /* This is a system-dependent way to specify the
588 bitmask used for the NaN. We expect it to be
589 a number which is put in the mantissa of the
590 number. */
591 STRING_TYPE *endp;
592 unsigned long long int mant;
594 mant = STRTOULL (startp + 1, &endp, 0);
595 if (endp == cp)
596 SET_MANTISSA (retval, mant);
598 /* Consume the closing brace. */
599 ++cp;
603 if (endptr != NULL)
604 *endptr = (STRING_TYPE *) cp;
606 return retval;
609 /* It is really a text we do not recognize. */
610 RETURN (0.0, nptr);
613 /* First look whether we are faced with a hexadecimal number. */
614 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
616 /* Okay, it is a hexa-decimal number. Remember this and skip
617 the characters. BTW: hexadecimal numbers must not be
618 grouped. */
619 base = 16;
620 cp += 2;
621 c = *cp;
622 grouping = NULL;
625 /* Record the start of the digits, in case we will check their grouping. */
626 start_of_digits = startp = cp;
628 /* Ignore leading zeroes. This helps us to avoid useless computations. */
629 #ifdef USE_WIDE_CHAR
630 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
631 c = *++cp;
632 #else
633 if (__builtin_expect (thousands == NULL, 1))
634 while (c == '0')
635 c = *++cp;
636 else
638 /* We also have the multibyte thousands string. */
639 while (1)
641 if (c != '0')
643 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
644 if (thousands[cnt] != cp[cnt])
645 break;
646 if (thousands[cnt] != '\0')
647 break;
648 cp += cnt - 1;
650 c = *++cp;
653 #endif
655 /* If no other digit but a '0' is found the result is 0.0.
656 Return current read pointer. */
657 CHAR_TYPE lowc = TOLOWER (c);
658 if (!((c >= L_('0') && c <= L_('9'))
659 || (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
660 || (
661 #ifdef USE_WIDE_CHAR
662 c == (wint_t) decimal
663 #else
664 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
665 if (decimal[cnt] != cp[cnt])
666 break;
667 decimal[cnt] == '\0'; })
668 #endif
669 /* '0x.' alone is not a valid hexadecimal number.
670 '.' alone is not valid either, but that has been checked
671 already earlier. */
672 && (base != 16
673 || cp != start_of_digits
674 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
675 || ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
676 lo >= L_('a') && lo <= L_('f'); })))
677 || (base == 16 && (cp != start_of_digits
678 && lowc == L_('p')))
679 || (base != 16 && lowc == L_('e'))))
681 #ifdef USE_WIDE_CHAR
682 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
683 grouping);
684 #else
685 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
686 grouping);
687 #endif
688 /* If TP is at the start of the digits, there was no correctly
689 grouped prefix of the string; so no number found. */
690 RETURN (negative ? -0.0 : 0.0,
691 tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
694 /* Remember first significant digit and read following characters until the
695 decimal point, exponent character or any non-FP number character. */
696 startp = cp;
697 dig_no = 0;
698 while (1)
700 if ((c >= L_('0') && c <= L_('9'))
701 || (base == 16
702 && ({ CHAR_TYPE lo = TOLOWER (c);
703 lo >= L_('a') && lo <= L_('f'); })))
704 ++dig_no;
705 else
707 #ifdef USE_WIDE_CHAR
708 if (__builtin_expect ((wint_t) thousands == L'\0', 1)
709 || c != (wint_t) thousands)
710 /* Not a digit or separator: end of the integer part. */
711 break;
712 #else
713 if (__builtin_expect (thousands == NULL, 1))
714 break;
715 else
717 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
718 if (thousands[cnt] != cp[cnt])
719 break;
720 if (thousands[cnt] != '\0')
721 break;
722 cp += cnt - 1;
724 #endif
726 c = *++cp;
729 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
731 /* Check the grouping of the digits. */
732 #ifdef USE_WIDE_CHAR
733 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
734 grouping);
735 #else
736 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
737 grouping);
738 #endif
739 if (cp != tp)
741 /* Less than the entire string was correctly grouped. */
743 if (tp == start_of_digits)
744 /* No valid group of numbers at all: no valid number. */
745 RETURN (0.0, nptr);
747 if (tp < startp)
748 /* The number is validly grouped, but consists
749 only of zeroes. The whole value is zero. */
750 RETURN (negative ? -0.0 : 0.0, tp);
752 /* Recompute DIG_NO so we won't read more digits than
753 are properly grouped. */
754 cp = tp;
755 dig_no = 0;
756 for (tp = startp; tp < cp; ++tp)
757 if (*tp >= L_('0') && *tp <= L_('9'))
758 ++dig_no;
760 int_no = dig_no;
761 lead_zero = 0;
763 goto number_parsed;
767 /* We have the number of digits in the integer part. Whether these
768 are all or any is really a fractional digit will be decided
769 later. */
770 int_no = dig_no;
771 lead_zero = int_no == 0 ? -1 : 0;
773 /* Read the fractional digits. A special case are the 'american
774 style' numbers like `16.' i.e. with decimal point but without
775 trailing digits. */
776 if (
777 #ifdef USE_WIDE_CHAR
778 c == (wint_t) decimal
779 #else
780 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
781 if (decimal[cnt] != cp[cnt])
782 break;
783 decimal[cnt] == '\0'; })
784 #endif
787 cp += decimal_len;
788 c = *cp;
789 while ((c >= L_('0') && c <= L_('9')) ||
790 (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
791 lo >= L_('a') && lo <= L_('f'); })))
793 if (c != L_('0') && lead_zero == -1)
794 lead_zero = dig_no - int_no;
795 ++dig_no;
796 c = *++cp;
800 /* Remember start of exponent (if any). */
801 expp = cp;
803 /* Read exponent. */
804 lowc = TOLOWER (c);
805 if ((base == 16 && lowc == L_('p'))
806 || (base != 16 && lowc == L_('e')))
808 int exp_negative = 0;
810 c = *++cp;
811 if (c == L_('-'))
813 exp_negative = 1;
814 c = *++cp;
816 else if (c == L_('+'))
817 c = *++cp;
819 if (c >= L_('0') && c <= L_('9'))
821 int exp_limit;
823 /* Get the exponent limit. */
824 if (base == 16)
825 exp_limit = (exp_negative ?
826 -MIN_EXP + MANT_DIG + 4 * int_no :
827 MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
828 else
829 exp_limit = (exp_negative ?
830 -MIN_10_EXP + MANT_DIG + int_no :
831 MAX_10_EXP - int_no + lead_zero + 1);
835 exponent *= 10;
836 exponent += c - L_('0');
838 if (__builtin_expect (exponent > exp_limit, 0))
839 /* The exponent is too large/small to represent a valid
840 number. */
842 FLOAT result;
844 /* We have to take care for special situation: a joker
845 might have written "0.0e100000" which is in fact
846 zero. */
847 if (lead_zero == -1)
848 result = negative ? -0.0 : 0.0;
849 else
851 /* Overflow or underflow. */
852 __set_errno (ERANGE);
853 result = (exp_negative ? (negative ? -0.0 : 0.0) :
854 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
857 /* Accept all following digits as part of the exponent. */
859 ++cp;
860 while (*cp >= L_('0') && *cp <= L_('9'));
862 RETURN (result, cp);
863 /* NOTREACHED */
866 c = *++cp;
868 while (c >= L_('0') && c <= L_('9'));
870 if (exp_negative)
871 exponent = -exponent;
873 else
874 cp = expp;
877 /* We don't want to have to work with trailing zeroes after the radix. */
878 if (dig_no > int_no)
880 while (expp[-1] == L_('0'))
882 --expp;
883 --dig_no;
885 assert (dig_no >= int_no);
888 if (dig_no == int_no && dig_no > 0 && exponent < 0)
891 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
892 --expp;
894 if (expp[-1] != L_('0'))
895 break;
897 --expp;
898 --dig_no;
899 --int_no;
900 exponent += base == 16 ? 4 : 1;
902 while (dig_no > 0 && exponent < 0);
904 number_parsed:
906 /* The whole string is parsed. Store the address of the next character. */
907 if (endptr)
908 *endptr = (STRING_TYPE *) cp;
910 if (dig_no == 0)
911 return negative ? -0.0 : 0.0;
913 if (lead_zero)
915 /* Find the decimal point */
916 #ifdef USE_WIDE_CHAR
917 while (*startp != decimal)
918 ++startp;
919 #else
920 while (1)
922 if (*startp == decimal[0])
924 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
925 if (decimal[cnt] != startp[cnt])
926 break;
927 if (decimal[cnt] == '\0')
928 break;
930 ++startp;
932 #endif
933 startp += lead_zero + decimal_len;
934 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
935 dig_no -= lead_zero;
938 /* If the BASE is 16 we can use a simpler algorithm. */
939 if (base == 16)
941 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
942 4, 4, 4, 4, 4, 4, 4, 4 };
943 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
944 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
945 mp_limb_t val;
947 while (!ISXDIGIT (*startp))
948 ++startp;
949 while (*startp == L_('0'))
950 ++startp;
951 if (ISDIGIT (*startp))
952 val = *startp++ - L_('0');
953 else
954 val = 10 + TOLOWER (*startp++) - L_('a');
955 bits = nbits[val];
956 /* We cannot have a leading zero. */
957 assert (bits != 0);
959 if (pos + 1 >= 4 || pos + 1 >= bits)
961 /* We don't have to care for wrapping. This is the normal
962 case so we add the first clause in the `if' expression as
963 an optimization. It is a compile-time constant and so does
964 not cost anything. */
965 retval[idx] = val << (pos - bits + 1);
966 pos -= bits;
968 else
970 retval[idx--] = val >> (bits - pos - 1);
971 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
972 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
975 /* Adjust the exponent for the bits we are shifting in. */
976 exponent += bits - 1 + (int_no - 1) * 4;
978 while (--dig_no > 0 && idx >= 0)
980 if (!ISXDIGIT (*startp))
981 startp += decimal_len;
982 if (ISDIGIT (*startp))
983 val = *startp++ - L_('0');
984 else
985 val = 10 + TOLOWER (*startp++) - L_('a');
987 if (pos + 1 >= 4)
989 retval[idx] |= val << (pos - 4 + 1);
990 pos -= 4;
992 else
994 retval[idx--] |= val >> (4 - pos - 1);
995 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
996 if (idx < 0)
997 return round_and_return (retval, exponent, negative, val,
998 BITS_PER_MP_LIMB - 1, dig_no > 0);
1000 retval[idx] = val;
1001 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1005 /* We ran out of digits. */
1006 MPN_ZERO (retval, idx);
1008 return round_and_return (retval, exponent, negative, 0, 0, 0);
1011 /* Now we have the number of digits in total and the integer digits as well
1012 as the exponent and its sign. We can decide whether the read digits are
1013 really integer digits or belong to the fractional part; i.e. we normalize
1014 123e-2 to 1.23. */
1016 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1017 : MIN (dig_no - int_no, exponent));
1018 int_no += incr;
1019 exponent -= incr;
1022 if (__builtin_expect (int_no + exponent > MAX_10_EXP + 1, 0))
1024 __set_errno (ERANGE);
1025 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1028 if (__builtin_expect (exponent < MIN_10_EXP - (DIG + 1), 0))
1030 __set_errno (ERANGE);
1031 return negative ? -0.0 : 0.0;
1034 if (int_no > 0)
1036 /* Read the integer part as a multi-precision number to NUM. */
1037 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1038 #ifndef USE_WIDE_CHAR
1039 , decimal, decimal_len, thousands
1040 #endif
1043 if (exponent > 0)
1045 /* We now multiply the gained number by the given power of ten. */
1046 mp_limb_t *psrc = num;
1047 mp_limb_t *pdest = den;
1048 int expbit = 1;
1049 const struct mp_power *ttab = &_fpioconst_pow10[0];
1053 if ((exponent & expbit) != 0)
1055 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1056 mp_limb_t cy;
1057 exponent ^= expbit;
1059 /* FIXME: not the whole multiplication has to be
1060 done. If we have the needed number of bits we
1061 only need the information whether more non-zero
1062 bits follow. */
1063 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1064 cy = __mpn_mul (pdest, psrc, numsize,
1065 &__tens[ttab->arrayoff
1066 + _FPIO_CONST_OFFSET],
1067 size);
1068 else
1069 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1070 + _FPIO_CONST_OFFSET],
1071 size, psrc, numsize);
1072 numsize += size;
1073 if (cy == 0)
1074 --numsize;
1075 (void) SWAP (psrc, pdest);
1077 expbit <<= 1;
1078 ++ttab;
1080 while (exponent != 0);
1082 if (psrc == den)
1083 memcpy (num, den, numsize * sizeof (mp_limb_t));
1086 /* Determine how many bits of the result we already have. */
1087 count_leading_zeros (bits, num[numsize - 1]);
1088 bits = numsize * BITS_PER_MP_LIMB - bits;
1090 /* Now we know the exponent of the number in base two.
1091 Check it against the maximum possible exponent. */
1092 if (__builtin_expect (bits > MAX_EXP, 0))
1094 __set_errno (ERANGE);
1095 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1098 /* We have already the first BITS bits of the result. Together with
1099 the information whether more non-zero bits follow this is enough
1100 to determine the result. */
1101 if (bits > MANT_DIG)
1103 int i;
1104 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1105 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1106 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1107 : least_idx;
1108 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1109 : least_bit - 1;
1111 if (least_bit == 0)
1112 memcpy (retval, &num[least_idx],
1113 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1114 else
1116 for (i = least_idx; i < numsize - 1; ++i)
1117 retval[i - least_idx] = (num[i] >> least_bit)
1118 | (num[i + 1]
1119 << (BITS_PER_MP_LIMB - least_bit));
1120 if (i - least_idx < RETURN_LIMB_SIZE)
1121 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1124 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1125 for (i = 0; num[i] == 0; ++i)
1128 return round_and_return (retval, bits - 1, negative,
1129 num[round_idx], round_bit,
1130 int_no < dig_no || i < round_idx);
1131 /* NOTREACHED */
1133 else if (dig_no == int_no)
1135 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1136 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1138 if (target_bit == is_bit)
1140 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1141 numsize * sizeof (mp_limb_t));
1142 /* FIXME: the following loop can be avoided if we assume a
1143 maximal MANT_DIG value. */
1144 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1146 else if (target_bit > is_bit)
1148 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1149 num, numsize, target_bit - is_bit);
1150 /* FIXME: the following loop can be avoided if we assume a
1151 maximal MANT_DIG value. */
1152 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1154 else
1156 mp_limb_t cy;
1157 assert (numsize < RETURN_LIMB_SIZE);
1159 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1160 num, numsize, is_bit - target_bit);
1161 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1162 /* FIXME: the following loop can be avoided if we assume a
1163 maximal MANT_DIG value. */
1164 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1167 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1168 /* NOTREACHED */
1171 /* Store the bits we already have. */
1172 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1173 #if RETURN_LIMB_SIZE > 1
1174 if (numsize < RETURN_LIMB_SIZE)
1175 # if RETURN_LIMB_SIZE == 2
1176 retval[numsize] = 0;
1177 # else
1178 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1179 # endif
1180 #endif
1183 /* We have to compute at least some of the fractional digits. */
1185 /* We construct a fraction and the result of the division gives us
1186 the needed digits. The denominator is 1.0 multiplied by the
1187 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1188 123e-6 gives 123 / 1000000. */
1190 int expbit;
1191 int neg_exp;
1192 int more_bits;
1193 mp_limb_t cy;
1194 mp_limb_t *psrc = den;
1195 mp_limb_t *pdest = num;
1196 const struct mp_power *ttab = &_fpioconst_pow10[0];
1198 assert (dig_no > int_no && exponent <= 0);
1201 /* For the fractional part we need not process too many digits. One
1202 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1203 ceil(BITS / 3) =: N
1204 digits we should have enough bits for the result. The remaining
1205 decimal digits give us the information that more bits are following.
1206 This can be used while rounding. (Two added as a safety margin.) */
1207 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1209 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1210 more_bits = 1;
1212 else
1213 more_bits = 0;
1215 neg_exp = dig_no - int_no - exponent;
1217 /* Construct the denominator. */
1218 densize = 0;
1219 expbit = 1;
1222 if ((neg_exp & expbit) != 0)
1224 mp_limb_t cy;
1225 neg_exp ^= expbit;
1227 if (densize == 0)
1229 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1230 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1231 densize * sizeof (mp_limb_t));
1233 else
1235 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1236 + _FPIO_CONST_OFFSET],
1237 ttab->arraysize - _FPIO_CONST_OFFSET,
1238 psrc, densize);
1239 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1240 if (cy == 0)
1241 --densize;
1242 (void) SWAP (psrc, pdest);
1245 expbit <<= 1;
1246 ++ttab;
1248 while (neg_exp != 0);
1250 if (psrc == num)
1251 memcpy (den, num, densize * sizeof (mp_limb_t));
1253 /* Read the fractional digits from the string. */
1254 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1255 #ifndef USE_WIDE_CHAR
1256 , decimal, decimal_len, thousands
1257 #endif
1260 /* We now have to shift both numbers so that the highest bit in the
1261 denominator is set. In the same process we copy the numerator to
1262 a high place in the array so that the division constructs the wanted
1263 digits. This is done by a "quasi fix point" number representation.
1265 num: ddddddddddd . 0000000000000000000000
1266 |--- m ---|
1267 den: ddddddddddd n >= m
1268 |--- n ---|
1271 count_leading_zeros (cnt, den[densize - 1]);
1273 if (cnt > 0)
1275 /* Don't call `mpn_shift' with a count of zero since the specification
1276 does not allow this. */
1277 (void) __mpn_lshift (den, den, densize, cnt);
1278 cy = __mpn_lshift (num, num, numsize, cnt);
1279 if (cy != 0)
1280 num[numsize++] = cy;
1283 /* Now we are ready for the division. But it is not necessary to
1284 do a full multi-precision division because we only need a small
1285 number of bits for the result. So we do not use __mpn_divmod
1286 here but instead do the division here by hand and stop whenever
1287 the needed number of bits is reached. The code itself comes
1288 from the GNU MP Library by Torbj\"orn Granlund. */
1290 exponent = bits;
1292 switch (densize)
1294 case 1:
1296 mp_limb_t d, n, quot;
1297 int used = 0;
1299 n = num[0];
1300 d = den[0];
1301 assert (numsize == 1 && n < d);
1305 udiv_qrnnd (quot, n, n, 0, d);
1307 #define got_limb \
1308 if (bits == 0) \
1310 register int cnt; \
1311 if (quot == 0) \
1312 cnt = BITS_PER_MP_LIMB; \
1313 else \
1314 count_leading_zeros (cnt, quot); \
1315 exponent -= cnt; \
1316 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1318 used = MANT_DIG + cnt; \
1319 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1320 bits = MANT_DIG + 1; \
1322 else \
1324 /* Note that we only clear the second element. */ \
1325 /* The conditional is determined at compile time. */ \
1326 if (RETURN_LIMB_SIZE > 1) \
1327 retval[1] = 0; \
1328 retval[0] = quot; \
1329 bits = -cnt; \
1332 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1333 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1334 quot); \
1335 else \
1337 used = MANT_DIG - bits; \
1338 if (used > 0) \
1339 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1341 bits += BITS_PER_MP_LIMB
1343 got_limb;
1345 while (bits <= MANT_DIG);
1347 return round_and_return (retval, exponent - 1, negative,
1348 quot, BITS_PER_MP_LIMB - 1 - used,
1349 more_bits || n != 0);
1351 case 2:
1353 mp_limb_t d0, d1, n0, n1;
1354 mp_limb_t quot = 0;
1355 int used = 0;
1357 d0 = den[0];
1358 d1 = den[1];
1360 if (numsize < densize)
1362 if (num[0] >= d1)
1364 /* The numerator of the number occupies fewer bits than
1365 the denominator but the one limb is bigger than the
1366 high limb of the numerator. */
1367 n1 = 0;
1368 n0 = num[0];
1370 else
1372 if (bits <= 0)
1373 exponent -= BITS_PER_MP_LIMB;
1374 else
1376 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1377 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1378 BITS_PER_MP_LIMB, 0);
1379 else
1381 used = MANT_DIG - bits;
1382 if (used > 0)
1383 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1385 bits += BITS_PER_MP_LIMB;
1387 n1 = num[0];
1388 n0 = 0;
1391 else
1393 n1 = num[1];
1394 n0 = num[0];
1397 while (bits <= MANT_DIG)
1399 mp_limb_t r;
1401 if (n1 == d1)
1403 /* QUOT should be either 111..111 or 111..110. We need
1404 special treatment of this rare case as normal division
1405 would give overflow. */
1406 quot = ~(mp_limb_t) 0;
1408 r = n0 + d1;
1409 if (r < d1) /* Carry in the addition? */
1411 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1412 goto have_quot;
1414 n1 = d0 - (d0 != 0);
1415 n0 = -d0;
1417 else
1419 udiv_qrnnd (quot, r, n1, n0, d1);
1420 umul_ppmm (n1, n0, d0, quot);
1423 q_test:
1424 if (n1 > r || (n1 == r && n0 > 0))
1426 /* The estimated QUOT was too large. */
1427 --quot;
1429 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1430 r += d1;
1431 if (r >= d1) /* If not carry, test QUOT again. */
1432 goto q_test;
1434 sub_ddmmss (n1, n0, r, 0, n1, n0);
1436 have_quot:
1437 got_limb;
1440 return round_and_return (retval, exponent - 1, negative,
1441 quot, BITS_PER_MP_LIMB - 1 - used,
1442 more_bits || n1 != 0 || n0 != 0);
1444 default:
1446 int i;
1447 mp_limb_t cy, dX, d1, n0, n1;
1448 mp_limb_t quot = 0;
1449 int used = 0;
1451 dX = den[densize - 1];
1452 d1 = den[densize - 2];
1454 /* The division does not work if the upper limb of the two-limb
1455 numerator is greater than the denominator. */
1456 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1457 num[numsize++] = 0;
1459 if (numsize < densize)
1461 mp_size_t empty = densize - numsize;
1462 register int i;
1464 if (bits <= 0)
1465 exponent -= empty * BITS_PER_MP_LIMB;
1466 else
1468 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1470 /* We make a difference here because the compiler
1471 cannot optimize the `else' case that good and
1472 this reflects all currently used FLOAT types
1473 and GMP implementations. */
1474 #if RETURN_LIMB_SIZE <= 2
1475 assert (empty == 1);
1476 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1477 BITS_PER_MP_LIMB, 0);
1478 #else
1479 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1480 retval[i] = retval[i - empty];
1481 while (i >= 0)
1482 retval[i--] = 0;
1483 #endif
1485 else
1487 used = MANT_DIG - bits;
1488 if (used >= BITS_PER_MP_LIMB)
1490 register int i;
1491 (void) __mpn_lshift (&retval[used
1492 / BITS_PER_MP_LIMB],
1493 retval, RETURN_LIMB_SIZE,
1494 used % BITS_PER_MP_LIMB);
1495 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1496 retval[i] = 0;
1498 else if (used > 0)
1499 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1501 bits += empty * BITS_PER_MP_LIMB;
1503 for (i = numsize; i > 0; --i)
1504 num[i + empty] = num[i - 1];
1505 MPN_ZERO (num, empty + 1);
1507 else
1509 int i;
1510 assert (numsize == densize);
1511 for (i = numsize; i > 0; --i)
1512 num[i] = num[i - 1];
1515 den[densize] = 0;
1516 n0 = num[densize];
1518 while (bits <= MANT_DIG)
1520 if (n0 == dX)
1521 /* This might over-estimate QUOT, but it's probably not
1522 worth the extra code here to find out. */
1523 quot = ~(mp_limb_t) 0;
1524 else
1526 mp_limb_t r;
1528 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1529 umul_ppmm (n1, n0, d1, quot);
1531 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1533 --quot;
1534 r += dX;
1535 if (r < dX) /* I.e. "carry in previous addition?" */
1536 break;
1537 n1 -= n0 < d1;
1538 n0 -= d1;
1542 /* Possible optimization: We already have (q * n0) and (1 * n1)
1543 after the calculation of QUOT. Taking advantage of this, we
1544 could make this loop make two iterations less. */
1546 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1548 if (num[densize] != cy)
1550 cy = __mpn_add_n (num, num, den, densize);
1551 assert (cy != 0);
1552 --quot;
1554 n0 = num[densize] = num[densize - 1];
1555 for (i = densize - 1; i > 0; --i)
1556 num[i] = num[i - 1];
1558 got_limb;
1561 for (i = densize; num[i] == 0 && i >= 0; --i)
1563 return round_and_return (retval, exponent - 1, negative,
1564 quot, BITS_PER_MP_LIMB - 1 - used,
1565 more_bits || i >= 0);
1570 /* NOTREACHED */
1572 #if defined _LIBC && !defined USE_WIDE_CHAR
1573 libc_hidden_def (____STRTOF_INTERNAL)
1574 #endif
1576 /* External user entry point. */
1578 FLOAT
1579 #ifdef weak_function
1580 weak_function
1581 #endif
1582 __STRTOF (nptr, endptr, loc)
1583 const STRING_TYPE *nptr;
1584 STRING_TYPE **endptr;
1585 __locale_t loc;
1587 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1589 #if defined _LIBC
1590 libc_hidden_def (__STRTOF)
1591 libc_hidden_ver (__STRTOF, STRTOF)
1592 #endif
1593 weak_alias (__STRTOF, STRTOF)
1595 #ifdef LONG_DOUBLE_COMPAT
1596 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1597 # ifdef USE_WIDE_CHAR
1598 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1599 # else
1600 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1601 # endif
1602 # endif
1603 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1604 # ifdef USE_WIDE_CHAR
1605 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1606 # else
1607 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1608 # endif
1609 # endif
1610 #endif