* nscd/grpcache.c: Include <sys/socket.h>.
[glibc.git] / stdlib / strtod_l.c
blob3a1c1ebd4b6a3e6396ec95cb7da34ed2b46d6c0a
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,98,2002,2004,2005 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 TOLOWER_C(Ch) __towlower_l ((Ch), &_nl_C_locobj)
104 # define STRNCASECMP(S1, S2, N) \
105 __wcsncasecmp_l ((S1), (S2), (N), &_nl_C_locobj)
106 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
107 #else
108 # define STRING_TYPE char
109 # define CHAR_TYPE char
110 # define L_(Ch) Ch
111 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
112 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
113 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
114 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
115 # define TOLOWER_C(Ch) __tolower_l ((Ch), &_nl_C_locobj)
116 # define STRNCASECMP(S1, S2, N) \
117 __strncasecmp_l ((S1), (S2), (N), &_nl_C_locobj)
118 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
119 #endif
122 /* Constants we need from float.h; select the set for the FLOAT precision. */
123 #define MANT_DIG PASTE(FLT,_MANT_DIG)
124 #define DIG PASTE(FLT,_DIG)
125 #define MAX_EXP PASTE(FLT,_MAX_EXP)
126 #define MIN_EXP PASTE(FLT,_MIN_EXP)
127 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
128 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
130 /* Extra macros required to get FLT expanded before the pasting. */
131 #define PASTE(a,b) PASTE1(a,b)
132 #define PASTE1(a,b) a##b
134 /* Function to construct a floating point number from an MP integer
135 containing the fraction bits, a base 2 exponent, and a sign flag. */
136 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
138 /* Definitions according to limb size used. */
139 #if BITS_PER_MP_LIMB == 32
140 # define MAX_DIG_PER_LIMB 9
141 # define MAX_FAC_PER_LIMB 1000000000UL
142 #elif BITS_PER_MP_LIMB == 64
143 # define MAX_DIG_PER_LIMB 19
144 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
145 #else
146 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
147 #endif
150 /* Local data structure. */
151 static const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
152 { 0, 10, 100,
153 1000, 10000, 100000L,
154 1000000L, 10000000L, 100000000L,
155 1000000000L
156 #if BITS_PER_MP_LIMB > 32
157 , 10000000000ULL, 100000000000ULL,
158 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
159 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
160 1000000000000000000ULL, 10000000000000000000ULL
161 #endif
162 #if BITS_PER_MP_LIMB > 64
163 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
164 #endif
167 #ifndef howmany
168 #define howmany(x,y) (((x)+((y)-1))/(y))
169 #endif
170 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
172 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
173 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
174 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
176 #define RETURN(val,end) \
177 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
178 return val; } while (0)
180 /* Maximum size necessary for mpn integers to hold floating point numbers. */
181 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
182 + 2)
183 /* Declare an mpn integer variable that big. */
184 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
185 /* Copy an mpn integer value. */
186 #define MPN_ASSIGN(dst, src) \
187 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
190 /* Return a floating point number of the needed type according to the given
191 multi-precision number after possible rounding. */
192 static FLOAT
193 round_and_return (mp_limb_t *retval, int exponent, int negative,
194 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
196 if (exponent < MIN_EXP - 1)
198 mp_size_t shift = MIN_EXP - 1 - exponent;
200 if (shift > MANT_DIG)
202 __set_errno (EDOM);
203 return 0.0;
206 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
207 if (shift == MANT_DIG)
208 /* This is a special case to handle the very seldom case where
209 the mantissa will be empty after the shift. */
211 int i;
213 round_limb = retval[RETURN_LIMB_SIZE - 1];
214 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
215 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
216 more_bits |= retval[i] != 0;
217 MPN_ZERO (retval, RETURN_LIMB_SIZE);
219 else if (shift >= BITS_PER_MP_LIMB)
221 int i;
223 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
224 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
225 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
226 more_bits |= retval[i] != 0;
227 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
228 != 0);
230 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
231 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
232 shift % BITS_PER_MP_LIMB);
233 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
234 shift / BITS_PER_MP_LIMB);
236 else if (shift > 0)
238 round_limb = retval[0];
239 round_bit = shift - 1;
240 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
242 /* This is a hook for the m68k long double format, where the
243 exponent bias is the same for normalized and denormalized
244 numbers. */
245 #ifndef DENORM_EXP
246 # define DENORM_EXP (MIN_EXP - 2)
247 #endif
248 exponent = DENORM_EXP;
251 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
252 && (more_bits || (retval[0] & 1) != 0
253 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
255 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
257 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
258 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
259 (retval[RETURN_LIMB_SIZE - 1]
260 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
262 ++exponent;
263 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
264 retval[RETURN_LIMB_SIZE - 1]
265 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
267 else if (exponent == DENORM_EXP
268 && (retval[RETURN_LIMB_SIZE - 1]
269 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
270 != 0)
271 /* The number was denormalized but now normalized. */
272 exponent = MIN_EXP - 1;
275 if (exponent > MAX_EXP)
276 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
278 return MPN2FLOAT (retval, exponent, negative);
282 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
283 into N. Return the size of the number limbs in NSIZE at the first
284 character od the string that is not part of the integer as the function
285 value. If the EXPONENT is small enough to be taken as an additional
286 factor for the resulting number (see code) multiply by it. */
287 static const STRING_TYPE *
288 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
289 int *exponent
290 #ifndef USE_WIDE_CHAR
291 , const char *decimal, size_t decimal_len, const char *thousands
292 #endif
296 /* Number of digits for actual limb. */
297 int cnt = 0;
298 mp_limb_t low = 0;
299 mp_limb_t start;
301 *nsize = 0;
302 assert (digcnt > 0);
305 if (cnt == MAX_DIG_PER_LIMB)
307 if (*nsize == 0)
309 n[0] = low;
310 *nsize = 1;
312 else
314 mp_limb_t cy;
315 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
316 cy += __mpn_add_1 (n, n, *nsize, low);
317 if (cy != 0)
319 n[*nsize] = cy;
320 ++(*nsize);
323 cnt = 0;
324 low = 0;
327 /* There might be thousands separators or radix characters in
328 the string. But these all can be ignored because we know the
329 format of the number is correct and we have an exact number
330 of characters to read. */
331 #ifdef USE_WIDE_CHAR
332 if (*str < L'0' || *str > L'9')
333 ++str;
334 #else
335 if (*str < '0' || *str > '9')
337 int inner = 0;
338 if (thousands != NULL && *str == *thousands
339 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
340 if (thousands[inner] != str[inner])
341 break;
342 thousands[inner] == '\0'; }))
343 str += inner;
344 else
345 str += decimal_len;
347 #endif
348 low = low * 10 + *str++ - L_('0');
349 ++cnt;
351 while (--digcnt > 0);
353 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
355 low *= _tens_in_limb[*exponent];
356 start = _tens_in_limb[cnt + *exponent];
357 *exponent = 0;
359 else
360 start = _tens_in_limb[cnt];
362 if (*nsize == 0)
364 n[0] = low;
365 *nsize = 1;
367 else
369 mp_limb_t cy;
370 cy = __mpn_mul_1 (n, n, *nsize, start);
371 cy += __mpn_add_1 (n, n, *nsize, low);
372 if (cy != 0)
373 n[(*nsize)++] = cy;
376 return str;
380 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
381 with the COUNT most significant bits of LIMB.
383 Tege doesn't like this function so I have to write it here myself. :)
384 --drepper */
385 static inline void
386 __attribute ((always_inline))
387 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
388 mp_limb_t limb)
390 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
392 /* Optimize the case of shifting by exactly a word:
393 just copy words, with no actual bit-shifting. */
394 mp_size_t i;
395 for (i = size - 1; i > 0; --i)
396 ptr[i] = ptr[i - 1];
397 ptr[0] = limb;
399 else
401 (void) __mpn_lshift (ptr, ptr, size, count);
402 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
407 #define INTERNAL(x) INTERNAL1(x)
408 #define INTERNAL1(x) __##x##_internal
410 /* This file defines a function to check for correct grouping. */
411 #include "grouping.h"
414 /* Return a floating point number with the value of the given string NPTR.
415 Set *ENDPTR to the character after the last used one. If the number is
416 smaller than the smallest representable number, set `errno' to ERANGE and
417 return 0.0. If the number is too big to be represented, set `errno' to
418 ERANGE and return HUGE_VAL with the appropriate sign. */
419 FLOAT
420 INTERNAL (__STRTOF) (nptr, endptr, group, loc)
421 const STRING_TYPE *nptr;
422 STRING_TYPE **endptr;
423 int group;
424 __locale_t loc;
426 int negative; /* The sign of the number. */
427 MPN_VAR (num); /* MP representation of the number. */
428 int exponent; /* Exponent of the number. */
430 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
431 int base = 10;
433 /* When we have to compute fractional digits we form a fraction with a
434 second multi-precision number (and we sometimes need a second for
435 temporary results). */
436 MPN_VAR (den);
438 /* Representation for the return value. */
439 mp_limb_t retval[RETURN_LIMB_SIZE];
440 /* Number of bits currently in result value. */
441 int bits;
443 /* Running pointer after the last character processed in the string. */
444 const STRING_TYPE *cp, *tp;
445 /* Start of significant part of the number. */
446 const STRING_TYPE *startp, *start_of_digits;
447 /* Points at the character following the integer and fractional digits. */
448 const STRING_TYPE *expp;
449 /* Total number of digit and number of digits in integer part. */
450 int dig_no, int_no, lead_zero;
451 /* Contains the last character read. */
452 CHAR_TYPE c;
454 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
455 there. So define it ourselves if it remains undefined. */
456 #ifndef _WINT_T
457 typedef unsigned int wint_t;
458 #endif
459 /* The radix character of the current locale. */
460 #ifdef USE_WIDE_CHAR
461 wchar_t decimal;
462 #else
463 const char *decimal;
464 size_t decimal_len;
465 #endif
466 /* The thousands character of the current locale. */
467 #ifdef USE_WIDE_CHAR
468 wchar_t thousands = L'\0';
469 #else
470 const char *thousands = NULL;
471 #endif
472 /* The numeric grouping specification of the current locale,
473 in the format described in <locale.h>. */
474 const char *grouping;
475 /* Used in several places. */
476 int cnt;
478 struct locale_data *current = loc->__locales[LC_NUMERIC];
480 if (group)
482 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
483 if (*grouping <= 0 || *grouping == CHAR_MAX)
484 grouping = NULL;
485 else
487 /* Figure out the thousands separator character. */
488 #ifdef USE_WIDE_CHAR
489 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
490 _NL_NUMERIC_THOUSANDS_SEP_WC);
491 if (thousands == L'\0')
492 grouping = NULL;
493 #else
494 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
495 if (*thousands == '\0')
497 thousands = NULL;
498 grouping = NULL;
500 #endif
503 else
504 grouping = NULL;
506 /* Find the locale's decimal point character. */
507 #ifdef USE_WIDE_CHAR
508 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
509 assert (decimal != L'\0');
510 # define decimal_len 1
511 #else
512 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
513 decimal_len = strlen (decimal);
514 assert (decimal_len > 0);
515 #endif
517 /* Prepare number representation. */
518 exponent = 0;
519 negative = 0;
520 bits = 0;
522 /* Parse string to get maximal legal prefix. We need the number of
523 characters of the integer part, the fractional part and the exponent. */
524 cp = nptr - 1;
525 /* Ignore leading white space. */
527 c = *++cp;
528 while (ISSPACE (c));
530 /* Get sign of the result. */
531 if (c == L_('-'))
533 negative = 1;
534 c = *++cp;
536 else if (c == L_('+'))
537 c = *++cp;
539 /* Return 0.0 if no legal string is found.
540 No character is used even if a sign was found. */
541 #ifdef USE_WIDE_CHAR
542 if (c == (wint_t) decimal
543 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
545 /* We accept it. This funny construct is here only to indent
546 the code directly. */
548 #else
549 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
550 if (cp[cnt] != decimal[cnt])
551 break;
552 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
554 /* We accept it. This funny construct is here only to indent
555 the code directly. */
557 #endif
558 else if (c < L_('0') || c > L_('9'))
560 /* Check for `INF' or `INFINITY'. */
561 if (TOLOWER_C (c) == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
563 /* Return +/- infinity. */
564 if (endptr != NULL)
565 *endptr = (STRING_TYPE *)
566 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
567 ? 8 : 3));
569 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
572 if (TOLOWER_C (c) == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
574 /* Return NaN. */
575 FLOAT retval = NAN;
577 cp += 3;
579 /* Match `(n-char-sequence-digit)'. */
580 if (*cp == L_('('))
582 const STRING_TYPE *startp = cp;
584 ++cp;
585 while ((*cp >= L_('0') && *cp <= L_('9'))
586 || (TOLOWER (*cp) >= L_('a') && TOLOWER (*cp) <= L_('z'))
587 || *cp == L_('_'));
589 if (*cp != L_(')'))
590 /* The closing brace is missing. Only match the NAN
591 part. */
592 cp = startp;
593 else
595 /* This is a system-dependent way to specify the
596 bitmask used for the NaN. We expect it to be
597 a number which is put in the mantissa of the
598 number. */
599 STRING_TYPE *endp;
600 unsigned long long int mant;
602 mant = STRTOULL (startp + 1, &endp, 0);
603 if (endp == cp)
604 SET_MANTISSA (retval, mant);
608 if (endptr != NULL)
609 *endptr = (STRING_TYPE *) cp;
611 return retval;
614 /* It is really a text we do not recognize. */
615 RETURN (0.0, nptr);
618 /* First look whether we are faced with a hexadecimal number. */
619 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
621 /* Okay, it is a hexa-decimal number. Remember this and skip
622 the characters. BTW: hexadecimal numbers must not be
623 grouped. */
624 base = 16;
625 cp += 2;
626 c = *cp;
627 grouping = NULL;
630 /* Record the start of the digits, in case we will check their grouping. */
631 start_of_digits = startp = cp;
633 /* Ignore leading zeroes. This helps us to avoid useless computations. */
634 #ifdef USE_WIDE_CHAR
635 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
636 c = *++cp;
637 #else
638 if (thousands == NULL)
639 while (c == '0')
640 c = *++cp;
641 else
643 /* We also have the multibyte thousands string. */
644 while (1)
646 if (c != '0')
648 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
649 if (c != thousands[cnt])
650 break;
651 if (thousands[cnt] != '\0')
652 break;
654 c = *++cp;
657 #endif
659 /* If no other digit but a '0' is found the result is 0.0.
660 Return current read pointer. */
661 if ((c < L_('0') || c > L_('9'))
662 && (base == 16 && (c < (CHAR_TYPE) TOLOWER (L_('a'))
663 || c > (CHAR_TYPE) TOLOWER (L_('f'))))
664 #ifdef USE_WIDE_CHAR
665 && c != (wint_t) decimal
666 #else
667 && ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
668 if (decimal[cnt] != cp[cnt])
669 break;
670 decimal[cnt] != '\0'; })
671 #endif
672 && (base == 16 && (cp == start_of_digits
673 || (CHAR_TYPE) TOLOWER (c) != L_('p')))
674 && (base != 16 && (CHAR_TYPE) TOLOWER (c) != L_('e')))
676 #ifdef USE_WIDE_CHAR
677 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
678 grouping);
679 #else
680 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
681 grouping);
682 #endif
683 /* If TP is at the start of the digits, there was no correctly
684 grouped prefix of the string; so no number found. */
685 RETURN (0.0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
688 /* Remember first significant digit and read following characters until the
689 decimal point, exponent character or any non-FP number character. */
690 startp = cp;
691 dig_no = 0;
692 while (1)
694 if ((c >= L_('0') && c <= L_('9'))
695 || (base == 16 && (wint_t) TOLOWER (c) >= L_('a')
696 && (wint_t) TOLOWER (c) <= L_('f')))
697 ++dig_no;
698 else
700 #ifdef USE_WIDE_CHAR
701 if ((wint_t) thousands == L'\0' || c != (wint_t) thousands)
702 /* Not a digit or separator: end of the integer part. */
703 break;
704 #else
705 if (thousands == NULL)
706 break;
707 else
709 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
710 if (thousands[cnt] != cp[cnt])
711 break;
712 if (thousands[cnt] != '\0')
713 break;
715 #endif
717 c = *++cp;
720 if (grouping && dig_no > 0)
722 /* Check the grouping of the digits. */
723 #ifdef USE_WIDE_CHAR
724 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
725 grouping);
726 #else
727 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
728 grouping);
729 #endif
730 if (cp != tp)
732 /* Less than the entire string was correctly grouped. */
734 if (tp == start_of_digits)
735 /* No valid group of numbers at all: no valid number. */
736 RETURN (0.0, nptr);
738 if (tp < startp)
739 /* The number is validly grouped, but consists
740 only of zeroes. The whole value is zero. */
741 RETURN (0.0, tp);
743 /* Recompute DIG_NO so we won't read more digits than
744 are properly grouped. */
745 cp = tp;
746 dig_no = 0;
747 for (tp = startp; tp < cp; ++tp)
748 if (*tp >= L_('0') && *tp <= L_('9'))
749 ++dig_no;
751 int_no = dig_no;
752 lead_zero = 0;
754 goto number_parsed;
758 /* We have the number digits in the integer part. Whether these are all or
759 any is really a fractional digit will be decided later. */
760 int_no = dig_no;
761 lead_zero = int_no == 0 ? -1 : 0;
763 /* Read the fractional digits. A special case are the 'american style'
764 numbers like `16.' i.e. with decimal but without trailing digits. */
765 if (
766 #ifdef USE_WIDE_CHAR
767 c == (wint_t) decimal
768 #else
769 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
770 if (decimal[cnt] != cp[cnt])
771 break;
772 decimal[cnt] == '\0'; })
773 #endif
776 cp += decimal_len;
777 c = *cp;
778 while ((c >= L_('0') && c <= L_('9')) ||
779 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
781 if (c != L_('0') && lead_zero == -1)
782 lead_zero = dig_no - int_no;
783 ++dig_no;
784 c = *++cp;
788 /* Remember start of exponent (if any). */
789 expp = cp;
791 /* Read exponent. */
792 if ((base == 16 && TOLOWER (c) == L_('p'))
793 || (base != 16 && TOLOWER (c) == L_('e')))
795 int exp_negative = 0;
797 c = *++cp;
798 if (c == L_('-'))
800 exp_negative = 1;
801 c = *++cp;
803 else if (c == L_('+'))
804 c = *++cp;
806 if (c >= L_('0') && c <= L_('9'))
808 int exp_limit;
810 /* Get the exponent limit. */
811 if (base == 16)
812 exp_limit = (exp_negative ?
813 -MIN_EXP + MANT_DIG + 4 * int_no :
814 MAX_EXP - 4 * int_no + lead_zero);
815 else
816 exp_limit = (exp_negative ?
817 -MIN_10_EXP + MANT_DIG + int_no :
818 MAX_10_EXP - int_no + lead_zero);
822 exponent *= 10;
824 if (exponent > exp_limit)
825 /* The exponent is too large/small to represent a valid
826 number. */
828 FLOAT result;
830 /* We have to take care for special situation: a joker
831 might have written "0.0e100000" which is in fact
832 zero. */
833 if (lead_zero == -1)
834 result = negative ? -0.0 : 0.0;
835 else
837 /* Overflow or underflow. */
838 __set_errno (ERANGE);
839 result = (exp_negative ? 0.0 :
840 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
843 /* Accept all following digits as part of the exponent. */
845 ++cp;
846 while (*cp >= L_('0') && *cp <= L_('9'));
848 RETURN (result, cp);
849 /* NOTREACHED */
852 exponent += c - L_('0');
853 c = *++cp;
855 while (c >= L_('0') && c <= L_('9'));
857 if (exp_negative)
858 exponent = -exponent;
860 else
861 cp = expp;
864 /* We don't want to have to work with trailing zeroes after the radix. */
865 if (dig_no > int_no)
867 while (expp[-1] == L_('0'))
869 --expp;
870 --dig_no;
872 assert (dig_no >= int_no);
875 if (dig_no == int_no && dig_no > 0 && exponent < 0)
878 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
879 --expp;
881 if (expp[-1] != L_('0'))
882 break;
884 --expp;
885 --dig_no;
886 --int_no;
887 ++exponent;
889 while (dig_no > 0 && exponent < 0);
891 number_parsed:
893 /* The whole string is parsed. Store the address of the next character. */
894 if (endptr)
895 *endptr = (STRING_TYPE *) cp;
897 if (dig_no == 0)
898 return negative ? -0.0 : 0.0;
900 if (lead_zero)
902 /* Find the decimal point */
903 #ifdef USE_WIDE_CHAR
904 while (*startp != decimal)
905 ++startp;
906 #else
907 while (1)
909 if (*startp == decimal[0])
911 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
912 if (decimal[cnt] != startp[cnt])
913 break;
914 if (decimal[cnt] == '\0')
915 break;
917 ++startp;
919 #endif
920 startp += lead_zero + decimal_len;
921 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
922 dig_no -= lead_zero;
925 /* If the BASE is 16 we can use a simpler algorithm. */
926 if (base == 16)
928 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
929 4, 4, 4, 4, 4, 4, 4, 4 };
930 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
931 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
932 mp_limb_t val;
934 while (!ISXDIGIT (*startp))
935 ++startp;
936 while (*startp == L_('0'))
937 ++startp;
938 if (ISDIGIT (*startp))
939 val = *startp++ - L_('0');
940 else
941 val = 10 + TOLOWER (*startp++) - L_('a');
942 bits = nbits[val];
943 /* We cannot have a leading zero. */
944 assert (bits != 0);
946 if (pos + 1 >= 4 || pos + 1 >= bits)
948 /* We don't have to care for wrapping. This is the normal
949 case so we add the first clause in the `if' expression as
950 an optimization. It is a compile-time constant and so does
951 not cost anything. */
952 retval[idx] = val << (pos - bits + 1);
953 pos -= bits;
955 else
957 retval[idx--] = val >> (bits - pos - 1);
958 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
959 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
962 /* Adjust the exponent for the bits we are shifting in. */
963 exponent += bits - 1 + (int_no - 1) * 4;
965 while (--dig_no > 0 && idx >= 0)
967 if (!ISXDIGIT (*startp))
968 startp += decimal_len;
969 if (ISDIGIT (*startp))
970 val = *startp++ - L_('0');
971 else
972 val = 10 + TOLOWER (*startp++) - L_('a');
974 if (pos + 1 >= 4)
976 retval[idx] |= val << (pos - 4 + 1);
977 pos -= 4;
979 else
981 retval[idx--] |= val >> (4 - pos - 1);
982 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
983 if (idx < 0)
984 return round_and_return (retval, exponent, negative, val,
985 BITS_PER_MP_LIMB - 1, dig_no > 0);
987 retval[idx] = val;
988 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
992 /* We ran out of digits. */
993 MPN_ZERO (retval, idx);
995 return round_and_return (retval, exponent, negative, 0, 0, 0);
998 /* Now we have the number of digits in total and the integer digits as well
999 as the exponent and its sign. We can decide whether the read digits are
1000 really integer digits or belong to the fractional part; i.e. we normalize
1001 123e-2 to 1.23. */
1003 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1004 : MIN (dig_no - int_no, exponent));
1005 int_no += incr;
1006 exponent -= incr;
1009 if (int_no + exponent > MAX_10_EXP + 1)
1011 __set_errno (ERANGE);
1012 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1015 if (exponent < MIN_10_EXP - (DIG + 1))
1017 __set_errno (ERANGE);
1018 return 0.0;
1021 if (int_no > 0)
1023 /* Read the integer part as a multi-precision number to NUM. */
1024 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1025 #ifndef USE_WIDE_CHAR
1026 , decimal, decimal_len, thousands
1027 #endif
1030 if (exponent > 0)
1032 /* We now multiply the gained number by the given power of ten. */
1033 mp_limb_t *psrc = num;
1034 mp_limb_t *pdest = den;
1035 int expbit = 1;
1036 const struct mp_power *ttab = &_fpioconst_pow10[0];
1040 if ((exponent & expbit) != 0)
1042 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1043 mp_limb_t cy;
1044 exponent ^= expbit;
1046 /* FIXME: not the whole multiplication has to be
1047 done. If we have the needed number of bits we
1048 only need the information whether more non-zero
1049 bits follow. */
1050 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1051 cy = __mpn_mul (pdest, psrc, numsize,
1052 &__tens[ttab->arrayoff
1053 + _FPIO_CONST_OFFSET],
1054 size);
1055 else
1056 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1057 + _FPIO_CONST_OFFSET],
1058 size, psrc, numsize);
1059 numsize += size;
1060 if (cy == 0)
1061 --numsize;
1062 (void) SWAP (psrc, pdest);
1064 expbit <<= 1;
1065 ++ttab;
1067 while (exponent != 0);
1069 if (psrc == den)
1070 memcpy (num, den, numsize * sizeof (mp_limb_t));
1073 /* Determine how many bits of the result we already have. */
1074 count_leading_zeros (bits, num[numsize - 1]);
1075 bits = numsize * BITS_PER_MP_LIMB - bits;
1077 /* Now we know the exponent of the number in base two.
1078 Check it against the maximum possible exponent. */
1079 if (bits > MAX_EXP)
1081 __set_errno (ERANGE);
1082 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1085 /* We have already the first BITS bits of the result. Together with
1086 the information whether more non-zero bits follow this is enough
1087 to determine the result. */
1088 if (bits > MANT_DIG)
1090 int i;
1091 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1092 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1093 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1094 : least_idx;
1095 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1096 : least_bit - 1;
1098 if (least_bit == 0)
1099 memcpy (retval, &num[least_idx],
1100 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1101 else
1103 for (i = least_idx; i < numsize - 1; ++i)
1104 retval[i - least_idx] = (num[i] >> least_bit)
1105 | (num[i + 1]
1106 << (BITS_PER_MP_LIMB - least_bit));
1107 if (i - least_idx < RETURN_LIMB_SIZE)
1108 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1111 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1112 for (i = 0; num[i] == 0; ++i)
1115 return round_and_return (retval, bits - 1, negative,
1116 num[round_idx], round_bit,
1117 int_no < dig_no || i < round_idx);
1118 /* NOTREACHED */
1120 else if (dig_no == int_no)
1122 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1123 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1125 if (target_bit == is_bit)
1127 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1128 numsize * sizeof (mp_limb_t));
1129 /* FIXME: the following loop can be avoided if we assume a
1130 maximal MANT_DIG value. */
1131 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1133 else if (target_bit > is_bit)
1135 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1136 num, numsize, target_bit - is_bit);
1137 /* FIXME: the following loop can be avoided if we assume a
1138 maximal MANT_DIG value. */
1139 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1141 else
1143 mp_limb_t cy;
1144 assert (numsize < RETURN_LIMB_SIZE);
1146 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1147 num, numsize, is_bit - target_bit);
1148 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1149 /* FIXME: the following loop can be avoided if we assume a
1150 maximal MANT_DIG value. */
1151 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1154 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1155 /* NOTREACHED */
1158 /* Store the bits we already have. */
1159 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1160 #if RETURN_LIMB_SIZE > 1
1161 if (numsize < RETURN_LIMB_SIZE)
1162 # if RETURN_LIMB_SIZE == 2
1163 retval[numsize] = 0;
1164 # else
1165 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1166 # endif
1167 #endif
1170 /* We have to compute at least some of the fractional digits. */
1172 /* We construct a fraction and the result of the division gives us
1173 the needed digits. The denominator is 1.0 multiplied by the
1174 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1175 123e-6 gives 123 / 1000000. */
1177 int expbit;
1178 int neg_exp;
1179 int more_bits;
1180 mp_limb_t cy;
1181 mp_limb_t *psrc = den;
1182 mp_limb_t *pdest = num;
1183 const struct mp_power *ttab = &_fpioconst_pow10[0];
1185 assert (dig_no > int_no && exponent <= 0);
1188 /* For the fractional part we need not process too many digits. One
1189 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1190 ceil(BITS / 3) =: N
1191 digits we should have enough bits for the result. The remaining
1192 decimal digits give us the information that more bits are following.
1193 This can be used while rounding. (Two added as a safety margin.) */
1194 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1196 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1197 more_bits = 1;
1199 else
1200 more_bits = 0;
1202 neg_exp = dig_no - int_no - exponent;
1204 /* Construct the denominator. */
1205 densize = 0;
1206 expbit = 1;
1209 if ((neg_exp & expbit) != 0)
1211 mp_limb_t cy;
1212 neg_exp ^= expbit;
1214 if (densize == 0)
1216 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1217 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1218 densize * sizeof (mp_limb_t));
1220 else
1222 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1223 + _FPIO_CONST_OFFSET],
1224 ttab->arraysize - _FPIO_CONST_OFFSET,
1225 psrc, densize);
1226 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1227 if (cy == 0)
1228 --densize;
1229 (void) SWAP (psrc, pdest);
1232 expbit <<= 1;
1233 ++ttab;
1235 while (neg_exp != 0);
1237 if (psrc == num)
1238 memcpy (den, num, densize * sizeof (mp_limb_t));
1240 /* Read the fractional digits from the string. */
1241 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1242 #ifndef USE_WIDE_CHAR
1243 , decimal, decimal_len, thousands
1244 #endif
1247 /* We now have to shift both numbers so that the highest bit in the
1248 denominator is set. In the same process we copy the numerator to
1249 a high place in the array so that the division constructs the wanted
1250 digits. This is done by a "quasi fix point" number representation.
1252 num: ddddddddddd . 0000000000000000000000
1253 |--- m ---|
1254 den: ddddddddddd n >= m
1255 |--- n ---|
1258 count_leading_zeros (cnt, den[densize - 1]);
1260 if (cnt > 0)
1262 /* Don't call `mpn_shift' with a count of zero since the specification
1263 does not allow this. */
1264 (void) __mpn_lshift (den, den, densize, cnt);
1265 cy = __mpn_lshift (num, num, numsize, cnt);
1266 if (cy != 0)
1267 num[numsize++] = cy;
1270 /* Now we are ready for the division. But it is not necessary to
1271 do a full multi-precision division because we only need a small
1272 number of bits for the result. So we do not use __mpn_divmod
1273 here but instead do the division here by hand and stop whenever
1274 the needed number of bits is reached. The code itself comes
1275 from the GNU MP Library by Torbj\"orn Granlund. */
1277 exponent = bits;
1279 switch (densize)
1281 case 1:
1283 mp_limb_t d, n, quot;
1284 int used = 0;
1286 n = num[0];
1287 d = den[0];
1288 assert (numsize == 1 && n < d);
1292 udiv_qrnnd (quot, n, n, 0, d);
1294 #define got_limb \
1295 if (bits == 0) \
1297 register int cnt; \
1298 if (quot == 0) \
1299 cnt = BITS_PER_MP_LIMB; \
1300 else \
1301 count_leading_zeros (cnt, quot); \
1302 exponent -= cnt; \
1303 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1305 used = MANT_DIG + cnt; \
1306 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1307 bits = MANT_DIG + 1; \
1309 else \
1311 /* Note that we only clear the second element. */ \
1312 /* The conditional is determined at compile time. */ \
1313 if (RETURN_LIMB_SIZE > 1) \
1314 retval[1] = 0; \
1315 retval[0] = quot; \
1316 bits = -cnt; \
1319 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1320 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1321 quot); \
1322 else \
1324 used = MANT_DIG - bits; \
1325 if (used > 0) \
1326 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1328 bits += BITS_PER_MP_LIMB
1330 got_limb;
1332 while (bits <= MANT_DIG);
1334 return round_and_return (retval, exponent - 1, negative,
1335 quot, BITS_PER_MP_LIMB - 1 - used,
1336 more_bits || n != 0);
1338 case 2:
1340 mp_limb_t d0, d1, n0, n1;
1341 mp_limb_t quot = 0;
1342 int used = 0;
1344 d0 = den[0];
1345 d1 = den[1];
1347 if (numsize < densize)
1349 if (num[0] >= d1)
1351 /* The numerator of the number occupies fewer bits than
1352 the denominator but the one limb is bigger than the
1353 high limb of the numerator. */
1354 n1 = 0;
1355 n0 = num[0];
1357 else
1359 if (bits <= 0)
1360 exponent -= BITS_PER_MP_LIMB;
1361 else
1363 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1364 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1365 BITS_PER_MP_LIMB, 0);
1366 else
1368 used = MANT_DIG - bits;
1369 if (used > 0)
1370 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1372 bits += BITS_PER_MP_LIMB;
1374 n1 = num[0];
1375 n0 = 0;
1378 else
1380 n1 = num[1];
1381 n0 = num[0];
1384 while (bits <= MANT_DIG)
1386 mp_limb_t r;
1388 if (n1 == d1)
1390 /* QUOT should be either 111..111 or 111..110. We need
1391 special treatment of this rare case as normal division
1392 would give overflow. */
1393 quot = ~(mp_limb_t) 0;
1395 r = n0 + d1;
1396 if (r < d1) /* Carry in the addition? */
1398 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1399 goto have_quot;
1401 n1 = d0 - (d0 != 0);
1402 n0 = -d0;
1404 else
1406 udiv_qrnnd (quot, r, n1, n0, d1);
1407 umul_ppmm (n1, n0, d0, quot);
1410 q_test:
1411 if (n1 > r || (n1 == r && n0 > 0))
1413 /* The estimated QUOT was too large. */
1414 --quot;
1416 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1417 r += d1;
1418 if (r >= d1) /* If not carry, test QUOT again. */
1419 goto q_test;
1421 sub_ddmmss (n1, n0, r, 0, n1, n0);
1423 have_quot:
1424 got_limb;
1427 return round_and_return (retval, exponent - 1, negative,
1428 quot, BITS_PER_MP_LIMB - 1 - used,
1429 more_bits || n1 != 0 || n0 != 0);
1431 default:
1433 int i;
1434 mp_limb_t cy, dX, d1, n0, n1;
1435 mp_limb_t quot = 0;
1436 int used = 0;
1438 dX = den[densize - 1];
1439 d1 = den[densize - 2];
1441 /* The division does not work if the upper limb of the two-limb
1442 numerator is greater than the denominator. */
1443 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1444 num[numsize++] = 0;
1446 if (numsize < densize)
1448 mp_size_t empty = densize - numsize;
1449 register int i;
1451 if (bits <= 0)
1452 exponent -= empty * BITS_PER_MP_LIMB;
1453 else
1455 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1457 /* We make a difference here because the compiler
1458 cannot optimize the `else' case that good and
1459 this reflects all currently used FLOAT types
1460 and GMP implementations. */
1461 #if RETURN_LIMB_SIZE <= 2
1462 assert (empty == 1);
1463 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1464 BITS_PER_MP_LIMB, 0);
1465 #else
1466 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1467 retval[i] = retval[i - empty];
1468 while (i >= 0)
1469 retval[i--] = 0;
1470 #endif
1472 else
1474 used = MANT_DIG - bits;
1475 if (used >= BITS_PER_MP_LIMB)
1477 register int i;
1478 (void) __mpn_lshift (&retval[used
1479 / BITS_PER_MP_LIMB],
1480 retval, RETURN_LIMB_SIZE,
1481 used % BITS_PER_MP_LIMB);
1482 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1483 retval[i] = 0;
1485 else if (used > 0)
1486 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1488 bits += empty * BITS_PER_MP_LIMB;
1490 for (i = numsize; i > 0; --i)
1491 num[i + empty] = num[i - 1];
1492 MPN_ZERO (num, empty + 1);
1494 else
1496 int i;
1497 assert (numsize == densize);
1498 for (i = numsize; i > 0; --i)
1499 num[i] = num[i - 1];
1502 den[densize] = 0;
1503 n0 = num[densize];
1505 while (bits <= MANT_DIG)
1507 if (n0 == dX)
1508 /* This might over-estimate QUOT, but it's probably not
1509 worth the extra code here to find out. */
1510 quot = ~(mp_limb_t) 0;
1511 else
1513 mp_limb_t r;
1515 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1516 umul_ppmm (n1, n0, d1, quot);
1518 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1520 --quot;
1521 r += dX;
1522 if (r < dX) /* I.e. "carry in previous addition?" */
1523 break;
1524 n1 -= n0 < d1;
1525 n0 -= d1;
1529 /* Possible optimization: We already have (q * n0) and (1 * n1)
1530 after the calculation of QUOT. Taking advantage of this, we
1531 could make this loop make two iterations less. */
1533 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1535 if (num[densize] != cy)
1537 cy = __mpn_add_n (num, num, den, densize);
1538 assert (cy != 0);
1539 --quot;
1541 n0 = num[densize] = num[densize - 1];
1542 for (i = densize - 1; i > 0; --i)
1543 num[i] = num[i - 1];
1545 got_limb;
1548 for (i = densize; num[i] == 0 && i >= 0; --i)
1550 return round_and_return (retval, exponent - 1, negative,
1551 quot, BITS_PER_MP_LIMB - 1 - used,
1552 more_bits || i >= 0);
1557 /* NOTREACHED */
1559 #if defined _LIBC && !defined USE_WIDE_CHAR
1560 libc_hidden_def (INTERNAL (__STRTOF))
1561 #endif
1563 /* External user entry point. */
1565 FLOAT
1566 #ifdef weak_function
1567 weak_function
1568 #endif
1569 __STRTOF (nptr, endptr, loc)
1570 const STRING_TYPE *nptr;
1571 STRING_TYPE **endptr;
1572 __locale_t loc;
1574 return INTERNAL (__STRTOF) (nptr, endptr, 0, loc);
1576 weak_alias (__STRTOF, STRTOF)