1
1
/* Convert string representing a number to float value, using given locale.
2
Copyright (C) 1997,1998,2002,2004,2005,2006,2007,2008,2009,2010,2011
3
Free Software Foundation, Inc.
2
Copyright (C) 1997-2012 Free Software Foundation, Inc.
4
3
This file is part of the GNU C Library.
5
4
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
15
14
Lesser General Public License for more details.
17
16
You should have received a copy of the GNU Lesser General Public
18
License along with the GNU C Library; if not, write to the Free
19
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17
License along with the GNU C Library; if not, see
18
<http://www.gnu.org/licenses/>. */
22
20
#include <gnu/option-groups.h>
23
21
#include <xlocale.h>
177
175
/* Return a floating point number of the needed type according to the given
178
176
multi-precision number after possible rounding. */
180
round_and_return (mp_limb_t *retval, int exponent, int negative,
178
round_and_return (mp_limb_t *retval, intmax_t exponent, int negative,
181
179
mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
183
181
if (exponent < MIN_EXP - 1)
183
if (exponent < MIN_EXP - 1 - MANT_DIG)
185
__set_errno (ERANGE);
185
189
mp_size_t shift = MIN_EXP - 1 - exponent;
187
if (shift > MANT_DIG)
189
__set_errno (ERANGE);
193
191
more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
194
192
if (shift == MANT_DIG)
195
193
/* This is a special case to handle the very seldom case where
274
276
factor for the resulting number (see code) multiply by it. */
275
277
static const STRING_TYPE *
276
278
str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
278
280
#ifndef USE_WIDE_CHAR
279
281
, const char *decimal, size_t decimal_len, const char *thousands
358
361
cy = __mpn_mul_1 (n, n, *nsize, start);
359
362
cy += __mpn_add_1 (n, n, *nsize, low);
365
assert (*nsize < MPNSIZE);
417
423
int negative; /* The sign of the number. */
418
424
MPN_VAR (num); /* MP representation of the number. */
419
int exponent; /* Exponent of the number. */
425
intmax_t exponent; /* Exponent of the number. */
421
427
/* Numbers starting `0X' or `0x' have to be processed with base 16. */
438
444
/* Points at the character following the integer and fractional digits. */
439
445
const STRING_TYPE *expp;
440
446
/* Total number of digit and number of digits in integer part. */
441
int dig_no, int_no, lead_zero;
447
size_t dig_no, int_no, lead_zero;
442
448
/* Contains the last character read. */
782
788
are all or any is really a fractional digit will be decided
785
lead_zero = int_no == 0 ? -1 : 0;
791
lead_zero = int_no == 0 ? (size_t) -1 : 0;
787
793
/* Read the fractional digits. A special case are the 'american
788
794
style' numbers like `16.' i.e. with decimal point but without
804
810
(base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
805
811
lo >= L_('a') && lo <= L_('f'); })))
807
if (c != L_('0') && lead_zero == -1)
813
if (c != L_('0') && lead_zero == (size_t) -1)
808
814
lead_zero = dig_no - int_no;
819
assert (dig_no <= (uintmax_t) INTMAX_MAX);
814
821
/* Remember start of exponent (if any). */
833
840
if (c >= L_('0') && c <= L_('9'))
837
844
/* Get the exponent limit. */
839
exp_limit = (exp_negative ?
840
-MIN_EXP + MANT_DIG + 4 * int_no :
841
MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
849
assert (int_no <= (uintmax_t) (INTMAX_MAX
850
+ MIN_EXP - MANT_DIG) / 4);
851
exp_limit = -MIN_EXP + MANT_DIG + 4 * (intmax_t) int_no;
857
assert (lead_zero == 0
858
&& int_no <= (uintmax_t) INTMAX_MAX / 4);
859
exp_limit = MAX_EXP - 4 * (intmax_t) int_no + 3;
861
else if (lead_zero == (size_t) -1)
863
/* The number is zero and this limit is
865
exp_limit = MAX_EXP + 3;
870
<= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
872
+ 4 * (intmax_t) lead_zero
843
exp_limit = (exp_negative ?
844
-MIN_10_EXP + MANT_DIG + int_no :
845
MAX_10_EXP - int_no + lead_zero + 1);
882
<= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
883
exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
889
assert (lead_zero == 0
890
&& int_no <= (uintmax_t) INTMAX_MAX);
891
exp_limit = MAX_10_EXP - (intmax_t) int_no + 1;
893
else if (lead_zero == (size_t) -1)
895
/* The number is zero and this limit is
897
exp_limit = MAX_10_EXP + 1;
902
<= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
903
exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
850
exponent += c - L_('0');
852
if (__builtin_expect (exponent > exp_limit, 0))
913
if (__builtin_expect ((exponent > exp_limit / 10
914
|| (exponent == exp_limit / 10
915
&& c - L_('0') > exp_limit % 10)), 0))
853
916
/* The exponent is too large/small to represent a valid
947
1013
startp += lead_zero + decimal_len;
948
exponent -= base == 16 ? 4 * lead_zero : lead_zero;
1014
assert (lead_zero <= (base == 16
1015
? (uintmax_t) INTMAX_MAX / 4
1016
: (uintmax_t) INTMAX_MAX));
1017
assert (lead_zero <= (base == 16
1018
? ((uintmax_t) exponent
1019
- (uintmax_t) INTMAX_MIN) / 4
1020
: ((uintmax_t) exponent - (uintmax_t) INTMAX_MIN)));
1021
exponent -= base == 16 ? 4 * (intmax_t) lead_zero : (intmax_t) lead_zero;
949
1022
dig_no -= lead_zero;
989
1062
/* Adjust the exponent for the bits we are shifting in. */
990
exponent += bits - 1 + (int_no - 1) * 4;
1063
assert (int_no <= (uintmax_t) (exponent < 0
1064
? (INTMAX_MAX - bits + 1) / 4
1065
: (INTMAX_MAX - exponent - bits + 1) / 4));
1066
exponent += bits - 1 + ((intmax_t) int_no - 1) * 4;
992
1068
while (--dig_no > 0 && idx >= 0)
1008
1084
retval[idx--] |= val >> (4 - pos - 1);
1009
1085
val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1011
return round_and_return (retval, exponent, negative, val,
1012
BITS_PER_MP_LIMB - 1, dig_no > 0);
1088
int rest_nonzero = 0;
1089
while (--dig_no > 0)
1091
if (*startp != L_('0'))
1098
return round_and_return (retval, exponent, negative, val,
1099
BITS_PER_MP_LIMB - 1, rest_nonzero);
1014
1102
retval[idx] = val;
1015
1103
pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1027
1115
really integer digits or belong to the fractional part; i.e. we normalize
1028
1116
123e-2 to 1.23. */
1030
register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1031
: MIN (dig_no - int_no, exponent));
1118
register intmax_t incr = (exponent < 0
1119
? MAX (-(intmax_t) int_no, exponent)
1120
: MIN ((intmax_t) dig_no - (intmax_t) int_no,
1032
1122
int_no += incr;
1033
1123
exponent -= incr;
1036
if (__builtin_expect (int_no + exponent > MAX_10_EXP + 1, 0))
1126
if (__builtin_expect (exponent > MAX_10_EXP + 1 - (intmax_t) int_no, 0))
1038
1128
__set_errno (ERANGE);
1039
1129
return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1218
1308
digits we should have enough bits for the result. The remaining
1219
1309
decimal digits give us the information that more bits are following.
1220
1310
This can be used while rounding. (Two added as a safety margin.) */
1221
if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1311
if ((intmax_t) dig_no > (intmax_t) int_no + (MANT_DIG - bits + 2) / 3 + 2)
1223
1313
dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;