Define NO_COMPAT_NEEDED.
[glibc/history.git] / stdlib / strtod_l.c
blob9ddfa3fa6635af51c59f4cd55f248e910a602a05
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006,2007,2008,2009
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;
235 __set_errno (ERANGE);
238 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
239 && (more_bits || (retval[0] & 1) != 0
240 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
242 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
244 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
245 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
246 (retval[RETURN_LIMB_SIZE - 1]
247 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
249 ++exponent;
250 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
251 retval[RETURN_LIMB_SIZE - 1]
252 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
254 else if (exponent == DENORM_EXP
255 && (retval[RETURN_LIMB_SIZE - 1]
256 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
257 != 0)
258 /* The number was denormalized but now normalized. */
259 exponent = MIN_EXP - 1;
262 if (exponent > MAX_EXP)
263 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
265 return MPN2FLOAT (retval, exponent, negative);
269 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
270 into N. Return the size of the number limbs in NSIZE at the first
271 character od the string that is not part of the integer as the function
272 value. If the EXPONENT is small enough to be taken as an additional
273 factor for the resulting number (see code) multiply by it. */
274 static const STRING_TYPE *
275 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
276 int *exponent
277 #ifndef USE_WIDE_CHAR
278 , const char *decimal, size_t decimal_len, const char *thousands
279 #endif
283 /* Number of digits for actual limb. */
284 int cnt = 0;
285 mp_limb_t low = 0;
286 mp_limb_t start;
288 *nsize = 0;
289 assert (digcnt > 0);
292 if (cnt == MAX_DIG_PER_LIMB)
294 if (*nsize == 0)
296 n[0] = low;
297 *nsize = 1;
299 else
301 mp_limb_t cy;
302 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
303 cy += __mpn_add_1 (n, n, *nsize, low);
304 if (cy != 0)
306 n[*nsize] = cy;
307 ++(*nsize);
310 cnt = 0;
311 low = 0;
314 /* There might be thousands separators or radix characters in
315 the string. But these all can be ignored because we know the
316 format of the number is correct and we have an exact number
317 of characters to read. */
318 #ifdef USE_WIDE_CHAR
319 if (*str < L'0' || *str > L'9')
320 ++str;
321 #else
322 if (*str < '0' || *str > '9')
324 int inner = 0;
325 if (thousands != NULL && *str == *thousands
326 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
327 if (thousands[inner] != str[inner])
328 break;
329 thousands[inner] == '\0'; }))
330 str += inner;
331 else
332 str += decimal_len;
334 #endif
335 low = low * 10 + *str++ - L_('0');
336 ++cnt;
338 while (--digcnt > 0);
340 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
342 low *= _tens_in_limb[*exponent];
343 start = _tens_in_limb[cnt + *exponent];
344 *exponent = 0;
346 else
347 start = _tens_in_limb[cnt];
349 if (*nsize == 0)
351 n[0] = low;
352 *nsize = 1;
354 else
356 mp_limb_t cy;
357 cy = __mpn_mul_1 (n, n, *nsize, start);
358 cy += __mpn_add_1 (n, n, *nsize, low);
359 if (cy != 0)
360 n[(*nsize)++] = cy;
363 return str;
367 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
368 with the COUNT most significant bits of LIMB.
370 Tege doesn't like this function so I have to write it here myself. :)
371 --drepper */
372 static inline void
373 __attribute ((always_inline))
374 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
375 mp_limb_t limb)
377 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
379 /* Optimize the case of shifting by exactly a word:
380 just copy words, with no actual bit-shifting. */
381 mp_size_t i;
382 for (i = size - 1; i > 0; --i)
383 ptr[i] = ptr[i - 1];
384 ptr[0] = limb;
386 else
388 (void) __mpn_lshift (ptr, ptr, size, count);
389 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
394 #define INTERNAL(x) INTERNAL1(x)
395 #define INTERNAL1(x) __##x##_internal
396 #ifndef ____STRTOF_INTERNAL
397 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
398 #endif
400 /* This file defines a function to check for correct grouping. */
401 #include "grouping.h"
404 /* Return a floating point number with the value of the given string NPTR.
405 Set *ENDPTR to the character after the last used one. If the number is
406 smaller than the smallest representable number, set `errno' to ERANGE and
407 return 0.0. If the number is too big to be represented, set `errno' to
408 ERANGE and return HUGE_VAL with the appropriate sign. */
409 FLOAT
410 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
411 const STRING_TYPE *nptr;
412 STRING_TYPE **endptr;
413 int group;
414 __locale_t loc;
416 int negative; /* The sign of the number. */
417 MPN_VAR (num); /* MP representation of the number. */
418 int exponent; /* Exponent of the number. */
420 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
421 int base = 10;
423 /* When we have to compute fractional digits we form a fraction with a
424 second multi-precision number (and we sometimes need a second for
425 temporary results). */
426 MPN_VAR (den);
428 /* Representation for the return value. */
429 mp_limb_t retval[RETURN_LIMB_SIZE];
430 /* Number of bits currently in result value. */
431 int bits;
433 /* Running pointer after the last character processed in the string. */
434 const STRING_TYPE *cp, *tp;
435 /* Start of significant part of the number. */
436 const STRING_TYPE *startp, *start_of_digits;
437 /* Points at the character following the integer and fractional digits. */
438 const STRING_TYPE *expp;
439 /* Total number of digit and number of digits in integer part. */
440 int dig_no, int_no, lead_zero;
441 /* Contains the last character read. */
442 CHAR_TYPE c;
444 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
445 there. So define it ourselves if it remains undefined. */
446 #ifndef _WINT_T
447 typedef unsigned int wint_t;
448 #endif
449 /* The radix character of the current locale. */
450 #ifdef USE_WIDE_CHAR
451 wchar_t decimal;
452 #else
453 const char *decimal;
454 size_t decimal_len;
455 #endif
456 /* The thousands character of the current locale. */
457 #ifdef USE_WIDE_CHAR
458 wchar_t thousands = L'\0';
459 #else
460 const char *thousands = NULL;
461 #endif
462 /* The numeric grouping specification of the current locale,
463 in the format described in <locale.h>. */
464 const char *grouping;
465 /* Used in several places. */
466 int cnt;
468 struct locale_data *current = loc->__locales[LC_NUMERIC];
470 if (__builtin_expect (group, 0))
472 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
473 if (*grouping <= 0 || *grouping == CHAR_MAX)
474 grouping = NULL;
475 else
477 /* Figure out the thousands separator character. */
478 #ifdef USE_WIDE_CHAR
479 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
480 _NL_NUMERIC_THOUSANDS_SEP_WC);
481 if (thousands == L'\0')
482 grouping = NULL;
483 #else
484 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
485 if (*thousands == '\0')
487 thousands = NULL;
488 grouping = NULL;
490 #endif
493 else
494 grouping = NULL;
496 /* Find the locale's decimal point character. */
497 #ifdef USE_WIDE_CHAR
498 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
499 assert (decimal != L'\0');
500 # define decimal_len 1
501 #else
502 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
503 decimal_len = strlen (decimal);
504 assert (decimal_len > 0);
505 #endif
507 /* Prepare number representation. */
508 exponent = 0;
509 negative = 0;
510 bits = 0;
512 /* Parse string to get maximal legal prefix. We need the number of
513 characters of the integer part, the fractional part and the exponent. */
514 cp = nptr - 1;
515 /* Ignore leading white space. */
517 c = *++cp;
518 while (ISSPACE (c));
520 /* Get sign of the result. */
521 if (c == L_('-'))
523 negative = 1;
524 c = *++cp;
526 else if (c == L_('+'))
527 c = *++cp;
529 /* Return 0.0 if no legal string is found.
530 No character is used even if a sign was found. */
531 #ifdef USE_WIDE_CHAR
532 if (c == (wint_t) decimal
533 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
535 /* We accept it. This funny construct is here only to indent
536 the code correctly. */
538 #else
539 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
540 if (cp[cnt] != decimal[cnt])
541 break;
542 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
544 /* We accept it. This funny construct is here only to indent
545 the code correctly. */
547 #endif
548 else if (c < L_('0') || c > L_('9'))
550 /* Check for `INF' or `INFINITY'. */
551 CHAR_TYPE lowc = TOLOWER_C (c);
553 if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
555 /* Return +/- infinity. */
556 if (endptr != NULL)
557 *endptr = (STRING_TYPE *)
558 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
559 ? 8 : 3));
561 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
564 if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
566 /* Return NaN. */
567 FLOAT retval = NAN;
569 cp += 3;
571 /* Match `(n-char-sequence-digit)'. */
572 if (*cp == L_('('))
574 const STRING_TYPE *startp = cp;
576 ++cp;
577 while ((*cp >= L_('0') && *cp <= L_('9'))
578 || ({ CHAR_TYPE lo = TOLOWER (*cp);
579 lo >= L_('a') && lo <= L_('z'); })
580 || *cp == L_('_'));
582 if (*cp != L_(')'))
583 /* The closing brace is missing. Only match the NAN
584 part. */
585 cp = startp;
586 else
588 /* This is a system-dependent way to specify the
589 bitmask used for the NaN. We expect it to be
590 a number which is put in the mantissa of the
591 number. */
592 STRING_TYPE *endp;
593 unsigned long long int mant;
595 mant = STRTOULL (startp + 1, &endp, 0);
596 if (endp == cp)
597 SET_MANTISSA (retval, mant);
599 /* Consume the closing brace. */
600 ++cp;
604 if (endptr != NULL)
605 *endptr = (STRING_TYPE *) cp;
607 return retval;
610 /* It is really a text we do not recognize. */
611 RETURN (0.0, nptr);
614 /* First look whether we are faced with a hexadecimal number. */
615 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
617 /* Okay, it is a hexa-decimal number. Remember this and skip
618 the characters. BTW: hexadecimal numbers must not be
619 grouped. */
620 base = 16;
621 cp += 2;
622 c = *cp;
623 grouping = NULL;
626 /* Record the start of the digits, in case we will check their grouping. */
627 start_of_digits = startp = cp;
629 /* Ignore leading zeroes. This helps us to avoid useless computations. */
630 #ifdef USE_WIDE_CHAR
631 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
632 c = *++cp;
633 #else
634 if (__builtin_expect (thousands == NULL, 1))
635 while (c == '0')
636 c = *++cp;
637 else
639 /* We also have the multibyte thousands string. */
640 while (1)
642 if (c != '0')
644 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
645 if (thousands[cnt] != cp[cnt])
646 break;
647 if (thousands[cnt] != '\0')
648 break;
649 cp += cnt - 1;
651 c = *++cp;
654 #endif
656 /* If no other digit but a '0' is found the result is 0.0.
657 Return current read pointer. */
658 CHAR_TYPE lowc = TOLOWER (c);
659 if (!((c >= L_('0') && c <= L_('9'))
660 || (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
661 || (
662 #ifdef USE_WIDE_CHAR
663 c == (wint_t) decimal
664 #else
665 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
666 if (decimal[cnt] != cp[cnt])
667 break;
668 decimal[cnt] == '\0'; })
669 #endif
670 /* '0x.' alone is not a valid hexadecimal number.
671 '.' alone is not valid either, but that has been checked
672 already earlier. */
673 && (base != 16
674 || cp != start_of_digits
675 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
676 || ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
677 lo >= L_('a') && lo <= L_('f'); })))
678 || (base == 16 && (cp != start_of_digits
679 && lowc == L_('p')))
680 || (base != 16 && lowc == L_('e'))))
682 #ifdef USE_WIDE_CHAR
683 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
684 grouping);
685 #else
686 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
687 grouping);
688 #endif
689 /* If TP is at the start of the digits, there was no correctly
690 grouped prefix of the string; so no number found. */
691 RETURN (negative ? -0.0 : 0.0,
692 tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
695 /* Remember first significant digit and read following characters until the
696 decimal point, exponent character or any non-FP number character. */
697 startp = cp;
698 dig_no = 0;
699 while (1)
701 if ((c >= L_('0') && c <= L_('9'))
702 || (base == 16
703 && ({ CHAR_TYPE lo = TOLOWER (c);
704 lo >= L_('a') && lo <= L_('f'); })))
705 ++dig_no;
706 else
708 #ifdef USE_WIDE_CHAR
709 if (__builtin_expect ((wint_t) thousands == L'\0', 1)
710 || c != (wint_t) thousands)
711 /* Not a digit or separator: end of the integer part. */
712 break;
713 #else
714 if (__builtin_expect (thousands == NULL, 1))
715 break;
716 else
718 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
719 if (thousands[cnt] != cp[cnt])
720 break;
721 if (thousands[cnt] != '\0')
722 break;
723 cp += cnt - 1;
725 #endif
727 c = *++cp;
730 if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
732 /* Check the grouping of the digits. */
733 #ifdef USE_WIDE_CHAR
734 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
735 grouping);
736 #else
737 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
738 grouping);
739 #endif
740 if (cp != tp)
742 /* Less than the entire string was correctly grouped. */
744 if (tp == start_of_digits)
745 /* No valid group of numbers at all: no valid number. */
746 RETURN (0.0, nptr);
748 if (tp < startp)
749 /* The number is validly grouped, but consists
750 only of zeroes. The whole value is zero. */
751 RETURN (negative ? -0.0 : 0.0, tp);
753 /* Recompute DIG_NO so we won't read more digits than
754 are properly grouped. */
755 cp = tp;
756 dig_no = 0;
757 for (tp = startp; tp < cp; ++tp)
758 if (*tp >= L_('0') && *tp <= L_('9'))
759 ++dig_no;
761 int_no = dig_no;
762 lead_zero = 0;
764 goto number_parsed;
768 /* We have the number of digits in the integer part. Whether these
769 are all or any is really a fractional digit will be decided
770 later. */
771 int_no = dig_no;
772 lead_zero = int_no == 0 ? -1 : 0;
774 /* Read the fractional digits. A special case are the 'american
775 style' numbers like `16.' i.e. with decimal point but without
776 trailing digits. */
777 if (
778 #ifdef USE_WIDE_CHAR
779 c == (wint_t) decimal
780 #else
781 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
782 if (decimal[cnt] != cp[cnt])
783 break;
784 decimal[cnt] == '\0'; })
785 #endif
788 cp += decimal_len;
789 c = *cp;
790 while ((c >= L_('0') && c <= L_('9')) ||
791 (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
792 lo >= L_('a') && lo <= L_('f'); })))
794 if (c != L_('0') && lead_zero == -1)
795 lead_zero = dig_no - int_no;
796 ++dig_no;
797 c = *++cp;
801 /* Remember start of exponent (if any). */
802 expp = cp;
804 /* Read exponent. */
805 lowc = TOLOWER (c);
806 if ((base == 16 && lowc == L_('p'))
807 || (base != 16 && lowc == L_('e')))
809 int exp_negative = 0;
811 c = *++cp;
812 if (c == L_('-'))
814 exp_negative = 1;
815 c = *++cp;
817 else if (c == L_('+'))
818 c = *++cp;
820 if (c >= L_('0') && c <= L_('9'))
822 int exp_limit;
824 /* Get the exponent limit. */
825 if (base == 16)
826 exp_limit = (exp_negative ?
827 -MIN_EXP + MANT_DIG + 4 * int_no :
828 MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
829 else
830 exp_limit = (exp_negative ?
831 -MIN_10_EXP + MANT_DIG + int_no :
832 MAX_10_EXP - int_no + lead_zero + 1);
836 exponent *= 10;
837 exponent += c - L_('0');
839 if (__builtin_expect (exponent > exp_limit, 0))
840 /* The exponent is too large/small to represent a valid
841 number. */
843 FLOAT result;
845 /* We have to take care for special situation: a joker
846 might have written "0.0e100000" which is in fact
847 zero. */
848 if (lead_zero == -1)
849 result = negative ? -0.0 : 0.0;
850 else
852 /* Overflow or underflow. */
853 __set_errno (ERANGE);
854 result = (exp_negative ? (negative ? -0.0 : 0.0) :
855 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
858 /* Accept all following digits as part of the exponent. */
860 ++cp;
861 while (*cp >= L_('0') && *cp <= L_('9'));
863 RETURN (result, cp);
864 /* NOTREACHED */
867 c = *++cp;
869 while (c >= L_('0') && c <= L_('9'));
871 if (exp_negative)
872 exponent = -exponent;
874 else
875 cp = expp;
878 /* We don't want to have to work with trailing zeroes after the radix. */
879 if (dig_no > int_no)
881 while (expp[-1] == L_('0'))
883 --expp;
884 --dig_no;
886 assert (dig_no >= int_no);
889 if (dig_no == int_no && dig_no > 0 && exponent < 0)
892 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
893 --expp;
895 if (expp[-1] != L_('0'))
896 break;
898 --expp;
899 --dig_no;
900 --int_no;
901 exponent += base == 16 ? 4 : 1;
903 while (dig_no > 0 && exponent < 0);
905 number_parsed:
907 /* The whole string is parsed. Store the address of the next character. */
908 if (endptr)
909 *endptr = (STRING_TYPE *) cp;
911 if (dig_no == 0)
912 return negative ? -0.0 : 0.0;
914 if (lead_zero)
916 /* Find the decimal point */
917 #ifdef USE_WIDE_CHAR
918 while (*startp != decimal)
919 ++startp;
920 #else
921 while (1)
923 if (*startp == decimal[0])
925 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
926 if (decimal[cnt] != startp[cnt])
927 break;
928 if (decimal[cnt] == '\0')
929 break;
931 ++startp;
933 #endif
934 startp += lead_zero + decimal_len;
935 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
936 dig_no -= lead_zero;
939 /* If the BASE is 16 we can use a simpler algorithm. */
940 if (base == 16)
942 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
943 4, 4, 4, 4, 4, 4, 4, 4 };
944 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
945 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
946 mp_limb_t val;
948 while (!ISXDIGIT (*startp))
949 ++startp;
950 while (*startp == L_('0'))
951 ++startp;
952 if (ISDIGIT (*startp))
953 val = *startp++ - L_('0');
954 else
955 val = 10 + TOLOWER (*startp++) - L_('a');
956 bits = nbits[val];
957 /* We cannot have a leading zero. */
958 assert (bits != 0);
960 if (pos + 1 >= 4 || pos + 1 >= bits)
962 /* We don't have to care for wrapping. This is the normal
963 case so we add the first clause in the `if' expression as
964 an optimization. It is a compile-time constant and so does
965 not cost anything. */
966 retval[idx] = val << (pos - bits + 1);
967 pos -= bits;
969 else
971 retval[idx--] = val >> (bits - pos - 1);
972 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
973 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
976 /* Adjust the exponent for the bits we are shifting in. */
977 exponent += bits - 1 + (int_no - 1) * 4;
979 while (--dig_no > 0 && idx >= 0)
981 if (!ISXDIGIT (*startp))
982 startp += decimal_len;
983 if (ISDIGIT (*startp))
984 val = *startp++ - L_('0');
985 else
986 val = 10 + TOLOWER (*startp++) - L_('a');
988 if (pos + 1 >= 4)
990 retval[idx] |= val << (pos - 4 + 1);
991 pos -= 4;
993 else
995 retval[idx--] |= val >> (4 - pos - 1);
996 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
997 if (idx < 0)
998 return round_and_return (retval, exponent, negative, val,
999 BITS_PER_MP_LIMB - 1, dig_no > 0);
1001 retval[idx] = val;
1002 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1006 /* We ran out of digits. */
1007 MPN_ZERO (retval, idx);
1009 return round_and_return (retval, exponent, negative, 0, 0, 0);
1012 /* Now we have the number of digits in total and the integer digits as well
1013 as the exponent and its sign. We can decide whether the read digits are
1014 really integer digits or belong to the fractional part; i.e. we normalize
1015 123e-2 to 1.23. */
1017 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1018 : MIN (dig_no - int_no, exponent));
1019 int_no += incr;
1020 exponent -= incr;
1023 if (__builtin_expect (int_no + exponent > MAX_10_EXP + 1, 0))
1025 __set_errno (ERANGE);
1026 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1029 if (__builtin_expect (exponent < MIN_10_EXP - (DIG + 1), 0))
1031 __set_errno (ERANGE);
1032 return negative ? -0.0 : 0.0;
1035 if (int_no > 0)
1037 /* Read the integer part as a multi-precision number to NUM. */
1038 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1039 #ifndef USE_WIDE_CHAR
1040 , decimal, decimal_len, thousands
1041 #endif
1044 if (exponent > 0)
1046 /* We now multiply the gained number by the given power of ten. */
1047 mp_limb_t *psrc = num;
1048 mp_limb_t *pdest = den;
1049 int expbit = 1;
1050 const struct mp_power *ttab = &_fpioconst_pow10[0];
1054 if ((exponent & expbit) != 0)
1056 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1057 mp_limb_t cy;
1058 exponent ^= expbit;
1060 /* FIXME: not the whole multiplication has to be
1061 done. If we have the needed number of bits we
1062 only need the information whether more non-zero
1063 bits follow. */
1064 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1065 cy = __mpn_mul (pdest, psrc, numsize,
1066 &__tens[ttab->arrayoff
1067 + _FPIO_CONST_OFFSET],
1068 size);
1069 else
1070 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1071 + _FPIO_CONST_OFFSET],
1072 size, psrc, numsize);
1073 numsize += size;
1074 if (cy == 0)
1075 --numsize;
1076 (void) SWAP (psrc, pdest);
1078 expbit <<= 1;
1079 ++ttab;
1081 while (exponent != 0);
1083 if (psrc == den)
1084 memcpy (num, den, numsize * sizeof (mp_limb_t));
1087 /* Determine how many bits of the result we already have. */
1088 count_leading_zeros (bits, num[numsize - 1]);
1089 bits = numsize * BITS_PER_MP_LIMB - bits;
1091 /* Now we know the exponent of the number in base two.
1092 Check it against the maximum possible exponent. */
1093 if (__builtin_expect (bits > MAX_EXP, 0))
1095 __set_errno (ERANGE);
1096 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1099 /* We have already the first BITS bits of the result. Together with
1100 the information whether more non-zero bits follow this is enough
1101 to determine the result. */
1102 if (bits > MANT_DIG)
1104 int i;
1105 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1106 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1107 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1108 : least_idx;
1109 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1110 : least_bit - 1;
1112 if (least_bit == 0)
1113 memcpy (retval, &num[least_idx],
1114 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1115 else
1117 for (i = least_idx; i < numsize - 1; ++i)
1118 retval[i - least_idx] = (num[i] >> least_bit)
1119 | (num[i + 1]
1120 << (BITS_PER_MP_LIMB - least_bit));
1121 if (i - least_idx < RETURN_LIMB_SIZE)
1122 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1125 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1126 for (i = 0; num[i] == 0; ++i)
1129 return round_and_return (retval, bits - 1, negative,
1130 num[round_idx], round_bit,
1131 int_no < dig_no || i < round_idx);
1132 /* NOTREACHED */
1134 else if (dig_no == int_no)
1136 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1137 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1139 if (target_bit == is_bit)
1141 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1142 numsize * sizeof (mp_limb_t));
1143 /* FIXME: the following loop can be avoided if we assume a
1144 maximal MANT_DIG value. */
1145 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1147 else if (target_bit > is_bit)
1149 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1150 num, numsize, target_bit - is_bit);
1151 /* FIXME: the following loop can be avoided if we assume a
1152 maximal MANT_DIG value. */
1153 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1155 else
1157 mp_limb_t cy;
1158 assert (numsize < RETURN_LIMB_SIZE);
1160 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1161 num, numsize, is_bit - target_bit);
1162 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1163 /* FIXME: the following loop can be avoided if we assume a
1164 maximal MANT_DIG value. */
1165 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1168 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1169 /* NOTREACHED */
1172 /* Store the bits we already have. */
1173 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1174 #if RETURN_LIMB_SIZE > 1
1175 if (numsize < RETURN_LIMB_SIZE)
1176 # if RETURN_LIMB_SIZE == 2
1177 retval[numsize] = 0;
1178 # else
1179 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1180 # endif
1181 #endif
1184 /* We have to compute at least some of the fractional digits. */
1186 /* We construct a fraction and the result of the division gives us
1187 the needed digits. The denominator is 1.0 multiplied by the
1188 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1189 123e-6 gives 123 / 1000000. */
1191 int expbit;
1192 int neg_exp;
1193 int more_bits;
1194 mp_limb_t cy;
1195 mp_limb_t *psrc = den;
1196 mp_limb_t *pdest = num;
1197 const struct mp_power *ttab = &_fpioconst_pow10[0];
1199 assert (dig_no > int_no && exponent <= 0);
1202 /* For the fractional part we need not process too many digits. One
1203 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1204 ceil(BITS / 3) =: N
1205 digits we should have enough bits for the result. The remaining
1206 decimal digits give us the information that more bits are following.
1207 This can be used while rounding. (Two added as a safety margin.) */
1208 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1210 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1211 more_bits = 1;
1213 else
1214 more_bits = 0;
1216 neg_exp = dig_no - int_no - exponent;
1218 /* Construct the denominator. */
1219 densize = 0;
1220 expbit = 1;
1223 if ((neg_exp & expbit) != 0)
1225 mp_limb_t cy;
1226 neg_exp ^= expbit;
1228 if (densize == 0)
1230 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1231 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1232 densize * sizeof (mp_limb_t));
1234 else
1236 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1237 + _FPIO_CONST_OFFSET],
1238 ttab->arraysize - _FPIO_CONST_OFFSET,
1239 psrc, densize);
1240 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1241 if (cy == 0)
1242 --densize;
1243 (void) SWAP (psrc, pdest);
1246 expbit <<= 1;
1247 ++ttab;
1249 while (neg_exp != 0);
1251 if (psrc == num)
1252 memcpy (den, num, densize * sizeof (mp_limb_t));
1254 /* Read the fractional digits from the string. */
1255 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1256 #ifndef USE_WIDE_CHAR
1257 , decimal, decimal_len, thousands
1258 #endif
1261 /* We now have to shift both numbers so that the highest bit in the
1262 denominator is set. In the same process we copy the numerator to
1263 a high place in the array so that the division constructs the wanted
1264 digits. This is done by a "quasi fix point" number representation.
1266 num: ddddddddddd . 0000000000000000000000
1267 |--- m ---|
1268 den: ddddddddddd n >= m
1269 |--- n ---|
1272 count_leading_zeros (cnt, den[densize - 1]);
1274 if (cnt > 0)
1276 /* Don't call `mpn_shift' with a count of zero since the specification
1277 does not allow this. */
1278 (void) __mpn_lshift (den, den, densize, cnt);
1279 cy = __mpn_lshift (num, num, numsize, cnt);
1280 if (cy != 0)
1281 num[numsize++] = cy;
1284 /* Now we are ready for the division. But it is not necessary to
1285 do a full multi-precision division because we only need a small
1286 number of bits for the result. So we do not use __mpn_divmod
1287 here but instead do the division here by hand and stop whenever
1288 the needed number of bits is reached. The code itself comes
1289 from the GNU MP Library by Torbj\"orn Granlund. */
1291 exponent = bits;
1293 switch (densize)
1295 case 1:
1297 mp_limb_t d, n, quot;
1298 int used = 0;
1300 n = num[0];
1301 d = den[0];
1302 assert (numsize == 1 && n < d);
1306 udiv_qrnnd (quot, n, n, 0, d);
1308 #define got_limb \
1309 if (bits == 0) \
1311 register int cnt; \
1312 if (quot == 0) \
1313 cnt = BITS_PER_MP_LIMB; \
1314 else \
1315 count_leading_zeros (cnt, quot); \
1316 exponent -= cnt; \
1317 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1319 used = MANT_DIG + cnt; \
1320 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1321 bits = MANT_DIG + 1; \
1323 else \
1325 /* Note that we only clear the second element. */ \
1326 /* The conditional is determined at compile time. */ \
1327 if (RETURN_LIMB_SIZE > 1) \
1328 retval[1] = 0; \
1329 retval[0] = quot; \
1330 bits = -cnt; \
1333 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1334 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1335 quot); \
1336 else \
1338 used = MANT_DIG - bits; \
1339 if (used > 0) \
1340 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1342 bits += BITS_PER_MP_LIMB
1344 got_limb;
1346 while (bits <= MANT_DIG);
1348 return round_and_return (retval, exponent - 1, negative,
1349 quot, BITS_PER_MP_LIMB - 1 - used,
1350 more_bits || n != 0);
1352 case 2:
1354 mp_limb_t d0, d1, n0, n1;
1355 mp_limb_t quot = 0;
1356 int used = 0;
1358 d0 = den[0];
1359 d1 = den[1];
1361 if (numsize < densize)
1363 if (num[0] >= d1)
1365 /* The numerator of the number occupies fewer bits than
1366 the denominator but the one limb is bigger than the
1367 high limb of the numerator. */
1368 n1 = 0;
1369 n0 = num[0];
1371 else
1373 if (bits <= 0)
1374 exponent -= BITS_PER_MP_LIMB;
1375 else
1377 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1378 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1379 BITS_PER_MP_LIMB, 0);
1380 else
1382 used = MANT_DIG - bits;
1383 if (used > 0)
1384 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1386 bits += BITS_PER_MP_LIMB;
1388 n1 = num[0];
1389 n0 = 0;
1392 else
1394 n1 = num[1];
1395 n0 = num[0];
1398 while (bits <= MANT_DIG)
1400 mp_limb_t r;
1402 if (n1 == d1)
1404 /* QUOT should be either 111..111 or 111..110. We need
1405 special treatment of this rare case as normal division
1406 would give overflow. */
1407 quot = ~(mp_limb_t) 0;
1409 r = n0 + d1;
1410 if (r < d1) /* Carry in the addition? */
1412 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1413 goto have_quot;
1415 n1 = d0 - (d0 != 0);
1416 n0 = -d0;
1418 else
1420 udiv_qrnnd (quot, r, n1, n0, d1);
1421 umul_ppmm (n1, n0, d0, quot);
1424 q_test:
1425 if (n1 > r || (n1 == r && n0 > 0))
1427 /* The estimated QUOT was too large. */
1428 --quot;
1430 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1431 r += d1;
1432 if (r >= d1) /* If not carry, test QUOT again. */
1433 goto q_test;
1435 sub_ddmmss (n1, n0, r, 0, n1, n0);
1437 have_quot:
1438 got_limb;
1441 return round_and_return (retval, exponent - 1, negative,
1442 quot, BITS_PER_MP_LIMB - 1 - used,
1443 more_bits || n1 != 0 || n0 != 0);
1445 default:
1447 int i;
1448 mp_limb_t cy, dX, d1, n0, n1;
1449 mp_limb_t quot = 0;
1450 int used = 0;
1452 dX = den[densize - 1];
1453 d1 = den[densize - 2];
1455 /* The division does not work if the upper limb of the two-limb
1456 numerator is greater than the denominator. */
1457 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1458 num[numsize++] = 0;
1460 if (numsize < densize)
1462 mp_size_t empty = densize - numsize;
1463 register int i;
1465 if (bits <= 0)
1466 exponent -= empty * BITS_PER_MP_LIMB;
1467 else
1469 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1471 /* We make a difference here because the compiler
1472 cannot optimize the `else' case that good and
1473 this reflects all currently used FLOAT types
1474 and GMP implementations. */
1475 #if RETURN_LIMB_SIZE <= 2
1476 assert (empty == 1);
1477 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1478 BITS_PER_MP_LIMB, 0);
1479 #else
1480 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1481 retval[i] = retval[i - empty];
1482 while (i >= 0)
1483 retval[i--] = 0;
1484 #endif
1486 else
1488 used = MANT_DIG - bits;
1489 if (used >= BITS_PER_MP_LIMB)
1491 register int i;
1492 (void) __mpn_lshift (&retval[used
1493 / BITS_PER_MP_LIMB],
1494 retval, RETURN_LIMB_SIZE,
1495 used % BITS_PER_MP_LIMB);
1496 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1497 retval[i] = 0;
1499 else if (used > 0)
1500 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1502 bits += empty * BITS_PER_MP_LIMB;
1504 for (i = numsize; i > 0; --i)
1505 num[i + empty] = num[i - 1];
1506 MPN_ZERO (num, empty + 1);
1508 else
1510 int i;
1511 assert (numsize == densize);
1512 for (i = numsize; i > 0; --i)
1513 num[i] = num[i - 1];
1516 den[densize] = 0;
1517 n0 = num[densize];
1519 while (bits <= MANT_DIG)
1521 if (n0 == dX)
1522 /* This might over-estimate QUOT, but it's probably not
1523 worth the extra code here to find out. */
1524 quot = ~(mp_limb_t) 0;
1525 else
1527 mp_limb_t r;
1529 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1530 umul_ppmm (n1, n0, d1, quot);
1532 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1534 --quot;
1535 r += dX;
1536 if (r < dX) /* I.e. "carry in previous addition?" */
1537 break;
1538 n1 -= n0 < d1;
1539 n0 -= d1;
1543 /* Possible optimization: We already have (q * n0) and (1 * n1)
1544 after the calculation of QUOT. Taking advantage of this, we
1545 could make this loop make two iterations less. */
1547 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1549 if (num[densize] != cy)
1551 cy = __mpn_add_n (num, num, den, densize);
1552 assert (cy != 0);
1553 --quot;
1555 n0 = num[densize] = num[densize - 1];
1556 for (i = densize - 1; i > 0; --i)
1557 num[i] = num[i - 1];
1559 got_limb;
1562 for (i = densize; num[i] == 0 && i >= 0; --i)
1564 return round_and_return (retval, exponent - 1, negative,
1565 quot, BITS_PER_MP_LIMB - 1 - used,
1566 more_bits || i >= 0);
1571 /* NOTREACHED */
1573 #if defined _LIBC && !defined USE_WIDE_CHAR
1574 libc_hidden_def (____STRTOF_INTERNAL)
1575 #endif
1577 /* External user entry point. */
1579 FLOAT
1580 #ifdef weak_function
1581 weak_function
1582 #endif
1583 __STRTOF (nptr, endptr, loc)
1584 const STRING_TYPE *nptr;
1585 STRING_TYPE **endptr;
1586 __locale_t loc;
1588 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1590 #if defined _LIBC
1591 libc_hidden_def (__STRTOF)
1592 libc_hidden_ver (__STRTOF, STRTOF)
1593 #endif
1594 weak_alias (__STRTOF, STRTOF)
1596 #ifdef LONG_DOUBLE_COMPAT
1597 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1598 # ifdef USE_WIDE_CHAR
1599 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1600 # else
1601 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1602 # endif
1603 # endif
1604 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1605 # ifdef USE_WIDE_CHAR
1606 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1607 # else
1608 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1609 # endif
1610 # endif
1611 #endif