Updated to fedora-glibc-20061219T1804
[glibc.git] / stdlib / strtod_l.c
blobb4e4819c87b0234f6fa1065fbd7226be428df36c
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 && ((CHAR_TYPE) TOLOWER (c) >= L_('a')
667 && (CHAR_TYPE) TOLOWER (c) <= 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 && cp > start_of_digits)
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 of digits in the integer part. Whether these
763 are all or any is really a fractional digit will be decided
764 later. */
765 int_no = dig_no;
766 lead_zero = int_no == 0 ? -1 : 0;
768 /* Read the fractional digits. A special case are the 'american
769 style' numbers like `16.' i.e. with decimal point but without
770 trailing digits. */
771 if (
772 #ifdef USE_WIDE_CHAR
773 c == (wint_t) decimal
774 #else
775 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
776 if (decimal[cnt] != cp[cnt])
777 break;
778 decimal[cnt] == '\0'; })
779 #endif
782 cp += decimal_len;
783 c = *cp;
784 while ((c >= L_('0') && c <= L_('9')) ||
785 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
787 if (c != L_('0') && lead_zero == -1)
788 lead_zero = dig_no - int_no;
789 ++dig_no;
790 c = *++cp;
794 /* Remember start of exponent (if any). */
795 expp = cp;
797 /* Read exponent. */
798 if ((base == 16 && TOLOWER (c) == L_('p'))
799 || (base != 16 && TOLOWER (c) == L_('e')))
801 int exp_negative = 0;
803 c = *++cp;
804 if (c == L_('-'))
806 exp_negative = 1;
807 c = *++cp;
809 else if (c == L_('+'))
810 c = *++cp;
812 if (c >= L_('0') && c <= L_('9'))
814 int exp_limit;
816 /* Get the exponent limit. */
817 if (base == 16)
818 exp_limit = (exp_negative ?
819 -MIN_EXP + MANT_DIG + 4 * int_no :
820 MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
821 else
822 exp_limit = (exp_negative ?
823 -MIN_10_EXP + MANT_DIG + int_no :
824 MAX_10_EXP - int_no + lead_zero + 1);
828 exponent *= 10;
829 exponent += c - L_('0');
831 if (exponent > exp_limit)
832 /* The exponent is too large/small to represent a valid
833 number. */
835 FLOAT result;
837 /* We have to take care for special situation: a joker
838 might have written "0.0e100000" which is in fact
839 zero. */
840 if (lead_zero == -1)
841 result = negative ? -0.0 : 0.0;
842 else
844 /* Overflow or underflow. */
845 __set_errno (ERANGE);
846 result = (exp_negative ? 0.0 :
847 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
850 /* Accept all following digits as part of the exponent. */
852 ++cp;
853 while (*cp >= L_('0') && *cp <= L_('9'));
855 RETURN (result, cp);
856 /* NOTREACHED */
859 c = *++cp;
861 while (c >= L_('0') && c <= L_('9'));
863 if (exp_negative)
864 exponent = -exponent;
866 else
867 cp = expp;
870 /* We don't want to have to work with trailing zeroes after the radix. */
871 if (dig_no > int_no)
873 while (expp[-1] == L_('0'))
875 --expp;
876 --dig_no;
878 assert (dig_no >= int_no);
881 if (dig_no == int_no && dig_no > 0 && exponent < 0)
884 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
885 --expp;
887 if (expp[-1] != L_('0'))
888 break;
890 --expp;
891 --dig_no;
892 --int_no;
893 exponent += base == 16 ? 4 : 1;
895 while (dig_no > 0 && exponent < 0);
897 number_parsed:
899 /* The whole string is parsed. Store the address of the next character. */
900 if (endptr)
901 *endptr = (STRING_TYPE *) cp;
903 if (dig_no == 0)
904 return negative ? -0.0 : 0.0;
906 if (lead_zero)
908 /* Find the decimal point */
909 #ifdef USE_WIDE_CHAR
910 while (*startp != decimal)
911 ++startp;
912 #else
913 while (1)
915 if (*startp == decimal[0])
917 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
918 if (decimal[cnt] != startp[cnt])
919 break;
920 if (decimal[cnt] == '\0')
921 break;
923 ++startp;
925 #endif
926 startp += lead_zero + decimal_len;
927 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
928 dig_no -= lead_zero;
931 /* If the BASE is 16 we can use a simpler algorithm. */
932 if (base == 16)
934 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
935 4, 4, 4, 4, 4, 4, 4, 4 };
936 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
937 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
938 mp_limb_t val;
940 while (!ISXDIGIT (*startp))
941 ++startp;
942 while (*startp == L_('0'))
943 ++startp;
944 if (ISDIGIT (*startp))
945 val = *startp++ - L_('0');
946 else
947 val = 10 + TOLOWER (*startp++) - L_('a');
948 bits = nbits[val];
949 /* We cannot have a leading zero. */
950 assert (bits != 0);
952 if (pos + 1 >= 4 || pos + 1 >= bits)
954 /* We don't have to care for wrapping. This is the normal
955 case so we add the first clause in the `if' expression as
956 an optimization. It is a compile-time constant and so does
957 not cost anything. */
958 retval[idx] = val << (pos - bits + 1);
959 pos -= bits;
961 else
963 retval[idx--] = val >> (bits - pos - 1);
964 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
965 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
968 /* Adjust the exponent for the bits we are shifting in. */
969 exponent += bits - 1 + (int_no - 1) * 4;
971 while (--dig_no > 0 && idx >= 0)
973 if (!ISXDIGIT (*startp))
974 startp += decimal_len;
975 if (ISDIGIT (*startp))
976 val = *startp++ - L_('0');
977 else
978 val = 10 + TOLOWER (*startp++) - L_('a');
980 if (pos + 1 >= 4)
982 retval[idx] |= val << (pos - 4 + 1);
983 pos -= 4;
985 else
987 retval[idx--] |= val >> (4 - pos - 1);
988 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
989 if (idx < 0)
990 return round_and_return (retval, exponent, negative, val,
991 BITS_PER_MP_LIMB - 1, dig_no > 0);
993 retval[idx] = val;
994 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
998 /* We ran out of digits. */
999 MPN_ZERO (retval, idx);
1001 return round_and_return (retval, exponent, negative, 0, 0, 0);
1004 /* Now we have the number of digits in total and the integer digits as well
1005 as the exponent and its sign. We can decide whether the read digits are
1006 really integer digits or belong to the fractional part; i.e. we normalize
1007 123e-2 to 1.23. */
1009 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1010 : MIN (dig_no - int_no, exponent));
1011 int_no += incr;
1012 exponent -= incr;
1015 if (int_no + exponent > MAX_10_EXP + 1)
1017 __set_errno (ERANGE);
1018 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1021 if (exponent < MIN_10_EXP - (DIG + 1))
1023 __set_errno (ERANGE);
1024 return 0.0;
1027 if (int_no > 0)
1029 /* Read the integer part as a multi-precision number to NUM. */
1030 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1031 #ifndef USE_WIDE_CHAR
1032 , decimal, decimal_len, thousands
1033 #endif
1036 if (exponent > 0)
1038 /* We now multiply the gained number by the given power of ten. */
1039 mp_limb_t *psrc = num;
1040 mp_limb_t *pdest = den;
1041 int expbit = 1;
1042 const struct mp_power *ttab = &_fpioconst_pow10[0];
1046 if ((exponent & expbit) != 0)
1048 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1049 mp_limb_t cy;
1050 exponent ^= expbit;
1052 /* FIXME: not the whole multiplication has to be
1053 done. If we have the needed number of bits we
1054 only need the information whether more non-zero
1055 bits follow. */
1056 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1057 cy = __mpn_mul (pdest, psrc, numsize,
1058 &__tens[ttab->arrayoff
1059 + _FPIO_CONST_OFFSET],
1060 size);
1061 else
1062 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1063 + _FPIO_CONST_OFFSET],
1064 size, psrc, numsize);
1065 numsize += size;
1066 if (cy == 0)
1067 --numsize;
1068 (void) SWAP (psrc, pdest);
1070 expbit <<= 1;
1071 ++ttab;
1073 while (exponent != 0);
1075 if (psrc == den)
1076 memcpy (num, den, numsize * sizeof (mp_limb_t));
1079 /* Determine how many bits of the result we already have. */
1080 count_leading_zeros (bits, num[numsize - 1]);
1081 bits = numsize * BITS_PER_MP_LIMB - bits;
1083 /* Now we know the exponent of the number in base two.
1084 Check it against the maximum possible exponent. */
1085 if (bits > MAX_EXP)
1087 __set_errno (ERANGE);
1088 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1091 /* We have already the first BITS bits of the result. Together with
1092 the information whether more non-zero bits follow this is enough
1093 to determine the result. */
1094 if (bits > MANT_DIG)
1096 int i;
1097 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1098 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1099 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1100 : least_idx;
1101 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1102 : least_bit - 1;
1104 if (least_bit == 0)
1105 memcpy (retval, &num[least_idx],
1106 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1107 else
1109 for (i = least_idx; i < numsize - 1; ++i)
1110 retval[i - least_idx] = (num[i] >> least_bit)
1111 | (num[i + 1]
1112 << (BITS_PER_MP_LIMB - least_bit));
1113 if (i - least_idx < RETURN_LIMB_SIZE)
1114 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1117 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1118 for (i = 0; num[i] == 0; ++i)
1121 return round_and_return (retval, bits - 1, negative,
1122 num[round_idx], round_bit,
1123 int_no < dig_no || i < round_idx);
1124 /* NOTREACHED */
1126 else if (dig_no == int_no)
1128 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1129 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1131 if (target_bit == is_bit)
1133 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1134 numsize * sizeof (mp_limb_t));
1135 /* FIXME: the following loop can be avoided if we assume a
1136 maximal MANT_DIG value. */
1137 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1139 else if (target_bit > is_bit)
1141 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1142 num, numsize, target_bit - is_bit);
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
1149 mp_limb_t cy;
1150 assert (numsize < RETURN_LIMB_SIZE);
1152 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1153 num, numsize, is_bit - target_bit);
1154 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1155 /* FIXME: the following loop can be avoided if we assume a
1156 maximal MANT_DIG value. */
1157 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1160 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1161 /* NOTREACHED */
1164 /* Store the bits we already have. */
1165 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1166 #if RETURN_LIMB_SIZE > 1
1167 if (numsize < RETURN_LIMB_SIZE)
1168 # if RETURN_LIMB_SIZE == 2
1169 retval[numsize] = 0;
1170 # else
1171 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1172 # endif
1173 #endif
1176 /* We have to compute at least some of the fractional digits. */
1178 /* We construct a fraction and the result of the division gives us
1179 the needed digits. The denominator is 1.0 multiplied by the
1180 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1181 123e-6 gives 123 / 1000000. */
1183 int expbit;
1184 int neg_exp;
1185 int more_bits;
1186 mp_limb_t cy;
1187 mp_limb_t *psrc = den;
1188 mp_limb_t *pdest = num;
1189 const struct mp_power *ttab = &_fpioconst_pow10[0];
1191 assert (dig_no > int_no && exponent <= 0);
1194 /* For the fractional part we need not process too many digits. One
1195 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1196 ceil(BITS / 3) =: N
1197 digits we should have enough bits for the result. The remaining
1198 decimal digits give us the information that more bits are following.
1199 This can be used while rounding. (Two added as a safety margin.) */
1200 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1202 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1203 more_bits = 1;
1205 else
1206 more_bits = 0;
1208 neg_exp = dig_no - int_no - exponent;
1210 /* Construct the denominator. */
1211 densize = 0;
1212 expbit = 1;
1215 if ((neg_exp & expbit) != 0)
1217 mp_limb_t cy;
1218 neg_exp ^= expbit;
1220 if (densize == 0)
1222 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1223 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1224 densize * sizeof (mp_limb_t));
1226 else
1228 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1229 + _FPIO_CONST_OFFSET],
1230 ttab->arraysize - _FPIO_CONST_OFFSET,
1231 psrc, densize);
1232 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1233 if (cy == 0)
1234 --densize;
1235 (void) SWAP (psrc, pdest);
1238 expbit <<= 1;
1239 ++ttab;
1241 while (neg_exp != 0);
1243 if (psrc == num)
1244 memcpy (den, num, densize * sizeof (mp_limb_t));
1246 /* Read the fractional digits from the string. */
1247 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1248 #ifndef USE_WIDE_CHAR
1249 , decimal, decimal_len, thousands
1250 #endif
1253 /* We now have to shift both numbers so that the highest bit in the
1254 denominator is set. In the same process we copy the numerator to
1255 a high place in the array so that the division constructs the wanted
1256 digits. This is done by a "quasi fix point" number representation.
1258 num: ddddddddddd . 0000000000000000000000
1259 |--- m ---|
1260 den: ddddddddddd n >= m
1261 |--- n ---|
1264 count_leading_zeros (cnt, den[densize - 1]);
1266 if (cnt > 0)
1268 /* Don't call `mpn_shift' with a count of zero since the specification
1269 does not allow this. */
1270 (void) __mpn_lshift (den, den, densize, cnt);
1271 cy = __mpn_lshift (num, num, numsize, cnt);
1272 if (cy != 0)
1273 num[numsize++] = cy;
1276 /* Now we are ready for the division. But it is not necessary to
1277 do a full multi-precision division because we only need a small
1278 number of bits for the result. So we do not use __mpn_divmod
1279 here but instead do the division here by hand and stop whenever
1280 the needed number of bits is reached. The code itself comes
1281 from the GNU MP Library by Torbj\"orn Granlund. */
1283 exponent = bits;
1285 switch (densize)
1287 case 1:
1289 mp_limb_t d, n, quot;
1290 int used = 0;
1292 n = num[0];
1293 d = den[0];
1294 assert (numsize == 1 && n < d);
1298 udiv_qrnnd (quot, n, n, 0, d);
1300 #define got_limb \
1301 if (bits == 0) \
1303 register int cnt; \
1304 if (quot == 0) \
1305 cnt = BITS_PER_MP_LIMB; \
1306 else \
1307 count_leading_zeros (cnt, quot); \
1308 exponent -= cnt; \
1309 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1311 used = MANT_DIG + cnt; \
1312 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1313 bits = MANT_DIG + 1; \
1315 else \
1317 /* Note that we only clear the second element. */ \
1318 /* The conditional is determined at compile time. */ \
1319 if (RETURN_LIMB_SIZE > 1) \
1320 retval[1] = 0; \
1321 retval[0] = quot; \
1322 bits = -cnt; \
1325 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1326 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1327 quot); \
1328 else \
1330 used = MANT_DIG - bits; \
1331 if (used > 0) \
1332 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1334 bits += BITS_PER_MP_LIMB
1336 got_limb;
1338 while (bits <= MANT_DIG);
1340 return round_and_return (retval, exponent - 1, negative,
1341 quot, BITS_PER_MP_LIMB - 1 - used,
1342 more_bits || n != 0);
1344 case 2:
1346 mp_limb_t d0, d1, n0, n1;
1347 mp_limb_t quot = 0;
1348 int used = 0;
1350 d0 = den[0];
1351 d1 = den[1];
1353 if (numsize < densize)
1355 if (num[0] >= d1)
1357 /* The numerator of the number occupies fewer bits than
1358 the denominator but the one limb is bigger than the
1359 high limb of the numerator. */
1360 n1 = 0;
1361 n0 = num[0];
1363 else
1365 if (bits <= 0)
1366 exponent -= BITS_PER_MP_LIMB;
1367 else
1369 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1370 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1371 BITS_PER_MP_LIMB, 0);
1372 else
1374 used = MANT_DIG - bits;
1375 if (used > 0)
1376 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1378 bits += BITS_PER_MP_LIMB;
1380 n1 = num[0];
1381 n0 = 0;
1384 else
1386 n1 = num[1];
1387 n0 = num[0];
1390 while (bits <= MANT_DIG)
1392 mp_limb_t r;
1394 if (n1 == d1)
1396 /* QUOT should be either 111..111 or 111..110. We need
1397 special treatment of this rare case as normal division
1398 would give overflow. */
1399 quot = ~(mp_limb_t) 0;
1401 r = n0 + d1;
1402 if (r < d1) /* Carry in the addition? */
1404 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1405 goto have_quot;
1407 n1 = d0 - (d0 != 0);
1408 n0 = -d0;
1410 else
1412 udiv_qrnnd (quot, r, n1, n0, d1);
1413 umul_ppmm (n1, n0, d0, quot);
1416 q_test:
1417 if (n1 > r || (n1 == r && n0 > 0))
1419 /* The estimated QUOT was too large. */
1420 --quot;
1422 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1423 r += d1;
1424 if (r >= d1) /* If not carry, test QUOT again. */
1425 goto q_test;
1427 sub_ddmmss (n1, n0, r, 0, n1, n0);
1429 have_quot:
1430 got_limb;
1433 return round_and_return (retval, exponent - 1, negative,
1434 quot, BITS_PER_MP_LIMB - 1 - used,
1435 more_bits || n1 != 0 || n0 != 0);
1437 default:
1439 int i;
1440 mp_limb_t cy, dX, d1, n0, n1;
1441 mp_limb_t quot = 0;
1442 int used = 0;
1444 dX = den[densize - 1];
1445 d1 = den[densize - 2];
1447 /* The division does not work if the upper limb of the two-limb
1448 numerator is greater than the denominator. */
1449 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1450 num[numsize++] = 0;
1452 if (numsize < densize)
1454 mp_size_t empty = densize - numsize;
1455 register int i;
1457 if (bits <= 0)
1458 exponent -= empty * BITS_PER_MP_LIMB;
1459 else
1461 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1463 /* We make a difference here because the compiler
1464 cannot optimize the `else' case that good and
1465 this reflects all currently used FLOAT types
1466 and GMP implementations. */
1467 #if RETURN_LIMB_SIZE <= 2
1468 assert (empty == 1);
1469 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1470 BITS_PER_MP_LIMB, 0);
1471 #else
1472 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1473 retval[i] = retval[i - empty];
1474 while (i >= 0)
1475 retval[i--] = 0;
1476 #endif
1478 else
1480 used = MANT_DIG - bits;
1481 if (used >= BITS_PER_MP_LIMB)
1483 register int i;
1484 (void) __mpn_lshift (&retval[used
1485 / BITS_PER_MP_LIMB],
1486 retval, RETURN_LIMB_SIZE,
1487 used % BITS_PER_MP_LIMB);
1488 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1489 retval[i] = 0;
1491 else if (used > 0)
1492 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1494 bits += empty * BITS_PER_MP_LIMB;
1496 for (i = numsize; i > 0; --i)
1497 num[i + empty] = num[i - 1];
1498 MPN_ZERO (num, empty + 1);
1500 else
1502 int i;
1503 assert (numsize == densize);
1504 for (i = numsize; i > 0; --i)
1505 num[i] = num[i - 1];
1508 den[densize] = 0;
1509 n0 = num[densize];
1511 while (bits <= MANT_DIG)
1513 if (n0 == dX)
1514 /* This might over-estimate QUOT, but it's probably not
1515 worth the extra code here to find out. */
1516 quot = ~(mp_limb_t) 0;
1517 else
1519 mp_limb_t r;
1521 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1522 umul_ppmm (n1, n0, d1, quot);
1524 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1526 --quot;
1527 r += dX;
1528 if (r < dX) /* I.e. "carry in previous addition?" */
1529 break;
1530 n1 -= n0 < d1;
1531 n0 -= d1;
1535 /* Possible optimization: We already have (q * n0) and (1 * n1)
1536 after the calculation of QUOT. Taking advantage of this, we
1537 could make this loop make two iterations less. */
1539 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1541 if (num[densize] != cy)
1543 cy = __mpn_add_n (num, num, den, densize);
1544 assert (cy != 0);
1545 --quot;
1547 n0 = num[densize] = num[densize - 1];
1548 for (i = densize - 1; i > 0; --i)
1549 num[i] = num[i - 1];
1551 got_limb;
1554 for (i = densize; num[i] == 0 && i >= 0; --i)
1556 return round_and_return (retval, exponent - 1, negative,
1557 quot, BITS_PER_MP_LIMB - 1 - used,
1558 more_bits || i >= 0);
1563 /* NOTREACHED */
1565 #if defined _LIBC && !defined USE_WIDE_CHAR
1566 libc_hidden_def (____STRTOF_INTERNAL)
1567 #endif
1569 /* External user entry point. */
1571 FLOAT
1572 #ifdef weak_function
1573 weak_function
1574 #endif
1575 __STRTOF (nptr, endptr, loc)
1576 const STRING_TYPE *nptr;
1577 STRING_TYPE **endptr;
1578 __locale_t loc;
1580 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1582 weak_alias (__STRTOF, STRTOF)
1584 #ifdef LONG_DOUBLE_COMPAT
1585 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1586 # ifdef USE_WIDE_CHAR
1587 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1588 # else
1589 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1590 # endif
1591 # endif
1592 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1593 # ifdef USE_WIDE_CHAR
1594 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1595 # else
1596 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1597 # endif
1598 # endif
1599 #endif