Update.
[glibc.git] / stdlib / strtod.c
blobd15237125e98cc2661caa3c93a3d68e207599e94
1 /* Read decimal floating point numbers.
2 This file is part of the GNU C Library.
3 Copyright (C) 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Configuration part. These macros are defined by `strtold.c',
22 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
23 `long double' and `float' versions of the reader. */
24 #ifndef FLOAT
25 # define FLOAT double
26 # define FLT DBL
27 # ifdef USE_WIDE_CHAR
28 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
29 # define STRTOF __wcstod_l
30 # else
31 # define STRTOF wcstod
32 # endif
33 # else
34 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
35 # define STRTOF __strtod_l
36 # else
37 # define STRTOF strtod
38 # endif
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 <math.h>
60 #include <stdlib.h>
61 #include <string.h>
63 /* The gmp headers need some configuration frobs. */
64 #define HAVE_ALLOCA 1
66 #include <gmp.h>
67 #include <gmp-impl.h>
68 #include <gmp-mparam.h>
69 #include <longlong.h>
70 #include "fpioconst.h"
72 #define NDEBUG 1
73 #include <assert.h>
76 /* We use this code also for the extended locale handling where the
77 function gets as an additional argument the locale which has to be
78 used. To access the values we have to redefine the _NL_CURRENT
79 macro. */
80 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
81 # undef _NL_CURRENT
82 # define _NL_CURRENT(category, item) \
83 (current->values[_NL_ITEM_INDEX (item)].string)
84 # define LOCALE_PARAM , loc
85 # define LOCALE_PARAM_DECL __locale_t loc;
86 #else
87 # define LOCALE_PARAM
88 # define LOCALE_PARAM_DECL
89 #endif
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 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
101 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
102 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
103 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
104 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
105 # define STRNCASECMP(S1, S2, N) __wcsncasecmp_l ((S1), (S2), (N), loc)
106 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
107 # else
108 # define ISSPACE(Ch) iswspace (Ch)
109 # define ISDIGIT(Ch) iswdigit (Ch)
110 # define ISXDIGIT(Ch) iswxdigit (Ch)
111 # define TOLOWER(Ch) towlower (Ch)
112 # define STRNCASECMP(S1, S2, N) __wcsncasecmp ((S1), (S2), (N))
113 # define STRTOULL(S, E, B) __wcstoull_internal ((S), (E), (B), 0)
114 # endif
115 #else
116 # define STRING_TYPE char
117 # define CHAR_TYPE char
118 # define L_(Ch) Ch
119 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
120 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
121 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
122 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
123 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
124 # define STRNCASECMP(S1, S2, N) __strncasecmp_l ((S1), (S2), (N), loc)
125 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
126 # else
127 # define ISSPACE(Ch) isspace (Ch)
128 # define ISDIGIT(Ch) isdigit (Ch)
129 # define ISXDIGIT(Ch) isxdigit (Ch)
130 # define TOLOWER(Ch) tolower (Ch)
131 # define STRNCASECMP(S1, S2, N) __strncasecmp ((S1), (S2), (N))
132 # define STRTOULL(S, E, B) __strtoull_internal ((S), (E), 0, (B))
133 # endif
134 #endif
137 /* Constants we need from float.h; select the set for the FLOAT precision. */
138 #define MANT_DIG PASTE(FLT,_MANT_DIG)
139 #define DIG PASTE(FLT,_DIG)
140 #define MAX_EXP PASTE(FLT,_MAX_EXP)
141 #define MIN_EXP PASTE(FLT,_MIN_EXP)
142 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
143 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
145 /* Extra macros required to get FLT expanded before the pasting. */
146 #define PASTE(a,b) PASTE1(a,b)
147 #define PASTE1(a,b) a##b
149 /* Function to construct a floating point number from an MP integer
150 containing the fraction bits, a base 2 exponent, and a sign flag. */
151 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
153 /* Definitions according to limb size used. */
154 #if BITS_PER_MP_LIMB == 32
155 # define MAX_DIG_PER_LIMB 9
156 # define MAX_FAC_PER_LIMB 1000000000UL
157 #elif BITS_PER_MP_LIMB == 64
158 # define MAX_DIG_PER_LIMB 19
159 # define MAX_FAC_PER_LIMB 10000000000000000000UL
160 #else
161 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
162 #endif
165 /* Local data structure. */
166 static const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
167 { 0, 10, 100,
168 1000, 10000, 100000,
169 1000000, 10000000, 100000000,
170 1000000000
171 #if BITS_PER_MP_LIMB > 32
172 , 10000000000U, 100000000000U,
173 1000000000000U, 10000000000000U, 100000000000000U,
174 1000000000000000U, 10000000000000000U, 100000000000000000U,
175 1000000000000000000U, 10000000000000000000U
176 #endif
177 #if BITS_PER_MP_LIMB > 64
178 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
179 #endif
182 #ifndef howmany
183 #define howmany(x,y) (((x)+((y)-1))/(y))
184 #endif
185 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
187 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
188 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
189 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
191 #define RETURN(val,end) \
192 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
193 return val; } while (0)
195 /* Maximum size necessary for mpn integers to hold floating point numbers. */
196 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
197 + 2)
198 /* Declare an mpn integer variable that big. */
199 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
200 /* Copy an mpn integer value. */
201 #define MPN_ASSIGN(dst, src) \
202 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
205 /* Return a floating point number of the needed type according to the given
206 multi-precision number after possible rounding. */
207 static inline FLOAT
208 round_and_return (mp_limb_t *retval, int exponent, int negative,
209 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
211 if (exponent < MIN_EXP - 1)
213 mp_size_t shift = MIN_EXP - 1 - exponent;
215 if (shift > MANT_DIG)
217 __set_errno (EDOM);
218 return 0.0;
221 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
222 if (shift == MANT_DIG)
223 /* This is a special case to handle the very seldom case where
224 the mantissa will be empty after the shift. */
226 int i;
228 round_limb = retval[RETURN_LIMB_SIZE - 1];
229 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
230 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
231 more_bits |= retval[i] != 0;
232 MPN_ZERO (retval, RETURN_LIMB_SIZE);
234 else if (shift >= BITS_PER_MP_LIMB)
236 int i;
238 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
239 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
240 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
241 more_bits |= retval[i] != 0;
242 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
243 != 0);
245 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
246 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
247 shift % BITS_PER_MP_LIMB);
248 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
249 shift / BITS_PER_MP_LIMB);
251 else if (shift > 0)
253 round_limb = retval[0];
254 round_bit = shift - 1;
255 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
257 /* This is a hook for the m68k long double format, where the
258 exponent bias is the same for normalized and denormalized
259 numbers. */
260 #ifndef DENORM_EXP
261 # define DENORM_EXP (MIN_EXP - 2)
262 #endif
263 exponent = DENORM_EXP;
266 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
267 && (more_bits || (retval[0] & 1) != 0
268 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
270 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
272 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
273 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
274 (retval[RETURN_LIMB_SIZE - 1]
275 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
277 ++exponent;
278 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
279 retval[RETURN_LIMB_SIZE - 1]
280 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
282 else if (exponent == DENORM_EXP
283 && (retval[RETURN_LIMB_SIZE - 1]
284 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
285 != 0)
286 /* The number was denormalized but now normalized. */
287 exponent = MIN_EXP - 1;
290 if (exponent > MAX_EXP)
291 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
293 return MPN2FLOAT (retval, exponent, negative);
297 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
298 into N. Return the size of the number limbs in NSIZE at the first
299 character od the string that is not part of the integer as the function
300 value. If the EXPONENT is small enough to be taken as an additional
301 factor for the resulting number (see code) multiply by it. */
302 static inline const STRING_TYPE *
303 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
304 int *exponent
305 #ifndef USE_WIDE_CHAR
306 , const char *decimal, size_t decimal_len, const char *thousands
307 #endif
311 /* Number of digits for actual limb. */
312 int cnt = 0;
313 mp_limb_t low = 0;
314 mp_limb_t start;
316 *nsize = 0;
317 assert (digcnt > 0);
320 if (cnt == MAX_DIG_PER_LIMB)
322 if (*nsize == 0)
324 n[0] = low;
325 *nsize = 1;
327 else
329 mp_limb_t cy;
330 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
331 cy += __mpn_add_1 (n, n, *nsize, low);
332 if (cy != 0)
334 n[*nsize] = cy;
335 ++(*nsize);
338 cnt = 0;
339 low = 0;
342 /* There might be thousands separators or radix characters in
343 the string. But these all can be ignored because we know the
344 format of the number is correct and we have an exact number
345 of characters to read. */
346 #ifdef USE_WIDE_CHAR
347 if (*str < L'0' || *str > L'9')
348 ++str;
349 #else
350 if (*str < '0' || *str > '9')
352 if (thousands != NULL && *str == *thousands
353 && ({ for (cnt == 1; thousands[cnt] != '\0'; ++cnt)
354 if (thousands[cnt] != str[cnt])
355 break;
356 thousands[cnt] == '\0'; }))
357 str += cnt;
358 else
359 str += decimal_len;
361 #endif
362 low = low * 10 + *str++ - L_('0');
363 ++cnt;
365 while (--digcnt > 0);
367 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
369 low *= _tens_in_limb[*exponent];
370 start = _tens_in_limb[cnt + *exponent];
371 *exponent = 0;
373 else
374 start = _tens_in_limb[cnt];
376 if (*nsize == 0)
378 n[0] = low;
379 *nsize = 1;
381 else
383 mp_limb_t cy;
384 cy = __mpn_mul_1 (n, n, *nsize, start);
385 cy += __mpn_add_1 (n, n, *nsize, low);
386 if (cy != 0)
387 n[(*nsize)++] = cy;
390 return str;
394 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
395 with the COUNT most significant bits of LIMB.
397 Tege doesn't like this function so I have to write it here myself. :)
398 --drepper */
399 static inline void
400 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
401 mp_limb_t limb)
403 if (count == BITS_PER_MP_LIMB)
405 /* Optimize the case of shifting by exactly a word:
406 just copy words, with no actual bit-shifting. */
407 mp_size_t i;
408 for (i = size - 1; i > 0; --i)
409 ptr[i] = ptr[i - 1];
410 ptr[0] = limb;
412 else
414 (void) __mpn_lshift (ptr, ptr, size, count);
415 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
420 #define INTERNAL(x) INTERNAL1(x)
421 #define INTERNAL1(x) __##x##_internal
423 /* This file defines a function to check for correct grouping. */
424 #include "grouping.h"
427 /* Return a floating point number with the value of the given string NPTR.
428 Set *ENDPTR to the character after the last used one. If the number is
429 smaller than the smallest representable number, set `errno' to ERANGE and
430 return 0.0. If the number is too big to be represented, set `errno' to
431 ERANGE and return HUGE_VAL with the appropriate sign. */
432 FLOAT
433 INTERNAL (STRTOF) (nptr, endptr, group LOCALE_PARAM)
434 const STRING_TYPE *nptr;
435 STRING_TYPE **endptr;
436 int group;
437 LOCALE_PARAM_DECL
439 int negative; /* The sign of the number. */
440 MPN_VAR (num); /* MP representation of the number. */
441 int exponent; /* Exponent of the number. */
443 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
444 int base = 10;
446 /* When we have to compute fractional digits we form a fraction with a
447 second multi-precision number (and we sometimes need a second for
448 temporary results). */
449 MPN_VAR (den);
451 /* Representation for the return value. */
452 mp_limb_t retval[RETURN_LIMB_SIZE];
453 /* Number of bits currently in result value. */
454 int bits;
456 /* Running pointer after the last character processed in the string. */
457 const STRING_TYPE *cp, *tp;
458 /* Start of significant part of the number. */
459 const STRING_TYPE *startp, *start_of_digits;
460 /* Points at the character following the integer and fractional digits. */
461 const STRING_TYPE *expp;
462 /* Total number of digit and number of digits in integer part. */
463 int dig_no, int_no, lead_zero;
464 /* Contains the last character read. */
465 CHAR_TYPE c;
467 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
468 there. So define it ourselves if it remains undefined. */
469 #ifndef _WINT_T
470 typedef unsigned int wint_t;
471 #endif
472 /* The radix character of the current locale. */
473 #ifdef USE_WIDE_CHAR
474 wchar_t decimal;
475 #else
476 const char *decimal;
477 size_t decimal_len;
478 #endif
479 /* The thousands character of the current locale. */
480 #ifdef USE_WIDE_CHAR
481 wchar_t thousands = L'\0';
482 #else
483 const char *thousands = NULL;
484 #endif
485 /* The numeric grouping specification of the current locale,
486 in the format described in <locale.h>. */
487 const char *grouping;
488 /* Used in several places. */
489 int cnt;
491 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
492 struct locale_data *current = loc->__locales[LC_NUMERIC];
493 #endif
495 if (group)
497 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
498 if (*grouping <= 0 || *grouping == CHAR_MAX)
499 grouping = NULL;
500 else
502 /* Figure out the thousands separator character. */
503 #ifdef USE_WIDE_CHAR
504 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
505 _NL_NUMERIC_THOUSANDS_SEP_WC);
506 if (thousands == L'\0')
507 grouping = NULL;
508 #else
509 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
510 if (*thousands == '\0')
512 thousands = NULL;
513 grouping = NULL;
515 #endif
518 else
519 grouping = NULL;
521 /* Find the locale's decimal point character. */
522 #ifdef USE_WIDE_CHAR
523 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
524 assert (decimal != L'\0');
525 # define decimal_len 1
526 #else
527 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
528 decimal_len = strlen (decimal);
529 assert (decimal_len > 0);
530 #endif
532 /* Prepare number representation. */
533 exponent = 0;
534 negative = 0;
535 bits = 0;
537 /* Parse string to get maximal legal prefix. We need the number of
538 characters of the integer part, the fractional part and the exponent. */
539 cp = nptr - 1;
540 /* Ignore leading white space. */
542 c = *++cp;
543 while (ISSPACE (c));
545 /* Get sign of the result. */
546 if (c == L_('-'))
548 negative = 1;
549 c = *++cp;
551 else if (c == L_('+'))
552 c = *++cp;
554 /* Return 0.0 if no legal string is found.
555 No character is used even if a sign was found. */
556 #ifdef USE_WIDE_CHAR
557 if (c == decimal && cp[1] >= L'0' && cp[1] <= L'9')
559 /* We accept it. This funny construct is here only to indent
560 the code directly. */
562 #else
563 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
564 if (cp[cnt] != decimal[cnt])
565 break;
566 if (decimal[cnt] == '\0' && cp[1] >= '0' && cp[1] <= '9')
568 /* We accept it. This funny construct is here only to indent
569 the code directly. */
571 #endif
572 else if (c < L_('0') || c > L_('9'))
574 int matched = 0;
575 /* Check for `INF' or `INFINITY'. */
576 if (TOLOWER (c) == L_('i')
577 && ((STRNCASECMP (cp, L_("inf"), 3) == 0 && (matched = 3))
578 || (STRNCASECMP (cp, L_("infinity"), 8) == 0 && (matched = 8))))
580 /* Return +/- infinity. */
581 if (endptr != NULL)
582 *endptr = (STRING_TYPE *) (cp + matched);
584 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
587 if (TOLOWER (c) == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
589 /* Return NaN. */
590 FLOAT retval = NAN;
592 cp += 3;
594 /* Match `(n-char-sequence-digit)'. */
595 if (*cp == L_('('))
597 const STRING_TYPE *startp = cp;
599 ++cp;
600 while ((*cp >= L_('0') && *cp <= L_('9'))
601 || (TOLOWER (*cp) >= L_('a') && TOLOWER (*cp) <= L_('z'))
602 || *cp == L_('_'));
604 if (*cp != L_(')'))
605 /* The closing brace is missing. Only match the NAN
606 part. */
607 cp = startp;
608 else
610 /* This is a system-dependent way to specify the
611 bitmask used for the NaN. We expect it to be
612 a number which is put in the mantissa of the
613 number. */
614 STRING_TYPE *endp;
615 unsigned long long int mant;
617 mant = STRTOULL (startp + 1, &endp, 0);
618 if (endp == cp)
619 SET_MANTISSA (retval, mant);
623 if (endptr != NULL)
624 *endptr = (STRING_TYPE *) cp;
626 return retval;
629 /* It is really a text we do not recognize. */
630 RETURN (0.0, nptr);
633 /* First look whether we are faced with a hexadecimal number. */
634 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
636 /* Okay, it is a hexa-decimal number. Remember this and skip
637 the characters. BTW: hexadecimal numbers must not be
638 grouped. */
639 base = 16;
640 cp += 2;
641 c = *cp;
642 grouping = NULL;
645 /* Record the start of the digits, in case we will check their grouping. */
646 start_of_digits = startp = cp;
648 /* Ignore leading zeroes. This helps us to avoid useless computations. */
649 #ifdef USE_WIDE_CHAR
650 while (c == L'0' || (thousands != L'\0' && c == thousands))
651 c = *++cp;
652 #else
653 if (thousands == NULL)
654 while (c == '0')
655 c = *++cp;
656 else
658 /* We also have the multibyte thousands string. */
659 while (1)
661 if (c != '0')
663 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
664 if (c != thousands[cnt])
665 break;
666 if (thousands[cnt] != '\0')
667 break;
669 c = *++cp;
672 #endif
674 /* If no other digit but a '0' is found the result is 0.0.
675 Return current read pointer. */
676 if ((c < L_('0') || c > L_('9'))
677 && (base == 16 && (c < TOLOWER (L_('a')) || c > TOLOWER (L_('f'))))
678 #ifdef USE_WIDE_CHAR
679 && c != decimal
680 #else
681 && ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
682 if (decimal[cnt] != cp[cnt])
683 break;
684 decimal[cnt] != '\0'; })
685 #endif
686 && (base == 16 && (cp == start_of_digits || TOLOWER (c) != L_('p')))
687 && (base != 16 && TOLOWER (c) != L_('e')))
689 tp = correctly_grouped_prefix (start_of_digits, cp, thousands, grouping);
690 /* If TP is at the start of the digits, there was no correctly
691 grouped prefix of the string; so no number found. */
692 RETURN (0.0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
695 /* Remember first significant digit and read following characters until the
696 decimal point, exponent character or any non-FP number character. */
697 startp = cp;
698 dig_no = 0;
699 while (dig_no < (base == 16 ? HEXNDIG : NDIG) ||
700 /* If parsing grouping info, keep going past useful digits
701 so we can check all the grouping separators. */
702 grouping)
704 if ((c >= L_('0') && c <= L_('9'))
705 || (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
706 ++dig_no;
707 else
709 #ifdef USE_WIDE_CHAR
710 if (thousands == L'\0' || c != thousands)
711 /* Not a digit or separator: end of the integer part. */
712 break;
713 #else
714 if (thousands == NULL)
715 break;
716 else
718 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
719 if (thousands[cnt] != cp[cnt])
720 break;
721 if (thousands[cnt] != '\0')
722 break;
724 #endif
726 c = *++cp;
729 if (grouping && dig_no > 0)
731 /* Check the grouping of the digits. */
732 tp = correctly_grouped_prefix (start_of_digits, cp, thousands, grouping);
733 if (cp != tp)
735 /* Less than the entire string was correctly grouped. */
737 if (tp == start_of_digits)
738 /* No valid group of numbers at all: no valid number. */
739 RETURN (0.0, nptr);
741 if (tp < startp)
742 /* The number is validly grouped, but consists
743 only of zeroes. The whole value is zero. */
744 RETURN (0.0, tp);
746 /* Recompute DIG_NO so we won't read more digits than
747 are properly grouped. */
748 cp = tp;
749 dig_no = 0;
750 for (tp = startp; tp < cp; ++tp)
751 if (*tp >= L_('0') && *tp <= L_('9'))
752 ++dig_no;
754 int_no = dig_no;
755 lead_zero = 0;
757 goto number_parsed;
761 if (dig_no >= (base == 16 ? HEXNDIG : NDIG))
762 /* Too many digits to be representable. Assigning this to EXPONENT
763 allows us to read the full number but return HUGE_VAL after parsing. */
764 exponent = MAX_10_EXP;
766 /* We have the number digits in the integer part. Whether these are all or
767 any is really a fractional digit will be decided later. */
768 int_no = dig_no;
769 lead_zero = int_no == 0 ? -1 : 0;
771 /* Read the fractional digits. A special case are the 'american style'
772 numbers like `16.' i.e. with decimal but without trailing digits. */
773 if (
774 #ifdef USE_WIDE_CHAR
775 c == decimal
776 #else
777 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
778 if (decimal[cnt] != cp[cnt])
779 break;
780 decimal[cnt] == '\0'; })
781 #endif
784 cp += decimal_len;
785 c = *cp;
786 while ((c >= L_('0') && c <= L_('9')) ||
787 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
789 if (c != L_('0') && lead_zero == -1)
790 lead_zero = dig_no - int_no;
791 ++dig_no;
792 c = *++cp;
796 /* Remember start of exponent (if any). */
797 expp = cp;
799 /* Read exponent. */
800 if ((base == 16 && TOLOWER (c) == L_('p'))
801 || (base != 16 && TOLOWER (c) == L_('e')))
803 int exp_negative = 0;
805 c = *++cp;
806 if (c == L_('-'))
808 exp_negative = 1;
809 c = *++cp;
811 else if (c == L_('+'))
812 c = *++cp;
814 if (c >= L_('0') && c <= L_('9'))
816 int exp_limit;
818 /* Get the exponent limit. */
819 if (base == 16)
820 exp_limit = (exp_negative ?
821 -MIN_EXP + MANT_DIG - 4 * int_no :
822 MAX_EXP - 4 * int_no + lead_zero);
823 else
824 exp_limit = (exp_negative ?
825 -MIN_10_EXP + MANT_DIG - int_no :
826 MAX_10_EXP - int_no + lead_zero);
830 exponent *= 10;
832 if (exponent > exp_limit)
833 /* The exponent is too large/small to represent a valid
834 number. */
836 FLOAT result;
838 /* We have to take care for special situation: a joker
839 might have written "0.0e100000" which is in fact
840 zero. */
841 if (lead_zero == -1)
842 result = negative ? -0.0 : 0.0;
843 else
845 /* Overflow or underflow. */
846 __set_errno (ERANGE);
847 result = (exp_negative ? 0.0 :
848 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
851 /* Accept all following digits as part of the exponent. */
853 ++cp;
854 while (*cp >= L_('0') && *cp <= L_('9'));
856 RETURN (result, cp);
857 /* NOTREACHED */
860 exponent += c - L_('0');
861 c = *++cp;
863 while (c >= L_('0') && c <= L_('9'));
865 if (exp_negative)
866 exponent = -exponent;
868 else
869 cp = expp;
872 /* We don't want to have to work with trailing zeroes after the radix. */
873 if (dig_no > int_no)
875 while (expp[-1] == L_('0'))
877 --expp;
878 --dig_no;
880 assert (dig_no >= int_no);
883 number_parsed:
885 /* The whole string is parsed. Store the address of the next character. */
886 if (endptr)
887 *endptr = (STRING_TYPE *) cp;
889 if (dig_no == 0)
890 return negative ? -0.0 : 0.0;
892 if (lead_zero)
894 /* Find the decimal point */
895 #ifdef USE_WIDE_CHAR
896 while (*startp != decimal)
897 ++startp;
898 #else
899 while (1)
901 if (*startp == decimal[0])
903 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
904 if (decimal[cnt] != startp[cnt])
905 break;
906 if (decimal[cnt] == '\0')
907 break;
909 ++startp;
911 #endif
912 startp += lead_zero + decimal_len;
913 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
914 dig_no -= lead_zero;
917 /* If the BASE is 16 we can use a simpler algorithm. */
918 if (base == 16)
920 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
921 4, 4, 4, 4, 4, 4, 4, 4 };
922 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
923 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
924 mp_limb_t val;
926 while (!ISXDIGIT (*startp))
927 ++startp;
928 while (*startp == L_('0'))
929 ++startp;
930 if (ISDIGIT (*startp))
931 val = *startp++ - L_('0');
932 else
933 val = 10 + TOLOWER (*startp++) - L_('a');
934 bits = nbits[val];
935 /* We cannot have a leading zero. */
936 assert (bits != 0);
938 if (pos + 1 >= 4 || pos + 1 >= bits)
940 /* We don't have to care for wrapping. This is the normal
941 case so we add the first clause in the `if' expression as
942 an optimization. It is a compile-time constant and so does
943 not cost anything. */
944 retval[idx] = val << (pos - bits + 1);
945 pos -= bits;
947 else
949 retval[idx--] = val >> (bits - pos - 1);
950 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
951 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
954 /* Adjust the exponent for the bits we are shifting in. */
955 exponent += bits - 1 + (int_no - 1) * 4;
957 while (--dig_no > 0 && idx >= 0)
959 if (!ISXDIGIT (*startp))
960 startp += decimal_len;
961 if (ISDIGIT (*startp))
962 val = *startp++ - L_('0');
963 else
964 val = 10 + TOLOWER (*startp++) - L_('a');
966 if (pos + 1 >= 4)
968 retval[idx] |= val << (pos - 4 + 1);
969 pos -= 4;
971 else
973 retval[idx--] |= val >> (4 - pos - 1);
974 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
975 if (idx < 0)
976 return round_and_return (retval, exponent, negative, val,
977 BITS_PER_MP_LIMB - 1, dig_no > 0);
979 retval[idx] = val;
980 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
984 /* We ran out of digits. */
985 MPN_ZERO (retval, idx);
987 return round_and_return (retval, exponent, negative, 0, 0, 0);
990 /* Now we have the number of digits in total and the integer digits as well
991 as the exponent and its sign. We can decide whether the read digits are
992 really integer digits or belong to the fractional part; i.e. we normalize
993 123e-2 to 1.23. */
995 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
996 : MIN (dig_no - int_no, exponent));
997 int_no += incr;
998 exponent -= incr;
1001 if (int_no + exponent > MAX_10_EXP + 1)
1003 __set_errno (ERANGE);
1004 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1007 if (exponent < MIN_10_EXP - (DIG + 1))
1009 __set_errno (ERANGE);
1010 return 0.0;
1013 if (int_no > 0)
1015 /* Read the integer part as a multi-precision number to NUM. */
1016 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1017 #ifndef USE_WIDE_CHAR
1018 , decimal, decimal_len, thousands
1019 #endif
1022 if (exponent > 0)
1024 /* We now multiply the gained number by the given power of ten. */
1025 mp_limb_t *psrc = num;
1026 mp_limb_t *pdest = den;
1027 int expbit = 1;
1028 const struct mp_power *ttab = &_fpioconst_pow10[0];
1032 if ((exponent & expbit) != 0)
1034 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1035 mp_limb_t cy;
1036 exponent ^= expbit;
1038 /* FIXME: not the whole multiplication has to be
1039 done. If we have the needed number of bits we
1040 only need the information whether more non-zero
1041 bits follow. */
1042 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1043 cy = __mpn_mul (pdest, psrc, numsize,
1044 &__tens[ttab->arrayoff
1045 + _FPIO_CONST_OFFSET],
1046 size);
1047 else
1048 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1049 + _FPIO_CONST_OFFSET],
1050 size, psrc, numsize);
1051 numsize += size;
1052 if (cy == 0)
1053 --numsize;
1054 SWAP (psrc, pdest);
1056 expbit <<= 1;
1057 ++ttab;
1059 while (exponent != 0);
1061 if (psrc == den)
1062 memcpy (num, den, numsize * sizeof (mp_limb_t));
1065 /* Determine how many bits of the result we already have. */
1066 count_leading_zeros (bits, num[numsize - 1]);
1067 bits = numsize * BITS_PER_MP_LIMB - bits;
1069 /* Now we know the exponent of the number in base two.
1070 Check it against the maximum possible exponent. */
1071 if (bits > MAX_EXP)
1073 __set_errno (ERANGE);
1074 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1077 /* We have already the first BITS bits of the result. Together with
1078 the information whether more non-zero bits follow this is enough
1079 to determine the result. */
1080 if (bits > MANT_DIG)
1082 int i;
1083 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1084 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1085 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1086 : least_idx;
1087 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1088 : least_bit - 1;
1090 if (least_bit == 0)
1091 memcpy (retval, &num[least_idx],
1092 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1093 else
1095 for (i = least_idx; i < numsize - 1; ++i)
1096 retval[i - least_idx] = (num[i] >> least_bit)
1097 | (num[i + 1]
1098 << (BITS_PER_MP_LIMB - least_bit));
1099 if (i - least_idx < RETURN_LIMB_SIZE)
1100 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1103 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1104 for (i = 0; num[i] == 0; ++i)
1107 return round_and_return (retval, bits - 1, negative,
1108 num[round_idx], round_bit,
1109 int_no < dig_no || i < round_idx);
1110 /* NOTREACHED */
1112 else if (dig_no == int_no)
1114 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1115 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1117 if (target_bit == is_bit)
1119 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1120 numsize * sizeof (mp_limb_t));
1121 /* FIXME: the following loop can be avoided if we assume a
1122 maximal MANT_DIG value. */
1123 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1125 else if (target_bit > is_bit)
1127 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1128 num, numsize, target_bit - is_bit);
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
1135 mp_limb_t cy;
1136 assert (numsize < RETURN_LIMB_SIZE);
1138 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1139 num, numsize, is_bit - target_bit);
1140 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
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 - 1);
1146 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1147 /* NOTREACHED */
1150 /* Store the bits we already have. */
1151 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1152 #if RETURN_LIMB_SIZE > 1
1153 if (numsize < RETURN_LIMB_SIZE)
1154 retval[numsize] = 0;
1155 #endif
1158 /* We have to compute at least some of the fractional digits. */
1160 /* We construct a fraction and the result of the division gives us
1161 the needed digits. The denominator is 1.0 multiplied by the
1162 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1163 123e-6 gives 123 / 1000000. */
1165 int expbit;
1166 int neg_exp;
1167 int more_bits;
1168 mp_limb_t cy;
1169 mp_limb_t *psrc = den;
1170 mp_limb_t *pdest = num;
1171 const struct mp_power *ttab = &_fpioconst_pow10[0];
1173 assert (dig_no > int_no && exponent <= 0);
1176 /* For the fractional part we need not process too many digits. One
1177 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1178 ceil(BITS / 3) =: N
1179 digits we should have enough bits for the result. The remaining
1180 decimal digits give us the information that more bits are following.
1181 This can be used while rounding. (One added as a safety margin.) */
1182 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 1)
1184 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 1;
1185 more_bits = 1;
1187 else
1188 more_bits = 0;
1190 neg_exp = dig_no - int_no - exponent;
1192 /* Construct the denominator. */
1193 densize = 0;
1194 expbit = 1;
1197 if ((neg_exp & expbit) != 0)
1199 mp_limb_t cy;
1200 neg_exp ^= expbit;
1202 if (densize == 0)
1204 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1205 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1206 densize * sizeof (mp_limb_t));
1208 else
1210 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1211 + _FPIO_CONST_OFFSET],
1212 ttab->arraysize - _FPIO_CONST_OFFSET,
1213 psrc, densize);
1214 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1215 if (cy == 0)
1216 --densize;
1217 SWAP (psrc, pdest);
1220 expbit <<= 1;
1221 ++ttab;
1223 while (neg_exp != 0);
1225 if (psrc == num)
1226 memcpy (den, num, densize * sizeof (mp_limb_t));
1228 /* Read the fractional digits from the string. */
1229 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1230 #ifndef USE_WIDE_CHAR
1231 , decimal, decimal_len, thousands
1232 #endif
1235 /* We now have to shift both numbers so that the highest bit in the
1236 denominator is set. In the same process we copy the numerator to
1237 a high place in the array so that the division constructs the wanted
1238 digits. This is done by a "quasi fix point" number representation.
1240 num: ddddddddddd . 0000000000000000000000
1241 |--- m ---|
1242 den: ddddddddddd n >= m
1243 |--- n ---|
1246 count_leading_zeros (cnt, den[densize - 1]);
1248 if (cnt > 0)
1250 /* Don't call `mpn_shift' with a count of zero since the specification
1251 does not allow this. */
1252 (void) __mpn_lshift (den, den, densize, cnt);
1253 cy = __mpn_lshift (num, num, numsize, cnt);
1254 if (cy != 0)
1255 num[numsize++] = cy;
1258 /* Now we are ready for the division. But it is not necessary to
1259 do a full multi-precision division because we only need a small
1260 number of bits for the result. So we do not use __mpn_divmod
1261 here but instead do the division here by hand and stop whenever
1262 the needed number of bits is reached. The code itself comes
1263 from the GNU MP Library by Torbj\"orn Granlund. */
1265 exponent = bits;
1267 switch (densize)
1269 case 1:
1271 mp_limb_t d, n, quot;
1272 int used = 0;
1274 n = num[0];
1275 d = den[0];
1276 assert (numsize == 1 && n < d);
1280 udiv_qrnnd (quot, n, n, 0, d);
1282 #define got_limb \
1283 if (bits == 0) \
1285 register int cnt; \
1286 if (quot == 0) \
1287 cnt = BITS_PER_MP_LIMB; \
1288 else \
1289 count_leading_zeros (cnt, quot); \
1290 exponent -= cnt; \
1291 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1293 used = MANT_DIG + cnt; \
1294 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1295 bits = MANT_DIG + 1; \
1297 else \
1299 /* Note that we only clear the second element. */ \
1300 /* The conditional is determined at compile time. */ \
1301 if (RETURN_LIMB_SIZE > 1) \
1302 retval[1] = 0; \
1303 retval[0] = quot; \
1304 bits = -cnt; \
1307 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1308 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1309 quot); \
1310 else \
1312 used = MANT_DIG - bits; \
1313 if (used > 0) \
1314 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1316 bits += BITS_PER_MP_LIMB
1318 got_limb;
1320 while (bits <= MANT_DIG);
1322 return round_and_return (retval, exponent - 1, negative,
1323 quot, BITS_PER_MP_LIMB - 1 - used,
1324 more_bits || n != 0);
1326 case 2:
1328 mp_limb_t d0, d1, n0, n1;
1329 mp_limb_t quot = 0;
1330 int used = 0;
1332 d0 = den[0];
1333 d1 = den[1];
1335 if (numsize < densize)
1337 if (num[0] >= d1)
1339 /* The numerator of the number occupies fewer bits than
1340 the denominator but the one limb is bigger than the
1341 high limb of the numerator. */
1342 n1 = 0;
1343 n0 = num[0];
1345 else
1347 if (bits <= 0)
1348 exponent -= BITS_PER_MP_LIMB;
1349 else
1351 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1352 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1353 BITS_PER_MP_LIMB, 0);
1354 else
1356 used = MANT_DIG - bits;
1357 if (used > 0)
1358 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1360 bits += BITS_PER_MP_LIMB;
1362 n1 = num[0];
1363 n0 = 0;
1366 else
1368 n1 = num[1];
1369 n0 = num[0];
1372 while (bits <= MANT_DIG)
1374 mp_limb_t r;
1376 if (n1 == d1)
1378 /* QUOT should be either 111..111 or 111..110. We need
1379 special treatment of this rare case as normal division
1380 would give overflow. */
1381 quot = ~(mp_limb_t) 0;
1383 r = n0 + d1;
1384 if (r < d1) /* Carry in the addition? */
1386 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1387 goto have_quot;
1389 n1 = d0 - (d0 != 0);
1390 n0 = -d0;
1392 else
1394 udiv_qrnnd (quot, r, n1, n0, d1);
1395 umul_ppmm (n1, n0, d0, quot);
1398 q_test:
1399 if (n1 > r || (n1 == r && n0 > 0))
1401 /* The estimated QUOT was too large. */
1402 --quot;
1404 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1405 r += d1;
1406 if (r >= d1) /* If not carry, test QUOT again. */
1407 goto q_test;
1409 sub_ddmmss (n1, n0, r, 0, n1, n0);
1411 have_quot:
1412 got_limb;
1415 return round_and_return (retval, exponent - 1, negative,
1416 quot, BITS_PER_MP_LIMB - 1 - used,
1417 more_bits || n1 != 0 || n0 != 0);
1419 default:
1421 int i;
1422 mp_limb_t cy, dX, d1, n0, n1;
1423 mp_limb_t quot = 0;
1424 int used = 0;
1426 dX = den[densize - 1];
1427 d1 = den[densize - 2];
1429 /* The division does not work if the upper limb of the two-limb
1430 numerator is greater than the denominator. */
1431 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1432 num[numsize++] = 0;
1434 if (numsize < densize)
1436 mp_size_t empty = densize - numsize;
1438 if (bits <= 0)
1440 register int i;
1441 for (i = numsize; i > 0; --i)
1442 num[i + empty] = num[i - 1];
1443 MPN_ZERO (num, empty + 1);
1444 exponent -= empty * BITS_PER_MP_LIMB;
1446 else
1448 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1450 /* We make a difference here because the compiler
1451 cannot optimize the `else' case that good and
1452 this reflects all currently used FLOAT types
1453 and GMP implementations. */
1454 register int i;
1455 #if RETURN_LIMB_SIZE <= 2
1456 assert (empty == 1);
1457 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1458 BITS_PER_MP_LIMB, 0);
1459 #else
1460 for (i = RETURN_LIMB_SIZE; i > empty; --i)
1461 retval[i] = retval[i - empty];
1462 #endif
1463 #if RETURN_LIMB_SIZE > 1
1464 retval[1] = 0;
1465 #endif
1466 for (i = numsize; i > 0; --i)
1467 num[i + empty] = num[i - 1];
1468 MPN_ZERO (num, empty + 1);
1470 else
1472 used = MANT_DIG - bits;
1473 if (used >= BITS_PER_MP_LIMB)
1475 register int i;
1476 (void) __mpn_lshift (&retval[used
1477 / BITS_PER_MP_LIMB],
1478 retval, RETURN_LIMB_SIZE,
1479 used % BITS_PER_MP_LIMB);
1480 for (i = used / BITS_PER_MP_LIMB; i >= 0; --i)
1481 retval[i] = 0;
1483 else if (used > 0)
1484 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1486 bits += empty * BITS_PER_MP_LIMB;
1489 else
1491 int i;
1492 assert (numsize == densize);
1493 for (i = numsize; i > 0; --i)
1494 num[i] = num[i - 1];
1497 den[densize] = 0;
1498 n0 = num[densize];
1500 while (bits <= MANT_DIG)
1502 if (n0 == dX)
1503 /* This might over-estimate QUOT, but it's probably not
1504 worth the extra code here to find out. */
1505 quot = ~(mp_limb_t) 0;
1506 else
1508 mp_limb_t r;
1510 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1511 umul_ppmm (n1, n0, d1, quot);
1513 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1515 --quot;
1516 r += dX;
1517 if (r < dX) /* I.e. "carry in previous addition?" */
1518 break;
1519 n1 -= n0 < d1;
1520 n0 -= d1;
1524 /* Possible optimization: We already have (q * n0) and (1 * n1)
1525 after the calculation of QUOT. Taking advantage of this, we
1526 could make this loop make two iterations less. */
1528 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1530 if (num[densize] != cy)
1532 cy = __mpn_add_n (num, num, den, densize);
1533 assert (cy != 0);
1534 --quot;
1536 n0 = num[densize] = num[densize - 1];
1537 for (i = densize - 1; i > 0; --i)
1538 num[i] = num[i - 1];
1540 got_limb;
1543 for (i = densize; num[i] == 0 && i >= 0; --i)
1545 return round_and_return (retval, exponent - 1, negative,
1546 quot, BITS_PER_MP_LIMB - 1 - used,
1547 more_bits || i >= 0);
1552 /* NOTREACHED */
1555 /* External user entry point. */
1557 FLOAT
1558 #ifdef weak_function
1559 weak_function
1560 #endif
1561 STRTOF (nptr, endptr LOCALE_PARAM)
1562 const STRING_TYPE *nptr;
1563 STRING_TYPE **endptr;
1564 LOCALE_PARAM_DECL
1566 return INTERNAL (STRTOF) (nptr, endptr, 0 LOCALE_PARAM);