Updated to fedora-glibc-20061128T1037
[glibc/history.git] / stdlib / strtod_l.c
blobe13f1086daaf27ea69f743585cc308b6b2b579b8
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006 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 # include <math_ldbl_opt.h>
32 # define FLOAT double
33 # define FLT DBL
34 # ifdef USE_WIDE_CHAR
35 # define STRTOF wcstod_l
36 # define __STRTOF __wcstod_l
37 # else
38 # define STRTOF strtod_l
39 # define __STRTOF __strtod_l
40 # endif
41 # define MPN2FLOAT __mpn_construct_double
42 # define FLOAT_HUGE_VAL HUGE_VAL
43 # define SET_MANTISSA(flt, mant) \
44 do { union ieee754_double u; \
45 u.d = (flt); \
46 if ((mant & 0xfffffffffffffULL) == 0) \
47 mant = 0x8000000000000ULL; \
48 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
49 u.ieee.mantissa1 = (mant) & 0xffffffff; \
50 (flt) = u.d; \
51 } while (0)
52 #endif
53 /* End of configuration part. */
55 #include <ctype.h>
56 #include <errno.h>
57 #include <float.h>
58 #include <ieee754.h>
59 #include "../locale/localeinfo.h"
60 #include <locale.h>
61 #include <math.h>
62 #include <stdlib.h>
63 #include <string.h>
65 /* The gmp headers need some configuration frobs. */
66 #define HAVE_ALLOCA 1
68 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
69 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
70 #include <gmp-mparam.h>
71 #include <gmp.h>
72 #include "gmp-impl.h"
73 #include "longlong.h"
74 #include "fpioconst.h"
76 #define NDEBUG 1
77 #include <assert.h>
80 /* We use this code for the extended locale handling where the
81 function gets as an additional argument the locale which has to be
82 used. To access the values we have to redefine the _NL_CURRENT and
83 _NL_CURRENT_WORD macros. */
84 #undef _NL_CURRENT
85 #define _NL_CURRENT(category, item) \
86 (current->values[_NL_ITEM_INDEX (item)].string)
87 #undef _NL_CURRENT_WORD
88 #define _NL_CURRENT_WORD(category, item) \
89 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
91 #if defined _LIBC || defined HAVE_WCHAR_H
92 # include <wchar.h>
93 #endif
95 #ifdef USE_WIDE_CHAR
96 # include <wctype.h>
97 # define STRING_TYPE wchar_t
98 # define CHAR_TYPE wint_t
99 # define L_(Ch) L##Ch
100 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
101 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
102 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
103 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
104 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
105 # define STRNCASECMP(S1, S2, N) \
106 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
107 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
108 #else
109 # define STRING_TYPE char
110 # define CHAR_TYPE char
111 # define L_(Ch) Ch
112 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
113 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
114 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
115 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
116 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
117 # define STRNCASECMP(S1, S2, N) \
118 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
119 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
120 #endif
123 /* Constants we need from float.h; select the set for the FLOAT precision. */
124 #define MANT_DIG PASTE(FLT,_MANT_DIG)
125 #define DIG PASTE(FLT,_DIG)
126 #define MAX_EXP PASTE(FLT,_MAX_EXP)
127 #define MIN_EXP PASTE(FLT,_MIN_EXP)
128 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
129 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
131 /* Extra macros required to get FLT expanded before the pasting. */
132 #define PASTE(a,b) PASTE1(a,b)
133 #define PASTE1(a,b) a##b
135 /* Function to construct a floating point number from an MP integer
136 containing the fraction bits, a base 2 exponent, and a sign flag. */
137 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
139 /* Definitions according to limb size used. */
140 #if BITS_PER_MP_LIMB == 32
141 # define MAX_DIG_PER_LIMB 9
142 # define MAX_FAC_PER_LIMB 1000000000UL
143 #elif BITS_PER_MP_LIMB == 64
144 # define MAX_DIG_PER_LIMB 19
145 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
146 #else
147 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
148 #endif
151 /* Local data structure. */
152 static const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
153 { 0, 10, 100,
154 1000, 10000, 100000L,
155 1000000L, 10000000L, 100000000L,
156 1000000000L
157 #if BITS_PER_MP_LIMB > 32
158 , 10000000000ULL, 100000000000ULL,
159 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
160 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
161 1000000000000000000ULL, 10000000000000000000ULL
162 #endif
163 #if BITS_PER_MP_LIMB > 64
164 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
165 #endif
168 #ifndef howmany
169 #define howmany(x,y) (((x)+((y)-1))/(y))
170 #endif
171 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
173 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
174 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
175 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
177 #define RETURN(val,end) \
178 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
179 return val; } while (0)
181 /* Maximum size necessary for mpn integers to hold floating point numbers. */
182 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
183 + 2)
184 /* Declare an mpn integer variable that big. */
185 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
186 /* Copy an mpn integer value. */
187 #define MPN_ASSIGN(dst, src) \
188 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
191 /* Return a floating point number of the needed type according to the given
192 multi-precision number after possible rounding. */
193 static FLOAT
194 round_and_return (mp_limb_t *retval, int exponent, int negative,
195 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
197 if (exponent < MIN_EXP - 1)
199 mp_size_t shift = MIN_EXP - 1 - exponent;
201 if (shift > MANT_DIG)
203 __set_errno (EDOM);
204 return 0.0;
207 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
208 if (shift == MANT_DIG)
209 /* This is a special case to handle the very seldom case where
210 the mantissa will be empty after the shift. */
212 int i;
214 round_limb = retval[RETURN_LIMB_SIZE - 1];
215 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
216 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
217 more_bits |= retval[i] != 0;
218 MPN_ZERO (retval, RETURN_LIMB_SIZE);
220 else if (shift >= BITS_PER_MP_LIMB)
222 int i;
224 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
225 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
226 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
227 more_bits |= retval[i] != 0;
228 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
229 != 0);
231 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
232 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
233 shift % BITS_PER_MP_LIMB);
234 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
235 shift / BITS_PER_MP_LIMB);
237 else if (shift > 0)
239 round_limb = retval[0];
240 round_bit = shift - 1;
241 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
243 /* This is a hook for the m68k long double format, where the
244 exponent bias is the same for normalized and denormalized
245 numbers. */
246 #ifndef DENORM_EXP
247 # define DENORM_EXP (MIN_EXP - 2)
248 #endif
249 exponent = DENORM_EXP;
252 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
253 && (more_bits || (retval[0] & 1) != 0
254 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
256 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
258 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
259 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
260 (retval[RETURN_LIMB_SIZE - 1]
261 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
263 ++exponent;
264 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
265 retval[RETURN_LIMB_SIZE - 1]
266 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
268 else if (exponent == DENORM_EXP
269 && (retval[RETURN_LIMB_SIZE - 1]
270 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
271 != 0)
272 /* The number was denormalized but now normalized. */
273 exponent = MIN_EXP - 1;
276 if (exponent > MAX_EXP)
277 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
279 return MPN2FLOAT (retval, exponent, negative);
283 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
284 into N. Return the size of the number limbs in NSIZE at the first
285 character od the string that is not part of the integer as the function
286 value. If the EXPONENT is small enough to be taken as an additional
287 factor for the resulting number (see code) multiply by it. */
288 static const STRING_TYPE *
289 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
290 int *exponent
291 #ifndef USE_WIDE_CHAR
292 , const char *decimal, size_t decimal_len, const char *thousands
293 #endif
297 /* Number of digits for actual limb. */
298 int cnt = 0;
299 mp_limb_t low = 0;
300 mp_limb_t start;
302 *nsize = 0;
303 assert (digcnt > 0);
306 if (cnt == MAX_DIG_PER_LIMB)
308 if (*nsize == 0)
310 n[0] = low;
311 *nsize = 1;
313 else
315 mp_limb_t cy;
316 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
317 cy += __mpn_add_1 (n, n, *nsize, low);
318 if (cy != 0)
320 n[*nsize] = cy;
321 ++(*nsize);
324 cnt = 0;
325 low = 0;
328 /* There might be thousands separators or radix characters in
329 the string. But these all can be ignored because we know the
330 format of the number is correct and we have an exact number
331 of characters to read. */
332 #ifdef USE_WIDE_CHAR
333 if (*str < L'0' || *str > L'9')
334 ++str;
335 #else
336 if (*str < '0' || *str > '9')
338 int inner = 0;
339 if (thousands != NULL && *str == *thousands
340 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
341 if (thousands[inner] != str[inner])
342 break;
343 thousands[inner] == '\0'; }))
344 str += inner;
345 else
346 str += decimal_len;
348 #endif
349 low = low * 10 + *str++ - L_('0');
350 ++cnt;
352 while (--digcnt > 0);
354 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
356 low *= _tens_in_limb[*exponent];
357 start = _tens_in_limb[cnt + *exponent];
358 *exponent = 0;
360 else
361 start = _tens_in_limb[cnt];
363 if (*nsize == 0)
365 n[0] = low;
366 *nsize = 1;
368 else
370 mp_limb_t cy;
371 cy = __mpn_mul_1 (n, n, *nsize, start);
372 cy += __mpn_add_1 (n, n, *nsize, low);
373 if (cy != 0)
374 n[(*nsize)++] = cy;
377 return str;
381 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
382 with the COUNT most significant bits of LIMB.
384 Tege doesn't like this function so I have to write it here myself. :)
385 --drepper */
386 static inline void
387 __attribute ((always_inline))
388 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
389 mp_limb_t limb)
391 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
393 /* Optimize the case of shifting by exactly a word:
394 just copy words, with no actual bit-shifting. */
395 mp_size_t i;
396 for (i = size - 1; i > 0; --i)
397 ptr[i] = ptr[i - 1];
398 ptr[0] = limb;
400 else
402 (void) __mpn_lshift (ptr, ptr, size, count);
403 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
408 #define INTERNAL(x) INTERNAL1(x)
409 #define INTERNAL1(x) __##x##_internal
410 #ifndef ____STRTOF_INTERNAL
411 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
412 #endif
414 /* This file defines a function to check for correct grouping. */
415 #include "grouping.h"
418 /* Return a floating point number with the value of the given string NPTR.
419 Set *ENDPTR to the character after the last used one. If the number is
420 smaller than the smallest representable number, set `errno' to ERANGE and
421 return 0.0. If the number is too big to be represented, set `errno' to
422 ERANGE and return HUGE_VAL with the appropriate sign. */
423 FLOAT
424 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
425 const STRING_TYPE *nptr;
426 STRING_TYPE **endptr;
427 int group;
428 __locale_t loc;
430 int negative; /* The sign of the number. */
431 MPN_VAR (num); /* MP representation of the number. */
432 int exponent; /* Exponent of the number. */
434 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
435 int base = 10;
437 /* When we have to compute fractional digits we form a fraction with a
438 second multi-precision number (and we sometimes need a second for
439 temporary results). */
440 MPN_VAR (den);
442 /* Representation for the return value. */
443 mp_limb_t retval[RETURN_LIMB_SIZE];
444 /* Number of bits currently in result value. */
445 int bits;
447 /* Running pointer after the last character processed in the string. */
448 const STRING_TYPE *cp, *tp;
449 /* Start of significant part of the number. */
450 const STRING_TYPE *startp, *start_of_digits;
451 /* Points at the character following the integer and fractional digits. */
452 const STRING_TYPE *expp;
453 /* Total number of digit and number of digits in integer part. */
454 int dig_no, int_no, lead_zero;
455 /* Contains the last character read. */
456 CHAR_TYPE c;
458 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
459 there. So define it ourselves if it remains undefined. */
460 #ifndef _WINT_T
461 typedef unsigned int wint_t;
462 #endif
463 /* The radix character of the current locale. */
464 #ifdef USE_WIDE_CHAR
465 wchar_t decimal;
466 #else
467 const char *decimal;
468 size_t decimal_len;
469 #endif
470 /* The thousands character of the current locale. */
471 #ifdef USE_WIDE_CHAR
472 wchar_t thousands = L'\0';
473 #else
474 const char *thousands = NULL;
475 #endif
476 /* The numeric grouping specification of the current locale,
477 in the format described in <locale.h>. */
478 const char *grouping;
479 /* Used in several places. */
480 int cnt;
482 struct locale_data *current = loc->__locales[LC_NUMERIC];
484 if (group)
486 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
487 if (*grouping <= 0 || *grouping == CHAR_MAX)
488 grouping = NULL;
489 else
491 /* Figure out the thousands separator character. */
492 #ifdef USE_WIDE_CHAR
493 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
494 _NL_NUMERIC_THOUSANDS_SEP_WC);
495 if (thousands == L'\0')
496 grouping = NULL;
497 #else
498 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
499 if (*thousands == '\0')
501 thousands = NULL;
502 grouping = NULL;
504 #endif
507 else
508 grouping = NULL;
510 /* Find the locale's decimal point character. */
511 #ifdef USE_WIDE_CHAR
512 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
513 assert (decimal != L'\0');
514 # define decimal_len 1
515 #else
516 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
517 decimal_len = strlen (decimal);
518 assert (decimal_len > 0);
519 #endif
521 /* Prepare number representation. */
522 exponent = 0;
523 negative = 0;
524 bits = 0;
526 /* Parse string to get maximal legal prefix. We need the number of
527 characters of the integer part, the fractional part and the exponent. */
528 cp = nptr - 1;
529 /* Ignore leading white space. */
531 c = *++cp;
532 while (ISSPACE (c));
534 /* Get sign of the result. */
535 if (c == L_('-'))
537 negative = 1;
538 c = *++cp;
540 else if (c == L_('+'))
541 c = *++cp;
543 /* Return 0.0 if no legal string is found.
544 No character is used even if a sign was found. */
545 #ifdef USE_WIDE_CHAR
546 if (c == (wint_t) decimal
547 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
549 /* We accept it. This funny construct is here only to indent
550 the code directly. */
552 #else
553 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
554 if (cp[cnt] != decimal[cnt])
555 break;
556 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
558 /* We accept it. This funny construct is here only to indent
559 the code directly. */
561 #endif
562 else if (c < L_('0') || c > L_('9'))
564 /* Check for `INF' or `INFINITY'. */
565 if (TOLOWER_C (c) == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
567 /* Return +/- infinity. */
568 if (endptr != NULL)
569 *endptr = (STRING_TYPE *)
570 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
571 ? 8 : 3));
573 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
576 if (TOLOWER_C (c) == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
578 /* Return NaN. */
579 FLOAT retval = NAN;
581 cp += 3;
583 /* Match `(n-char-sequence-digit)'. */
584 if (*cp == L_('('))
586 const STRING_TYPE *startp = cp;
588 ++cp;
589 while ((*cp >= L_('0') && *cp <= L_('9'))
590 || (TOLOWER (*cp) >= L_('a') && TOLOWER (*cp) <= L_('z'))
591 || *cp == L_('_'));
593 if (*cp != L_(')'))
594 /* The closing brace is missing. Only match the NAN
595 part. */
596 cp = startp;
597 else
599 /* This is a system-dependent way to specify the
600 bitmask used for the NaN. We expect it to be
601 a number which is put in the mantissa of the
602 number. */
603 STRING_TYPE *endp;
604 unsigned long long int mant;
606 mant = STRTOULL (startp + 1, &endp, 0);
607 if (endp == cp)
608 SET_MANTISSA (retval, mant);
612 if (endptr != NULL)
613 *endptr = (STRING_TYPE *) cp;
615 return retval;
618 /* It is really a text we do not recognize. */
619 RETURN (0.0, nptr);
622 /* First look whether we are faced with a hexadecimal number. */
623 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
625 /* Okay, it is a hexa-decimal number. Remember this and skip
626 the characters. BTW: hexadecimal numbers must not be
627 grouped. */
628 base = 16;
629 cp += 2;
630 c = *cp;
631 grouping = NULL;
634 /* Record the start of the digits, in case we will check their grouping. */
635 start_of_digits = startp = cp;
637 /* Ignore leading zeroes. This helps us to avoid useless computations. */
638 #ifdef USE_WIDE_CHAR
639 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
640 c = *++cp;
641 #else
642 if (thousands == NULL)
643 while (c == '0')
644 c = *++cp;
645 else
647 /* We also have the multibyte thousands string. */
648 while (1)
650 if (c != '0')
652 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
653 if (c != thousands[cnt])
654 break;
655 if (thousands[cnt] != '\0')
656 break;
658 c = *++cp;
661 #endif
663 /* If no other digit but a '0' is found the result is 0.0.
664 Return current read pointer. */
665 if ((c < L_('0') || c > L_('9'))
666 && (base == 16 && (c < (CHAR_TYPE) TOLOWER (L_('a'))
667 || c > (CHAR_TYPE) TOLOWER (L_('f'))))
668 #ifdef USE_WIDE_CHAR
669 && c != (wint_t) decimal
670 #else
671 && ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
672 if (decimal[cnt] != cp[cnt])
673 break;
674 decimal[cnt] != '\0'; })
675 #endif
676 && (base == 16 && (cp == start_of_digits
677 || (CHAR_TYPE) TOLOWER (c) != L_('p')))
678 && (base != 16 && (CHAR_TYPE) TOLOWER (c) != L_('e')))
680 #ifdef USE_WIDE_CHAR
681 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
682 grouping);
683 #else
684 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
685 grouping);
686 #endif
687 /* If TP is at the start of the digits, there was no correctly
688 grouped prefix of the string; so no number found. */
689 RETURN (0.0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
692 /* Remember first significant digit and read following characters until the
693 decimal point, exponent character or any non-FP number character. */
694 startp = cp;
695 dig_no = 0;
696 while (1)
698 if ((c >= L_('0') && c <= L_('9'))
699 || (base == 16 && (wint_t) TOLOWER (c) >= L_('a')
700 && (wint_t) TOLOWER (c) <= L_('f')))
701 ++dig_no;
702 else
704 #ifdef USE_WIDE_CHAR
705 if ((wint_t) thousands == L'\0' || c != (wint_t) thousands)
706 /* Not a digit or separator: end of the integer part. */
707 break;
708 #else
709 if (thousands == NULL)
710 break;
711 else
713 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
714 if (thousands[cnt] != cp[cnt])
715 break;
716 if (thousands[cnt] != '\0')
717 break;
719 #endif
721 c = *++cp;
724 if (grouping && dig_no > 0)
726 /* Check the grouping of the digits. */
727 #ifdef USE_WIDE_CHAR
728 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
729 grouping);
730 #else
731 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
732 grouping);
733 #endif
734 if (cp != tp)
736 /* Less than the entire string was correctly grouped. */
738 if (tp == start_of_digits)
739 /* No valid group of numbers at all: no valid number. */
740 RETURN (0.0, nptr);
742 if (tp < startp)
743 /* The number is validly grouped, but consists
744 only of zeroes. The whole value is zero. */
745 RETURN (0.0, tp);
747 /* Recompute DIG_NO so we won't read more digits than
748 are properly grouped. */
749 cp = tp;
750 dig_no = 0;
751 for (tp = startp; tp < cp; ++tp)
752 if (*tp >= L_('0') && *tp <= L_('9'))
753 ++dig_no;
755 int_no = dig_no;
756 lead_zero = 0;
758 goto number_parsed;
762 /* We have the number digits in the integer part. Whether these are all or
763 any is really a fractional digit will be decided later. */
764 int_no = dig_no;
765 lead_zero = int_no == 0 ? -1 : 0;
767 /* Read the fractional digits. A special case are the 'american style'
768 numbers like `16.' i.e. with decimal but without trailing digits. */
769 if (
770 #ifdef USE_WIDE_CHAR
771 c == (wint_t) decimal
772 #else
773 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
774 if (decimal[cnt] != cp[cnt])
775 break;
776 decimal[cnt] == '\0'; })
777 #endif
780 cp += decimal_len;
781 c = *cp;
782 while ((c >= L_('0') && c <= L_('9')) ||
783 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
785 if (c != L_('0') && lead_zero == -1)
786 lead_zero = dig_no - int_no;
787 ++dig_no;
788 c = *++cp;
792 /* Remember start of exponent (if any). */
793 expp = cp;
795 /* Read exponent. */
796 if ((base == 16 && TOLOWER (c) == L_('p'))
797 || (base != 16 && TOLOWER (c) == L_('e')))
799 int exp_negative = 0;
801 c = *++cp;
802 if (c == L_('-'))
804 exp_negative = 1;
805 c = *++cp;
807 else if (c == L_('+'))
808 c = *++cp;
810 if (c >= L_('0') && c <= L_('9'))
812 int exp_limit;
814 /* Get the exponent limit. */
815 if (base == 16)
816 exp_limit = (exp_negative ?
817 -MIN_EXP + MANT_DIG + 4 * int_no :
818 MAX_EXP - 4 * int_no + lead_zero);
819 else
820 exp_limit = (exp_negative ?
821 -MIN_10_EXP + MANT_DIG + int_no :
822 MAX_10_EXP - int_no + lead_zero);
826 exponent *= 10;
828 if (exponent > exp_limit)
829 /* The exponent is too large/small to represent a valid
830 number. */
832 FLOAT result;
834 /* We have to take care for special situation: a joker
835 might have written "0.0e100000" which is in fact
836 zero. */
837 if (lead_zero == -1)
838 result = negative ? -0.0 : 0.0;
839 else
841 /* Overflow or underflow. */
842 __set_errno (ERANGE);
843 result = (exp_negative ? 0.0 :
844 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
847 /* Accept all following digits as part of the exponent. */
849 ++cp;
850 while (*cp >= L_('0') && *cp <= L_('9'));
852 RETURN (result, cp);
853 /* NOTREACHED */
856 exponent += c - L_('0');
857 c = *++cp;
859 while (c >= L_('0') && c <= L_('9'));
861 if (exp_negative)
862 exponent = -exponent;
864 else
865 cp = expp;
868 /* We don't want to have to work with trailing zeroes after the radix. */
869 if (dig_no > int_no)
871 while (expp[-1] == L_('0'))
873 --expp;
874 --dig_no;
876 assert (dig_no >= int_no);
879 if (dig_no == int_no && dig_no > 0 && exponent < 0)
882 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
883 --expp;
885 if (expp[-1] != L_('0'))
886 break;
888 --expp;
889 --dig_no;
890 --int_no;
891 ++exponent;
893 while (dig_no > 0 && exponent < 0);
895 number_parsed:
897 /* The whole string is parsed. Store the address of the next character. */
898 if (endptr)
899 *endptr = (STRING_TYPE *) cp;
901 if (dig_no == 0)
902 return negative ? -0.0 : 0.0;
904 if (lead_zero)
906 /* Find the decimal point */
907 #ifdef USE_WIDE_CHAR
908 while (*startp != decimal)
909 ++startp;
910 #else
911 while (1)
913 if (*startp == decimal[0])
915 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
916 if (decimal[cnt] != startp[cnt])
917 break;
918 if (decimal[cnt] == '\0')
919 break;
921 ++startp;
923 #endif
924 startp += lead_zero + decimal_len;
925 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
926 dig_no -= lead_zero;
929 /* If the BASE is 16 we can use a simpler algorithm. */
930 if (base == 16)
932 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
933 4, 4, 4, 4, 4, 4, 4, 4 };
934 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
935 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
936 mp_limb_t val;
938 while (!ISXDIGIT (*startp))
939 ++startp;
940 while (*startp == L_('0'))
941 ++startp;
942 if (ISDIGIT (*startp))
943 val = *startp++ - L_('0');
944 else
945 val = 10 + TOLOWER (*startp++) - L_('a');
946 bits = nbits[val];
947 /* We cannot have a leading zero. */
948 assert (bits != 0);
950 if (pos + 1 >= 4 || pos + 1 >= bits)
952 /* We don't have to care for wrapping. This is the normal
953 case so we add the first clause in the `if' expression as
954 an optimization. It is a compile-time constant and so does
955 not cost anything. */
956 retval[idx] = val << (pos - bits + 1);
957 pos -= bits;
959 else
961 retval[idx--] = val >> (bits - pos - 1);
962 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
963 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
966 /* Adjust the exponent for the bits we are shifting in. */
967 exponent += bits - 1 + (int_no - 1) * 4;
969 while (--dig_no > 0 && idx >= 0)
971 if (!ISXDIGIT (*startp))
972 startp += decimal_len;
973 if (ISDIGIT (*startp))
974 val = *startp++ - L_('0');
975 else
976 val = 10 + TOLOWER (*startp++) - L_('a');
978 if (pos + 1 >= 4)
980 retval[idx] |= val << (pos - 4 + 1);
981 pos -= 4;
983 else
985 retval[idx--] |= val >> (4 - pos - 1);
986 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
987 if (idx < 0)
988 return round_and_return (retval, exponent, negative, val,
989 BITS_PER_MP_LIMB - 1, dig_no > 0);
991 retval[idx] = val;
992 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
996 /* We ran out of digits. */
997 MPN_ZERO (retval, idx);
999 return round_and_return (retval, exponent, negative, 0, 0, 0);
1002 /* Now we have the number of digits in total and the integer digits as well
1003 as the exponent and its sign. We can decide whether the read digits are
1004 really integer digits or belong to the fractional part; i.e. we normalize
1005 123e-2 to 1.23. */
1007 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1008 : MIN (dig_no - int_no, exponent));
1009 int_no += incr;
1010 exponent -= incr;
1013 if (int_no + exponent > MAX_10_EXP + 1)
1015 __set_errno (ERANGE);
1016 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1019 if (exponent < MIN_10_EXP - (DIG + 1))
1021 __set_errno (ERANGE);
1022 return 0.0;
1025 if (int_no > 0)
1027 /* Read the integer part as a multi-precision number to NUM. */
1028 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1029 #ifndef USE_WIDE_CHAR
1030 , decimal, decimal_len, thousands
1031 #endif
1034 if (exponent > 0)
1036 /* We now multiply the gained number by the given power of ten. */
1037 mp_limb_t *psrc = num;
1038 mp_limb_t *pdest = den;
1039 int expbit = 1;
1040 const struct mp_power *ttab = &_fpioconst_pow10[0];
1044 if ((exponent & expbit) != 0)
1046 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1047 mp_limb_t cy;
1048 exponent ^= expbit;
1050 /* FIXME: not the whole multiplication has to be
1051 done. If we have the needed number of bits we
1052 only need the information whether more non-zero
1053 bits follow. */
1054 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1055 cy = __mpn_mul (pdest, psrc, numsize,
1056 &__tens[ttab->arrayoff
1057 + _FPIO_CONST_OFFSET],
1058 size);
1059 else
1060 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1061 + _FPIO_CONST_OFFSET],
1062 size, psrc, numsize);
1063 numsize += size;
1064 if (cy == 0)
1065 --numsize;
1066 (void) SWAP (psrc, pdest);
1068 expbit <<= 1;
1069 ++ttab;
1071 while (exponent != 0);
1073 if (psrc == den)
1074 memcpy (num, den, numsize * sizeof (mp_limb_t));
1077 /* Determine how many bits of the result we already have. */
1078 count_leading_zeros (bits, num[numsize - 1]);
1079 bits = numsize * BITS_PER_MP_LIMB - bits;
1081 /* Now we know the exponent of the number in base two.
1082 Check it against the maximum possible exponent. */
1083 if (bits > MAX_EXP)
1085 __set_errno (ERANGE);
1086 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1089 /* We have already the first BITS bits of the result. Together with
1090 the information whether more non-zero bits follow this is enough
1091 to determine the result. */
1092 if (bits > MANT_DIG)
1094 int i;
1095 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1096 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1097 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1098 : least_idx;
1099 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1100 : least_bit - 1;
1102 if (least_bit == 0)
1103 memcpy (retval, &num[least_idx],
1104 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1105 else
1107 for (i = least_idx; i < numsize - 1; ++i)
1108 retval[i - least_idx] = (num[i] >> least_bit)
1109 | (num[i + 1]
1110 << (BITS_PER_MP_LIMB - least_bit));
1111 if (i - least_idx < RETURN_LIMB_SIZE)
1112 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1115 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1116 for (i = 0; num[i] == 0; ++i)
1119 return round_and_return (retval, bits - 1, negative,
1120 num[round_idx], round_bit,
1121 int_no < dig_no || i < round_idx);
1122 /* NOTREACHED */
1124 else if (dig_no == int_no)
1126 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1127 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1129 if (target_bit == is_bit)
1131 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1132 numsize * sizeof (mp_limb_t));
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 if (target_bit > is_bit)
1139 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1140 num, numsize, target_bit - is_bit);
1141 /* FIXME: the following loop can be avoided if we assume a
1142 maximal MANT_DIG value. */
1143 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1145 else
1147 mp_limb_t cy;
1148 assert (numsize < RETURN_LIMB_SIZE);
1150 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1151 num, numsize, is_bit - target_bit);
1152 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1153 /* FIXME: the following loop can be avoided if we assume a
1154 maximal MANT_DIG value. */
1155 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1158 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1159 /* NOTREACHED */
1162 /* Store the bits we already have. */
1163 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1164 #if RETURN_LIMB_SIZE > 1
1165 if (numsize < RETURN_LIMB_SIZE)
1166 # if RETURN_LIMB_SIZE == 2
1167 retval[numsize] = 0;
1168 # else
1169 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1170 # endif
1171 #endif
1174 /* We have to compute at least some of the fractional digits. */
1176 /* We construct a fraction and the result of the division gives us
1177 the needed digits. The denominator is 1.0 multiplied by the
1178 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1179 123e-6 gives 123 / 1000000. */
1181 int expbit;
1182 int neg_exp;
1183 int more_bits;
1184 mp_limb_t cy;
1185 mp_limb_t *psrc = den;
1186 mp_limb_t *pdest = num;
1187 const struct mp_power *ttab = &_fpioconst_pow10[0];
1189 assert (dig_no > int_no && exponent <= 0);
1192 /* For the fractional part we need not process too many digits. One
1193 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1194 ceil(BITS / 3) =: N
1195 digits we should have enough bits for the result. The remaining
1196 decimal digits give us the information that more bits are following.
1197 This can be used while rounding. (Two added as a safety margin.) */
1198 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1200 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1201 more_bits = 1;
1203 else
1204 more_bits = 0;
1206 neg_exp = dig_no - int_no - exponent;
1208 /* Construct the denominator. */
1209 densize = 0;
1210 expbit = 1;
1213 if ((neg_exp & expbit) != 0)
1215 mp_limb_t cy;
1216 neg_exp ^= expbit;
1218 if (densize == 0)
1220 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1221 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1222 densize * sizeof (mp_limb_t));
1224 else
1226 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1227 + _FPIO_CONST_OFFSET],
1228 ttab->arraysize - _FPIO_CONST_OFFSET,
1229 psrc, densize);
1230 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1231 if (cy == 0)
1232 --densize;
1233 (void) SWAP (psrc, pdest);
1236 expbit <<= 1;
1237 ++ttab;
1239 while (neg_exp != 0);
1241 if (psrc == num)
1242 memcpy (den, num, densize * sizeof (mp_limb_t));
1244 /* Read the fractional digits from the string. */
1245 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1246 #ifndef USE_WIDE_CHAR
1247 , decimal, decimal_len, thousands
1248 #endif
1251 /* We now have to shift both numbers so that the highest bit in the
1252 denominator is set. In the same process we copy the numerator to
1253 a high place in the array so that the division constructs the wanted
1254 digits. This is done by a "quasi fix point" number representation.
1256 num: ddddddddddd . 0000000000000000000000
1257 |--- m ---|
1258 den: ddddddddddd n >= m
1259 |--- n ---|
1262 count_leading_zeros (cnt, den[densize - 1]);
1264 if (cnt > 0)
1266 /* Don't call `mpn_shift' with a count of zero since the specification
1267 does not allow this. */
1268 (void) __mpn_lshift (den, den, densize, cnt);
1269 cy = __mpn_lshift (num, num, numsize, cnt);
1270 if (cy != 0)
1271 num[numsize++] = cy;
1274 /* Now we are ready for the division. But it is not necessary to
1275 do a full multi-precision division because we only need a small
1276 number of bits for the result. So we do not use __mpn_divmod
1277 here but instead do the division here by hand and stop whenever
1278 the needed number of bits is reached. The code itself comes
1279 from the GNU MP Library by Torbj\"orn Granlund. */
1281 exponent = bits;
1283 switch (densize)
1285 case 1:
1287 mp_limb_t d, n, quot;
1288 int used = 0;
1290 n = num[0];
1291 d = den[0];
1292 assert (numsize == 1 && n < d);
1296 udiv_qrnnd (quot, n, n, 0, d);
1298 #define got_limb \
1299 if (bits == 0) \
1301 register int cnt; \
1302 if (quot == 0) \
1303 cnt = BITS_PER_MP_LIMB; \
1304 else \
1305 count_leading_zeros (cnt, quot); \
1306 exponent -= cnt; \
1307 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1309 used = MANT_DIG + cnt; \
1310 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1311 bits = MANT_DIG + 1; \
1313 else \
1315 /* Note that we only clear the second element. */ \
1316 /* The conditional is determined at compile time. */ \
1317 if (RETURN_LIMB_SIZE > 1) \
1318 retval[1] = 0; \
1319 retval[0] = quot; \
1320 bits = -cnt; \
1323 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1324 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1325 quot); \
1326 else \
1328 used = MANT_DIG - bits; \
1329 if (used > 0) \
1330 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1332 bits += BITS_PER_MP_LIMB
1334 got_limb;
1336 while (bits <= MANT_DIG);
1338 return round_and_return (retval, exponent - 1, negative,
1339 quot, BITS_PER_MP_LIMB - 1 - used,
1340 more_bits || n != 0);
1342 case 2:
1344 mp_limb_t d0, d1, n0, n1;
1345 mp_limb_t quot = 0;
1346 int used = 0;
1348 d0 = den[0];
1349 d1 = den[1];
1351 if (numsize < densize)
1353 if (num[0] >= d1)
1355 /* The numerator of the number occupies fewer bits than
1356 the denominator but the one limb is bigger than the
1357 high limb of the numerator. */
1358 n1 = 0;
1359 n0 = num[0];
1361 else
1363 if (bits <= 0)
1364 exponent -= BITS_PER_MP_LIMB;
1365 else
1367 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1368 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1369 BITS_PER_MP_LIMB, 0);
1370 else
1372 used = MANT_DIG - bits;
1373 if (used > 0)
1374 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1376 bits += BITS_PER_MP_LIMB;
1378 n1 = num[0];
1379 n0 = 0;
1382 else
1384 n1 = num[1];
1385 n0 = num[0];
1388 while (bits <= MANT_DIG)
1390 mp_limb_t r;
1392 if (n1 == d1)
1394 /* QUOT should be either 111..111 or 111..110. We need
1395 special treatment of this rare case as normal division
1396 would give overflow. */
1397 quot = ~(mp_limb_t) 0;
1399 r = n0 + d1;
1400 if (r < d1) /* Carry in the addition? */
1402 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1403 goto have_quot;
1405 n1 = d0 - (d0 != 0);
1406 n0 = -d0;
1408 else
1410 udiv_qrnnd (quot, r, n1, n0, d1);
1411 umul_ppmm (n1, n0, d0, quot);
1414 q_test:
1415 if (n1 > r || (n1 == r && n0 > 0))
1417 /* The estimated QUOT was too large. */
1418 --quot;
1420 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1421 r += d1;
1422 if (r >= d1) /* If not carry, test QUOT again. */
1423 goto q_test;
1425 sub_ddmmss (n1, n0, r, 0, n1, n0);
1427 have_quot:
1428 got_limb;
1431 return round_and_return (retval, exponent - 1, negative,
1432 quot, BITS_PER_MP_LIMB - 1 - used,
1433 more_bits || n1 != 0 || n0 != 0);
1435 default:
1437 int i;
1438 mp_limb_t cy, dX, d1, n0, n1;
1439 mp_limb_t quot = 0;
1440 int used = 0;
1442 dX = den[densize - 1];
1443 d1 = den[densize - 2];
1445 /* The division does not work if the upper limb of the two-limb
1446 numerator is greater than the denominator. */
1447 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1448 num[numsize++] = 0;
1450 if (numsize < densize)
1452 mp_size_t empty = densize - numsize;
1453 register int i;
1455 if (bits <= 0)
1456 exponent -= empty * BITS_PER_MP_LIMB;
1457 else
1459 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1461 /* We make a difference here because the compiler
1462 cannot optimize the `else' case that good and
1463 this reflects all currently used FLOAT types
1464 and GMP implementations. */
1465 #if RETURN_LIMB_SIZE <= 2
1466 assert (empty == 1);
1467 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1468 BITS_PER_MP_LIMB, 0);
1469 #else
1470 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1471 retval[i] = retval[i - empty];
1472 while (i >= 0)
1473 retval[i--] = 0;
1474 #endif
1476 else
1478 used = MANT_DIG - bits;
1479 if (used >= BITS_PER_MP_LIMB)
1481 register int i;
1482 (void) __mpn_lshift (&retval[used
1483 / BITS_PER_MP_LIMB],
1484 retval, RETURN_LIMB_SIZE,
1485 used % BITS_PER_MP_LIMB);
1486 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1487 retval[i] = 0;
1489 else if (used > 0)
1490 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1492 bits += empty * BITS_PER_MP_LIMB;
1494 for (i = numsize; i > 0; --i)
1495 num[i + empty] = num[i - 1];
1496 MPN_ZERO (num, empty + 1);
1498 else
1500 int i;
1501 assert (numsize == densize);
1502 for (i = numsize; i > 0; --i)
1503 num[i] = num[i - 1];
1506 den[densize] = 0;
1507 n0 = num[densize];
1509 while (bits <= MANT_DIG)
1511 if (n0 == dX)
1512 /* This might over-estimate QUOT, but it's probably not
1513 worth the extra code here to find out. */
1514 quot = ~(mp_limb_t) 0;
1515 else
1517 mp_limb_t r;
1519 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1520 umul_ppmm (n1, n0, d1, quot);
1522 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1524 --quot;
1525 r += dX;
1526 if (r < dX) /* I.e. "carry in previous addition?" */
1527 break;
1528 n1 -= n0 < d1;
1529 n0 -= d1;
1533 /* Possible optimization: We already have (q * n0) and (1 * n1)
1534 after the calculation of QUOT. Taking advantage of this, we
1535 could make this loop make two iterations less. */
1537 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1539 if (num[densize] != cy)
1541 cy = __mpn_add_n (num, num, den, densize);
1542 assert (cy != 0);
1543 --quot;
1545 n0 = num[densize] = num[densize - 1];
1546 for (i = densize - 1; i > 0; --i)
1547 num[i] = num[i - 1];
1549 got_limb;
1552 for (i = densize; num[i] == 0 && i >= 0; --i)
1554 return round_and_return (retval, exponent - 1, negative,
1555 quot, BITS_PER_MP_LIMB - 1 - used,
1556 more_bits || i >= 0);
1561 /* NOTREACHED */
1563 #if defined _LIBC && !defined USE_WIDE_CHAR
1564 libc_hidden_def (____STRTOF_INTERNAL)
1565 #endif
1567 /* External user entry point. */
1569 FLOAT
1570 #ifdef weak_function
1571 weak_function
1572 #endif
1573 __STRTOF (nptr, endptr, loc)
1574 const STRING_TYPE *nptr;
1575 STRING_TYPE **endptr;
1576 __locale_t loc;
1578 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1580 weak_alias (__STRTOF, STRTOF)
1582 #ifdef LONG_DOUBLE_COMPAT
1583 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1584 # ifdef USE_WIDE_CHAR
1585 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1586 # else
1587 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1588 # endif
1589 # endif
1590 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1591 # ifdef USE_WIDE_CHAR
1592 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1593 # else
1594 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1595 # endif
1596 # endif
1597 #endif