michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: #include "stlport_prefix.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #if (defined (__GNUC__) && !defined (__sun) && !defined (__hpux)) || \ michael@0: defined (__DMC__) michael@0: # include michael@0: #endif michael@0: michael@0: #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \ michael@0: defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC) michael@0: michael@0: # if defined (__BORLANDC__) michael@0: typedef unsigned int uint32_t; michael@0: typedef unsigned __int64 uint64_t; michael@0: # endif michael@0: michael@0: union _ll { michael@0: uint64_t i64; michael@0: struct { michael@0: # if defined (_STLP_BIG_ENDIAN) michael@0: uint32_t hi; michael@0: uint32_t lo; michael@0: # elif defined (_STLP_LITTLE_ENDIAN) michael@0: uint32_t lo; michael@0: uint32_t hi; michael@0: # else michael@0: # error Unknown endianess michael@0: # endif michael@0: } i32; michael@0: }; michael@0: michael@0: # if defined (__linux__) && !defined (__ANDROID__) michael@0: # include michael@0: # else michael@0: union ieee854_long_double { michael@0: long double d; michael@0: michael@0: /* This is the IEEE 854 double-extended-precision format. */ michael@0: struct { michael@0: unsigned int mantissa1:32; michael@0: unsigned int mantissa0:32; michael@0: unsigned int exponent:15; michael@0: unsigned int negative:1; michael@0: unsigned int empty:16; michael@0: } ieee; michael@0: }; michael@0: michael@0: # define IEEE854_LONG_DOUBLE_BIAS 0x3fff michael@0: # endif michael@0: #endif michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: _STLP_MOVE_TO_PRIV_NAMESPACE michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // num_get michael@0: michael@0: // Helper functions for _M_do_get_float. michael@0: michael@0: #if !defined (_STLP_NO_WCHAR_T) michael@0: void _STLP_CALL michael@0: _Initialize_get_float( const ctype& ct, michael@0: wchar_t& Plus, wchar_t& Minus, michael@0: wchar_t& pow_e, wchar_t& pow_E, michael@0: wchar_t* digits) { michael@0: char ndigits[11] = "0123456789"; michael@0: Plus = ct.widen('+'); michael@0: Minus = ct.widen('-'); michael@0: pow_e = ct.widen('e'); michael@0: pow_E = ct.widen('E'); michael@0: ct.widen(ndigits + 0, ndigits + 10, digits); michael@0: } michael@0: #endif /* WCHAR_T */ michael@0: michael@0: /* michael@0: * __string_to_double is just lifted from atof, the difference being michael@0: * that we just use '.' for the decimal point, rather than let it michael@0: * be taken from the current C locale, which of course is not accessible michael@0: * to us. michael@0: */ michael@0: #if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL) michael@0: typedef unsigned long uint32; michael@0: typedef unsigned __int64 uint64; michael@0: # define ULL(x) x##Ui64 michael@0: #elif defined (__unix) || defined (__MINGW32__) || \ michael@0: (defined (__DMC__) && (__LONGLONG)) || defined (__WATCOMC__) || \ michael@0: defined (__ANDROID__) michael@0: typedef uint32_t uint32; michael@0: typedef uint64_t uint64; michael@0: # define ULL(x) x##ULL michael@0: #else michael@0: # error There should be some unsigned 64-bit integer on the system! michael@0: #endif michael@0: michael@0: // Multiplication of two 64-bit integers, giving a 128-bit result. michael@0: // Taken from Algorithm M in Knuth section 4.3.1, with the loop michael@0: // hand-unrolled. michael@0: static void _Stl_mult64(const uint64 u, const uint64 v, michael@0: uint64& high, uint64& low) { michael@0: const uint64 low_mask = ULL(0xffffffff); michael@0: const uint64 u0 = u & low_mask; michael@0: const uint64 u1 = u >> 32; michael@0: const uint64 v0 = v & low_mask; michael@0: const uint64 v1 = v >> 32; michael@0: michael@0: uint64 t = u0 * v0; michael@0: low = t & low_mask; michael@0: michael@0: t = u1 * v0 + (t >> 32); michael@0: uint64 w1 = t & low_mask; michael@0: uint64 w2 = t >> 32; michael@0: michael@0: uint64 x = u0 * v1 + w1; michael@0: low += (x & low_mask) << 32; michael@0: high = u1 * v1 + w2 + (x >> 32); michael@0: } michael@0: michael@0: #if !defined (__linux__) || defined (__ANDROID__) michael@0: michael@0: # define bit11 ULL(0x7ff) michael@0: # define exponent_mask (bit11 << 52) michael@0: michael@0: # if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \ michael@0: (!defined (__CYGWIN__) && !defined (__MINGW32__)) michael@0: //Generate bad code when compiled with -O2 option. michael@0: inline michael@0: # endif michael@0: void _Stl_set_exponent(uint64 &val, uint64 exp) michael@0: { val = (val & ~exponent_mask) | ((exp & bit11) << 52); } michael@0: michael@0: #endif // __linux__ michael@0: michael@0: /* Power of ten fractions for tenscale*/ michael@0: /* The constants are factored so that at most two constants michael@0: * and two multiplies are needed. Furthermore, one of the constants michael@0: * is represented exactly - 10**n where 1<= n <= 27. michael@0: */ michael@0: michael@0: static const uint64 _Stl_tenpow[80] = { michael@0: ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */ michael@0: ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */ michael@0: ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */ michael@0: ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */ michael@0: ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */ michael@0: ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */ michael@0: ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */ michael@0: ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */ michael@0: ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */ michael@0: ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */ michael@0: ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */ michael@0: ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */ michael@0: ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */ michael@0: ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */ michael@0: ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */ michael@0: ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */ michael@0: ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */ michael@0: ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */ michael@0: ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */ michael@0: ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */ michael@0: ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */ michael@0: ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */ michael@0: ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */ michael@0: ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */ michael@0: ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */ michael@0: ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */ michael@0: ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */ michael@0: michael@0: ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */ michael@0: ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */ michael@0: ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */ michael@0: ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */ michael@0: ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */ michael@0: ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */ michael@0: ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */ michael@0: ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */ michael@0: ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */ michael@0: ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */ michael@0: michael@0: // /* _Stl_tenpow[36]=(10**335)/(2**) */ michael@0: // /* _Stl_tenpow[36]=(10**335)/(2**) */ michael@0: michael@0: ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */ michael@0: ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */ michael@0: ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */ michael@0: ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */ michael@0: ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */ michael@0: ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */ michael@0: ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */ michael@0: ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */ michael@0: ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */ michael@0: ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */ michael@0: ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */ michael@0: ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */ michael@0: ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */ michael@0: }; michael@0: michael@0: static const short _Stl_twoexp[80] = { michael@0: 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, michael@0: 183,276,369,462,555,648,741,834,927,1020, michael@0: -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209 michael@0: }; michael@0: michael@0: #define TEN_1 0 /* offset to 10 ** 1 */ michael@0: #define TEN_27 26 /* offset to 10 ** 27 */ michael@0: #define TEN_M28 37 /* offset to 10 ** -28 */ michael@0: #define NUM_HI_P 11 michael@0: #define NUM_HI_N 13 michael@0: michael@0: #define _Stl_HIBITULL (ULL(1) << 63) michael@0: michael@0: static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) { michael@0: norm = 0; michael@0: if ((prodhi & _Stl_HIBITULL) == 0) { michael@0: /* leading bit is a zero michael@0: * may have to normalize michael@0: */ michael@0: if ((prodhi == ~_Stl_HIBITULL) && michael@0: ((prodlo >> 62) == 0x3)) { /* normalization followed by round michael@0: * would cause carry to create michael@0: * extra bit, so don't normalize michael@0: */ michael@0: p = _Stl_HIBITULL; michael@0: return; michael@0: } michael@0: p = (prodhi << 1) | (prodlo >> 63); /* normalize */ michael@0: norm = 1; michael@0: prodlo <<= 1; michael@0: } michael@0: else { michael@0: p = prodhi; michael@0: } michael@0: michael@0: if ((prodlo & _Stl_HIBITULL) != 0) { /* first guard bit a one */ michael@0: if (((p & 0x1) != 0) || michael@0: prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */ michael@0: /* round */ michael@0: ++p; michael@0: if (p == 0) michael@0: ++p; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp. michael@0: // p: 64-bit fraction michael@0: // exp: base-10 exponent michael@0: // bexp: base-2 exponent (output parameter) michael@0: static void _Stl_tenscale(uint64& p, int exp, int& bexp) { michael@0: bexp = 0; michael@0: michael@0: if ( exp == 0 ) { /* no scaling needed */ michael@0: return; michael@0: } michael@0: michael@0: int exp_hi = 0, exp_lo = exp; /* exp = exp_hi*32 + exp_lo */ michael@0: int tlo = TEN_1, thi; /* offsets in power of ten table */ michael@0: int num_hi; /* number of high exponent powers */ michael@0: michael@0: if (exp > 0) { /* split exponent */ michael@0: if (exp_lo > 27) { michael@0: exp_lo++; michael@0: while (exp_lo > 27) { michael@0: exp_hi++; michael@0: exp_lo -= 28; michael@0: } michael@0: } michael@0: thi = TEN_27; michael@0: num_hi = NUM_HI_P; michael@0: } else { // exp < 0 michael@0: while (exp_lo < 0) { michael@0: exp_hi++; michael@0: exp_lo += 28; michael@0: } michael@0: thi = TEN_M28; michael@0: num_hi = NUM_HI_N; michael@0: } michael@0: michael@0: uint64 prodhi, prodlo; /* 128b product */ michael@0: int norm; /* number of bits of normalization */ michael@0: michael@0: int hi, lo; /* offsets in power of ten table */ michael@0: while (exp_hi) { /* scale */ michael@0: hi = (min) (exp_hi, num_hi); /* only a few large powers of 10 */ michael@0: exp_hi -= hi; /* could iterate in extreme case */ michael@0: hi += thi-1; michael@0: _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo); michael@0: _Stl_norm_and_round(p, norm, prodhi, prodlo); michael@0: bexp += _Stl_twoexp[hi] - norm; michael@0: } michael@0: michael@0: if (exp_lo) { michael@0: lo = tlo + exp_lo -1; michael@0: _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo); michael@0: _Stl_norm_and_round(p, norm, prodhi, prodlo); michael@0: bexp += _Stl_twoexp[lo] - norm; michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: // First argument is a buffer of values from 0 to 9, NOT ascii. michael@0: // Second argument is number of digits in buffer, 1 <= digits <= 17. michael@0: // Third argument is base-10 exponent. michael@0: michael@0: /* IEEE representation */ michael@0: #if !defined (__linux__) || defined (__ANDROID__) michael@0: michael@0: union _Double_rep { michael@0: uint64 ival; michael@0: double val; michael@0: }; michael@0: michael@0: static double _Stl_atod(char *buffer, ptrdiff_t ndigit, int dexp) { michael@0: typedef numeric_limits limits; michael@0: _Double_rep drep; michael@0: uint64 &value = drep.ival; /* Value develops as follows: michael@0: * 1) decimal digits as an integer michael@0: * 2) left adjusted fraction michael@0: * 3) right adjusted fraction michael@0: * 4) exponent and fraction michael@0: */ michael@0: michael@0: uint32 guard; /* First guard bit */ michael@0: uint64 rest; /* Remaining guard bits */ michael@0: michael@0: int bexp; /* binary exponent */ michael@0: int nzero; /* number of non-zero bits */ michael@0: int sexp; /* scaling exponent */ michael@0: michael@0: char *bufferend; /* pointer to char after last digit */ michael@0: michael@0: /* Convert the decimal digits to a binary integer. */ michael@0: bufferend = buffer + ndigit; michael@0: value = 0; michael@0: michael@0: while (buffer < bufferend) { michael@0: value *= 10; michael@0: value += *buffer++; michael@0: } michael@0: michael@0: /* Check for zero and treat it as a special case */ michael@0: if (value == 0) { michael@0: return 0.0; michael@0: } michael@0: michael@0: /* Normalize value */ michael@0: bexp = 64; /* convert from 64b int to fraction */ michael@0: michael@0: /* Count number of non-zeroes in value */ michael@0: nzero = 0; michael@0: if ((value >> 32) != 0) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator michael@0: if ((value >> (16 + nzero)) != 0) { nzero += 16; } michael@0: if ((value >> ( 8 + nzero)) != 0) { nzero += 8; } michael@0: if ((value >> ( 4 + nzero)) != 0) { nzero += 4; } michael@0: if ((value >> ( 2 + nzero)) != 0) { nzero += 2; } michael@0: if ((value >> ( 1 + nzero)) != 0) { nzero += 1; } michael@0: if ((value >> ( nzero)) != 0) { nzero += 1; } michael@0: michael@0: /* Normalize */ michael@0: value <<= /*(uint64)*/ (64 - nzero); //*TY 03/25/2000 - removed extraneous cast to uint64 michael@0: bexp -= 64 - nzero; michael@0: michael@0: /* At this point we have a 64b fraction and a binary exponent michael@0: * but have yet to incorporate the decimal exponent. michael@0: */ michael@0: michael@0: /* multiply by 10^dexp */ michael@0: _Stl_tenscale(value, dexp, sexp); michael@0: bexp += sexp; michael@0: michael@0: if (bexp <= -1022) { /* HI denorm or underflow */ michael@0: bexp += 1022; michael@0: if (bexp < -53) { /* guaranteed underflow */ michael@0: value = 0; michael@0: } michael@0: else { /* denorm or possible underflow */ michael@0: int lead0 = 12 - bexp; /* 12 sign and exponent bits */ michael@0: michael@0: /* we must special case right shifts of more than 63 */ michael@0: if (lead0 > 64) { michael@0: rest = value; michael@0: guard = 0; michael@0: value = 0; michael@0: } michael@0: else if (lead0 == 64) { michael@0: rest = value & ((ULL(1)<< 63)-1); michael@0: guard = (uint32) ((value>> 63) & 1 ); michael@0: value = 0; michael@0: } michael@0: else { michael@0: rest = value & (((ULL(1) << lead0)-1)-1); michael@0: guard = (uint32) (((value>> lead0)-1) & 1); michael@0: value >>= /*(uint64)*/ lead0; /* exponent is zero */ michael@0: } michael@0: michael@0: /* Round */ michael@0: if (guard && ((value & 1) || rest) ) { michael@0: ++value; michael@0: if (value == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */ michael@0: value = 0; michael@0: _Stl_set_exponent(value, 1); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else { /* not zero or denorm */ michael@0: /* Round to 53 bits */ michael@0: rest = value & ((1 << 10) - 1); michael@0: value >>= 10; michael@0: guard = (uint32) value & 1; michael@0: value >>= 1; michael@0: michael@0: /* value&1 guard rest Action michael@0: * michael@0: * dc 0 dc none michael@0: * 1 1 dc round michael@0: * 0 1 0 none michael@0: * 0 1 !=0 round michael@0: */ michael@0: if (guard) { michael@0: if (((value&1)!=0) || (rest!=0)) { michael@0: ++value; /* round */ michael@0: if ((value >> 53) != 0) { /* carry all the way across */ michael@0: value >>= 1; /* renormalize */ michael@0: ++bexp; michael@0: } michael@0: } michael@0: } michael@0: /* michael@0: * Check for overflow michael@0: * IEEE Double Precision Format michael@0: * (From Table 7-8 of Kane and Heinrich) michael@0: * michael@0: * Fraction bits 52 michael@0: * Emax +1023 michael@0: * Emin -1022 michael@0: * Exponent bias +1023 michael@0: * Exponent bits 11 michael@0: * Integer bit hidden michael@0: * Total width in bits 64 michael@0: */ michael@0: michael@0: if (bexp > limits::max_exponent) { /* overflow */ michael@0: return limits::infinity(); michael@0: } michael@0: else { /* value is normal */ michael@0: value &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */ michael@0: _Stl_set_exponent(value, bexp + 1022); /* add bias */ michael@0: } michael@0: } michael@0: michael@0: _STLP_STATIC_ASSERT(sizeof(uint64) >= sizeof(double)) michael@0: return drep.val; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \ michael@0: defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC) michael@0: michael@0: template michael@0: D _Stl_atodT(char *buffer, ptrdiff_t ndigit, int dexp) michael@0: { michael@0: typedef numeric_limits limits; michael@0: michael@0: /* Convert the decimal digits to a binary integer. */ michael@0: char *bufferend = buffer + ndigit; /* pointer to char after last digit */ michael@0: _ll vv; michael@0: vv.i64 = 0L; michael@0: michael@0: while ( buffer < bufferend ) { michael@0: vv.i64 *= 10; michael@0: vv.i64 += *buffer++; michael@0: } michael@0: michael@0: if ( vv.i64 == ULL(0) ) { /* Check for zero and treat it as a special case */ michael@0: return D(0.0); michael@0: } michael@0: michael@0: /* Normalize value */ michael@0: michael@0: int bexp = 64; /* convert from 64b int to fraction */ michael@0: michael@0: /* Count number of non-zeroes in value */ michael@0: int nzero = 0; michael@0: if ((vv.i64 >> 32) != 0) { nzero = 32; } michael@0: if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; } michael@0: if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero += 8; } michael@0: if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero += 4; } michael@0: if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero += 2; } michael@0: if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero += 1; } michael@0: if ((vv.i64 >> ( nzero)) != 0) { nzero += 1; } michael@0: michael@0: /* Normalize */ michael@0: nzero = 64 - nzero; michael@0: vv.i64 <<= nzero; // * TY 03/25/2000 - removed extraneous cast to uint64 michael@0: bexp -= nzero; michael@0: michael@0: /* At this point we have a 64b fraction and a binary exponent michael@0: * but have yet to incorporate the decimal exponent. michael@0: */ michael@0: michael@0: /* multiply by 10^dexp */ michael@0: int sexp; michael@0: _Stl_tenscale(vv.i64, dexp, sexp); michael@0: bexp += sexp; michael@0: michael@0: if ( bexp >= limits::min_exponent ) { /* not zero or denorm */ michael@0: if ( limits::digits < 64 ) { michael@0: /* Round to (64 - M + 1) bits */ michael@0: uint64_t rest = vv.i64 & ((~ULL(0) / ULL(2)) >> (limits::digits - 1)); michael@0: vv.i64 >>= M - 2; michael@0: uint32_t guard = (uint32) vv.i64 & 1; michael@0: vv.i64 >>= 1; michael@0: michael@0: /* value&1 guard rest Action michael@0: * michael@0: * dc 0 dc none michael@0: * 1 1 dc round michael@0: * 0 1 0 none michael@0: * 0 1 !=0 round michael@0: */ michael@0: michael@0: if (guard) { michael@0: if ( ((vv.i64 & 1) != 0) || (rest != 0) ) { michael@0: vv.i64++; /* round */ michael@0: if ( (vv.i64 >> (limits::digits < 64 ? limits::digits : 0)) != 0 ) { /* carry all the way across */ michael@0: vv.i64 >>= 1; /* renormalize */ michael@0: ++bexp; michael@0: } michael@0: } michael@0: } michael@0: michael@0: vv.i64 &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */ michael@0: } michael@0: /* michael@0: * Check for overflow michael@0: * IEEE Double Precision Format michael@0: * (From Table 7-8 of Kane and Heinrich) michael@0: * michael@0: * Fraction bits 52 michael@0: * Emax +1023 michael@0: * Emin -1022 michael@0: * Exponent bias +1023 michael@0: * Exponent bits 11 michael@0: * Integer bit hidden michael@0: * Total width in bits 64 michael@0: */ michael@0: michael@0: if (bexp > limits::max_exponent) { /* overflow */ michael@0: return limits::infinity(); michael@0: } michael@0: michael@0: /* value is normal */ michael@0: michael@0: IEEE v; michael@0: michael@0: v.ieee.mantissa0 = vv.i32.hi; michael@0: v.ieee.mantissa1 = vv.i32.lo; michael@0: v.ieee.negative = 0; michael@0: v.ieee.exponent = bexp + BIAS - 1; michael@0: michael@0: return v.d; michael@0: } michael@0: michael@0: /* HI denorm or underflow */ michael@0: bexp += BIAS - 1; michael@0: if (bexp < -limits::digits) { /* guaranteed underflow */ michael@0: vv.i64 = 0; michael@0: } else { /* denorm or possible underflow */ michael@0: michael@0: /* michael@0: * Problem point for long double: looks like this code reflect shareing of mantissa michael@0: * and exponent in 64b int; not so for long double michael@0: */ michael@0: michael@0: int lead0 = M - bexp; /* M = 12 sign and exponent bits */ michael@0: uint64_t rest; michael@0: uint32_t guard; michael@0: michael@0: /* we must special case right shifts of more than 63 */ michael@0: michael@0: if (lead0 > 64) { michael@0: rest = vv.i64; michael@0: guard = 0; michael@0: vv.i64 = 0; michael@0: } else if (lead0 == 64) { michael@0: rest = vv.i64 & ((ULL(1) << 63)-1); michael@0: guard = (uint32) ((vv.i64 >> 63) & 1 ); michael@0: vv.i64 = 0; michael@0: } else { michael@0: rest = vv.i64 & (((ULL(1) << lead0)-1)-1); michael@0: guard = (uint32) (((vv.i64 >> lead0)-1) & 1); michael@0: vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */ michael@0: } michael@0: michael@0: /* Round */ michael@0: if (guard && ( (vv.i64 & 1) || rest)) { michael@0: vv.i64++; michael@0: if (vv.i64 == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */ michael@0: IEEE v; michael@0: michael@0: v.ieee.mantissa0 = 0; michael@0: v.ieee.mantissa1 = 0; michael@0: v.ieee.negative = 0; michael@0: v.ieee.exponent = 1; michael@0: return v.d; michael@0: } michael@0: } michael@0: } michael@0: michael@0: IEEE v; michael@0: michael@0: v.ieee.mantissa0 = vv.i32.hi; michael@0: v.ieee.mantissa1 = vv.i32.lo; michael@0: v.ieee.negative = 0; michael@0: v.ieee.exponent = 0; michael@0: michael@0: return v.d; michael@0: } michael@0: #endif // __linux__ michael@0: michael@0: #if !defined (__linux__) || defined (__ANDROID__) michael@0: static double _Stl_string_to_double(const char *s) { michael@0: typedef numeric_limits limits; michael@0: const int max_digits = limits::digits10 + 2; michael@0: unsigned c; michael@0: unsigned Negate, decimal_point; michael@0: char *d; michael@0: int exp; michael@0: int dpchar; michael@0: char digits[max_digits]; michael@0: michael@0: c = *s++; michael@0: michael@0: /* process sign */ michael@0: Negate = 0; michael@0: if (c == '+') { michael@0: c = *s++; michael@0: } else if (c == '-') { michael@0: Negate = 1; michael@0: c = *s++; michael@0: } michael@0: michael@0: d = digits; michael@0: dpchar = '.' - '0'; michael@0: decimal_point = 0; michael@0: exp = 0; michael@0: michael@0: for (;;) { michael@0: c -= '0'; michael@0: if (c < 10) { michael@0: if (d == digits + max_digits) { michael@0: /* ignore more than max_digits digits, but adjust exponent */ michael@0: exp += (decimal_point ^ 1); michael@0: } else { michael@0: if (c == 0 && d == digits) { michael@0: /* ignore leading zeros */ michael@0: } else { michael@0: *d++ = (char) c; michael@0: } michael@0: exp -= decimal_point; michael@0: } michael@0: } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */ michael@0: decimal_point = 1; michael@0: } else { michael@0: break; michael@0: } michael@0: c = *s++; michael@0: } michael@0: michael@0: /* strtod cant return until it finds the end of the exponent */ michael@0: if (d == digits) { michael@0: return 0.0; michael@0: } michael@0: michael@0: if (c == 'e' - '0' || c == 'E' - '0') { michael@0: register unsigned negate_exp = 0; michael@0: register int e = 0; michael@0: c = *s++; michael@0: if (c == '+' || c == ' ') { michael@0: c = *s++; michael@0: } else if (c == '-') { michael@0: negate_exp = 1; michael@0: c = *s++; michael@0: } michael@0: if (c -= '0', c < 10) { michael@0: do { michael@0: e = e * 10 + (int)c; michael@0: c = *s++; michael@0: } while (c -= '0', c < 10); michael@0: michael@0: if (negate_exp) { michael@0: e = -e; michael@0: } michael@0: exp += e; michael@0: } michael@0: } michael@0: michael@0: double x; michael@0: ptrdiff_t n = d - digits; michael@0: if ((exp + n - 1) < limits::min_exponent10) { michael@0: x = 0; michael@0: } michael@0: else if ((exp + n - 1) > limits::max_exponent10) { michael@0: x = limits::infinity(); michael@0: } michael@0: else { michael@0: /* Let _Stl_atod diagnose under- and over-flows. michael@0: * If the input was == 0.0, we have already returned, michael@0: * so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW */ michael@0: x = _Stl_atod(digits, n, exp); michael@0: } michael@0: michael@0: if (Negate) { michael@0: x = -x; michael@0: } michael@0: michael@0: return x; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \ michael@0: defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC) michael@0: michael@0: template michael@0: D _Stl_string_to_doubleT(const char *s) michael@0: { michael@0: typedef numeric_limits limits; michael@0: const int max_digits = limits::digits10; /* + 2 17 */; michael@0: unsigned c; michael@0: unsigned decimal_point; michael@0: char *d; michael@0: int exp; michael@0: D x; michael@0: int dpchar; michael@0: char digits[max_digits]; michael@0: michael@0: c = *s++; michael@0: michael@0: /* process sign */ michael@0: bool Negate = false; michael@0: if (c == '+') { michael@0: c = *s++; michael@0: } else if (c == '-') { michael@0: Negate = true; michael@0: c = *s++; michael@0: } michael@0: michael@0: d = digits; michael@0: dpchar = '.' - '0'; michael@0: decimal_point = 0; michael@0: exp = 0; michael@0: michael@0: for (;;) { michael@0: c -= '0'; michael@0: if (c < 10) { michael@0: if (d == digits + max_digits) { michael@0: /* ignore more than max_digits digits, but adjust exponent */ michael@0: exp += (decimal_point ^ 1); michael@0: } else { michael@0: if (c == 0 && d == digits) { michael@0: /* ignore leading zeros */ michael@0: } else { michael@0: *d++ = (char) c; michael@0: } michael@0: exp -= decimal_point; michael@0: } michael@0: } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */ michael@0: decimal_point = 1; michael@0: } else { michael@0: break; michael@0: } michael@0: c = *s++; michael@0: } michael@0: /* strtod cant return until it finds the end of the exponent */ michael@0: if (d == digits) { michael@0: return D(0.0); michael@0: } michael@0: michael@0: if (c == 'e'-'0' || c == 'E'-'0') { michael@0: bool negate_exp = false; michael@0: register int e = 0; michael@0: c = *s++; michael@0: if (c == '+' || c == ' ') { michael@0: c = *s++; michael@0: } else if (c == '-') { michael@0: negate_exp = true; michael@0: c = *s++; michael@0: } michael@0: if (c -= '0', c < 10) { michael@0: do { michael@0: e = e * 10 + (int)c; michael@0: c = *s++; michael@0: } while (c -= '0', c < 10); michael@0: michael@0: if (negate_exp) { michael@0: e = -e; michael@0: } michael@0: exp += e; michael@0: } michael@0: } michael@0: michael@0: ptrdiff_t n = d - digits; michael@0: if ((exp + n - 1) < limits::min_exponent10) { michael@0: return D(0.0); // +0.0 is the same as -0.0 michael@0: } else if ((exp + n - 1) > limits::max_exponent10 ) { michael@0: // not good, because of x = -x below; this may lead to portability problems michael@0: x = limits::infinity(); michael@0: } else { michael@0: /* let _Stl_atod diagnose under- and over-flows */ michael@0: /* if the input was == 0.0, we have already returned, michael@0: so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW michael@0: */ michael@0: x = _Stl_atodT(digits, n, exp); michael@0: } michael@0: michael@0: return Negate ? -x : x; michael@0: } michael@0: michael@0: #endif // __linux__ michael@0: michael@0: void _STLP_CALL michael@0: __string_to_float(const __iostring& v, float& val) michael@0: { michael@0: #if !defined (__linux__) || defined (__ANDROID__) michael@0: val = (float)_Stl_string_to_double(v.c_str()); michael@0: #else michael@0: val = (float)_Stl_string_to_doubleT(v.c_str()); michael@0: #endif michael@0: } michael@0: michael@0: void _STLP_CALL michael@0: __string_to_float(const __iostring& v, double& val) michael@0: { michael@0: #if !defined (__linux__) || defined (__ANDROID__) michael@0: val = _Stl_string_to_double(v.c_str()); michael@0: #else michael@0: val = _Stl_string_to_doubleT(v.c_str()); michael@0: #endif michael@0: } michael@0: michael@0: #if !defined (_STLP_NO_LONG_DOUBLE) michael@0: void _STLP_CALL michael@0: __string_to_float(const __iostring& v, long double& val) { michael@0: #if !defined (__linux__) && !defined (__MINGW32__) && !defined (__CYGWIN__) && \ michael@0: !defined (__BORLANDC__) && !defined (__DMC__) && !defined (__HP_aCC) michael@0: //The following function is valid only if long double is an alias for double. michael@0: _STLP_STATIC_ASSERT( sizeof(long double) <= sizeof(double) ) michael@0: val = _Stl_string_to_double(v.c_str()); michael@0: #else michael@0: val = _Stl_string_to_doubleT(v.c_str()); michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: _STLP_MOVE_TO_STD_NAMESPACE michael@0: _STLP_END_NAMESPACE michael@0: michael@0: // Local Variables: michael@0: // mode:C++ michael@0: // End: