~ubuntu-branches/ubuntu/utopic/eglibc/utopic

« back to all changes in this revision

Viewing changes to stdlib/strtod_l.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-10-26 05:14:58 UTC
  • mfrom: (1.5.1) (4.4.22 experimental)
  • Revision ID: package-import@ubuntu.com-20121026051458-oryotr4i03ob5pab
Tags: 2.16-0ubuntu1
* Merge with unreleased 2.16 in Debian experimental, remaining changes:
  - Drop the Breaks line from libc6, which refers to a Debian transition
  - Remove the libc6 recommends on libc6-i686, which we don't build
  - Enable libc6{,-dev}-armel on armhf and libc6{-dev}-armhf on armel
  - Ship update-locale and validlocale in /usr/sbin in libc-bin
  - Don't build locales or locales-all in Ubuntu, we rely on langpacks
  - Heavily mangle the way we do service restarting on major upgrades
  - Use different MIN_KERNEL_SUPPORTED versions than Debian, due to
    buildd needs.  This should be universally bumped to 3.2.0 once all
    our buildds (including the PPA guests) are running precise kernels
  - Build i386 variants as -march=i686, build amd64 with -O3, and build
    ppc64 variants (both 64-bit and 32-bit) with -O3 -fno-tree-vectorize
  - Re-enable unsubmitted-ldconfig-cache-abi.diff and rebuild the cache
    on upgrades from previous versions that used a different constant
  - debian/patches/any/local-CVE-2012-3406.diff: switch to malloc when
    array grows too large to handle via alloca extension (CVE-2012-3406)
  - Build generic i386/i686 flavour with -mno-tls-direct-seg-refs
* Changes added/dropped with this merge while reducing our delta:
  - Stop building glibc docs from the eglibc source, and instead make
    the glibc-docs stub have a hard dependency on glibc-doc-reference
  - Remove outdated conflicts against ancient versions of ia32-libs
  - Drop the tzdata dependency from libc6, it's in required and minimal
  - Use gcc-4.7/g++-4.7 by default on all our supported architectures
  - Save our historical changelog as changelog.ubuntu in the source
  - Drop nscd's libaudit build-dep for now, as libaudit is in universe
  - Drop the unnecessary Breaks from libc6 to locales and locales-all
  - Ship xen's ld.so.conf.d snippet as /etc/ld.so.conf.d/libc6-xen.conf
* Disable hard failures on the test suite for the first upload to raring

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
6
5
 
15
14
   Lesser General Public License for more details.
16
15
 
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
20
 
   02111-1307 USA.  */
 
17
   License along with the GNU C Library; if not, see
 
18
   <http://www.gnu.org/licenses/>.  */
21
19
 
22
20
#include <gnu/option-groups.h>
23
21
#include <xlocale.h>
63
61
#include <math.h>
64
62
#include <stdlib.h>
65
63
#include <string.h>
 
64
#include <stdint.h>
66
65
 
67
66
/* The gmp headers need some configuration frobs.  */
68
67
#define HAVE_ALLOCA 1
75
74
#include "longlong.h"
76
75
#include "fpioconst.h"
77
76
 
78
 
#define NDEBUG 1
79
77
#include <assert.h>
80
78
 
81
79
 
177
175
/* Return a floating point number of the needed type according to the given
178
176
   multi-precision number after possible rounding.  */
