Tweak archive deps.
[glibc.git] / stdlib / strtod_l.c
blobdce65cb83c7904e61aa82348e3c9d2e73a7bffbd
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,98,2002, 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <xlocale.h>
23 extern double ____strtod_l_internal (const char *, char **, int, __locale_t);
24 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
25 int, int, __locale_t);
27 /* Configuration part. These macros are defined by `strtold.c',
28 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
29 `long double' and `float' versions of the reader. */
30 #ifndef FLOAT
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>
64 /* The gmp headers need some configuration frobs. */
65 #define HAVE_ALLOCA 1
67 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
68 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
69 #include <gmp-mparam.h>
70 #include <gmp.h>
71 #include <gmp-impl.h>
72 #include <longlong.h>
73 #include "fpioconst.h"
75 #define NDEBUG 1
76 #include <assert.h>
79 /* We use this code for the extended locale handling where the
80 function gets as an additional argument the locale which has to be
81 used. To access the values we have to redefine the _NL_CURRENT and
82 _NL_CURRENT_WORD macros. */
83 #undef _NL_CURRENT
84 #define _NL_CURRENT(category, item) \
85 (current->values[_NL_ITEM_INDEX (item)].string)
86 #undef _NL_CURRENT_WORD
87 #define _NL_CURRENT_WORD(category, item) \
88 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
90 #if defined _LIBC || defined HAVE_WCHAR_H
91 # include <wchar.h>
92 #endif
94 #ifdef USE_WIDE_CHAR
95 # include <wctype.h>
96 # define STRING_TYPE wchar_t
97 # define CHAR_TYPE wint_t
98 # define L_(Ch) L##Ch
99 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
100 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
101 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
102 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
103 # define STRNCASECMP(S1, S2, N) __wcsncasecmp_l ((S1), (S2), (N), loc)
104 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
105 #else
106 # define STRING_TYPE char
107 # define CHAR_TYPE char
108 # define L_(Ch) Ch
109 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
110 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
111 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
112 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
113 # define STRNCASECMP(S1, S2, N) __strncasecmp_l ((S1), (S2), (N), loc)
114 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
115 #endif
118 /* Constants we need from float.h; select the set for the FLOAT precision. */
119 #define MANT_DIG PASTE(FLT,_MANT_DIG)
120 #define DIG PASTE(FLT,_DIG)
121 #define MAX_EXP PASTE(FLT,_MAX_EXP)
122 #define MIN_EXP PASTE(FLT,_MIN_EXP)
123 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
124 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
126 /* Extra macros required to get FLT expanded before the pasting. */
127 #define PASTE(a,b) PASTE1(a,b)
128 #define PASTE1(a,b) a##b
130 /* Function to construct a floating point number from an MP integer
131 containing the fraction bits, a base 2 exponent, and a sign flag. */
132 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
134 /* Definitions according to limb size used. */
135 #if BITS_PER_MP_LIMB == 32
136 # define MAX_DIG_PER_LIMB 9
137 # define MAX_FAC_PER_LIMB 1000000000UL
138 #elif BITS_PER_MP_LIMB == 64
139 # define MAX_DIG_PER_LIMB 19
140 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
141 #else
142 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
143 #endif
146 /* Local data structure. */
147 static const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
148 { 0, 10, 100,
149 1000, 10000, 100000L,
150 1000000L, 10000000L, 100000000L,
151 1000000000L
152 #if BITS_PER_MP_LIMB > 32
153 , 10000000000ULL, 100000000000ULL,
154 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
155 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
156 1000000000000000000ULL, 10000000000000000000ULL
157 #endif
158 #if BITS_PER_MP_LIMB > 64
159 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
160 #endif
163 #ifndef howmany
164 #define howmany(x,y) (((x)+((y)-1))/(y))
165 #endif
166 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
168 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
169 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
170 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
172 #define RETURN(val,end) \
173 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
174 return val; } while (0)
176 /* Maximum size necessary for mpn integers to hold floating point numbers. */
177 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
178 + 2)
179 /* Declare an mpn integer variable that big. */
180 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
181 /* Copy an mpn integer value. */
182 #define MPN_ASSIGN(dst, src) \
183 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
186 /* Return a floating point number of the needed type according to the given
187 multi-precision number after possible rounding. */
188 static FLOAT
189 round_and_return (mp_limb_t *retval, int exponent, int negative,
190 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
192 if (exponent < MIN_EXP - 1)
194 mp_size_t shift = MIN_EXP - 1 - exponent;
196 if (shift > MANT_DIG)
198 __set_errno (EDOM);
199 return 0.0;
202 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
203 if (shift == MANT_DIG)
204 /* This is a special case to handle the very seldom case where
205 the mantissa will be empty after the shift. */
207 int i;
209 round_limb = retval[RETURN_LIMB_SIZE - 1];
210 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
211 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
212 more_bits |= retval[i] != 0;
213 MPN_ZERO (retval, RETURN_LIMB_SIZE);
215 else if (shift >= BITS_PER_MP_LIMB)
217 int i;
219 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
220 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
221 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
222 more_bits |= retval[i] != 0;
223 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
224 != 0);
226 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
227 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
228 shift % BITS_PER_MP_LIMB);
229 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
230 shift / BITS_PER_MP_LIMB);
232 else if (shift > 0)
234 round_limb = retval[0];
235 round_bit = shift - 1;
236 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
238 /* This is a hook for the m68k long double format, where the
239 exponent bias is the same for normalized and denormalized
240 numbers. */
241 #ifndef DENORM_EXP
242 # define DENORM_EXP (MIN_EXP - 2)
243 #endif
244 exponent = DENORM_EXP;
247 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
248 && (more_bits || (retval[0] & 1) != 0
249 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
251 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
253 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
254 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
255 (retval[RETURN_LIMB_SIZE - 1]
256 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
258 ++exponent;
259 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
260 retval[RETURN_LIMB_SIZE - 1]
261 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
263 else if (exponent == DENORM_EXP
264 && (retval[RETURN_LIMB_SIZE - 1]
265 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
266 != 0)
267 /* The number was denormalized but now normalized. */
268 exponent = MIN_EXP - 1;
271 if (exponent > MAX_EXP)
272 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
274 return MPN2FLOAT (retval, exponent, negative);
278 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
279 into N. Return the size of the number limbs in NSIZE at the first
280 character od the string that is not part of the integer as the function
281 value. If the EXPONENT is small enough to be taken as an additional
282 factor for the resulting number (see code) multiply by it. */
283 static const STRING_TYPE *
284 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
285 int *exponent
286 #ifndef USE_WIDE_CHAR
287 , const char *decimal, size_t decimal_len, const char *thousands
288 #endif
292 /* Number of digits for actual limb. */
293 int cnt = 0;
294 mp_limb_t low = 0;
295 mp_limb_t start;
297 *nsize = 0;
298 assert (digcnt > 0);
301 if (cnt == MAX_DIG_PER_LIMB)
303 if (*nsize == 0)
305 n[0] = low;
306 *nsize = 1;
308 else
310 mp_limb_t cy;
311 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
312 cy += __mpn_add_1 (n, n, *nsize, low);
313 if (cy != 0)
315 n[*nsize] = cy;
316 ++(*nsize);
319 cnt = 0;
320 low = 0;
323 /* There might be thousands separators or radix characters in
324 the string. But these all can be ignored because we know the
325 format of the number is correct and we have an exact number
326 of characters to read. */
327 #ifdef USE_WIDE_CHAR
328 if (*str < L'0' || *str > L'9')
329 ++str;
330 #else
331 if (*str < '0' || *str > '9')
333 int inner = 0;
334 if (thousands != NULL && *str == *thousands
335 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
336 if (thousands[inner] != str[inner])
337 break;
338 thousands[inner] == '\0'; }))
339 str += inner;
340 else
341 str += decimal_len;
343 #endif
344 low = low * 10 + *str++ - L_('0');
345 ++cnt;
347 while (--digcnt > 0);
349 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
351 low *= _tens_in_limb[*exponent];
352 start = _tens_in_limb[cnt + *exponent];
353 *exponent = 0;
355 else
356 start = _tens_in_limb[cnt];
358 if (*nsize == 0)
360 n[0] = low;
361 *nsize = 1;
363 else
365 mp_limb_t cy;
366 cy = __mpn_mul_1 (n, n, *nsize, start);
367 cy += __mpn_add_1 (n, n, *nsize, low);
368 if (cy != 0)
369 n[(*nsize)++] = cy;
372 return str;
376 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
377 with the COUNT most significant bits of LIMB.
379 Tege doesn't like this function so I have to write it here myself. :)
380 --drepper */
381 static inline void
382 __attribute ((always_inline))
383 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
384 mp_limb_t limb)
386 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
388 /* Optimize the case of shifting by exactly a word:
389 just copy words, with no actual bit-shifting. */
390 mp_size_t i;
391 for (i = size - 1; i > 0; --i)
392 ptr[i] = ptr[i - 1];
393 ptr[0] = limb;
395 else
397 (void) __mpn_lshift (ptr, ptr, size, count);
398 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
403 #define INTERNAL(x) INTERNAL1(x)
404 #define INTERNAL1(x) __##x##_internal
406 /* This file defines a function to check for correct grouping. */
407 #include "grouping.h"
410 /* Return a floating point number with the value of the given string NPTR.
411 Set *ENDPTR to the character after the last used one. If the number is
412 smaller than the smallest representable number, set `errno' to ERANGE and
413 return 0.0. If the number is too big to be represented, set `errno' to
414 ERANGE and return HUGE_VAL with the appropriate sign. */
415 FLOAT
416 INTERNAL (__STRTOF) (nptr, endptr, group, loc)
417 const STRING_TYPE *nptr;
418 STRING_TYPE **endptr;
419 int group;
420 __locale_t loc;
422 int negative; /* The sign of the number. */
423 MPN_VAR (num); /* MP representation of the number. */
424 int exponent; /* Exponent of the number. */
426 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
427 int base = 10;
429 /* When we have to compute fractional digits we form a fraction with a
430 second multi-precision number (and we sometimes need a second for
431 temporary results). */
432 MPN_VAR (den);
434 /* Representation for the return value. */
435 mp_limb_t retval[RETURN_LIMB_SIZE];
436 /* Number of bits currently in result value. */
437 int bits;
439 /* Running pointer after the last character processed in the string. */
440 const STRING_TYPE *cp, *tp;
441 /* Start of significant part of the number. */
442 const STRING_TYPE *startp, *start_of_digits;
443 /* Points at the character following the integer and fractional digits. */
444 const STRING_TYPE *expp;
445 /* Total number of digit and number of digits in integer part. */
446 int dig_no, int_no, lead_zero;
447 /* Contains the last character read. */
448 CHAR_TYPE c;
450 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
451 there. So define it ourselves if it remains undefined. */
452 #ifndef _WINT_T
453 typedef unsigned int wint_t;
454 #endif
455 /* The radix character of the current locale. */
456 #ifdef USE_WIDE_CHAR
457 wchar_t decimal;
458 #else
459 const char *decimal;
460 size_t decimal_len;
461 #endif
462 /* The thousands character of the current locale. */
463 #ifdef USE_WIDE_CHAR
464 wchar_t thousands = L'\0';
465 #else
466 const char *thousands = NULL;
467 #endif
468 /* The numeric grouping specification of the current locale,
469 in the format described in <locale.h>. */
470 const char *grouping;
471 /* Used in several places. */
472 int cnt;
474 struct locale_data *current = loc->__locales[LC_NUMERIC];
476 if (group)
478 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
479 if (*grouping <= 0 || *grouping == CHAR_MAX)
480 grouping = NULL;
481 else
483 /* Figure out the thousands separator character. */
484 #ifdef USE_WIDE_CHAR
485 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
486 _NL_NUMERIC_THOUSANDS_SEP_WC);
487 if (thousands == L'\0')
488 grouping = NULL;
489 #else
490 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
491 if (*thousands == '\0')
493 thousands = NULL;
494 grouping = NULL;
496 #endif
499 else
500 grouping = NULL;
502 /* Find the locale's decimal point character. */
503 #ifdef USE_WIDE_CHAR
504 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
505 assert (decimal != L'\0');
506 # define decimal_len 1
507 #else
508 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
509 decimal_len = strlen (decimal);
510 assert (decimal_len > 0);
511 #endif
513 /* Prepare number representation. */
514 exponent = 0;
515 negative = 0;
516 bits = 0;
518 /* Parse string to get maximal legal prefix. We need the number of
519 characters of the integer part, the fractional part and the exponent. */
520 cp = nptr - 1;
521 /* Ignore leading white space. */
523 c = *++cp;
524 while (ISSPACE (c));
526 /* Get sign of the result. */
527 if (c == L_('-'))
529 negative = 1;
530 c = *++cp;
532 else if (c == L_('+'))
533 c = *++cp;
535 /* Return 0.0 if no legal string is found.
536 No character is used even if a sign was found. */
537 #ifdef USE_WIDE_CHAR
538 if (c == (wint_t) decimal
539 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
541 /* We accept it. This funny construct is here only to indent
542 the code directly. */
544 #else
545 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
546 if (cp[cnt] != decimal[cnt])
547 break;
548 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
550 /* We accept it. This funny construct is here only to indent
551 the code directly. */
553 #endif
554 else if (c < L_('0') || c > L_('9'))
556 /* Check for `INF' or `INFINITY'. */
557 if (TOLOWER (c) == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
559 /* Return +/- infinity. */
560 if (endptr != NULL)
561 *endptr = (STRING_TYPE *)
562 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
563 ? 8 : 3));
565 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
568 if (TOLOWER (c) == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
570 /* Return NaN. */
571 FLOAT retval = NAN;
573 cp += 3;
575 /* Match `(n-char-sequence-digit)'. */
576 if (*cp == L_('('))
578 const STRING_TYPE *startp = cp;
580 ++cp;
581 while ((*cp >= L_('0') && *cp <= L_('9'))
582 || (TOLOWER (*cp) >= L_('a') && TOLOWER (*cp) <= L_('z'))
583 || *cp == L_('_'));
585 if (*cp != L_(')'))
586 /* The closing brace is missing. Only match the NAN
587 part. */
588 cp = startp;
589 else
591 /* This is a system-dependent way to specify the
592 bitmask used for the NaN. We expect it to be
593 a number which is put in the mantissa of the
594 number. */
595 STRING_TYPE *endp;
596 unsigned long long int mant;
598 mant = STRTOULL (startp + 1, &endp, 0);
599 if (endp == cp)
600 SET_MANTISSA (retval, mant);
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 (thousands == NULL)
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 (c != thousands[cnt])
646 break;
647 if (thousands[cnt] != '\0')
648 break;
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 if ((c < L_('0') || c > L_('9'))
658 && (base == 16 && (c < (CHAR_TYPE) TOLOWER (L_('a'))
659 || c > (CHAR_TYPE) TOLOWER (L_('f'))))
660 #ifdef USE_WIDE_CHAR
661 && c != (wint_t) decimal
662 #else
663 && ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
664 if (decimal[cnt] != cp[cnt])
665 break;
666 decimal[cnt] != '\0'; })
667 #endif
668 && (base == 16 && (cp == start_of_digits
669 || (CHAR_TYPE) TOLOWER (c) != L_('p')))
670 && (base != 16 && (CHAR_TYPE) TOLOWER (c) != L_('e')))
672 #ifdef USE_WIDE_CHAR
673 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
674 grouping);
675 #else
676 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
677 grouping);
678 #endif
679 /* If TP is at the start of the digits, there was no correctly
680 grouped prefix of the string; so no number found. */
681 RETURN (0.0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
684 /* Remember first significant digit and read following characters until the
685 decimal point, exponent character or any non-FP number character. */
686 startp = cp;
687 dig_no = 0;
688 while (1)
690 if ((c >= L_('0') && c <= L_('9'))
691 || (base == 16 && (wint_t) TOLOWER (c) >= L_('a')
692 && (wint_t) TOLOWER (c) <= L_('f')))
693 ++dig_no;
694 else
696 #ifdef USE_WIDE_CHAR
697 if ((wint_t) thousands == L'\0' || c != (wint_t) thousands)
698 /* Not a digit or separator: end of the integer part. */
699 break;
700 #else
701 if (thousands == NULL)
702 break;
703 else
705 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
706 if (thousands[cnt] != cp[cnt])
707 break;
708 if (thousands[cnt] != '\0')
709 break;
711 #endif
713 c = *++cp;
716 if (grouping && dig_no > 0)
718 /* Check the grouping of the digits. */
719 #ifdef USE_WIDE_CHAR
720 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
721 grouping);
722 #else
723 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
724 grouping);
725 #endif
726 if (cp != tp)
728 /* Less than the entire string was correctly grouped. */
730 if (tp == start_of_digits)
731 /* No valid group of numbers at all: no valid number. */
732 RETURN (0.0, nptr);
734 if (tp < startp)
735 /* The number is validly grouped, but consists
736 only of zeroes. The whole value is zero. */
737 RETURN (0.0, tp);
739 /* Recompute DIG_NO so we won't read more digits than
740 are properly grouped. */
741 cp = tp;
742 dig_no = 0;
743 for (tp = startp; tp < cp; ++tp)
744 if (*tp >= L_('0') && *tp <= L_('9'))
745 ++dig_no;
747 int_no = dig_no;
748 lead_zero = 0;
750 goto number_parsed;
754 /* We have the number digits in the integer part. Whether these are all or
755 any is really a fractional digit will be decided later. */
756 int_no = dig_no;
757 lead_zero = int_no == 0 ? -1 : 0;
759 /* Read the fractional digits. A special case are the 'american style'
760 numbers like `16.' i.e. with decimal but without trailing digits. */
761 if (
762 #ifdef USE_WIDE_CHAR
763 c == (wint_t) decimal
764 #else
765 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
766 if (decimal[cnt] != cp[cnt])
767 break;
768 decimal[cnt] == '\0'; })
769 #endif
772 cp += decimal_len;
773 c = *cp;
774 while ((c >= L_('0') && c <= L_('9')) ||
775 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
777 if (c != L_('0') && lead_zero == -1)
778 lead_zero = dig_no - int_no;
779 ++dig_no;
780 c = *++cp;
784 /* Remember start of exponent (if any). */
785 expp = cp;
787 /* Read exponent. */
788 if ((base == 16 && TOLOWER (c) == L_('p'))
789 || (base != 16 && TOLOWER (c) == L_('e')))
791 int exp_negative = 0;
793 c = *++cp;
794 if (c == L_('-'))
796 exp_negative = 1;
797 c = *++cp;
799 else if (c == L_('+'))
800 c = *++cp;
802 if (c >= L_('0') && c <= L_('9'))
804 int exp_limit;
806 /* Get the exponent limit. */
807 if (base == 16)
808 exp_limit = (exp_negative ?
809 -MIN_EXP + MANT_DIG + 4 * int_no :
810 MAX_EXP - 4 * int_no + lead_zero);
811 else
812 exp_limit = (exp_negative ?
813 -MIN_10_EXP + MANT_DIG + int_no :
814 MAX_10_EXP - int_no + lead_zero);
818 exponent *= 10;
820 if (exponent > exp_limit)
821 /* The exponent is too large/small to represent a valid
822 number. */
824 FLOAT result;
826 /* We have to take care for special situation: a joker
827 might have written "0.0e100000" which is in fact
828 zero. */
829 if (lead_zero == -1)
830 result = negative ? -0.0 : 0.0;
831 else
833 /* Overflow or underflow. */
834 __set_errno (ERANGE);
835 result = (exp_negative ? 0.0 :
836 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
839 /* Accept all following digits as part of the exponent. */
841 ++cp;
842 while (*cp >= L_('0') && *cp <= L_('9'));
844 RETURN (result, cp);
845 /* NOTREACHED */
848 exponent += c - L_('0');
849 c = *++cp;
851 while (c >= L_('0') && c <= L_('9'));
853 if (exp_negative)
854 exponent = -exponent;
856 else
857 cp = expp;
860 /* We don't want to have to work with trailing zeroes after the radix. */
861 if (dig_no > int_no)
863 while (expp[-1] == L_('0'))
865 --expp;
866 --dig_no;
868 assert (dig_no >= int_no);
871 if (dig_no == int_no && dig_no > 0 && exponent < 0)
874 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
875 --expp;
877 if (expp[-1] != L_('0'))
878 break;
880 --expp;
881 --dig_no;
882 --int_no;
883 ++exponent;
885 while (dig_no > 0 && exponent < 0);
887 number_parsed:
889 /* The whole string is parsed. Store the address of the next character. */
890 if (endptr)
891 *endptr = (STRING_TYPE *) cp;
893 if (dig_no == 0)
894 return negative ? -0.0 : 0.0;
896 if (lead_zero)
898 /* Find the decimal point */
899 #ifdef USE_WIDE_CHAR
900 while (*startp != decimal)
901 ++startp;
902 #else
903 while (1)
905 if (*startp == decimal[0])
907 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
908 if (decimal[cnt] != startp[cnt])
909 break;
910 if (decimal[cnt] == '\0')
911 break;
913 ++startp;
915 #endif
916 startp += lead_zero + decimal_len;
917 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
918 dig_no -= lead_zero;
921 /* If the BASE is 16 we can use a simpler algorithm. */
922 if (base == 16)
924 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
925 4, 4, 4, 4, 4, 4, 4, 4 };
926 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
927 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
928 mp_limb_t val;
930 while (!ISXDIGIT (*startp))
931 ++startp;
932 while (*startp == L_('0'))
933 ++startp;
934 if (ISDIGIT (*startp))
935 val = *startp++ - L_('0');
936 else
937 val = 10 + TOLOWER (*startp++) - L_('a');
938 bits = nbits[val];
939 /* We cannot have a leading zero. */
940 assert (bits != 0);
942 if (pos + 1 >= 4 || pos + 1 >= bits)
944 /* We don't have to care for wrapping. This is the normal
945 case so we add the first clause in the `if' expression as
946 an optimization. It is a compile-time constant and so does
947 not cost anything. */
948 retval[idx] = val << (pos - bits + 1);
949 pos -= bits;
951 else
953 retval[idx--] = val >> (bits - pos - 1);
954 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
955 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
958 /* Adjust the exponent for the bits we are shifting in. */
959 exponent += bits - 1 + (int_no - 1) * 4;
961 while (--dig_no > 0 && idx >= 0)
963 if (!ISXDIGIT (*startp))
964 startp += decimal_len;
965 if (ISDIGIT (*startp))
966 val = *startp++ - L_('0');
967 else
968 val = 10 + TOLOWER (*startp++) - L_('a');
970 if (pos + 1 >= 4)
972 retval[idx] |= val << (pos - 4 + 1);
973 pos -= 4;
975 else
977 retval[idx--] |= val >> (4 - pos - 1);
978 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
979 if (idx < 0)
980 return round_and_return (retval, exponent, negative, val,
981 BITS_PER_MP_LIMB - 1, dig_no > 0);
983 retval[idx] = val;
984 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
988 /* We ran out of digits. */
989 MPN_ZERO (retval, idx);
991 return round_and_return (retval, exponent, negative, 0, 0, 0);
994 /* Now we have the number of digits in total and the integer digits as well
995 as the exponent and its sign. We can decide whether the read digits are
996 really integer digits or belong to the fractional part; i.e. we normalize
997 123e-2 to 1.23. */
999 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1000 : MIN (dig_no - int_no, exponent));
1001 int_no += incr;
1002 exponent -= incr;
1005 if (int_no + exponent > MAX_10_EXP + 1)
1007 __set_errno (ERANGE);
1008 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1011 if (exponent < MIN_10_EXP - (DIG + 1))
1013 __set_errno (ERANGE);
1014 return 0.0;
1017 if (int_no > 0)
1019 /* Read the integer part as a multi-precision number to NUM. */
1020 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1021 #ifndef USE_WIDE_CHAR
1022 , decimal, decimal_len, thousands
1023 #endif
1026 if (exponent > 0)
1028 /* We now multiply the gained number by the given power of ten. */
1029 mp_limb_t *psrc = num;
1030 mp_limb_t *pdest = den;
1031 int expbit = 1;
1032 const struct mp_power *ttab = &_fpioconst_pow10[0];
1036 if ((exponent & expbit) != 0)
1038 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1039 mp_limb_t cy;
1040 exponent ^= expbit;
1042 /* FIXME: not the whole multiplication has to be
1043 done. If we have the needed number of bits we
1044 only need the information whether more non-zero
1045 bits follow. */
1046 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1047 cy = __mpn_mul (pdest, psrc, numsize,
1048 &__tens[ttab->arrayoff
1049 + _FPIO_CONST_OFFSET],
1050 size);
1051 else
1052 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1053 + _FPIO_CONST_OFFSET],
1054 size, psrc, numsize);
1055 numsize += size;
1056 if (cy == 0)
1057 --numsize;
1058 (void) SWAP (psrc, pdest);
1060 expbit <<= 1;
1061 ++ttab;
1063 while (exponent != 0);
1065 if (psrc == den)
1066 memcpy (num, den, numsize * sizeof (mp_limb_t));
1069 /* Determine how many bits of the result we already have. */
1070 count_leading_zeros (bits, num[numsize - 1]);
1071 bits = numsize * BITS_PER_MP_LIMB - bits;
1073 /* Now we know the exponent of the number in base two.
1074 Check it against the maximum possible exponent. */
1075 if (bits > MAX_EXP)
1077 __set_errno (ERANGE);
1078 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1081 /* We have already the first BITS bits of the result. Together with
1082 the information whether more non-zero bits follow this is enough
1083 to determine the result. */
1084 if (bits > MANT_DIG)
1086 int i;
1087 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1088 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1089 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1090 : least_idx;
1091 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1092 : least_bit - 1;
1094 if (least_bit == 0)
1095 memcpy (retval, &num[least_idx],
1096 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1097 else
1099 for (i = least_idx; i < numsize - 1; ++i)
1100 retval[i - least_idx] = (num[i] >> least_bit)
1101 | (num[i + 1]
1102 << (BITS_PER_MP_LIMB - least_bit));
1103 if (i - least_idx < RETURN_LIMB_SIZE)
1104 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1107 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1108 for (i = 0; num[i] == 0; ++i)
1111 return round_and_return (retval, bits - 1, negative,
1112 num[round_idx], round_bit,
1113 int_no < dig_no || i < round_idx);
1114 /* NOTREACHED */
1116 else if (dig_no == int_no)
1118 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1119 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1121 if (target_bit == is_bit)
1123 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1124 numsize * sizeof (mp_limb_t));
1125 /* FIXME: the following loop can be avoided if we assume a
1126 maximal MANT_DIG value. */
1127 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1129 else if (target_bit > is_bit)
1131 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1132 num, numsize, target_bit - is_bit);
1133 /* FIXME: the following loop can be avoided if we assume a
1134 maximal MANT_DIG value. */
1135 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1137 else
1139 mp_limb_t cy;
1140 assert (numsize < RETURN_LIMB_SIZE);
1142 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1143 num, numsize, is_bit - target_bit);
1144 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1145 /* FIXME: the following loop can be avoided if we assume a
1146 maximal MANT_DIG value. */
1147 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1150 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1151 /* NOTREACHED */
1154 /* Store the bits we already have. */
1155 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1156 #if RETURN_LIMB_SIZE > 1
1157 if (numsize < RETURN_LIMB_SIZE)
1158 # if RETURN_LIMB_SIZE == 2
1159 retval[numsize] = 0;
1160 # else
1161 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1162 # endif
1163 #endif
1166 /* We have to compute at least some of the fractional digits. */
1168 /* We construct a fraction and the result of the division gives us
1169 the needed digits. The denominator is 1.0 multiplied by the
1170 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1171 123e-6 gives 123 / 1000000. */
1173 int expbit;
1174 int neg_exp;
1175 int more_bits;
1176 mp_limb_t cy;
1177 mp_limb_t *psrc = den;
1178 mp_limb_t *pdest = num;
1179 const struct mp_power *ttab = &_fpioconst_pow10[0];
1181 assert (dig_no > int_no && exponent <= 0);
1184 /* For the fractional part we need not process too many digits. One
1185 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1186 ceil(BITS / 3) =: N
1187 digits we should have enough bits for the result. The remaining
1188 decimal digits give us the information that more bits are following.
1189 This can be used while rounding. (Two added as a safety margin.) */
1190 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1192 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1193 more_bits = 1;
1195 else
1196 more_bits = 0;
1198 neg_exp = dig_no - int_no - exponent;
1200 /* Construct the denominator. */
1201 densize = 0;
1202 expbit = 1;
1205 if ((neg_exp & expbit) != 0)
1207 mp_limb_t cy;
1208 neg_exp ^= expbit;
1210 if (densize == 0)
1212 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1213 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1214 densize * sizeof (mp_limb_t));
1216 else
1218 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1219 + _FPIO_CONST_OFFSET],
1220 ttab->arraysize - _FPIO_CONST_OFFSET,
1221 psrc, densize);
1222 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1223 if (cy == 0)
1224 --densize;
1225 (void) SWAP (psrc, pdest);
1228 expbit <<= 1;
1229 ++ttab;
1231 while (neg_exp != 0);
1233 if (psrc == num)
1234 memcpy (den, num, densize * sizeof (mp_limb_t));
1236 /* Read the fractional digits from the string. */
1237 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1238 #ifndef USE_WIDE_CHAR
1239 , decimal, decimal_len, thousands
1240 #endif
1243 /* We now have to shift both numbers so that the highest bit in the
1244 denominator is set. In the same process we copy the numerator to
1245 a high place in the array so that the division constructs the wanted
1246 digits. This is done by a "quasi fix point" number representation.
1248 num: ddddddddddd . 0000000000000000000000
1249 |--- m ---|
1250 den: ddddddddddd n >= m
1251 |--- n ---|
1254 count_leading_zeros (cnt, den[densize - 1]);
1256 if (cnt > 0)
1258 /* Don't call `mpn_shift' with a count of zero since the specification
1259 does not allow this. */
1260 (void) __mpn_lshift (den, den, densize, cnt);
1261 cy = __mpn_lshift (num, num, numsize, cnt);
1262 if (cy != 0)
1263 num[numsize++] = cy;
1266 /* Now we are ready for the division. But it is not necessary to
1267 do a full multi-precision division because we only need a small
1268 number of bits for the result. So we do not use __mpn_divmod
1269 here but instead do the division here by hand and stop whenever
1270 the needed number of bits is reached. The code itself comes
1271 from the GNU MP Library by Torbj\"orn Granlund. */
1273 exponent = bits;
1275 switch (densize)
1277 case 1:
1279 mp_limb_t d, n, quot;
1280 int used = 0;
1282 n = num[0];
1283 d = den[0];
1284 assert (numsize == 1 && n < d);
1288 udiv_qrnnd (quot, n, n, 0, d);
1290 #define got_limb \
1291 if (bits == 0) \
1293 register int cnt; \
1294 if (quot == 0) \
1295 cnt = BITS_PER_MP_LIMB; \
1296 else \
1297 count_leading_zeros (cnt, quot); \
1298 exponent -= cnt; \
1299 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1301 used = MANT_DIG + cnt; \
1302 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1303 bits = MANT_DIG + 1; \
1305 else \
1307 /* Note that we only clear the second element. */ \
1308 /* The conditional is determined at compile time. */ \
1309 if (RETURN_LIMB_SIZE > 1) \
1310 retval[1] = 0; \
1311 retval[0] = quot; \
1312 bits = -cnt; \
1315 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1316 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1317 quot); \
1318 else \
1320 used = MANT_DIG - bits; \
1321 if (used > 0) \
1322 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1324 bits += BITS_PER_MP_LIMB
1326 got_limb;
1328 while (bits <= MANT_DIG);
1330 return round_and_return (retval, exponent - 1, negative,
1331 quot, BITS_PER_MP_LIMB - 1 - used,
1332 more_bits || n != 0);
1334 case 2:
1336 mp_limb_t d0, d1, n0, n1;
1337 mp_limb_t quot = 0;
1338 int used = 0;
1340 d0 = den[0];
1341 d1 = den[1];
1343 if (numsize < densize)
1345 if (num[0] >= d1)
1347 /* The numerator of the number occupies fewer bits than
1348 the denominator but the one limb is bigger than the
1349 high limb of the numerator. */
1350 n1 = 0;
1351 n0 = num[0];
1353 else
1355 if (bits <= 0)
1356 exponent -= BITS_PER_MP_LIMB;
1357 else
1359 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1360 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1361 BITS_PER_MP_LIMB, 0);
1362 else
1364 used = MANT_DIG - bits;
1365 if (used > 0)
1366 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1368 bits += BITS_PER_MP_LIMB;
1370 n1 = num[0];
1371 n0 = 0;
1374 else
1376 n1 = num[1];
1377 n0 = num[0];
1380 while (bits <= MANT_DIG)
1382 mp_limb_t r;
1384 if (n1 == d1)
1386 /* QUOT should be either 111..111 or 111..110. We need
1387 special treatment of this rare case as normal division
1388 would give overflow. */
1389 quot = ~(mp_limb_t) 0;
1391 r = n0 + d1;
1392 if (r < d1) /* Carry in the addition? */
1394 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1395 goto have_quot;
1397 n1 = d0 - (d0 != 0);
1398 n0 = -d0;
1400 else
1402 udiv_qrnnd (quot, r, n1, n0, d1);
1403 umul_ppmm (n1, n0, d0, quot);
1406 q_test:
1407 if (n1 > r || (n1 == r && n0 > 0))
1409 /* The estimated QUOT was too large. */
1410 --quot;
1412 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1413 r += d1;
1414 if (r >= d1) /* If not carry, test QUOT again. */
1415 goto q_test;
1417 sub_ddmmss (n1, n0, r, 0, n1, n0);
1419 have_quot:
1420 got_limb;
1423 return round_and_return (retval, exponent - 1, negative,
1424 quot, BITS_PER_MP_LIMB - 1 - used,
1425 more_bits || n1 != 0 || n0 != 0);
1427 default:
1429 int i;
1430 mp_limb_t cy, dX, d1, n0, n1;
1431 mp_limb_t quot = 0;
1432 int used = 0;
1434 dX = den[densize - 1];
1435 d1 = den[densize - 2];
1437 /* The division does not work if the upper limb of the two-limb
1438 numerator is greater than the denominator. */
1439 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1440 num[numsize++] = 0;
1442 if (numsize < densize)
1444 mp_size_t empty = densize - numsize;
1446 if (bits <= 0)
1448 register int i;
1449 for (i = numsize; i > 0; --i)
1450 num[i + empty] = num[i - 1];
1451 MPN_ZERO (num, empty + 1);
1452 exponent -= empty * BITS_PER_MP_LIMB;
1454 else
1456 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1458 /* We make a difference here because the compiler
1459 cannot optimize the `else' case that good and
1460 this reflects all currently used FLOAT types
1461 and GMP implementations. */
1462 register int i;
1463 #if RETURN_LIMB_SIZE <= 2
1464 assert (empty == 1);
1465 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1466 BITS_PER_MP_LIMB, 0);
1467 #else
1468 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1469 retval[i] = retval[i - empty];
1470 while (i >= 0)
1471 retval[i--] = 0;
1472 #endif
1473 for (i = numsize; i > 0; --i)
1474 num[i + empty] = num[i - 1];
1475 MPN_ZERO (num, empty + 1);
1477 else
1479 used = MANT_DIG - bits;
1480 if (used >= BITS_PER_MP_LIMB)
1482 register int i;
1483 (void) __mpn_lshift (&retval[used
1484 / BITS_PER_MP_LIMB],
1485 retval, RETURN_LIMB_SIZE,
1486 used % BITS_PER_MP_LIMB);
1487 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1488 retval[i] = 0;
1490 else if (used > 0)
1491 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1493 bits += empty * BITS_PER_MP_LIMB;
1496 else
1498 int i;
1499 assert (numsize == densize);
1500 for (i = numsize; i > 0; --i)
1501 num[i] = num[i - 1];
1504 den[densize] = 0;
1505 n0 = num[densize];
1507 while (bits <= MANT_DIG)
1509 if (n0 == dX)
1510 /* This might over-estimate QUOT, but it's probably not
1511 worth the extra code here to find out. */
1512 quot = ~(mp_limb_t) 0;
1513 else
1515 mp_limb_t r;
1517 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1518 umul_ppmm (n1, n0, d1, quot);
1520 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1522 --quot;
1523 r += dX;
1524 if (r < dX) /* I.e. "carry in previous addition?" */
1525 break;
1526 n1 -= n0 < d1;
1527 n0 -= d1;
1531 /* Possible optimization: We already have (q * n0) and (1 * n1)
1532 after the calculation of QUOT. Taking advantage of this, we
1533 could make this loop make two iterations less. */
1535 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1537 if (num[densize] != cy)
1539 cy = __mpn_add_n (num, num, den, densize);
1540 assert (cy != 0);
1541 --quot;
1543 n0 = num[densize] = num[densize - 1];
1544 for (i = densize - 1; i > 0; --i)
1545 num[i] = num[i - 1];
1547 got_limb;
1550 for (i = densize; num[i] == 0 && i >= 0; --i)
1552 return round_and_return (retval, exponent - 1, negative,
1553 quot, BITS_PER_MP_LIMB - 1 - used,
1554 more_bits || i >= 0);
1559 /* NOTREACHED */
1561 #if defined _LIBC && !defined USE_WIDE_CHAR
1562 libc_hidden_def (INTERNAL (__STRTOF))
1563 #endif
1565 /* External user entry point. */
1567 FLOAT
1568 #ifdef weak_function
1569 weak_function
1570 #endif
1571 __STRTOF (nptr, endptr, loc)
1572 const STRING_TYPE *nptr;
1573 STRING_TYPE **endptr;
1574 __locale_t loc;
1576 return INTERNAL (__STRTOF) (nptr, endptr, 0, loc);
1578 weak_alias (__STRTOF, STRTOF)