1 /* Read decimal floating point numbers.
2 This file is part of the GNU C Library.
3 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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. */
28 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
29 # define STRTOF __wcstod_l
31 # define STRTOF wcstod
34 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
35 # define STRTOF __strtod_l
37 # define STRTOF strtod
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; \
45 if ((mant & 0xfffffffffffffULL) == 0) \
46 mant = 0x8000000000000ULL; \
47 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
48 u.ieee.mantissa1 = (mant) & 0xffffffff; \
51 /* End of configuration part. */
57 #include "../locale/localeinfo.h"
62 /* The gmp headers need some configuration frobs. */
67 #include <gmp-mparam.h>
69 #include "fpioconst.h"
75 /* We use this code also for the extended locale handling where the
76 function gets as an additional argument the locale which has to be
77 used. To access the values we have to redefine the _NL_CURRENT
79 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
81 # define _NL_CURRENT(category, item) \
82 (current->values[_NL_ITEM_INDEX (item)].string)
83 # define LOCALE_PARAM , loc
84 # define LOCALE_PARAM_DECL __locale_t loc;
87 # define LOCALE_PARAM_DECL
94 # define STRING_TYPE wchar_t
95 # define CHAR_TYPE wint_t
97 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
98 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
99 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
100 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
101 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
102 # define STRNCASECMP(S1, S2, N) __wcsncasecmp_l ((S1), (S2), (N), loc)
103 # define STRTOULL(S, E, B) __wcstoull_l ((S), (E), (B), loc)
105 # define ISSPACE(Ch) iswspace (Ch)
106 # define ISDIGIT(Ch) iswdigit (Ch)
107 # define ISXDIGIT(Ch) iswxdigit (Ch)
108 # define TOLOWER(Ch) towlower (Ch)
109 # define STRNCASECMP(S1, S2, N) __wcsncasecmp ((S1), (S2), (N))
110 # define STRTOULL(S, E, B) wcstoull ((S), (E), (B))
113 # define STRING_TYPE char
114 # define CHAR_TYPE char
116 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
117 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
118 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
119 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
120 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
121 # define STRNCASECMP(S1, S2, N) __strncasecmp_l ((S1), (S2), (N), loc)
122 # define STRTOULL(S, E, B) __strtoull_l ((S), (E), (B), loc)
124 # define ISSPACE(Ch) isspace (Ch)
125 # define ISDIGIT(Ch) isdigit (Ch)
126 # define ISXDIGIT(Ch) isxdigit (Ch)
127 # define TOLOWER(Ch) tolower (Ch)
128 # define STRNCASECMP(S1, S2, N) __strncasecmp ((S1), (S2), (N))
129 # define STRTOULL(S, E, B) strtoull ((S), (E), (B))
134 /* Constants we need from float.h; select the set for the FLOAT precision. */
135 #define MANT_DIG PASTE(FLT,_MANT_DIG)
136 #define DIG PASTE(FLT,_DIG)
137 #define MAX_EXP PASTE(FLT,_MAX_EXP)
138 #define MIN_EXP PASTE(FLT,_MIN_EXP)
139 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
140 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
142 /* Extra macros required to get FLT expanded before the pasting. */
143 #define PASTE(a,b) PASTE1(a,b)
144 #define PASTE1(a,b) a##b
146 /* Function to construct a floating point number from an MP integer
147 containing the fraction bits, a base 2 exponent, and a sign flag. */
148 extern FLOAT
MPN2FLOAT (mp_srcptr mpn
, int exponent
, int negative
);
150 /* Definitions according to limb size used. */
151 #if BITS_PER_MP_LIMB == 32
152 # define MAX_DIG_PER_LIMB 9
153 # define MAX_FAC_PER_LIMB 1000000000UL
154 #elif BITS_PER_MP_LIMB == 64
155 # define MAX_DIG_PER_LIMB 19
156 # define MAX_FAC_PER_LIMB 10000000000000000000UL
158 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
162 /* Local data structure. */
163 static const mp_limb_t _tens_in_limb
[MAX_DIG_PER_LIMB
+ 1] =
166 1000000, 10000000, 100000000,
168 #if BITS_PER_MP_LIMB > 32
169 , 10000000000U, 100000000000U,
170 1000000000000U, 10000000000000U, 100000000000000U,
171 1000000000000000U, 10000000000000000U, 100000000000000000U,
172 1000000000000000000U, 10000000000000000000U
174 #if BITS_PER_MP_LIMB > 64
175 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
180 #define howmany(x,y) (((x)+((y)-1))/(y))
182 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
184 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
185 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
186 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
188 #define RETURN(val,end) \
189 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
190 return val; } while (0)
192 /* Maximum size necessary for mpn integers to hold floating point numbers. */
193 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
195 /* Declare an mpn integer variable that big. */
196 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
197 /* Copy an mpn integer value. */
198 #define MPN_ASSIGN(dst, src) \
199 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
202 /* Return a floating point number of the needed type according to the given
203 multi-precision number after possible rounding. */
205 round_and_return (mp_limb_t
*retval
, int exponent
, int negative
,
206 mp_limb_t round_limb
, mp_size_t round_bit
, int more_bits
)
208 if (exponent
< MIN_EXP
- 1)
210 mp_size_t shift
= MIN_EXP
- 1 - exponent
;
212 if (shift
> MANT_DIG
)
218 more_bits
|= (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0;
219 if (shift
== MANT_DIG
)
220 /* This is a special case to handle the very seldom case where
221 the mantissa will be empty after the shift. */
225 round_limb
= retval
[RETURN_LIMB_SIZE
- 1];
226 round_bit
= BITS_PER_MP_LIMB
- 1;
227 for (i
= 0; i
< RETURN_LIMB_SIZE
; ++i
)
228 more_bits
|= retval
[i
] != 0;
229 MPN_ZERO (retval
, RETURN_LIMB_SIZE
);
231 else if (shift
>= BITS_PER_MP_LIMB
)
235 round_limb
= retval
[(shift
- 1) / BITS_PER_MP_LIMB
];
236 round_bit
= (shift
- 1) % BITS_PER_MP_LIMB
;
237 for (i
= 0; i
< (shift
- 1) / BITS_PER_MP_LIMB
; ++i
)
238 more_bits
|= retval
[i
] != 0;
239 more_bits
|= ((round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1))
242 (void) __mpn_rshift (retval
, &retval
[shift
/ BITS_PER_MP_LIMB
],
243 RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
),
244 shift
% BITS_PER_MP_LIMB
);
245 MPN_ZERO (&retval
[RETURN_LIMB_SIZE
- (shift
/ BITS_PER_MP_LIMB
)],
246 shift
/ BITS_PER_MP_LIMB
);
250 round_limb
= retval
[0];
251 round_bit
= shift
- 1;
252 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, shift
);
254 exponent
= MIN_EXP
- 2;
257 if ((round_limb
& (((mp_limb_t
) 1) << round_bit
)) != 0
258 && (more_bits
|| (retval
[0] & 1) != 0
259 || (round_limb
& ((((mp_limb_t
) 1) << round_bit
) - 1)) != 0))
261 mp_limb_t cy
= __mpn_add_1 (retval
, retval
, RETURN_LIMB_SIZE
, 1);
263 if (((MANT_DIG
% BITS_PER_MP_LIMB
) == 0 && cy
) ||
264 ((MANT_DIG
% BITS_PER_MP_LIMB
) != 0 &&
265 (retval
[RETURN_LIMB_SIZE
- 1]
266 & (((mp_limb_t
) 1) << (MANT_DIG
% BITS_PER_MP_LIMB
))) != 0))
269 (void) __mpn_rshift (retval
, retval
, RETURN_LIMB_SIZE
, 1);
270 retval
[RETURN_LIMB_SIZE
- 1]
271 |= ((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
);
273 else if (exponent
== MIN_EXP
- 2
274 && (retval
[RETURN_LIMB_SIZE
- 1]
275 & (((mp_limb_t
) 1) << ((MANT_DIG
- 1) % BITS_PER_MP_LIMB
)))
277 /* The number was denormalized but now normalized. */
278 exponent
= MIN_EXP
- 1;
281 if (exponent
> MAX_EXP
)
282 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
284 return MPN2FLOAT (retval
, exponent
, negative
);
288 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
289 into N. Return the size of the number limbs in NSIZE at the first
290 character od the string that is not part of the integer as the function
291 value. If the EXPONENT is small enough to be taken as an additional
292 factor for the resulting number (see code) multiply by it. */
293 static inline const STRING_TYPE
*
294 str_to_mpn (const STRING_TYPE
*str
, int digcnt
, mp_limb_t
*n
, mp_size_t
*nsize
,
297 /* Number of digits for actual limb. */
306 if (cnt
== MAX_DIG_PER_LIMB
)
313 cy
= __mpn_mul_1 (n
, n
, *nsize
, MAX_FAC_PER_LIMB
);
314 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
323 /* There might be thousands separators or radix characters in
324 the string. But these all can be ignored because we know the
325 format of the number is correct and we have an exact number
326 of characters to read. */
327 while (*str
< L_('0') || *str
> L_('9'))
329 low
= low
* 10 + *str
++ - L_('0');
332 while (--digcnt
> 0);
334 if (*exponent
> 0 && cnt
+ *exponent
<= MAX_DIG_PER_LIMB
)
336 low
*= _tens_in_limb
[*exponent
];
337 start
= _tens_in_limb
[cnt
+ *exponent
];
341 start
= _tens_in_limb
[cnt
];
351 cy
= __mpn_mul_1 (n
, n
, *nsize
, start
);
352 cy
+= __mpn_add_1 (n
, n
, *nsize
, low
);
361 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
362 with the COUNT most significant bits of LIMB.
364 Tege doesn't like this function so I have to write it here myself. :)
367 __mpn_lshift_1 (mp_limb_t
*ptr
, mp_size_t size
, unsigned int count
,
370 if (count
== BITS_PER_MP_LIMB
)
372 /* Optimize the case of shifting by exactly a word:
373 just copy words, with no actual bit-shifting. */
375 for (i
= size
- 1; i
> 0; --i
)
381 (void) __mpn_lshift (ptr
, ptr
, size
, count
);
382 ptr
[0] |= limb
>> (BITS_PER_MP_LIMB
- count
);
387 #define INTERNAL(x) INTERNAL1(x)
388 #define INTERNAL1(x) __##x##_internal
390 /* This file defines a function to check for correct grouping. */
391 #include "grouping.h"
394 /* Return a floating point number with the value of the given string NPTR.
395 Set *ENDPTR to the character after the last used one. If the number is
396 smaller than the smallest representable number, set `errno' to ERANGE and
397 return 0.0. If the number is too big to be represented, set `errno' to
398 ERANGE and return HUGE_VAL with the appropriate sign. */
400 INTERNAL (STRTOF
) (nptr
, endptr
, group LOCALE_PARAM
)
401 const STRING_TYPE
*nptr
;
402 STRING_TYPE
**endptr
;
406 int negative
; /* The sign of the number. */
407 MPN_VAR (num
); /* MP representation of the number. */
408 int exponent
; /* Exponent of the number. */
410 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
413 /* When we have to compute fractional digits we form a fraction with a
414 second multi-precision number (and we sometimes need a second for
415 temporary results). */
418 /* Representation for the return value. */
419 mp_limb_t retval
[RETURN_LIMB_SIZE
];
420 /* Number of bits currently in result value. */
423 /* Running pointer after the last character processed in the string. */
424 const STRING_TYPE
*cp
, *tp
;
425 /* Start of significant part of the number. */
426 const STRING_TYPE
*startp
, *start_of_digits
;
427 /* Points at the character following the integer and fractional digits. */
428 const STRING_TYPE
*expp
;
429 /* Total number of digit and number of digits in integer part. */
430 int dig_no
, int_no
, lead_zero
;
431 /* Contains the last character read. */
434 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
435 there. So define it ourselves if it remains undefined. */
437 typedef unsigned int wint_t;
439 /* The radix character of the current locale. */
441 /* The thousands character of the current locale. */
443 /* The numeric grouping specification of the current locale,
444 in the format described in <locale.h>. */
445 const char *grouping
;
447 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
448 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
453 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
454 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
458 /* Figure out the thousands separator character. */
459 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
460 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
461 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
462 if (thousands
== L
'\0')
472 /* Find the locale's decimal point character. */
473 if (mbtowc ((wchar_t *) &decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
474 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
475 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
476 assert (decimal
!= L
'\0');
478 /* Prepare number representation. */
483 /* Parse string to get maximal legal prefix. We need the number of
484 characters of the integer part, the fractional part and the exponent. */
486 /* Ignore leading white space. */
491 /* Get sign of the result. */
497 else if (c
== L_('+'))
500 /* Return 0.0 if no legal string is found.
501 No character is used even if a sign was found. */
502 if ((c
< L_('0') || c
> L_('9'))
503 && ((wchar_t) c
!= decimal
|| cp
[1] < L_('0') || cp
[1] > L_('9')))
506 /* Check for `INF' or `INFINITY'. */
507 if (TOLOWER (c
) == L_('i') && ((STRNCASECMP (cp
, L_("nf"), 2) == 0
509 || (STRNCASECMP (cp
, L_("nfinity"), 7)
513 /* Return +/- inifity. */
515 *endptr
= (STRING_TYPE
*) (cp
+ matched
);
517 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
520 if (TOLOWER (c
) == L_('n') && STRNCASECMP (cp
, L_("an"), 2) == 0)
529 /* Match `(n-char-sequence-digit)'. */
532 const STRING_TYPE
*startp
= cp
;
535 while ((*cp
>= '0' && *cp
<= '9')
536 || (TOLOWER (*cp
) >= 'a' && TOLOWER (*cp
) <= 'z')
540 /* The closing brace is missing. Only match the NAN
545 /* This is a system-dependent way to specify the
546 bitmask used for the NaN. We expect it to be
547 a number which is put in the mantissa of the
550 unsigned long long int mant
;
552 mant
= STRTOULL (startp
, &endp
, 0);
554 SET_MANTISSA (retval
, mant
);
558 *endptr
= (STRING_TYPE
*) cp
;
564 /* It is really a text we do not recognize. */
568 /* First look whether we are faced with a hexadecimal number. */
569 if (c
== L_('0') && TOLOWER (cp
[1]) == L_('x'))
571 /* Okay, it is a hexa-decimal number. Remember this and skip
572 the characters. BTW: hexadecimal numbers must not be
580 /* Record the start of the digits, in case we will check their grouping. */
581 start_of_digits
= startp
= cp
;
583 /* Ignore leading zeroes. This helps us to avoid useless computations. */
584 while (c
== L_('0') || (thousands
!= L
'\0' && (wchar_t) c
== thousands
))
587 /* If no other digit but a '0' is found the result is 0.0.
588 Return current read pointer. */
589 if ((c
< L_('0') || c
> L_('9')) &&
590 (base
== 16 && (c
< TOLOWER (L_('a')) || c
> TOLOWER (L_('f')))) &&
591 (wchar_t) c
!= decimal
&&
592 (base
== 16 && (cp
== start_of_digits
|| TOLOWER (c
) != L_('p'))) &&
593 (base
!= 16 && TOLOWER (c
) != L_('e')))
595 tp
= correctly_grouped_prefix (start_of_digits
, cp
, thousands
, grouping
);
596 /* If TP is at the start of the digits, there was no correctly
597 grouped prefix of the string; so no number found. */
598 RETURN (0.0, tp
== start_of_digits
? (base
== 16 ? cp
- 1 : nptr
) : tp
);
601 /* Remember first significant digit and read following characters until the
602 decimal point, exponent character or any non-FP number character. */
605 while (dig_no
< (base
== 16 ? HEXNDIG
: NDIG
) ||
606 /* If parsing grouping info, keep going past useful digits
607 so we can check all the grouping separators. */
610 if ((c
>= L_('0') && c
<= L_('9'))
611 || (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
613 else if (thousands
== L
'\0' || (wchar_t) c
!= thousands
)
614 /* Not a digit or separator: end of the integer part. */
619 if (grouping
&& dig_no
> 0)
621 /* Check the grouping of the digits. */
622 tp
= correctly_grouped_prefix (start_of_digits
, cp
, thousands
, grouping
);
625 /* Less than the entire string was correctly grouped. */
627 if (tp
== start_of_digits
)
628 /* No valid group of numbers at all: no valid number. */
632 /* The number is validly grouped, but consists
633 only of zeroes. The whole value is zero. */
636 /* Recompute DIG_NO so we won't read more digits than
637 are properly grouped. */
640 for (tp
= startp
; tp
< cp
; ++tp
)
641 if (*tp
>= L_('0') && *tp
<= L_('9'))
651 if (dig_no
>= (base
== 16 ? HEXNDIG
: NDIG
))
652 /* Too many digits to be representable. Assigning this to EXPONENT
653 allows us to read the full number but return HUGE_VAL after parsing. */
654 exponent
= MAX_10_EXP
;
656 /* We have the number digits in the integer part. Whether these are all or
657 any is really a fractional digit will be decided later. */
659 lead_zero
= int_no
== 0 ? -1 : 0;
661 /* Read the fractional digits. A special case are the 'american style'
662 numbers like `16.' i.e. with decimal but without trailing digits. */
663 if ((wchar_t) c
== decimal
)
666 while (c
>= L_('0') && c
<= L_('9') ||
667 (base
== 16 && TOLOWER (c
) >= L_('a') && TOLOWER (c
) <= L_('f')))
669 if (c
!= L_('0') && lead_zero
== -1)
670 lead_zero
= dig_no
- int_no
;
676 /* Remember start of exponent (if any). */
680 if ((base
== 16 && TOLOWER (c
) == L_('p'))
681 || (base
!= 16 && TOLOWER (c
) == L_('e')))
683 int exp_negative
= 0;
691 else if (c
== L_('+'))
694 if (c
>= L_('0') && c
<= L_('9'))
698 /* Get the exponent limit. */
700 exp_limit
= (exp_negative
?
701 -MIN_EXP
+ MANT_DIG
- 4 * int_no
:
702 MAX_EXP
- 4 * int_no
+ lead_zero
);
704 exp_limit
= (exp_negative
?
705 -MIN_10_EXP
+ MANT_DIG
- int_no
:
706 MAX_10_EXP
- int_no
+ lead_zero
);
712 if (exponent
> exp_limit
)
713 /* The exponent is too large/small to represent a valid
718 /* Overflow or underflow. */
719 __set_errno (ERANGE
);
720 result
= (exp_negative
? 0.0 :
721 negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
);
723 /* Accept all following digits as part of the exponent. */
726 while (*cp
>= L_('0') && *cp
<= L_('9'));
732 exponent
+= c
- L_('0');
735 while (c
>= L_('0') && c
<= L_('9'));
738 exponent
= -exponent
;
744 /* We don't want to have to work with trailing zeroes after the radix. */
747 while (expp
[-1] == L_('0'))
752 assert (dig_no
>= int_no
);
757 /* The whole string is parsed. Store the address of the next character. */
759 *endptr
= (STRING_TYPE
*) cp
;
766 /* Find the decimal point */
767 while ((wchar_t) *startp
!= decimal
)
769 startp
+= lead_zero
+ 1;
770 exponent
-= base
== 16 ? 4 * lead_zero
: lead_zero
;
774 /* If the BASE is 16 we can use a simpler algorithm. */
777 static const int nbits
[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
778 4, 4, 4, 4, 4, 4, 4, 4 };
779 int idx
= (MANT_DIG
- 1) / BITS_PER_MP_LIMB
;
780 int pos
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
783 while (!ISXDIGIT (*startp
))
785 if (ISDIGIT (*startp
))
786 val
= *startp
++ - L_('0');
788 val
= 10 + TOLOWER (*startp
++) - L_('a');
793 /* We don't have to care for wrapping. This is the normal
794 case so we add this optimization. */
795 retval
[idx
] = val
<< (pos
- bits
+ 1);
802 retval
[idx
] = val
<< (pos
- bits
+ 1);
807 retval
[idx
--] = val
>> (bits
- pos
- 1);
808 retval
[idx
] = val
<< (BITS_PER_MP_LIMB
- (bits
- pos
- 1));
809 pos
= BITS_PER_MP_LIMB
- 1 - (bits
- pos
- 1);
813 while (--dig_no
> 0 && idx
>= 0)
815 while (!ISXDIGIT (*startp
))
817 if (ISDIGIT (*startp
))
818 val
= *startp
++ - L_('0');
820 val
= 10 + TOLOWER (*startp
++) - L_('a');
824 retval
[idx
] |= val
<< (pos
- 4 + 1);
829 retval
[idx
--] |= val
>> (4 - pos
- 1);
830 val
<<= BITS_PER_MP_LIMB
- (4 - pos
- 1);
832 return round_and_return (retval
, exponent
, negative
, val
,
833 BITS_PER_MP_LIMB
- 1, dig_no
> 0);
836 pos
= BITS_PER_MP_LIMB
- 1 - (4 - pos
- 1);
840 /* We ran out of digits. */
841 MPN_ZERO (retval
, idx
);
843 return round_and_return (retval
, exponent
, negative
, 0, 0, 0);
846 /* Now we have the number of digits in total and the integer digits as well
847 as the exponent and its sign. We can decide whether the read digits are
848 really integer digits or belong to the fractional part; i.e. we normalize
851 register int incr
= (exponent
< 0 ? MAX (-int_no
, exponent
)
852 : MIN (dig_no
- int_no
, exponent
));
857 if (int_no
+ exponent
> MAX_10_EXP
+ 1)
859 __set_errno (ERANGE
);
860 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
863 if (exponent
< MIN_10_EXP
- (DIG
+ 1))
865 __set_errno (ERANGE
);
871 /* Read the integer part as a multi-precision number to NUM. */
872 startp
= str_to_mpn (startp
, int_no
, num
, &numsize
, &exponent
);
876 /* We now multiply the gained number by the given power of ten. */
877 mp_limb_t
*psrc
= num
;
878 mp_limb_t
*pdest
= den
;
880 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
884 if ((exponent
& expbit
) != 0)
889 /* FIXME: not the whole multiplication has to be
890 done. If we have the needed number of bits we
891 only need the information whether more non-zero
893 if (numsize
>= ttab
->arraysize
- _FPIO_CONST_OFFSET
)
894 cy
= __mpn_mul (pdest
, psrc
, numsize
,
895 &ttab
->array
[_FPIO_CONST_OFFSET
],
896 ttab
->arraysize
- _FPIO_CONST_OFFSET
);
898 cy
= __mpn_mul (pdest
, &ttab
->array
[_FPIO_CONST_OFFSET
],
899 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
901 numsize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
909 while (exponent
!= 0);
912 memcpy (num
, den
, numsize
* sizeof (mp_limb_t
));
915 /* Determine how many bits of the result we already have. */
916 count_leading_zeros (bits
, num
[numsize
- 1]);
917 bits
= numsize
* BITS_PER_MP_LIMB
- bits
;
919 /* Now we know the exponent of the number in base two.
920 Check it against the maximum possible exponent. */
923 __set_errno (ERANGE
);
924 return negative
? -FLOAT_HUGE_VAL
: FLOAT_HUGE_VAL
;
927 /* We have already the first BITS bits of the result. Together with
928 the information whether more non-zero bits follow this is enough
929 to determine the result. */
933 const mp_size_t least_idx
= (bits
- MANT_DIG
) / BITS_PER_MP_LIMB
;
934 const mp_size_t least_bit
= (bits
- MANT_DIG
) % BITS_PER_MP_LIMB
;
935 const mp_size_t round_idx
= least_bit
== 0 ? least_idx
- 1
937 const mp_size_t round_bit
= least_bit
== 0 ? BITS_PER_MP_LIMB
- 1
941 memcpy (retval
, &num
[least_idx
],
942 RETURN_LIMB_SIZE
* sizeof (mp_limb_t
));
945 for (i
= least_idx
; i
< numsize
- 1; ++i
)
946 retval
[i
- least_idx
] = (num
[i
] >> least_bit
)
948 << (BITS_PER_MP_LIMB
- least_bit
));
949 if (i
- least_idx
< RETURN_LIMB_SIZE
)
950 retval
[RETURN_LIMB_SIZE
- 1] = num
[i
] >> least_bit
;
953 /* Check whether any limb beside the ones in RETVAL are non-zero. */
954 for (i
= 0; num
[i
] == 0; ++i
)
957 return round_and_return (retval
, bits
- 1, negative
,
958 num
[round_idx
], round_bit
,
959 int_no
< dig_no
|| i
< round_idx
);
962 else if (dig_no
== int_no
)
964 const mp_size_t target_bit
= (MANT_DIG
- 1) % BITS_PER_MP_LIMB
;
965 const mp_size_t is_bit
= (bits
- 1) % BITS_PER_MP_LIMB
;
967 if (target_bit
== is_bit
)
969 memcpy (&retval
[RETURN_LIMB_SIZE
- numsize
], num
,
970 numsize
* sizeof (mp_limb_t
));
971 /* FIXME: the following loop can be avoided if we assume a
972 maximal MANT_DIG value. */
973 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
975 else if (target_bit
> is_bit
)
977 (void) __mpn_lshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
978 num
, numsize
, target_bit
- is_bit
);
979 /* FIXME: the following loop can be avoided if we assume a
980 maximal MANT_DIG value. */
981 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
);
986 assert (numsize
< RETURN_LIMB_SIZE
);
988 cy
= __mpn_rshift (&retval
[RETURN_LIMB_SIZE
- numsize
],
989 num
, numsize
, is_bit
- target_bit
);
990 retval
[RETURN_LIMB_SIZE
- numsize
- 1] = cy
;
991 /* FIXME: the following loop can be avoided if we assume a
992 maximal MANT_DIG value. */
993 MPN_ZERO (retval
, RETURN_LIMB_SIZE
- numsize
- 1);
996 return round_and_return (retval
, bits
- 1, negative
, 0, 0, 0);
1000 /* Store the bits we already have. */
1001 memcpy (retval
, num
, numsize
* sizeof (mp_limb_t
));
1002 #if RETURN_LIMB_SIZE > 1
1003 if (numsize
< RETURN_LIMB_SIZE
)
1004 retval
[numsize
] = 0;
1008 /* We have to compute at least some of the fractional digits. */
1010 /* We construct a fraction and the result of the division gives us
1011 the needed digits. The denominator is 1.0 multiplied by the
1012 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1013 123e-6 gives 123 / 1000000. */
1020 mp_limb_t
*psrc
= den
;
1021 mp_limb_t
*pdest
= num
;
1022 const struct mp_power
*ttab
= &_fpioconst_pow10
[0];
1024 assert (dig_no
> int_no
&& exponent
<= 0);
1027 /* For the fractional part we need not process too many digits. One
1028 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1030 digits we should have enough bits for the result. The remaining
1031 decimal digits give us the information that more bits are following.
1032 This can be used while rounding. (One added as a safety margin.) */
1033 if (dig_no
- int_no
> (MANT_DIG
- bits
+ 2) / 3 + 1)
1035 dig_no
= int_no
+ (MANT_DIG
- bits
+ 2) / 3 + 1;
1041 neg_exp
= dig_no
- int_no
- exponent
;
1043 /* Construct the denominator. */
1048 if ((neg_exp
& expbit
) != 0)
1055 densize
= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1056 memcpy (psrc
, &ttab
->array
[_FPIO_CONST_OFFSET
],
1057 densize
* sizeof (mp_limb_t
));
1061 cy
= __mpn_mul (pdest
, &ttab
->array
[_FPIO_CONST_OFFSET
],
1062 ttab
->arraysize
- _FPIO_CONST_OFFSET
,
1064 densize
+= ttab
->arraysize
- _FPIO_CONST_OFFSET
;
1073 while (neg_exp
!= 0);
1076 memcpy (den
, num
, densize
* sizeof (mp_limb_t
));
1078 /* Read the fractional digits from the string. */
1079 (void) str_to_mpn (startp
, dig_no
- int_no
, num
, &numsize
, &exponent
);
1082 /* We now have to shift both numbers so that the highest bit in the
1083 denominator is set. In the same process we copy the numerator to
1084 a high place in the array so that the division constructs the wanted
1085 digits. This is done by a "quasi fix point" number representation.
1087 num: ddddddddddd . 0000000000000000000000
1089 den: ddddddddddd n >= m
1093 count_leading_zeros (cnt
, den
[densize
- 1]);
1095 (void) __mpn_lshift (den
, den
, densize
, cnt
);
1096 cy
= __mpn_lshift (num
, num
, numsize
, cnt
);
1098 num
[numsize
++] = cy
;
1100 /* Now we are ready for the division. But it is not necessary to
1101 do a full multi-precision division because we only need a small
1102 number of bits for the result. So we do not use __mpn_divmod
1103 here but instead do the division here by hand and stop whenever
1104 the needed number of bits is reached. The code itself comes
1105 from the GNU MP Library by Torbj\"orn Granlund. */
1113 mp_limb_t d
, n
, quot
;
1118 assert (numsize
== 1 && n
< d
);
1122 udiv_qrnnd (quot
, n
, n
, 0, d
);
1129 cnt = BITS_PER_MP_LIMB; \
1131 count_leading_zeros (cnt, quot); \
1133 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1135 used = MANT_DIG + cnt; \
1136 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1137 bits = MANT_DIG + 1; \
1141 /* Note that we only clear the second element. */ \
1142 /* The conditional is determined at compile time. */ \
1143 if (RETURN_LIMB_SIZE > 1) \
1149 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1150 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1154 used = MANT_DIG - bits; \
1156 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1158 bits += BITS_PER_MP_LIMB
1162 while (bits
<= MANT_DIG
);
1164 return round_and_return (retval
, exponent
- 1, negative
,
1165 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1166 more_bits
|| n
!= 0);
1170 mp_limb_t d0
, d1
, n0
, n1
;
1177 if (numsize
< densize
)
1181 /* The numerator of the number occupies fewer bits than
1182 the denominator but the one limb is bigger than the
1183 high limb of the numerator. */
1190 exponent
-= BITS_PER_MP_LIMB
;
1193 if (bits
+ BITS_PER_MP_LIMB
<= MANT_DIG
)
1194 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1195 BITS_PER_MP_LIMB
, 0);
1198 used
= MANT_DIG
- bits
;
1200 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1202 bits
+= BITS_PER_MP_LIMB
;
1214 while (bits
<= MANT_DIG
)
1220 /* QUOT should be either 111..111 or 111..110. We need
1221 special treatment of this rare case as normal division
1222 would give overflow. */
1223 quot
= ~(mp_limb_t
) 0;
1226 if (r
< d1
) /* Carry in the addition? */
1228 add_ssaaaa (n1
, n0
, r
- d0
, 0, 0, d0
);
1231 n1
= d0
- (d0
!= 0);
1236 udiv_qrnnd (quot
, r
, n1
, n0
, d1
);
1237 umul_ppmm (n1
, n0
, d0
, quot
);
1241 if (n1
> r
|| (n1
== r
&& n0
> 0))
1243 /* The estimated QUOT was too large. */
1246 sub_ddmmss (n1
, n0
, n1
, n0
, 0, d0
);
1248 if (r
>= d1
) /* If not carry, test QUOT again. */
1251 sub_ddmmss (n1
, n0
, r
, 0, n1
, n0
);
1257 return round_and_return (retval
, exponent
- 1, negative
,
1258 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1259 more_bits
|| n1
!= 0 || n0
!= 0);
1264 mp_limb_t cy
, dX
, d1
, n0
, n1
;
1268 dX
= den
[densize
- 1];
1269 d1
= den
[densize
- 2];
1271 /* The division does not work if the upper limb of the two-limb
1272 numerator is greater than the denominator. */
1273 if (__mpn_cmp (num
, &den
[densize
- numsize
], numsize
) > 0)
1276 if (numsize
< densize
)
1278 mp_size_t empty
= densize
- numsize
;
1283 for (i
= numsize
; i
> 0; --i
)
1284 num
[i
+ empty
] = num
[i
- 1];
1285 MPN_ZERO (num
, empty
+ 1);
1286 exponent
-= empty
* BITS_PER_MP_LIMB
;
1290 if (bits
+ empty
* BITS_PER_MP_LIMB
<= MANT_DIG
)
1292 /* We make a difference here because the compiler
1293 cannot optimize the `else' case that good and
1294 this reflects all currently used FLOAT types
1295 and GMP implementations. */
1297 #if RETURN_LIMB_SIZE <= 2
1298 assert (empty
== 1);
1299 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
,
1300 BITS_PER_MP_LIMB
, 0);
1302 for (i
= RETURN_LIMB_SIZE
; i
> empty
; --i
)
1303 retval
[i
] = retval
[i
- empty
];
1305 #if RETURN_LIMB_SIZE > 1
1308 for (i
= numsize
; i
> 0; --i
)
1309 num
[i
+ empty
] = num
[i
- 1];
1310 MPN_ZERO (num
, empty
+ 1);
1314 used
= MANT_DIG
- bits
;
1315 if (used
>= BITS_PER_MP_LIMB
)
1318 (void) __mpn_lshift (&retval
[used
1319 / BITS_PER_MP_LIMB
],
1320 retval
, RETURN_LIMB_SIZE
,
1321 used
% BITS_PER_MP_LIMB
);
1322 for (i
= used
/ BITS_PER_MP_LIMB
; i
>= 0; --i
)
1326 __mpn_lshift_1 (retval
, RETURN_LIMB_SIZE
, used
, 0);
1328 bits
+= empty
* BITS_PER_MP_LIMB
;
1334 assert (numsize
== densize
);
1335 for (i
= numsize
; i
> 0; --i
)
1336 num
[i
] = num
[i
- 1];
1342 while (bits
<= MANT_DIG
)
1345 /* This might over-estimate QUOT, but it's probably not
1346 worth the extra code here to find out. */
1347 quot
= ~(mp_limb_t
) 0;
1352 udiv_qrnnd (quot
, r
, n0
, num
[densize
- 1], dX
);
1353 umul_ppmm (n1
, n0
, d1
, quot
);
1355 while (n1
> r
|| (n1
== r
&& n0
> num
[densize
- 2]))
1359 if (r
< dX
) /* I.e. "carry in previous addition?" */
1366 /* Possible optimization: We already have (q * n0) and (1 * n1)
1367 after the calculation of QUOT. Taking advantage of this, we
1368 could make this loop make two iterations less. */
1370 cy
= __mpn_submul_1 (num
, den
, densize
+ 1, quot
);
1372 if (num
[densize
] != cy
)
1374 cy
= __mpn_add_n (num
, num
, den
, densize
);
1378 n0
= num
[densize
] = num
[densize
- 1];
1379 for (i
= densize
- 1; i
> 0; --i
)
1380 num
[i
] = num
[i
- 1];
1385 for (i
= densize
; num
[i
] == 0 && i
>= 0; --i
)
1387 return round_and_return (retval
, exponent
- 1, negative
,
1388 quot
, BITS_PER_MP_LIMB
- 1 - used
,
1389 more_bits
|| i
>= 0);
1397 /* External user entry point. */
1400 #ifdef weak_function
1403 STRTOF (nptr
, endptr LOCALE_PARAM
)
1404 const STRING_TYPE
*nptr
;
1405 STRING_TYPE
**endptr
;
1408 return INTERNAL (STRTOF
) (nptr
, endptr
, 0 LOCALE_PARAM
);