179
177
static FLOAT
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)
182
180
{
183
181
  if (exponent < MIN_EXP - 1)
184
182
    {
 
183
      if (exponent < MIN_EXP - 1 - MANT_DIG)
 
184
        {
 
185
          __set_errno (ERANGE);
 
186
          return 0.0;
 
187
        }
 
188
 
185
189
      mp_size_t shift = MIN_EXP - 1 - exponent;
186
190
 
187
 
      if (shift > MANT_DIG)
188
 
        {
189
 
          __set_errno (ERANGE);
190
 
          return 0.0;
191
 
        }
192
 
 
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
236
234
      __set_errno (ERANGE);
237
235
    }
238
236
 
 
237
  if (exponent > MAX_EXP)
 
238
    goto overflow;
 
239
 
239
240
  if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
240
241
      && (more_bits || (retval[0] & 1) != 0
241
242
          || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
261
262
    }
262
263
 
263
264
  if (exponent > MAX_EXP)
 
265
  overflow:
264
266
    return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
265
267
 
266
268
  return MPN2FLOAT (retval, exponent, negative);
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,
277
 
            int *exponent
 
279
            intmax_t *exponent
278
280
#ifndef USE_WIDE_CHAR
279
281
            , const char *decimal, size_t decimal_len, const char *thousands
280
282
#endif
304
306
              cy += __mpn_add_1 (n, n, *nsize, low);
305
307
              if (cy != 0)
306
308
                {
 
309
                  assert (*nsize < MPNSIZE);
307
310
                  n[*nsize] = cy;
308
311
                  ++(*nsize);
309
312
                }
338
341
    }
339
342
  while (--digcnt > 0);
340
343
 
341
 
  if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
 
344
  if (*exponent > 0 && *exponent <= MAX_DIG_PER_LIMB - cnt)
342
345
    {
343
346
      low *= _tens_in_limb[*exponent];
344
347
      start = _tens_in_limb[cnt + *exponent];
358
361
      cy = __mpn_mul_1 (n, n, *nsize, start);
359
362
      cy += __mpn_add_1 (n, n, *nsize, low);
360
363
      if (cy != 0)
361
 
        n[(*nsize)++] = cy;
 
364
        {
 
365
          assert (*nsize < MPNSIZE);
 
366
          n[(*nsize)++] = cy;
 
367
        }
362
368
    }
363
369
 
364
370
  return str;
416
422
{
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.  */
420
426
 
421
427
  /* Numbers starting `0X' or `0x' have to be processed with base 16.  */
422
428
  int base = 10;
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.  */
443
449
  CHAR_TYPE c;
444
450
 
782
788
     are all or any is really a fractional digit will be decided
783
789
     later.  */
784
790
  int_no = dig_no;
785
 
  lead_zero = int_no == 0 ? -1 : 0;
 
791
  lead_zero = int_no == 0 ? (size_t) -1 : 0;
786
792
 
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'); })))
806
812
        {
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;
809
815
          ++dig_no;
810
816
          c = *++cp;
811
817
        }
812
818
    }
 
819
  assert (dig_no <= (uintmax_t) INTMAX_MAX);
813
820
 
814
821
  /* Remember start of exponent (if any).  */
815
822
  expp = cp;
832
839
 
833
840
      if (c >= L_('0') && c <= L_('9'))
834
841
        {
835
 
          int exp_limit;
 
842
          intmax_t exp_limit;
836
843
 
837
844
          /* Get the exponent limit. */
838
845
          if (base == 16)
839
 
            exp_limit = (exp_negative ?
840
 
                         -MIN_EXP + MANT_DIG + 4 * int_no :
841
 
                         MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
 
846
            {
 
847
              if (exp_negative)
 
848
                {
 
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;
 
852
                }
 
853
              else
 
854
                {
 
855
                  if (int_no)
 
856
                    {
 
857
                      assert (lead_zero == 0
 
858
                              && int_no <= (uintmax_t) INTMAX_MAX / 4);
 
859
                      exp_limit = MAX_EXP - 4 * (intmax_t) int_no + 3;
 
860
                    }
 
861
                  else if (lead_zero == (size_t) -1)
 
862
                    {
 
863
                      /* The number is zero and this limit is
 
864
                         arbitrary.  */
 
865
                      exp_limit = MAX_EXP + 3;
 
866
                    }
 
867
                  else
 
868
                    {
 
869
                      assert (lead_zero
 
870
                              <= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
 
871
                      exp_limit = (MAX_EXP
 
872
                                   + 4 * (intmax_t) lead_zero
 
873
                                   + 3);
 
874
                    }
 
875
                }
 
876
            }
842
877
          else
843
 
            exp_limit = (exp_negative ?
844
 
                         -MIN_10_EXP + MANT_DIG + int_no :
845
 
                         MAX_10_EXP - int_no + lead_zero + 1);
 
878
            {
 
879
              if (exp_negative)
 
880
                {
 
881
                  assert (int_no
 
882
                          <= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
 
883
                  exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
 
884
                }
 
885
              else
 
886
                {
 
887
                  if (int_no)
 
888
                    {
 
889
                      assert (lead_zero == 0
 
890
                              && int_no <= (uintmax_t) INTMAX_MAX);
 
891
                      exp_limit = MAX_10_EXP - (intmax_t) int_no + 1;
 
892
                    }
 
893
                  else if (lead_zero == (size_t) -1)
 
894
                    {
 
895
                      /* The number is zero and this limit is
 
896
                         arbitrary.  */
 
897
                      exp_limit = MAX_10_EXP + 1;
 
898
                    }
 
899
                  else
 
900
                    {
 
901
                      assert (lead_zero
 
902
                              <= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
 
903
                      exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
 
904
                    }
 
905
                }
 
906
            }
 
907
 
 
908
          if (exp_limit < 0)
 
909
            exp_limit = 0;
846
910
 
847
911
          do
848
912
            {
849
 
              exponent *= 10;
850
 
              exponent += c - L_('0');
851
 
 
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
854
917
                   number.  */
855
918
                {
858
921
                  /* We have to take care for special situation: a joker
859
922
                     might have written "0.0e100000" which is in fact
860
923
                     zero.  */
861
 
                  if (lead_zero == -1)
 
924
                  if (lead_zero == (size_t) -1)
862
925
                    result = negative ? -0.0 : 0.0;
863
926
                  else
864
927
                    {
877
940
                  /* NOTREACHED */
878
941
                }
879
942
 
 
943
              exponent *= 10;
 
944
              exponent += c - L_('0');
 
945
 
880
946
              c = *++cp;
881
947
            }
882
948
          while (c >= L_('0') && c <= L_('9'));
945
1011
        }
946
1012
#endif
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;
950
1023
    }
951
1024
 
987
1060
        }
988
1061
 
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;
991
1067
 
992
1068
      while (--dig_no > 0 && idx >= 0)
993
1069
        {
1008
1084
              retval[idx--] |= val >> (4 - pos - 1);
1009
1085
              val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1010
1086
              if (idx < 0)
1011
 
                return round_and_return (retval, exponent, negative, val,
1012
 
                                         BITS_PER_MP_LIMB - 1, dig_no > 0);
 
1087
                {
 
1088
                  int rest_nonzero = 0;
 
1089
                  while (--dig_no > 0)
 
1090
                    {
 
1091
                      if (*startp != L_('0'))
 
1092
                        {
 
1093
                          rest_nonzero = 1;
 
1094
                          break;
 
1095
                        }
 
1096
                      startp++;
 
1097
                    }
 
1098
                  return round_and_return (retval, exponent, negative, val,
 
1099
                                           BITS_PER_MP_LIMB - 1, rest_nonzero);
 
1100
                }
1013
1101
 
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.  */
1029
1117
  {
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,
 
1121
                                     exponent));
1032
1122
    int_no += incr;
1033
1123
    exponent -= incr;
1034
1124
  }
1035
1125
 
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))
1037
1127
    {
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)
1222
1312
      {
1223
1313
        dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1224
1314
        more_bits = 1;
1226
1316
    else
1227
1317
      more_bits = 0;
1228
1318
 
1229
 
    neg_exp = dig_no - int_no - exponent;
 
1319
    neg_exp = (intmax_t) dig_no - (intmax_t) int_no - exponent;
1230
1320
 
1231
1321
    /* Construct the denominator.  */
1232
1322
    densize = 0;