build/stlport/src/num_get_float.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build/stlport/src/num_get_float.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,884 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999
     1.6 + * Silicon Graphics Computer Systems, Inc.
     1.7 + *
     1.8 + * Copyright (c) 1999
     1.9 + * Boris Fomitchev
    1.10 + *
    1.11 + * This material is provided "as is", with absolutely no warranty expressed
    1.12 + * or implied. Any use is at your own risk.
    1.13 + *
    1.14 + * Permission to use or copy this software for any purpose is hereby granted
    1.15 + * without fee, provided the above notices are retained on all copies.
    1.16 + * Permission to modify the code and to distribute modified code is granted,
    1.17 + * provided the above notices are retained, and a notice that the code was
    1.18 + * modified is included with the above copyright notice.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include "stlport_prefix.h"
    1.23 +
    1.24 +#include <limits>
    1.25 +#include <locale>
    1.26 +#include <istream>
    1.27 +
    1.28 +#if (defined (__GNUC__) && !defined (__sun) && !defined (__hpux)) || \
    1.29 +    defined (__DMC__)
    1.30 +#  include <stdint.h>
    1.31 +#endif
    1.32 +
    1.33 +#if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
    1.34 +    defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
    1.35 +
    1.36 +#  if defined (__BORLANDC__)
    1.37 +typedef unsigned int uint32_t;
    1.38 +typedef unsigned __int64 uint64_t;
    1.39 +#  endif
    1.40 +
    1.41 +union _ll {
    1.42 +  uint64_t i64;
    1.43 +  struct {
    1.44 +#  if defined (_STLP_BIG_ENDIAN)
    1.45 +    uint32_t hi;
    1.46 +    uint32_t lo;
    1.47 +#  elif defined (_STLP_LITTLE_ENDIAN)
    1.48 +    uint32_t lo;
    1.49 +    uint32_t hi;
    1.50 +#  else
    1.51 +#    error Unknown endianess
    1.52 +#  endif
    1.53 +  } i32;
    1.54 +};
    1.55 +
    1.56 +#  if defined (__linux__) && !defined (__ANDROID__)
    1.57 +#    include <ieee754.h>
    1.58 +#  else
    1.59 +union ieee854_long_double {
    1.60 +  long double d;
    1.61 +
    1.62 +  /* This is the IEEE 854 double-extended-precision format.  */
    1.63 +  struct {
    1.64 +    unsigned int mantissa1:32;
    1.65 +    unsigned int mantissa0:32;
    1.66 +    unsigned int exponent:15;
    1.67 +    unsigned int negative:1;
    1.68 +    unsigned int empty:16;
    1.69 +  } ieee;
    1.70 +};
    1.71 +
    1.72 +#    define IEEE854_LONG_DOUBLE_BIAS 0x3fff
    1.73 +#  endif
    1.74 +#endif
    1.75 +
    1.76 +_STLP_BEGIN_NAMESPACE
    1.77 +_STLP_MOVE_TO_PRIV_NAMESPACE
    1.78 +
    1.79 +//----------------------------------------------------------------------
    1.80 +// num_get
    1.81 +
    1.82 +// Helper functions for _M_do_get_float.
    1.83 +
    1.84 +#if !defined (_STLP_NO_WCHAR_T)
    1.85 +void  _STLP_CALL
    1.86 +_Initialize_get_float( const ctype<wchar_t>& ct,
    1.87 +                       wchar_t& Plus, wchar_t& Minus,
    1.88 +                       wchar_t& pow_e, wchar_t& pow_E,
    1.89 +                       wchar_t* digits) {
    1.90 +  char ndigits[11] = "0123456789";
    1.91 +  Plus  = ct.widen('+');
    1.92 +  Minus = ct.widen('-');
    1.93 +  pow_e = ct.widen('e');
    1.94 +  pow_E = ct.widen('E');
    1.95 +  ct.widen(ndigits + 0, ndigits + 10, digits);
    1.96 +}
    1.97 +#endif /* WCHAR_T */
    1.98 +
    1.99 +/*
   1.100 + * __string_to_double is just lifted from atof, the difference being
   1.101 + * that we just use '.' for the decimal point, rather than let it
   1.102 + * be taken from the current C locale, which of course is not accessible
   1.103 + * to us.
   1.104 + */
   1.105 +#if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
   1.106 +typedef unsigned long uint32;
   1.107 +typedef unsigned __int64 uint64;
   1.108 +#  define ULL(x) x##Ui64
   1.109 +#elif defined (__unix) || defined (__MINGW32__) || \
   1.110 +      (defined (__DMC__) && (__LONGLONG)) || defined (__WATCOMC__) || \
   1.111 +      defined (__ANDROID__)
   1.112 +typedef uint32_t uint32;
   1.113 +typedef uint64_t uint64;
   1.114 +#  define ULL(x) x##ULL
   1.115 +#else
   1.116 +#  error There should be some unsigned 64-bit integer on the system!
   1.117 +#endif
   1.118 +
   1.119 +// Multiplication of two 64-bit integers, giving a 128-bit result.
   1.120 +// Taken from Algorithm M in Knuth section 4.3.1, with the loop
   1.121 +// hand-unrolled.
   1.122 +static void _Stl_mult64(const uint64 u, const uint64 v,
   1.123 +                        uint64& high, uint64& low) {
   1.124 +  const uint64 low_mask = ULL(0xffffffff);
   1.125 +  const uint64 u0 = u & low_mask;
   1.126 +  const uint64 u1 = u >> 32;
   1.127 +  const uint64 v0 = v & low_mask;
   1.128 +  const uint64 v1 = v >> 32;
   1.129 +
   1.130 +  uint64 t = u0 * v0;
   1.131 +  low = t & low_mask;
   1.132 +
   1.133 +  t = u1 * v0 + (t >> 32);
   1.134 +  uint64 w1 = t & low_mask;
   1.135 +  uint64 w2 = t >> 32;
   1.136 +
   1.137 +  uint64 x = u0 * v1 + w1;
   1.138 +  low += (x & low_mask) << 32;
   1.139 +  high = u1 * v1 + w2 + (x >> 32);
   1.140 +}
   1.141 +
   1.142 +#if !defined (__linux__) || defined (__ANDROID__)
   1.143 +
   1.144 +#  define bit11 ULL(0x7ff)
   1.145 +#  define exponent_mask (bit11 << 52)
   1.146 +
   1.147 +#  if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
   1.148 +      (!defined (__CYGWIN__) && !defined (__MINGW32__))
   1.149 +//Generate bad code when compiled with -O2 option.
   1.150 +inline
   1.151 +#  endif
   1.152 +void _Stl_set_exponent(uint64 &val, uint64 exp)
   1.153 +{ val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
   1.154 +
   1.155 +#endif // __linux__
   1.156 +
   1.157 +/* Power of ten fractions for tenscale*/
   1.158 +/* The constants are factored so that at most two constants
   1.159 + * and two multiplies are needed. Furthermore, one of the constants
   1.160 + * is represented exactly - 10**n where 1<= n <= 27.
   1.161 + */
   1.162 +
   1.163 +static const uint64 _Stl_tenpow[80] = {
   1.164 +ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
   1.165 +ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
   1.166 +ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
   1.167 +ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
   1.168 +ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
   1.169 +ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
   1.170 +ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
   1.171 +ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
   1.172 +ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
   1.173 +ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
   1.174 +ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
   1.175 +ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
   1.176 +ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
   1.177 +ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
   1.178 +ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
   1.179 +ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
   1.180 +ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
   1.181 +ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
   1.182 +ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
   1.183 +ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
   1.184 +ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
   1.185 +ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
   1.186 +ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
   1.187 +ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
   1.188 +ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
   1.189 +ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
   1.190 +ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
   1.191 +
   1.192 +ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
   1.193 +ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
   1.194 +ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
   1.195 +ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
   1.196 +ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
   1.197 +ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
   1.198 +ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
   1.199 +ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
   1.200 +ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
   1.201 +ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
   1.202 +
   1.203 +// /* _Stl_tenpow[36]=(10**335)/(2**) */
   1.204 +// /* _Stl_tenpow[36]=(10**335)/(2**) */
   1.205 +
   1.206 +ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
   1.207 +ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
   1.208 +ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
   1.209 +ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
   1.210 +ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
   1.211 +ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
   1.212 +ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
   1.213 +ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
   1.214 +ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837)     */
   1.215 +ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
   1.216 +ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023)    */
   1.217 +ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
   1.218 +ULL(0xe1afa13afbd14d6e)  /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
   1.219 +};
   1.220 +
   1.221 +static const short _Stl_twoexp[80] = {
   1.222 +4,7,10,14,17,20,24,27,30,34,37,40,44,47,50,54,57,60,64,67,70,74,77,80,84,87,90,
   1.223 +183,276,369,462,555,648,741,834,927,1020,
   1.224 +-93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
   1.225 +};
   1.226 +
   1.227 +#define  TEN_1  0           /* offset to 10 **   1 */
   1.228 +#define  TEN_27   26        /* offset to 10 **  27 */
   1.229 +#define  TEN_M28  37        /* offset to 10 ** -28 */
   1.230 +#define  NUM_HI_P 11
   1.231 +#define  NUM_HI_N 13
   1.232 +
   1.233 +#define _Stl_HIBITULL (ULL(1) << 63)
   1.234 +
   1.235 +static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
   1.236 +  norm = 0;
   1.237 +  if ((prodhi & _Stl_HIBITULL) == 0) {
   1.238 +                                /* leading bit is a zero
   1.239 +                                 * may have to normalize
   1.240 +                                 */
   1.241 +    if ((prodhi == ~_Stl_HIBITULL) &&
   1.242 +        ((prodlo >> 62) == 0x3)) {  /* normalization followed by round
   1.243 +                                     * would cause carry to create
   1.244 +                                     * extra bit, so don't normalize
   1.245 +                                     */
   1.246 +      p = _Stl_HIBITULL;
   1.247 +      return;
   1.248 +    }
   1.249 +    p = (prodhi << 1) | (prodlo >> 63); /* normalize */
   1.250 +    norm = 1;
   1.251 +    prodlo <<= 1;
   1.252 +  }
   1.253 +  else {
   1.254 +    p = prodhi;
   1.255 +  }
   1.256 +
   1.257 +  if ((prodlo & _Stl_HIBITULL) != 0) {     /* first guard bit a one */
   1.258 +    if (((p & 0x1) != 0) ||
   1.259 +        prodlo != _Stl_HIBITULL ) {    /* not borderline for round to even */
   1.260 +      /* round */
   1.261 +      ++p;
   1.262 +      if (p == 0)
   1.263 +        ++p;
   1.264 +    }
   1.265 +  }
   1.266 +}
   1.267 +
   1.268 +// Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
   1.269 +// p:    64-bit fraction
   1.270 +// exp:  base-10 exponent
   1.271 +// bexp: base-2 exponent (output parameter)
   1.272 +static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
   1.273 +  bexp = 0;
   1.274 +
   1.275 +  if ( exp == 0 ) {              /* no scaling needed */
   1.276 +    return;
   1.277 +  }
   1.278 +
   1.279 +  int exp_hi = 0, exp_lo = exp; /* exp = exp_hi*32 + exp_lo */
   1.280 +  int tlo = TEN_1, thi;         /* offsets in power of ten table */
   1.281 +  int num_hi;                   /* number of high exponent powers */
   1.282 +
   1.283 +  if (exp > 0) {                /* split exponent */
   1.284 +    if (exp_lo > 27) {
   1.285 +      exp_lo++;
   1.286 +      while (exp_lo > 27) {
   1.287 +        exp_hi++;
   1.288 +        exp_lo -= 28;
   1.289 +      }
   1.290 +    }
   1.291 +    thi = TEN_27;
   1.292 +    num_hi = NUM_HI_P;
   1.293 +  } else { // exp < 0
   1.294 +    while (exp_lo < 0) {
   1.295 +      exp_hi++;
   1.296 +      exp_lo += 28;
   1.297 +    }
   1.298 +    thi = TEN_M28;
   1.299 +    num_hi = NUM_HI_N;
   1.300 +  }
   1.301 +
   1.302 +  uint64 prodhi, prodlo;        /* 128b product */
   1.303 +  int norm;                     /* number of bits of normalization */
   1.304 +
   1.305 +  int hi, lo;                   /* offsets in power of ten table */
   1.306 +  while (exp_hi) {              /* scale */
   1.307 +    hi = (min) (exp_hi, num_hi);    /* only a few large powers of 10 */
   1.308 +    exp_hi -= hi;               /* could iterate in extreme case */
   1.309 +    hi += thi-1;
   1.310 +    _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
   1.311 +    _Stl_norm_and_round(p, norm, prodhi, prodlo);
   1.312 +    bexp += _Stl_twoexp[hi] - norm;
   1.313 +  }
   1.314 +
   1.315 +  if (exp_lo) {
   1.316 +    lo = tlo + exp_lo -1;
   1.317 +    _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
   1.318 +    _Stl_norm_and_round(p, norm, prodhi, prodlo);
   1.319 +    bexp += _Stl_twoexp[lo] - norm;
   1.320 +  }
   1.321 +
   1.322 +  return;
   1.323 +}
   1.324 +
   1.325 +// First argument is a buffer of values from 0 to 9, NOT ascii.
   1.326 +// Second argument is number of digits in buffer, 1 <= digits <= 17.
   1.327 +// Third argument is base-10 exponent.
   1.328 +
   1.329 +/* IEEE representation */
   1.330 +#if !defined (__linux__) || defined (__ANDROID__)
   1.331 +
   1.332 +union _Double_rep {
   1.333 +  uint64 ival;
   1.334 +  double val;
   1.335 +};
   1.336 +
   1.337 +static double _Stl_atod(char *buffer, ptrdiff_t ndigit, int dexp) {
   1.338 +  typedef numeric_limits<double> limits;
   1.339 +  _Double_rep drep;
   1.340 +  uint64 &value = drep.ival;  /* Value develops as follows:
   1.341 +                                 * 1) decimal digits as an integer
   1.342 +                                 * 2) left adjusted fraction
   1.343 +                                 * 3) right adjusted fraction
   1.344 +                                 * 4) exponent and fraction
   1.345 +                                 */
   1.346 +
   1.347 +  uint32 guard;         /* First guard bit */
   1.348 +  uint64 rest;          /* Remaining guard bits */
   1.349 +
   1.350 +  int bexp;             /* binary exponent */
   1.351 +  int nzero;            /* number of non-zero bits */
   1.352 +  int sexp;             /* scaling exponent */
   1.353 +
   1.354 +  char *bufferend;              /* pointer to char after last digit */
   1.355 +
   1.356 +  /* Convert the decimal digits to a binary integer. */
   1.357 +  bufferend = buffer + ndigit;
   1.358 +  value = 0;
   1.359 +
   1.360 +  while (buffer < bufferend) {
   1.361 +    value *= 10;
   1.362 +    value += *buffer++;
   1.363 +  }
   1.364 +
   1.365 +  /* Check for zero and treat it as a special case */
   1.366 +  if (value == 0) {
   1.367 +    return 0.0;
   1.368 +  }
   1.369 +
   1.370 +  /* Normalize value */
   1.371 +  bexp = 64;                    /* convert from 64b int to fraction */
   1.372 +
   1.373 +  /* Count number of non-zeroes in value */
   1.374 +  nzero = 0;
   1.375 +  if ((value >> 32) != 0) { nzero  = 32; }    //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
   1.376 +  if ((value >> (16 + nzero)) != 0) { nzero += 16; }
   1.377 +  if ((value >> ( 8 + nzero)) != 0) { nzero +=  8; }
   1.378 +  if ((value >> ( 4 + nzero)) != 0) { nzero +=  4; }
   1.379 +  if ((value >> ( 2 + nzero)) != 0) { nzero +=  2; }
   1.380 +  if ((value >> ( 1 + nzero)) != 0) { nzero +=  1; }
   1.381 +  if ((value >> (     nzero)) != 0) { nzero +=  1; }
   1.382 +
   1.383 +  /* Normalize */
   1.384 +  value <<= /*(uint64)*/ (64 - nzero);    //*TY 03/25/2000 - removed extraneous cast to uint64
   1.385 +  bexp -= 64 - nzero;
   1.386 +
   1.387 +  /* At this point we have a 64b fraction and a binary exponent
   1.388 +   * but have yet to incorporate the decimal exponent.
   1.389 +   */
   1.390 +
   1.391 +  /* multiply by 10^dexp */
   1.392 +  _Stl_tenscale(value, dexp, sexp);
   1.393 +  bexp += sexp;
   1.394 +
   1.395 +  if (bexp <= -1022) {          /* HI denorm or underflow */
   1.396 +    bexp += 1022;
   1.397 +    if (bexp < -53) {          /* guaranteed underflow */
   1.398 +      value = 0;
   1.399 +    }
   1.400 +    else {                      /* denorm or possible underflow */
   1.401 +      int lead0 = 12 - bexp;          /* 12 sign and exponent bits */
   1.402 +
   1.403 +      /* we must special case right shifts of more than 63 */
   1.404 +      if (lead0 > 64) {
   1.405 +        rest = value;
   1.406 +        guard = 0;
   1.407 +        value = 0;
   1.408 +      }
   1.409 +      else if (lead0 == 64) {
   1.410 +        rest = value & ((ULL(1)<< 63)-1);
   1.411 +        guard = (uint32) ((value>> 63) & 1 );
   1.412 +        value = 0;
   1.413 +      }
   1.414 +      else {
   1.415 +        rest = value & (((ULL(1) << lead0)-1)-1);
   1.416 +        guard = (uint32) (((value>> lead0)-1) & 1);
   1.417 +        value >>= /*(uint64)*/ lead0; /* exponent is zero */
   1.418 +      }
   1.419 +
   1.420 +      /* Round */
   1.421 +      if (guard && ((value & 1) || rest) ) {
   1.422 +        ++value;
   1.423 +        if (value == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
   1.424 +          value = 0;
   1.425 +          _Stl_set_exponent(value, 1);
   1.426 +        }
   1.427 +      }
   1.428 +    }
   1.429 +  }
   1.430 +  else {                        /* not zero or denorm */
   1.431 +    /* Round to 53 bits */
   1.432 +    rest = value & ((1 << 10) - 1);
   1.433 +    value >>= 10;
   1.434 +    guard = (uint32) value & 1;
   1.435 +    value >>= 1;
   1.436 +
   1.437 +    /*  value&1 guard   rest    Action
   1.438 +     *
   1.439 +     *  dc      0       dc      none
   1.440 +     *  1       1       dc      round
   1.441 +     *  0       1       0       none
   1.442 +     *  0       1       !=0     round
   1.443 +     */
   1.444 +    if (guard) {
   1.445 +      if (((value&1)!=0) || (rest!=0)) {
   1.446 +        ++value;                        /* round */
   1.447 +        if ((value >> 53) != 0) {       /* carry all the way across */
   1.448 +          value >>= 1;          /* renormalize */
   1.449 +          ++bexp;
   1.450 +        }
   1.451 +      }
   1.452 +    }
   1.453 +    /*
   1.454 +     * Check for overflow
   1.455 +     * IEEE Double Precision Format
   1.456 +     * (From Table 7-8 of Kane and Heinrich)
   1.457 +     *
   1.458 +     * Fraction bits               52
   1.459 +     * Emax                     +1023
   1.460 +     * Emin                     -1022
   1.461 +     * Exponent bias            +1023
   1.462 +     * Exponent bits               11
   1.463 +     * Integer bit             hidden
   1.464 +     * Total width in bits         64
   1.465 +     */
   1.466 +
   1.467 +    if (bexp > limits::max_exponent) {          /* overflow */
   1.468 +      return limits::infinity();
   1.469 +    }
   1.470 +    else {                      /* value is normal */
   1.471 +      value &= ~(ULL(1) << (limits::digits - 1));   /* hide hidden bit */
   1.472 +      _Stl_set_exponent(value, bexp + 1022); /* add bias */
   1.473 +    }
   1.474 +  }
   1.475 +
   1.476 +  _STLP_STATIC_ASSERT(sizeof(uint64) >= sizeof(double))
   1.477 +  return drep.val;
   1.478 +}
   1.479 +
   1.480 +#endif
   1.481 +
   1.482 +#if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
   1.483 +    defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
   1.484 +
   1.485 +template <class D, class IEEE, int M, int BIAS>
   1.486 +D _Stl_atodT(char *buffer, ptrdiff_t ndigit, int dexp)
   1.487 +{
   1.488 +  typedef numeric_limits<D> limits;
   1.489 +
   1.490 +  /* Convert the decimal digits to a binary integer. */
   1.491 +  char *bufferend = buffer + ndigit; /* pointer to char after last digit */
   1.492 +  _ll vv;
   1.493 +  vv.i64 = 0L;
   1.494 +
   1.495 +  while ( buffer < bufferend ) {
   1.496 +    vv.i64 *= 10;
   1.497 +    vv.i64 += *buffer++;
   1.498 +  }
   1.499 +
   1.500 +  if ( vv.i64 == ULL(0) ) { /* Check for zero and treat it as a special case */
   1.501 +    return D(0.0);
   1.502 +  }
   1.503 +
   1.504 +  /* Normalize value */
   1.505 +
   1.506 +  int bexp = 64; /* convert from 64b int to fraction */
   1.507 +
   1.508 +  /* Count number of non-zeroes in value */
   1.509 +  int nzero = 0;
   1.510 +  if ((vv.i64 >> 32) != 0) { nzero = 32; }
   1.511 +  if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; }
   1.512 +  if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero +=  8; }
   1.513 +  if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero +=  4; }
   1.514 +  if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero +=  2; }
   1.515 +  if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero +=  1; }
   1.516 +  if ((vv.i64 >> (     nzero)) != 0) { nzero +=  1; }
   1.517 +
   1.518 +  /* Normalize */
   1.519 +  nzero = 64 - nzero;
   1.520 +  vv.i64 <<= nzero;    // * TY 03/25/2000 - removed extraneous cast to uint64
   1.521 +  bexp -= nzero;
   1.522 +
   1.523 +  /* At this point we have a 64b fraction and a binary exponent
   1.524 +   * but have yet to incorporate the decimal exponent.
   1.525 +   */
   1.526 +
   1.527 +  /* multiply by 10^dexp */
   1.528 +  int sexp;
   1.529 +  _Stl_tenscale(vv.i64, dexp, sexp);
   1.530 +  bexp += sexp;
   1.531 +
   1.532 +  if ( bexp >= limits::min_exponent ) { /* not zero or denorm */
   1.533 +    if ( limits::digits < 64 ) {
   1.534 +      /* Round to (64 - M + 1) bits */
   1.535 +      uint64_t rest = vv.i64 & ((~ULL(0) / ULL(2)) >> (limits::digits - 1));
   1.536 +      vv.i64 >>= M - 2;
   1.537 +      uint32_t guard = (uint32) vv.i64 & 1;
   1.538 +      vv.i64 >>= 1;
   1.539 +
   1.540 +      /*  value&1 guard   rest    Action
   1.541 +       *
   1.542 +       *  dc      0       dc      none
   1.543 +       *  1       1       dc      round
   1.544 +       *  0       1       0       none
   1.545 +       *  0       1       !=0     round
   1.546 +       */
   1.547 +
   1.548 +      if (guard) {
   1.549 +        if ( ((vv.i64 & 1) != 0) || (rest != 0) ) {
   1.550 +          vv.i64++;       /* round */
   1.551 +          if ( (vv.i64 >> (limits::digits < 64 ? limits::digits : 0)) != 0 ) { /* carry all the way across */
   1.552 +            vv.i64 >>= 1; /* renormalize */
   1.553 +            ++bexp;
   1.554 +          }
   1.555 +        }
   1.556 +      }
   1.557 +
   1.558 +      vv.i64 &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */
   1.559 +    }
   1.560 +    /*
   1.561 +     * Check for overflow
   1.562 +     * IEEE Double Precision Format
   1.563 +     * (From Table 7-8 of Kane and Heinrich)
   1.564 +     *
   1.565 +     * Fraction bits               52
   1.566 +     * Emax                     +1023
   1.567 +     * Emin                     -1022
   1.568 +     * Exponent bias            +1023
   1.569 +     * Exponent bits               11
   1.570 +     * Integer bit             hidden
   1.571 +     * Total width in bits         64
   1.572 +     */
   1.573 +
   1.574 +    if (bexp > limits::max_exponent) { /* overflow */
   1.575 +      return limits::infinity();
   1.576 +    }
   1.577 +
   1.578 +    /* value is normal */
   1.579 +
   1.580 +    IEEE v;
   1.581 +
   1.582 +    v.ieee.mantissa0 = vv.i32.hi;
   1.583 +    v.ieee.mantissa1 = vv.i32.lo;
   1.584 +    v.ieee.negative = 0;
   1.585 +    v.ieee.exponent = bexp + BIAS - 1;
   1.586 +
   1.587 +    return v.d;
   1.588 +  }
   1.589 +
   1.590 +  /* HI denorm or underflow */
   1.591 +  bexp += BIAS - 1;
   1.592 +  if (bexp < -limits::digits) { /* guaranteed underflow */
   1.593 +    vv.i64 = 0;
   1.594 +  } else {  /* denorm or possible underflow */
   1.595 +
   1.596 +    /*
   1.597 +     * Problem point for long double: looks like this code reflect shareing of mantissa
   1.598 +     * and exponent in 64b int; not so for long double
   1.599 +     */
   1.600 +
   1.601 +    int lead0 = M - bexp; /* M = 12 sign and exponent bits */
   1.602 +    uint64_t rest;
   1.603 +    uint32_t guard;
   1.604 +
   1.605 +    /* we must special case right shifts of more than 63 */
   1.606 +
   1.607 +    if (lead0 > 64) {
   1.608 +      rest = vv.i64;
   1.609 +      guard = 0;
   1.610 +      vv.i64 = 0;
   1.611 +    } else if (lead0 == 64) {
   1.612 +      rest = vv.i64 & ((ULL(1) << 63)-1);
   1.613 +      guard = (uint32) ((vv.i64 >> 63) & 1 );
   1.614 +      vv.i64 = 0;
   1.615 +    } else {
   1.616 +      rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
   1.617 +      guard = (uint32) (((vv.i64 >> lead0)-1) & 1);
   1.618 +      vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */
   1.619 +    }
   1.620 +
   1.621 +    /* Round */
   1.622 +    if (guard && ( (vv.i64 & 1) || rest)) {
   1.623 +      vv.i64++;
   1.624 +      if (vv.i64 == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
   1.625 +        IEEE v;
   1.626 +
   1.627 +        v.ieee.mantissa0 = 0;
   1.628 +        v.ieee.mantissa1 = 0;
   1.629 +        v.ieee.negative = 0;
   1.630 +        v.ieee.exponent = 1;
   1.631 +        return v.d;
   1.632 +      }
   1.633 +    }
   1.634 +  }
   1.635 +
   1.636 +  IEEE v;
   1.637 +
   1.638 +  v.ieee.mantissa0 = vv.i32.hi;
   1.639 +  v.ieee.mantissa1 = vv.i32.lo;
   1.640 +  v.ieee.negative = 0;
   1.641 +  v.ieee.exponent = 0;
   1.642 +
   1.643 +  return v.d;
   1.644 +}
   1.645 +#endif // __linux__
   1.646 +
   1.647 +#if !defined (__linux__) || defined (__ANDROID__)
   1.648 +static double _Stl_string_to_double(const char *s) {
   1.649 +  typedef numeric_limits<double> limits;
   1.650 +  const int max_digits = limits::digits10 + 2;
   1.651 +  unsigned c;
   1.652 +  unsigned Negate, decimal_point;
   1.653 +  char *d;
   1.654 +  int exp;
   1.655 +  int dpchar;
   1.656 +  char digits[max_digits];
   1.657 +
   1.658 +  c = *s++;
   1.659 +
   1.660 +  /* process sign */
   1.661 +  Negate = 0;
   1.662 +  if (c == '+') {
   1.663 +    c = *s++;
   1.664 +  } else if (c == '-') {
   1.665 +    Negate = 1;
   1.666 +    c = *s++;
   1.667 +  }
   1.668 +
   1.669 +  d = digits;
   1.670 +  dpchar = '.' - '0';
   1.671 +  decimal_point = 0;
   1.672 +  exp = 0;
   1.673 +
   1.674 +  for (;;) {
   1.675 +    c -= '0';
   1.676 +    if (c < 10) {
   1.677 +      if (d == digits + max_digits) {
   1.678 +        /* ignore more than max_digits digits, but adjust exponent */
   1.679 +        exp += (decimal_point ^ 1);
   1.680 +      } else {
   1.681 +        if (c == 0 && d == digits) {
   1.682 +          /* ignore leading zeros */
   1.683 +        } else {
   1.684 +          *d++ = (char) c;
   1.685 +        }
   1.686 +        exp -= decimal_point;
   1.687 +      }
   1.688 +    } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
   1.689 +      decimal_point = 1;
   1.690 +    } else {
   1.691 +      break;
   1.692 +    }
   1.693 +    c = *s++;
   1.694 +  }
   1.695 +
   1.696 +  /* strtod cant return until it finds the end of the exponent */
   1.697 +  if (d == digits) {
   1.698 +    return 0.0;
   1.699 +  }
   1.700 +
   1.701 +  if (c == 'e' - '0' || c == 'E' - '0') {
   1.702 +    register unsigned negate_exp = 0;
   1.703 +    register int e = 0;
   1.704 +    c = *s++;
   1.705 +    if (c == '+' || c == ' ') {
   1.706 +      c = *s++;
   1.707 +    } else if (c == '-') {
   1.708 +      negate_exp = 1;
   1.709 +      c = *s++;
   1.710 +    }
   1.711 +    if (c -= '0', c < 10) {
   1.712 +      do {
   1.713 +        e = e * 10 + (int)c;
   1.714 +        c = *s++;
   1.715 +      } while (c -= '0', c < 10);
   1.716 +
   1.717 +      if (negate_exp) {
   1.718 +        e = -e;
   1.719 +      }
   1.720 +      exp += e;
   1.721 +    }
   1.722 +  }
   1.723 +
   1.724 +  double x;
   1.725 +  ptrdiff_t n = d - digits;
   1.726 +  if ((exp + n - 1) < limits::min_exponent10) {
   1.727 +    x = 0;
   1.728 +  }
   1.729 +  else if ((exp + n - 1) > limits::max_exponent10) {
   1.730 +    x = limits::infinity();
   1.731 +  }
   1.732 +  else {
   1.733 +    /* Let _Stl_atod diagnose under- and over-flows.
   1.734 +     * If the input was == 0.0, we have already returned,
   1.735 +     * so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW */
   1.736 +    x = _Stl_atod(digits, n, exp);
   1.737 +  }
   1.738 +
   1.739 +  if (Negate) {
   1.740 +    x = -x;
   1.741 +  }
   1.742 +
   1.743 +  return x;
   1.744 +}
   1.745 +
   1.746 +#endif
   1.747 +
   1.748 +#if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
   1.749 +    defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
   1.750 +
   1.751 +template <class D, class IEEE, int M, int BIAS>
   1.752 +D _Stl_string_to_doubleT(const char *s)
   1.753 +{
   1.754 +  typedef numeric_limits<D> limits;
   1.755 +  const int max_digits = limits::digits10; /* + 2 17 */;
   1.756 +  unsigned c;
   1.757 +  unsigned decimal_point;
   1.758 +  char *d;
   1.759 +  int exp;
   1.760 +  D x;
   1.761 +  int dpchar;
   1.762 +  char digits[max_digits];
   1.763 +
   1.764 +  c = *s++;
   1.765 +
   1.766 +  /* process sign */
   1.767 +  bool Negate = false;
   1.768 +  if (c == '+') {
   1.769 +    c = *s++;
   1.770 +  } else if (c == '-') {
   1.771 +    Negate = true;
   1.772 +    c = *s++;
   1.773 +  }
   1.774 +
   1.775 +  d = digits;
   1.776 +  dpchar = '.' - '0';
   1.777 +  decimal_point = 0;
   1.778 +  exp = 0;
   1.779 +
   1.780 +  for (;;) {
   1.781 +    c -= '0';
   1.782 +    if (c < 10) {
   1.783 +      if (d == digits + max_digits) {
   1.784 +        /* ignore more than max_digits digits, but adjust exponent */
   1.785 +        exp += (decimal_point ^ 1);
   1.786 +      } else {
   1.787 +        if (c == 0 && d == digits) {
   1.788 +          /* ignore leading zeros */
   1.789 +        } else {
   1.790 +          *d++ = (char) c;
   1.791 +        }
   1.792 +        exp -= decimal_point;
   1.793 +      }
   1.794 +    } else if (c == (unsigned int) dpchar && !decimal_point) {    /* INTERNATIONAL */
   1.795 +      decimal_point = 1;
   1.796 +    } else {
   1.797 +      break;
   1.798 +    }
   1.799 +    c = *s++;
   1.800 +  }
   1.801 +  /* strtod cant return until it finds the end of the exponent */
   1.802 +  if (d == digits) {
   1.803 +    return D(0.0);
   1.804 +  }
   1.805 +
   1.806 +  if (c == 'e'-'0' || c == 'E'-'0') {
   1.807 +    bool negate_exp = false;
   1.808 +    register int e = 0;
   1.809 +    c = *s++;
   1.810 +    if (c == '+' || c == ' ') {
   1.811 +      c = *s++;
   1.812 +    } else if (c == '-') {
   1.813 +      negate_exp = true;
   1.814 +      c = *s++;
   1.815 +    }
   1.816 +    if (c -= '0', c < 10) {
   1.817 +      do {
   1.818 +        e = e * 10 + (int)c;
   1.819 +        c = *s++;
   1.820 +      } while (c -= '0', c < 10);
   1.821 +
   1.822 +      if (negate_exp) {
   1.823 +        e = -e;
   1.824 +      }
   1.825 +      exp += e;
   1.826 +    }
   1.827 +  }
   1.828 +
   1.829 +  ptrdiff_t n = d - digits;
   1.830 +  if ((exp + n - 1) < limits::min_exponent10) {
   1.831 +    return D(0.0); // +0.0 is the same as -0.0
   1.832 +  } else if ((exp + n - 1) > limits::max_exponent10 ) {
   1.833 +    // not good, because of x = -x below; this may lead to portability problems
   1.834 +    x = limits::infinity();
   1.835 +  } else {
   1.836 +    /* let _Stl_atod diagnose under- and over-flows */
   1.837 +    /* if the input was == 0.0, we have already returned,
   1.838 +       so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
   1.839 +    */
   1.840 +    x = _Stl_atodT<D,IEEE,M,BIAS>(digits, n, exp);
   1.841 +  }
   1.842 +
   1.843 +  return Negate ? -x : x;
   1.844 +}
   1.845 +
   1.846 +#endif // __linux__
   1.847 +
   1.848 +void _STLP_CALL
   1.849 +__string_to_float(const __iostring& v, float& val)
   1.850 +{
   1.851 +#if !defined (__linux__) || defined (__ANDROID__)
   1.852 +  val = (float)_Stl_string_to_double(v.c_str());
   1.853 +#else
   1.854 +  val = (float)_Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
   1.855 +#endif
   1.856 +}
   1.857 +
   1.858 +void _STLP_CALL
   1.859 +__string_to_float(const __iostring& v, double& val)
   1.860 +{
   1.861 +#if !defined (__linux__) || defined (__ANDROID__)
   1.862 +  val = _Stl_string_to_double(v.c_str());
   1.863 +#else
   1.864 +  val = _Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
   1.865 +#endif
   1.866 +}
   1.867 +
   1.868 +#if !defined (_STLP_NO_LONG_DOUBLE)
   1.869 +void _STLP_CALL
   1.870 +__string_to_float(const __iostring& v, long double& val) {
   1.871 +#if !defined (__linux__) && !defined (__MINGW32__) && !defined (__CYGWIN__) && \
   1.872 +    !defined (__BORLANDC__) && !defined (__DMC__) && !defined (__HP_aCC)
   1.873 +  //The following function is valid only if long double is an alias for double.
   1.874 +  _STLP_STATIC_ASSERT( sizeof(long double) <= sizeof(double) )
   1.875 +  val = _Stl_string_to_double(v.c_str());
   1.876 +#else
   1.877 +  val = _Stl_string_to_doubleT<long double,ieee854_long_double,16,IEEE854_LONG_DOUBLE_BIAS>(v.c_str());
   1.878 +#endif
   1.879 +}
   1.880 +#endif
   1.881 +
   1.882 +_STLP_MOVE_TO_STD_NAMESPACE
   1.883 +_STLP_END_NAMESPACE
   1.884 +
   1.885 +// Local Variables:
   1.886 +// mode:C++
   1.887 +// End:

mercurial