sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: * Copyright (c) 1999 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Boris Fomitchev sl@0: * sl@0: * This material is provided "as is", with absolutely no warranty expressed sl@0: * or implied. Any use is at your own risk. sl@0: * sl@0: * Permission to use or copy this software for any purpose is hereby granted sl@0: * without fee, provided the above notices are retained on all copies. sl@0: * Permission to modify the code and to distribute modified code is granted, sl@0: * provided the above notices are retained, and a notice that the code was sl@0: * modified is included with the above copyright notice. sl@0: * sl@0: */ sl@0: sl@0: # include "stlport_prefix.h" sl@0: #include <stl/_limits.h> sl@0: #include <stl/_num_get.h> sl@0: #include <stl/_istream.h> sl@0: #ifdef __SYMBIAN32__ sl@0: #include <stdlib.h> sl@0: #include <errno.h> sl@0: #endif sl@0: sl@0: _STLP_BEGIN_NAMESPACE sl@0: sl@0: //---------------------------------------------------------------------- sl@0: // num_get sl@0: sl@0: sl@0: /* sl@0: * __string_to_double is just lifted from atof, the difference being sl@0: * that we just use '.' for the decimal point, rather than let it sl@0: * be taken from the current C locale, which of course is not accessible sl@0: * to us. sl@0: */ sl@0: sl@0: typedef unsigned int uint32; sl@0: # if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL) sl@0: # define ULL(x) x##Ui64 sl@0: typedef unsigned _STLP_LONG_LONG uint64; sl@0: # elif defined (_STLP_LONG_LONG) sl@0: typedef unsigned _STLP_LONG_LONG uint64; sl@0: # define ULL(x) x##ULL sl@0: # elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers sl@0: # include "uint64.h" //*TY 03/25/2000 - added 64bit math type definition sl@0: # else sl@0: # error "there should be some long long type on the system!" sl@0: # define NUMERIC_NO_64 1 sl@0: # endif sl@0: sl@0: // Multiplication of two 64-bit integers, giving a 128-bit result. sl@0: // Taken from Algorithm M in Knuth section 4.3.1, with the loop sl@0: // hand-unrolled. sl@0: void _Stl_mult64(const uint64 u, const uint64 v, sl@0: uint64& high, uint64& low) sl@0: { sl@0: const uint64 low_mask = ULL(0xffffffff); sl@0: const uint64 u0 = u & low_mask; sl@0: const uint64 u1 = u >> 32; sl@0: const uint64 v0 = v & low_mask; sl@0: const uint64 v1 = v >> 32; sl@0: sl@0: uint64 t = u0 * v0; sl@0: low = t & low_mask; sl@0: sl@0: t = u1 * v0 + (t >> 32); sl@0: uint64 w1 = t & low_mask; sl@0: uint64 w2 = t >> 32; sl@0: sl@0: uint64 x = u0 * v1 + w1; sl@0: low += (x & low_mask) << 32; sl@0: high = u1 * v1 + w2 + (x >> 32); sl@0: } sl@0: sl@0: # define bit11 ULL(0x7ff) sl@0: # define exponent_mask (bit11 << 52) sl@0: sl@0: inline void _Stl_set_exponent(uint64& val, uint64 exp) sl@0: { sl@0: val = (val & ~exponent_mask) | ((exp & bit11) << 52); sl@0: } sl@0: sl@0: /* Power of ten fractions for tenscale*/ sl@0: /* The constants are factored so that at most two constants sl@0: * and two multiplies are needed. Furthermore, one of the constants sl@0: * is represented exactly - 10**n where 1<= n <= 27. sl@0: */ sl@0: sl@0: #if !defined(__SC__) //*TY 03/25/2000 - no native 64bit integer under SCpp sl@0: static const uint64 _Stl_tenpow[80] = { sl@0: ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */ sl@0: ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */ sl@0: ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */ sl@0: ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */ sl@0: ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */ sl@0: ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */ sl@0: ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */ sl@0: ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */ sl@0: ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */ sl@0: ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */ sl@0: ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */ sl@0: ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */ sl@0: ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */ sl@0: ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */ sl@0: ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */ sl@0: ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */ sl@0: ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */ sl@0: ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */ sl@0: ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */ sl@0: ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */ sl@0: ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */ sl@0: ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */ sl@0: ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */ sl@0: ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */ sl@0: ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */ sl@0: ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */ sl@0: ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */ sl@0: sl@0: ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */ sl@0: ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */ sl@0: ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */ sl@0: ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */ sl@0: ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */ sl@0: ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */ sl@0: ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */ sl@0: ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */ sl@0: ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */ sl@0: ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */ sl@0: sl@0: ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */ sl@0: ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */ sl@0: ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */ sl@0: ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */ sl@0: ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */ sl@0: ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */ sl@0: ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */ sl@0: ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */ sl@0: ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */ sl@0: ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */ sl@0: ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */ sl@0: ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */ sl@0: ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */ sl@0: sl@0: #else //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type sl@0: static const UnsignedWide _Stl_tenpow[80] = { sl@0: ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */ sl@0: ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */ sl@0: ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */ sl@0: ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */ sl@0: ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */ sl@0: ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */ sl@0: ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */ sl@0: ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */ sl@0: ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */ sl@0: ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */ sl@0: ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */ sl@0: ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */ sl@0: ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */ sl@0: ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */ sl@0: ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */ sl@0: ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */ sl@0: ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */ sl@0: ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */ sl@0: ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */ sl@0: ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */ sl@0: ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */ sl@0: ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */ sl@0: ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */ sl@0: ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */ sl@0: ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */ sl@0: ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */ sl@0: ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */ sl@0: sl@0: ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */ sl@0: ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */ sl@0: ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */ sl@0: ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */ sl@0: ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */ sl@0: ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */ sl@0: ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */ sl@0: ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */ sl@0: ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */ sl@0: ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */ sl@0: sl@0: ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */ sl@0: ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */ sl@0: ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */ sl@0: ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */ sl@0: ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */ sl@0: ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */ sl@0: ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */ sl@0: ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */ sl@0: ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */ sl@0: ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */ sl@0: ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */ sl@0: ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */ sl@0: ULL2(0xe1afa13a,0xfbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */ sl@0: #endif sl@0: }; sl@0: sl@0: static const short _Stl_twoexp[80] = { sl@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, sl@0: 183,276,369,462,555,648,741,834,927,1020, sl@0: -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209 sl@0: }; sl@0: sl@0: # define TEN_1 0 /* offset to 10 ** 1 */ sl@0: # define TEN_27 26 /* offset to 10 ** 27 */ sl@0: # define TEN_M28 37 /* offset to 10 ** -28 */ sl@0: # define NUM_HI_P 11 sl@0: # define NUM_HI_N 13 sl@0: sl@0: # define _Stl_HIBITULL (ULL(1) << 63) sl@0: sl@0: void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) sl@0: { sl@0: norm = 0; sl@0: if( ! (prodhi & _Stl_HIBITULL) ) { sl@0: /* leading bit is a zero sl@0: * may have to normalize sl@0: */ sl@0: if(( prodhi == ~_Stl_HIBITULL) && sl@0: ((prodlo >> 62) == 0x3) ) { /* normalization followed by round sl@0: * would cause carry to create sl@0: * extra bit, so don't normalize sl@0: */ sl@0: p = _Stl_HIBITULL; sl@0: return; sl@0: } sl@0: p = (prodhi<<1) | (prodlo>>63); /* normalize */ sl@0: norm=1; sl@0: prodlo <<= 1; sl@0: } sl@0: else { sl@0: p = prodhi; sl@0: } sl@0: sl@0: if( (prodlo & _Stl_HIBITULL) != 0 ) { /* first guard bit a one */ //*TY 03/25/2000 - added explicit comparison to zero to avoid reliance to the implicit conversion from uint64 to bool sl@0: #if !defined(__SC__) //*TY 03/25/2000 - sl@0: if( ((p & 0x1) != 0) || sl@0: prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */ sl@0: #else //*TY 03/25/2000 - added workaround for SCpp compiler sl@0: bool b1 = ((p & 0x1) != 0); sl@0: if( b1 || prodlo != _Stl_HIBITULL ) { //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression sl@0: #endif //*TY 03/25/2000 - sl@0: /* round */ sl@0: p++; sl@0: if(p==0) sl@0: p++; sl@0: } sl@0: } sl@0: sl@0: return; sl@0: } sl@0: sl@0: // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp. sl@0: // p: 64-bit fraction sl@0: // exp: base-10 exponent sl@0: // bexp: base-2 exponent (output parameter) sl@0: sl@0: void _Stl_tenscale(uint64& p, int exp, int& bexp) sl@0: { sl@0: uint64 prodhi, prodlo; /* 128b product */ sl@0: int exp_hi, exp_lo; /* exp = exp_hi*32 + exp_lo */ sl@0: int hi, lo, tlo, thi; /* offsets in power of ten table */ sl@0: int norm; /* number of bits of normalization */ sl@0: int num_hi; /* number of high exponent powers */ sl@0: sl@0: bexp = 0; sl@0: if(exp > 0) { /* split exponent */ sl@0: exp_lo = exp; sl@0: exp_hi = 0; sl@0: if(exp_lo>27) { sl@0: exp_lo++; sl@0: while(exp_lo>27) { sl@0: exp_hi++; sl@0: exp_lo-=28; sl@0: } sl@0: } sl@0: tlo = TEN_1; sl@0: thi = TEN_27; sl@0: num_hi = NUM_HI_P; sl@0: } sl@0: else if(exp < 0) { sl@0: exp_lo = exp; sl@0: exp_hi = 0; sl@0: while(exp_lo<0) { sl@0: exp_hi++; sl@0: exp_lo+=28; sl@0: } sl@0: tlo = TEN_1; sl@0: thi = TEN_M28; sl@0: num_hi = NUM_HI_N; sl@0: } sl@0: else { /* no scaling needed */ sl@0: return; sl@0: } sl@0: while(exp_hi) { /* scale */ sl@0: hi = (min) (exp_hi,num_hi); /* only a few large powers of 10 */ sl@0: exp_hi -= hi; /* could iterate in extreme case */ sl@0: hi += thi-1; sl@0: _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo); sl@0: _Stl_norm_and_round(p, norm, prodhi, prodlo); sl@0: bexp += _Stl_twoexp[hi] - norm; sl@0: } sl@0: if(exp_lo) { sl@0: lo = tlo + exp_lo -1; sl@0: _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo); sl@0: _Stl_norm_and_round(p, norm, prodhi, prodlo); sl@0: bexp += _Stl_twoexp[lo] - norm; sl@0: } sl@0: sl@0: return; sl@0: } sl@0: sl@0: // First argument is a buffer of values from 0 to 9, NOT ascii. sl@0: // Second argument is number of digits in buffer, 1 <= digits <= 17. sl@0: // Third argument is base-10 exponent. sl@0: sl@0: #if defined(__SC__) || defined(__MRC__) sl@0: sl@0: //*TY 04/06/2000 - powermac's 68K emulator utilizes apple's SANE floating point, which is not compatible with IEEE format. sl@0: _STLP_END_NAMESPACE sl@0: # include <fp.h> sl@0: _STLP_BEGIN_NAMESPACE sl@0: inline double _Stl_atod(char *buffer, int ndigit, int dexp) sl@0: { sl@0: decimal d; // ref. inside macintosh powerpc numerics p.9-13 sl@0: sl@0: d.sgn = 0; sl@0: d.exp = dexp; sl@0: d.sig.length = ndigit; sl@0: for( int i = 0; i < ndigit; ++i ) sl@0: { sl@0: d.sig.text[i] = buffer[i] + '0'; sl@0: } sl@0: return dec2num( &d ); sl@0: } sl@0: sl@0: #else /* IEEE representation */ sl@0: sl@0: #if 0 // def __ICL sl@0: // turn off optimization here sl@0: # pragma optimize "off" sl@0: #endif sl@0: sl@0: double _Stl_atod(char *buffer, int ndigit, int dexp) sl@0: { sl@0: sl@0: uint64 value; /* Value develops as follows: sl@0: * 1) decimal digits as an integer sl@0: * 2) left adjusted fraction sl@0: * 3) right adjusted fraction sl@0: * 4) exponent and fraction sl@0: */ sl@0: sl@0: uint32 guard; /* First guard bit */ sl@0: uint64 rest; /* Remaining guard bits */ sl@0: sl@0: int bexp; /* binary exponent */ sl@0: int nzero; /* number of non-zero bits */ sl@0: int sexp; /* scaling exponent */ sl@0: sl@0: char *bufferend; /* pointer to char after last digit */ sl@0: sl@0: /* Check for zero and treat it as a special case */ sl@0: sl@0: if (buffer == 0){ sl@0: return 0.0; sl@0: } sl@0: sl@0: /* Convert the decimal digits to a binary integer. */ sl@0: sl@0: bufferend = buffer + ndigit; sl@0: value = 0; sl@0: sl@0: while( buffer < bufferend ) { sl@0: value *= 10; sl@0: value += *buffer++; sl@0: } sl@0: sl@0: /* Check for zero and treat it as a special case */ sl@0: sl@0: if (value == 0){ sl@0: return 0.0; sl@0: } sl@0: sl@0: /* Normalize value */ sl@0: sl@0: bexp = 64; /* convert from 64b int to fraction */ sl@0: sl@0: /* Count number of non-zeroes in value */ sl@0: nzero = 0; sl@0: if ( (value >> 32) !=0 ){ nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator sl@0: if ( (value >> (16 + nzero)) !=0 ){ nzero += 16; } sl@0: if ( (value >> ( 8 + nzero)) !=0 ){ nzero += 8; } sl@0: if ( (value >> ( 4 + nzero)) !=0 ){ nzero += 4; } sl@0: if ( (value >> ( 2 + nzero)) !=0 ){ nzero += 2; } sl@0: if ( (value >> ( 1 + nzero)) !=0 ){ nzero += 1; } sl@0: if ( (value >> ( nzero)) !=0 ){ nzero += 1; } sl@0: sl@0: /* Normalize */ sl@0: value <<= /*(uint64)*/ (64-nzero); //*TY 03/25/2000 - removed extraneous cast to uint64 sl@0: bexp -= 64-nzero; sl@0: sl@0: /* At this point we have a 64b fraction and a binary exponent sl@0: * but have yet to incorporate the decimal exponent. sl@0: */ sl@0: sl@0: /* multiply by 10^dexp */ sl@0: sl@0: _Stl_tenscale(value, dexp, sexp); sl@0: bexp += sexp; sl@0: sl@0: if (bexp <= -1022) { /* HI denorm or underflow */ sl@0: bexp += 1022; sl@0: if( bexp < -53 ) { /* guaranteed underflow */ sl@0: value = 0; sl@0: } sl@0: else { /* denorm or possible underflow */ sl@0: int lead0; sl@0: sl@0: lead0 = 12-bexp; /* 12 sign and exponent bits */ sl@0: sl@0: /* we must special case right shifts of more than 63 */ sl@0: sl@0: if ( lead0 > 64 ) sl@0: { sl@0: rest = value; sl@0: guard = 0; sl@0: value = 0; sl@0: } sl@0: else if ( lead0 == 64 ) sl@0: { sl@0: rest = value & ((ULL(1)<< 63)-1); sl@0: #if !defined(__SC__) sl@0: guard = (uint32) ((value>> 63) & 1 ); sl@0: #else sl@0: guard = to_ulong((value>> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization sl@0: #endif sl@0: value = 0; sl@0: } sl@0: else sl@0: { sl@0: rest = value & (((ULL(1) << lead0)-1)-1); sl@0: #if !defined(__SC__) sl@0: guard = (uint32) (((value>> lead0)-1) & 1); sl@0: #else //*TY 03/25/2000 - sl@0: guard = to_ulong(((value>> lead0)-1) & 1); sl@0: #endif //*TY 03/25/2000 - sl@0: value >>= /*(uint64)*/ lead0; /* exponent is zero */ sl@0: } sl@0: sl@0: /* Round */ sl@0: if ( guard && ( (value&1) || rest) ) { sl@0: value++; sl@0: if( value == (ULL(1) << 52) ) { /* carry created normal number */ sl@0: value = 0; sl@0: _Stl_set_exponent(value, 1); sl@0: } sl@0: } sl@0: } sl@0: sl@0: } sl@0: else { /* not zero or denorm */ sl@0: /* Round to 53 bits */ sl@0: sl@0: rest = value & (1<<10)-1; sl@0: value >>= 10; sl@0: #if !defined(__SC__) sl@0: guard = (uint32) value & 1; sl@0: #else //*TY 03/25/2000 - sl@0: guard = to_ulong(value & 1); sl@0: #endif sl@0: value >>= 1; sl@0: sl@0: /* value&1 guard rest Action sl@0: * sl@0: * dc 0 dc none sl@0: * 1 1 dc round sl@0: * 0 1 0 none sl@0: * 0 1 !=0 round sl@0: */ sl@0: if(guard) { sl@0: if(((value&1)!=0) || (rest!=0)) { sl@0: value++; /* round */ sl@0: if((value>>53)!=0) { /* carry all the way across */ sl@0: value >>= 1; /* renormalize */ sl@0: bexp ++; sl@0: } sl@0: } sl@0: } sl@0: /* sl@0: * Check for overflow sl@0: * IEEE Double Precision Format sl@0: * (From Table 7-8 of Kane and Heinrich) sl@0: * sl@0: * Fraction bits 52 sl@0: * Emax +1023 sl@0: * Emin -1022 sl@0: * Exponent bias +1023 sl@0: * Exponent bits 11 sl@0: * Integer bit hidden sl@0: * Total width in bits 64 sl@0: */ sl@0: sl@0: if (bexp > 1024) { /* overflow */ sl@0: return numeric_limits<double>::infinity(); sl@0: } sl@0: else { /* value is normal */ sl@0: value &= ~(ULL(1) << 52); /* hide hidden bit */ sl@0: _Stl_set_exponent(value, bexp + 1022); /* add bias */ sl@0: } sl@0: } sl@0: sl@0: return *((double *) &value); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: double _Stl_string_to_double(const char * s) { sl@0: const int max_digits = 17; sl@0: unsigned c; sl@0: unsigned Negate, decimal_point; sl@0: char *d; sl@0: int exp; sl@0: double x; sl@0: int dpchar; sl@0: char digits[max_digits]; sl@0: sl@0: // Skip leading whitespace, if any. sl@0: const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); sl@0: while (c = *s++, ct.is(ctype_base::space, char(c))) sl@0: ; sl@0: sl@0: /* process sign */ sl@0: Negate = 0; sl@0: if (c == '+') { sl@0: c = *s++; sl@0: } sl@0: else if (c == '-') { sl@0: Negate = 1; sl@0: c = *s++; sl@0: } sl@0: d = digits; sl@0: dpchar = '.' - '0'; sl@0: decimal_point = 0; sl@0: exp = 0; sl@0: for (;;) { sl@0: c -= '0'; sl@0: if (c < 10) { sl@0: if (d == digits+max_digits) { sl@0: /* ignore more than 17 digits, but adjust exponent */ sl@0: exp += (decimal_point ^ 1); sl@0: } sl@0: else { sl@0: if (c == 0 && d == digits) { sl@0: /* ignore leading zeros */ sl@0: } sl@0: else { sl@0: *d++ = (char) c; sl@0: } sl@0: exp -= decimal_point; sl@0: } sl@0: } sl@0: else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */ sl@0: decimal_point = 1; sl@0: } sl@0: else { sl@0: break; sl@0: } sl@0: c = *s++; sl@0: } sl@0: /* strtod cant return until it finds the end of the exponent */ sl@0: if (d == digits) { sl@0: return 0.0; sl@0: } sl@0: if (c == 'e'-'0' || c == 'E'-'0') { sl@0: register unsigned negate_exp = 0; sl@0: register int e = 0; sl@0: c = *s++; sl@0: if (c == '+' || c == ' ') { sl@0: c = *s++; sl@0: } sl@0: else if (c == '-') { sl@0: negate_exp = 1; sl@0: c = *s++; sl@0: } sl@0: if (c -= '0', c < 10) { sl@0: do { sl@0: if (e <= 340) sl@0: e = e * 10 + (int)c; sl@0: else break; sl@0: c = *s++; sl@0: } sl@0: while (c -= '0', c < 10); sl@0: if (negate_exp) { sl@0: e = -e; sl@0: } sl@0: if (e < -340 || e > 340) sl@0: exp = e; sl@0: else sl@0: exp += e; sl@0: } sl@0: } sl@0: sl@0: if (exp < -340) { sl@0: x = 0; sl@0: } sl@0: else if (exp > 308) { sl@0: x = numeric_limits<double>::infinity(); sl@0: } sl@0: else { sl@0: /* let _Stl_atod diagnose under- and over-flows */ sl@0: /* if the input was == 0.0, we have already returned, sl@0: so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW sl@0: */ sl@0: x = _Stl_atod (digits, (int)(d - digits), exp); sl@0: } sl@0: if (Negate) { sl@0: x = -x; sl@0: } sl@0: return x; sl@0: } sl@0: sl@0: sl@0: #ifndef _STLP_NO_LONG_DOUBLE sl@0: /* sl@0: * __string_to_long_double is just lifted from atold, the difference being sl@0: * that we just use '.' for the decimal point, rather than let it sl@0: * be taken from the current C locale, which of course is not accessible sl@0: * to us. sl@0: */ sl@0: sl@0: long double sl@0: _Stl_string_to_long_double(const char * s) { sl@0: const int max_digits = 34; sl@0: register unsigned c; sl@0: register unsigned Negate, decimal_point; sl@0: register char *d; sl@0: register int exp; sl@0: long double x; sl@0: register int dpchar; sl@0: char digits[max_digits]; sl@0: sl@0: const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); sl@0: while (c = *s++, ct.is(ctype_base::space, char(c))) sl@0: ; sl@0: sl@0: /* process sign */ sl@0: Negate = 0; sl@0: if (c == '+') { sl@0: c = *s++; sl@0: } sl@0: else if (c == '-') { sl@0: Negate = 1; sl@0: c = *s++; sl@0: } sl@0: sl@0: d = digits; sl@0: dpchar = '.' -'0'; sl@0: decimal_point = 0; sl@0: exp = 0; sl@0: sl@0: for (;;) { sl@0: c -= '0'; sl@0: if (c < 10) { sl@0: if (d == digits+max_digits) { sl@0: /* ignore more than 34 digits, but adjust exponent */ sl@0: exp += (decimal_point ^ 1); sl@0: } sl@0: else { sl@0: if (c == 0 && d == digits) { sl@0: /* ignore leading zeros */ sl@0: ; sl@0: } sl@0: else { sl@0: *d++ = c; sl@0: } sl@0: exp -= decimal_point; sl@0: } sl@0: } sl@0: else if (c == dpchar && !decimal_point) { /* INTERNATIONAL */ sl@0: decimal_point = 1; sl@0: } sl@0: else { sl@0: break; sl@0: } sl@0: c = *s++; sl@0: } /* for */ sl@0: sl@0: if (d == digits) { sl@0: return 0.0L; sl@0: } sl@0: if (c == 'e'-'0' || c == 'E'-'0') { sl@0: register unsigned negate_exp = 0; sl@0: register int e = 0; sl@0: c = *s++; sl@0: if (c == '+' || c == ' ') { sl@0: c = *s++; sl@0: } sl@0: else if (c == '-') { sl@0: negate_exp = 1; sl@0: c = *s++; sl@0: } sl@0: if (c -= '0', c < 10) { sl@0: do { sl@0: if (e <= 340) sl@0: e = e * 10 + c; sl@0: else break; sl@0: c = *s++; sl@0: } sl@0: while (c -= '0', c < 10); sl@0: if (negate_exp) { sl@0: e = -e; sl@0: } sl@0: if (e < -(323+max_digits) || e > 308) sl@0: exp = e; sl@0: else sl@0: exp += e; sl@0: } sl@0: } sl@0: sl@0: sl@0: if (exp < -(324+max_digits)) { sl@0: x = 0; sl@0: } sl@0: else if (exp > 308) { sl@0: x = numeric_limits<long double>::infinity(); sl@0: } sl@0: else { sl@0: /* let _Stl_atod diagnose under- and over-flows */ sl@0: /* if the input was == 0.0, we have already returned, sl@0: so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW sl@0: */ sl@0: sl@0: // x = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1 sl@0: double tmp = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1 sl@0: x = tmp == numeric_limits<double>::infinity() sl@0: ? numeric_limits<long double>::infinity() sl@0: : tmp; sl@0: } sl@0: sl@0: if (Negate) { sl@0: x = -x; sl@0: } sl@0: sl@0: return x; sl@0: } sl@0: #endif sl@0: sl@0: _STLP_EXP_DECLSPEC int _STLP_CALL sl@0: __string_to_float(const string& v, float& val) { sl@0: #ifdef __SYMBIAN32__ sl@0: char *endpt = NULL; sl@0: unsigned c; sl@0: const char *s = v.data(); sl@0: // Skip leading whitespace, if any. sl@0: const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); sl@0: while (c = *s, ct.is(ctype_base::space, char(c))) sl@0: s++; sl@0: int prv_error = errno; sl@0: errno = 0; sl@0: val = strtof(s, &endpt); sl@0: bool __ok = (errno == 0); sl@0: int tt = errno; sl@0: errno= prv_error; sl@0: return (__ok); sl@0: #else sl@0: val = _Stl_string_to_double(v.data()); sl@0: return 0; sl@0: #endif sl@0: } sl@0: sl@0: _STLP_EXP_DECLSPEC int _STLP_CALL sl@0: __string_to_float(const string& v, double& val) { sl@0: #ifdef __SYMBIAN32__ sl@0: char *endpt = NULL; sl@0: unsigned c; sl@0: const char *s = v.data(); sl@0: // Skip leading whitespace, if any. sl@0: const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); sl@0: while (c = *s, ct.is(ctype_base::space, char(c))) sl@0: s++; sl@0: int prv_error = errno; sl@0: errno = 0; sl@0: val = strtod(s, &endpt); sl@0: bool __ok = (errno == 0); sl@0: errno = prv_error; sl@0: return (__ok); sl@0: #else sl@0: val = _Stl_string_to_double(v.data()); sl@0: return 0; sl@0: #endif sl@0: } sl@0: sl@0: #ifndef _STLP_NO_LONG_DOUBLE sl@0: _STLP_EXP_DECLSPEC int _STLP_CALL sl@0: __string_to_float(const string& v, long double& val) { sl@0: #ifdef __SYMBIAN32__ sl@0: char *endpt = NULL; sl@0: unsigned c; sl@0: const char *s = v.data(); sl@0: // Skip leading whitespace, if any. sl@0: const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); sl@0: while (c = *s, ct.is(ctype_base::space, char(c))) sl@0: s++; sl@0: int prv_error = errno; sl@0: errno = 0; sl@0: val = strtold(s, &endpt); sl@0: bool __ok = (errno == 0); sl@0: errno = prv_error; sl@0: return (__ok); sl@0: #else sl@0: val = _Stl_string_to_long_double(v.data()); sl@0: return 0; sl@0: #endif sl@0: } sl@0: #endif sl@0: sl@0: _STLP_END_NAMESPACE sl@0: sl@0: // Local Variables: sl@0: // mode:C++ sl@0: // End: