home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / src / num_get_float.cpp < prev    next >
C/C++ Source or Header  |  2002-01-10  |  25KB  |  793 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18.  
  19. # include "stlport_prefix.h"
  20. #include <stl/_limits.h>
  21. #include <stl/_num_get.h>
  22. #include <stl/_istream.h>
  23.  
  24. _STLP_BEGIN_NAMESPACE
  25.  
  26. //----------------------------------------------------------------------
  27. // num_get
  28.  
  29. // Helper functions for _M_do_get_float.
  30.  
  31. # ifndef _STLP_NO_WCHAR_T
  32.  
  33. void  _STLP_CALL
  34. _Initialize_get_float( const ctype<wchar_t>& ct,
  35.                        wchar_t& Plus, wchar_t& Minus,
  36.                        wchar_t& pow_e, wchar_t& pow_E,
  37.                        wchar_t* digits)
  38. {
  39.   char ndigits[11] = "0123456789";
  40.   Plus  = ct.widen('+');
  41.   Minus = ct.widen('-');
  42.   pow_e = ct.widen('e');
  43.   pow_E = ct.widen('E');
  44.   ct.widen(ndigits + 0, ndigits + 10, digits);
  45. }
  46.  
  47. # endif /* WCHAR_T */
  48.  
  49. /*
  50.  * __string_to_double is just lifted from atof, the difference being
  51.  * that we just use '.' for the decimal point, rather than let it
  52.  * be taken from the current C locale, which of course is not accessible
  53.  * to us.
  54.  */
  55.  
  56. typedef unsigned int uint32;
  57. # if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
  58. # define ULL(x) x##Ui64
  59. typedef unsigned _STLP_LONG_LONG uint64;
  60. # elif defined (_STLP_LONG_LONG)
  61. typedef unsigned _STLP_LONG_LONG uint64;
  62. # define ULL(x) x##ULL
  63. # elif defined(__MRC__) || defined(__SC__)        //*TY 02/25/2000 - added support for MPW compilers
  64. # include "uint64.h"        //*TY 03/25/2000 - added 64bit math type definition
  65. # else
  66. #  error "there should be some long long type on the system!"
  67. #  define NUMERIC_NO_64 1
  68. # endif
  69.  
  70. // Multiplication of two 64-bit integers, giving a 128-bit result.
  71. // Taken from Algorithm M in Knuth section 4.3.1, with the loop 
  72. // hand-unrolled.
  73. void _Stl_mult64(const uint64 u, const uint64 v,
  74.          uint64& high, uint64& low)
  75. {
  76.   const uint64 low_mask = ULL(0xffffffff);
  77.   const uint64 u0 = u & low_mask;
  78.   const uint64 u1 = u >> 32;
  79.   const uint64 v0 = v & low_mask;
  80.   const uint64 v1 = v >> 32;
  81.  
  82.   uint64 t = u0 * v0;
  83.   low = t & low_mask;
  84.  
  85.   t = u1 * v0 + (t >> 32);
  86.   uint64 w1 = t & low_mask;
  87.   uint64 w2 = t >> 32;
  88.  
  89.   uint64 x = u0 * v1 + w1;
  90.   low += (x & low_mask) << 32;
  91.   high = u1 * v1 + w2 + (x >> 32);
  92. }
  93.  
  94. # define bit11 ULL(0x7ff)
  95. # define exponent_mask (bit11 << 52)
  96.  
  97. inline void _Stl_set_exponent(uint64& val, uint64 exp)
  98. {
  99.   val = (val & ~exponent_mask) | ((exp & bit11) << 52);
  100. }
  101.  
  102. /* Power of ten fractions for tenscale*/
  103. /* The constants are factored so that at most two constants
  104.  * and two multiplies are needed. Furthermore, one of the constants
  105.  * is represented exactly - 10**n where 1<= n <= 27.
  106.  */
  107.  
  108. #if !defined(__SC__)        //*TY 03/25/2000 - no native 64bit integer under SCpp
  109. static const uint64 _Stl_tenpow[80] = {
  110. ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
  111. ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
  112. ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
  113. ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
  114. ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
  115. ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
  116. ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
  117. ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
  118. ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
  119. ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
  120. ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
  121. ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
  122. ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
  123. ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
  124. ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
  125. ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
  126. ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
  127. ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
  128. ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
  129. ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
  130. ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
  131. ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
  132. ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
  133. ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
  134. ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
  135. ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
  136. ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
  137.  
  138. ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
  139. ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
  140. ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
  141. ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
  142. ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
  143. ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
  144. ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
  145. ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
  146. ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
  147. ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
  148.  
  149. ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
  150. ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
  151. ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
  152. ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
  153. ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
  154. ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
  155. ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
  156. ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
  157. ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837)     */
  158. ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
  159. ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023)    */
  160. ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
  161. ULL(0xe1afa13afbd14d6e)  /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
  162.  
  163. #else        //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type
  164. static const UnsignedWide _Stl_tenpow[80] = {
  165. ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
  166. ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
  167. ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
  168. ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
  169. ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
  170. ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
  171. ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
  172. ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
  173. ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
  174. ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
  175. ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
  176. ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
  177. ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
  178. ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
  179. ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
  180. ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
  181. ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
  182. ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
  183. ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
  184. ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
  185. ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
  186. ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
  187. ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
  188. ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
  189. ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
  190. ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
  191. ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
  192.  
  193. ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
  194. ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
  195. ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
  196. ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
  197. ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
  198. ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
  199. ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
  200. ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
  201. ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
  202. ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
  203.  
  204. ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
  205. ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
  206. ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
  207. ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
  208. ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
  209. ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
  210. ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
  211. ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
  212. ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837)     */
  213. ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
  214. ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023)    */
  215. ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
  216. ULL2(0xe1afa13a,0xfbd14d6e)  /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
  217. #endif
  218. };
  219.  
  220. static const short _Stl_twoexp[80] = {
  221. 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,
  222. 183,276,369,462,555,648,741,834,927,1020,
  223. -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
  224. };
  225.  
  226. # define  TEN_1  0           /* offset to 10 **   1 */
  227. # define  TEN_27   26        /* offset to 10 **  27 */
  228. # define  TEN_M28  37        /* offset to 10 ** -28 */
  229. # define  NUM_HI_P 11
  230. # define  NUM_HI_N 13
  231.  
  232. # define _Stl_HIBITULL (ULL(1) << 63)
  233.  
  234. void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo)
  235. {
  236.   norm = 0;
  237.   if( ! (prodhi & _Stl_HIBITULL) ) { 
  238.                                 /* leading bit is a zero 
  239.                                  * may have to normalize 
  240.                                  */
  241.     if(( prodhi == ~_Stl_HIBITULL) &&
  242.        ((prodlo >> 62) == 0x3) ) {  /* normalization followed by round
  243.                                      * would cause carry to create
  244.                                      * extra bit, so don't normalize 
  245.                                      */
  246.       p = _Stl_HIBITULL;
  247.       return;
  248.     }
  249.     p = (prodhi<<1) | (prodlo>>63); /* normalize */
  250.     norm=1;
  251.     prodlo <<= 1;
  252.   }
  253.   else {
  254.     p = prodhi;
  255.   }
  256.  
  257.   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
  258. #if !defined(__SC__)            //*TY 03/25/2000 - 
  259.     if( ((p & 0x1) != 0) ||
  260.        prodlo != _Stl_HIBITULL ) {    /* not borderline for round to even */
  261. #else                            //*TY 03/25/2000 - added workaround for SCpp compiler
  262.     bool b1 = ((p & 0x1) != 0);
  263.     if( b1 || prodlo != _Stl_HIBITULL ) {        //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression
  264. #endif                            //*TY 03/25/2000 - 
  265.       /* round */
  266.       p++;
  267.       if(p==0)
  268.         p++;
  269.     }
  270.   }
  271.  
  272.   return;
  273. }
  274.  
  275. // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
  276. // p:    64-bit fraction
  277. // exp:  base-10 exponent
  278. // bexp: base-2 exponent (output parameter)
  279.  
  280. void _Stl_tenscale(uint64& p, int exp, int& bexp)
  281. {
  282.   uint64 prodhi, prodlo;        /* 128b product */
  283.   int exp_hi, exp_lo;           /* exp = exp_hi*32 + exp_lo */
  284.   int hi, lo, tlo, thi;         /* offsets in power of ten table */
  285.   int norm;                     /* number of bits of normalization */
  286.   int num_hi;                   /* number of high exponent powers */
  287.  
  288.   bexp = 0;
  289.   if(exp > 0) {                 /* split exponent */
  290.     exp_lo = exp;
  291.     exp_hi = 0;
  292.     if(exp_lo>27) {
  293.       exp_lo++;
  294.       while(exp_lo>27) {
  295.         exp_hi++;
  296.         exp_lo-=28;
  297.       }
  298.     }
  299.     tlo = TEN_1;
  300.     thi = TEN_27;
  301.     num_hi = NUM_HI_P;
  302.   }
  303.   else if(exp < 0) {
  304.     exp_lo = exp;
  305.     exp_hi = 0;
  306.     while(exp_lo<0) {
  307.       exp_hi++;
  308.       exp_lo+=28;
  309.     }
  310.     tlo = TEN_1;
  311.     thi = TEN_M28;
  312.     num_hi = NUM_HI_N;
  313.   }
  314.   else {                        /* no scaling needed */
  315.     return;
  316.   }
  317.   while(exp_hi) {               /* scale */
  318.     hi = (min) (exp_hi,num_hi);    /* only a few large powers of 10 */
  319.     exp_hi -= hi;               /* could iterate in extreme case */
  320.     hi += thi-1;
  321.     _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
  322.     _Stl_norm_and_round(p, norm, prodhi, prodlo);
  323.     bexp += _Stl_twoexp[hi] - norm;
  324.   }
  325.   if(exp_lo) {
  326.     lo = tlo + exp_lo -1;
  327.     _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
  328.     _Stl_norm_and_round(p, norm, prodhi, prodlo);
  329.     bexp += _Stl_twoexp[lo] - norm;
  330.   }
  331.  
  332.   return;
  333. }
  334.  
  335. // First argument is a buffer of values from 0 to 9, NOT ascii.
  336. // Second argument is number of digits in buffer, 1 <= digits <= 17.
  337. // Third argument is base-10 exponent.
  338.  
  339. #if defined(__SC__) || defined(__MRC__)
  340.  
  341. //*TY 04/06/2000 - powermac's 68K emulator utilizes apple's SANE floating point, which is not compatible with IEEE format.
  342. _STLP_END_NAMESPACE
  343. # include <fp.h>
  344. _STLP_BEGIN_NAMESPACE
  345. inline double _Stl_atod(char *buffer, int ndigit, int dexp)
  346. {
  347.     decimal d;    // ref. inside macintosh powerpc numerics p.9-13
  348.     
  349.     d.sgn = 0;
  350.     d.exp = dexp;
  351.     d.sig.length = ndigit;
  352.     for( int i = 0; i < ndigit; ++i )
  353.     {
  354.         d.sig.text[i] = buffer[i] + '0';
  355.     }
  356.     return dec2num( &d );
  357. }
  358.  
  359. #else  /* IEEE representation */
  360.  
  361. #if 0 // def __ICL
  362. // turn off optimization here
  363. #  pragma optimize "off"
  364. #endif
  365.  
  366. double _Stl_atod(char *buffer, int ndigit, int dexp)
  367. {
  368.  
  369.   uint64 value;         /* Value develops as follows:
  370.                                  * 1) decimal digits as an integer
  371.                                  * 2) left adjusted fraction
  372.                                  * 3) right adjusted fraction
  373.                                  * 4) exponent and fraction
  374.                                  */
  375.  
  376.   uint32 guard;         /* First guard bit */
  377.   uint64 rest;          /* Remaining guard bits */
  378.  
  379.   int bexp;             /* binary exponent */
  380.   int nzero;            /* number of non-zero bits */
  381.   int sexp;             /* scaling exponent */
  382.  
  383.   char *bufferend;              /* pointer to char after last digit */
  384.   
  385.   /* Check for zero and treat it as a special case */
  386.  
  387.   if (buffer == 0){
  388.     return 0.0; 
  389.   }
  390.  
  391.   /* Convert the decimal digits to a binary integer. */
  392.  
  393.   bufferend = buffer + ndigit;
  394.   value = 0;                    
  395.  
  396.   while( buffer < bufferend ) {
  397.     value *= 10;
  398.     value += *buffer++;
  399.   }
  400.  
  401.   /* Check for zero and treat it as a special case */
  402.  
  403.   if (value == 0){
  404.     return 0.0; 
  405.   }
  406.  
  407.   /* Normalize value */
  408.  
  409.   bexp = 64;                    /* convert from 64b int to fraction */
  410.  
  411.   /* Count number of non-zeroes in value */
  412.   nzero = 0;
  413.   if ( (value >> 32) !=0 ){ nzero  = 32; }        //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
  414.   if ( (value >> (16 + nzero)) !=0 ){ nzero += 16; }
  415.   if ( (value >> ( 8 + nzero)) !=0 ){ nzero +=  8; }
  416.   if ( (value >> ( 4 + nzero)) !=0 ){ nzero +=  4; }
  417.   if ( (value >> ( 2 + nzero)) !=0 ){ nzero +=  2; }
  418.   if ( (value >> ( 1 + nzero)) !=0 ){ nzero +=  1; }
  419.   if ( (value >> (     nzero)) !=0 ){ nzero +=  1; }
  420.  
  421.   /* Normalize */
  422.   value <<= /*(uint64)*/ (64-nzero);        //*TY 03/25/2000 - removed extraneous cast to uint64
  423.   bexp -= 64-nzero;
  424.  
  425.   /* At this point we have a 64b fraction and a binary exponent 
  426.    * but have yet to incorporate the decimal exponent.
  427.    */
  428.  
  429.   /* multiply by 10^dexp */
  430.  
  431.   _Stl_tenscale(value, dexp, sexp);
  432.   bexp += sexp;
  433.  
  434.   if (bexp <= -1022) {          /* HI denorm or underflow */
  435.     bexp += 1022;
  436.     if( bexp < -53 ) {          /* guaranteed underflow */
  437.       value = 0;
  438.     }
  439.     else {                      /* denorm or possible underflow */
  440.     int lead0;
  441.  
  442.       lead0 = 12-bexp;          /* 12 sign and exponent bits */
  443.  
  444.       /* we must special case right shifts of more than 63 */
  445.  
  446.       if ( lead0 > 64 )
  447.       {
  448.            rest = value;
  449.            guard = 0;
  450.            value = 0;
  451.       }
  452.       else if ( lead0 == 64 )
  453.       {
  454.            rest = value & ((ULL(1)<< 63)-1);
  455. #if !defined(__SC__)
  456.            guard = (uint32) ((value>> 63) & 1 );
  457. #else
  458.            guard = to_ulong((value>> 63) & 1 );        //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
  459. #endif
  460.            value = 0;
  461.       }
  462.       else
  463.       {
  464.           rest = value & (((ULL(1) << lead0)-1)-1);
  465. #if !defined(__SC__)
  466.           guard = (uint32) (((value>> lead0)-1) & 1);
  467. #else        //*TY 03/25/2000 - 
  468.           guard = to_ulong(((value>> lead0)-1) & 1); 
  469. #endif        //*TY 03/25/2000 - 
  470.           value >>= /*(uint64)*/ lead0; /* exponent is zero */
  471.       }
  472.  
  473.       /* Round */
  474.       if (  guard && ( (value&1) || rest) ) {        
  475.         value++;
  476.         if( value == (ULL(1) << 52) ) { /* carry created normal number */
  477.           value = 0;
  478.           _Stl_set_exponent(value, 1);
  479.         }
  480.       }
  481.     }
  482.  
  483.   }
  484.   else {                        /* not zero or denorm */
  485.     /* Round to 53 bits */
  486.  
  487.     rest = value & (1<<10)-1;
  488.     value >>= 10;
  489. #if !defined(__SC__)
  490.     guard = (uint32) value & 1;
  491. #else        //*TY 03/25/2000 - 
  492.     guard = to_ulong(value & 1);
  493. #endif
  494.     value >>= 1;
  495.  
  496.     /*  value&1 guard   rest    Action
  497.      *  
  498.      *  dc      0       dc      none
  499.      *  1       1       dc      round
  500.      *  0       1       0       none
  501.      *  0       1       !=0     round
  502.      */
  503.     if(guard) {
  504.       if(((value&1)!=0) || (rest!=0)) {
  505.         value++;                        /* round */
  506.         if((value>>53)!=0) {         /* carry all the way across */        
  507.           value >>= 1;          /* renormalize */
  508.           bexp ++;
  509.         }
  510.       }
  511.     }
  512.     /*
  513.      * Check for overflow
  514.      * IEEE Double Precision Format
  515.      * (From Table 7-8 of Kane and Heinrich)
  516.      * 
  517.      * Fraction bits               52
  518.      * Emax                     +1023
  519.      * Emin                     -1022
  520.      * Exponent bias            +1023
  521.      * Exponent bits               11
  522.      * Integer bit             hidden
  523.      * Total width in bits         64
  524.      */
  525.   
  526.     if (bexp > 1024) {          /* overflow */
  527.       return numeric_limits<double>::infinity();
  528.     }
  529.     else {                      /* value is normal */
  530.       value &= ~(ULL(1) << 52);   /* hide hidden bit */
  531.       _Stl_set_exponent(value, bexp + 1022); /* add bias */
  532.     }
  533.   }
  534.  
  535.   return *((double *) &value);
  536. }
  537.  
  538. #endif
  539.  
  540. double _Stl_string_to_double(const char * s) {
  541.   const int max_digits = 17;
  542.   unsigned c;
  543.   unsigned Negate, decimal_point;
  544.   char *d;
  545.   int exp;
  546.   double x;
  547.   int dpchar;
  548.   char digits[max_digits];
  549.  
  550.   // Skip leading whitespace, if any.
  551.   const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
  552.   while (c = *s++, ct.is(ctype_base::space, char(c)))
  553.     ;
  554.  
  555.   /* process sign */
  556.   Negate = 0;
  557.   if (c == '+') {
  558.     c = *s++;
  559.   }
  560.   else if (c == '-') {
  561.     Negate = 1;
  562.     c = *s++;
  563.   }
  564.   d = digits;
  565.   dpchar = '.' - '0';
  566.   decimal_point = 0;
  567.   exp = 0;
  568.   for (;;) {
  569.     c -= '0';
  570.     if (c < 10) {
  571.       if (d == digits+max_digits) {
  572.         /* ignore more than 17 digits, but adjust exponent */
  573.         exp += (decimal_point ^ 1);
  574.       }
  575.       else {
  576.         if (c == 0 && d == digits) {
  577.           /* ignore leading zeros */
  578.         }
  579.         else {
  580.           *d++ = (char) c;
  581.         }
  582.         exp -= decimal_point;
  583.       }
  584.     }
  585.     else if (c == (unsigned int) dpchar && !decimal_point) {    /* INTERNATIONAL */
  586.       decimal_point = 1;
  587.     }
  588.     else {
  589.       break;
  590.     }
  591.     c = *s++;
  592.   }
  593.   /* strtod cant return until it finds the end of the exponent */
  594.   if (d == digits) {
  595.     return 0.0;
  596.   }
  597.   if (c == 'e'-'0' || c == 'E'-'0') {
  598.     register unsigned negate_exp = 0;
  599.     register int e = 0;
  600.     c = *s++;
  601.     if (c == '+' || c == ' ') {
  602.       c = *s++;
  603.     }
  604.     else if (c == '-') {
  605.       negate_exp = 1;
  606.       c = *s++;
  607.     }
  608.     if (c -= '0', c < 10) {
  609.       do {
  610.         if (e <= 340) 
  611.           e = e * 10 + (int)c;
  612.         else break;
  613.         c = *s++;
  614.       }
  615.       while (c -= '0', c < 10);
  616.       if (negate_exp) {
  617.         e = -e;
  618.       }
  619.       if (e < -340 || e > 340) 
  620.         exp = e;
  621.       else 
  622.         exp += e;
  623.     }
  624.   }
  625.  
  626.   if (exp < -340) {
  627.     x = 0;
  628.   }
  629.   else if (exp > 308) {
  630.     x = numeric_limits<double>::infinity();
  631.   }
  632.   else {
  633.     /* let _Stl_atod diagnose under- and over-flows */
  634.     /* if the input was == 0.0, we have already returned,
  635.        so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
  636.     */
  637.     x = _Stl_atod (digits, (int)(d - digits), exp);
  638.   }
  639.   if (Negate) {
  640.     x = -x;
  641.   }
  642.   return x;
  643. }
  644.  
  645.  
  646. #ifndef _STLP_NO_LONG_DOUBLE
  647. /*
  648.  * __string_to_long_double is just lifted from atold, the difference being
  649.  * that we just use '.' for the decimal point, rather than let it
  650.  * be taken from the current C locale, which of course is not accessible
  651.  * to us.
  652.  */
  653.  
  654. long double 
  655. _Stl_string_to_long_double(const char * s) {
  656.   const int max_digits = 34;
  657.   register unsigned c;
  658.   register unsigned Negate, decimal_point;
  659.   register char *d;
  660.   register int exp;
  661.   long double x;
  662.   register int dpchar;
  663.   char digits[max_digits];
  664.  
  665.   const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
  666.   while (c = *s++, ct.is(ctype_base::space, char(c)))
  667.     ;
  668.  
  669.   /* process sign */
  670.   Negate = 0;
  671.   if (c == '+') {
  672.     c = *s++;
  673.   }
  674.   else if (c == '-') {
  675.     Negate = 1;
  676.     c = *s++;
  677.   }
  678.  
  679.   d = digits;
  680.   dpchar = '.' -'0';
  681.   decimal_point = 0;
  682.   exp = 0;
  683.  
  684.   for (;;) {
  685.     c -= '0';
  686.     if (c < 10) {
  687.       if (d == digits+max_digits) {
  688.         /* ignore more than 34 digits, but adjust exponent */
  689.         exp += (decimal_point ^ 1);
  690.       }
  691.       else {
  692.         if (c == 0 && d == digits) {
  693.           /* ignore leading zeros */
  694.           ;
  695.         }
  696.         else {
  697.           *d++ = c;
  698.         }
  699.         exp -= decimal_point;
  700.       }
  701.     }
  702.     else if (c == dpchar && !decimal_point) {    /* INTERNATIONAL */
  703.       decimal_point = 1;
  704.     }
  705.     else {
  706.       break;
  707.     }
  708.     c = *s++;
  709.   } /* for */
  710.  
  711.   if (d == digits) {
  712.     return 0.0L;
  713.   }
  714.   if (c == 'e'-'0' || c == 'E'-'0') {
  715.     register unsigned negate_exp = 0;
  716.     register int e = 0;
  717.     c = *s++;
  718.     if (c == '+' || c == ' ') {
  719.       c = *s++;
  720.     }
  721.     else if (c == '-') {
  722.       negate_exp = 1;
  723.       c = *s++;
  724.     }
  725.     if (c -= '0', c < 10) {
  726.       do {
  727.         if (e <= 340) 
  728.           e = e * 10 + c;
  729.         else break;
  730.         c = *s++;
  731.       }
  732.       while (c -= '0', c < 10);
  733.       if (negate_exp) {
  734.         e = -e;
  735.       }
  736.       if (e < -(323+max_digits) || e > 308) 
  737.         exp = e;
  738.       else 
  739.         exp += e;
  740.     }
  741.   }
  742.  
  743.  
  744.   if (exp < -(324+max_digits)) {
  745.     x = 0;
  746.   }
  747.   else if (exp > 308) {
  748.     x =  numeric_limits<long double>::infinity();
  749.   }
  750.   else {
  751.     /* let _Stl_atod diagnose under- and over-flows */
  752.     /* if the input was == 0.0, we have already returned,
  753.            so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
  754.         */
  755.  
  756.     //    x = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
  757.     double tmp = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
  758.     x = tmp == numeric_limits<double>::infinity()
  759.       ? numeric_limits<long double>::infinity()
  760.       : tmp;
  761.   }
  762.  
  763.   if (Negate) {
  764.     x = -x;
  765.   }
  766.  
  767.   return x;
  768. }
  769. #endif
  770.  
  771. void  _STLP_CALL
  772. __string_to_float(const string& v, float& val) {
  773.     val = _Stl_string_to_double(v.data());
  774. }
  775.  
  776. void  _STLP_CALL
  777. __string_to_float(const string& v, double& val) {
  778.     val = _Stl_string_to_double(v.data());
  779. }
  780.  
  781. #ifndef _STLP_NO_LONG_DOUBLE
  782. void  _STLP_CALL
  783. __string_to_float(const string& v, long double& val) {
  784.     val = _Stl_string_to_long_double(v.data());
  785. }
  786. #endif
  787.  
  788. _STLP_END_NAMESPACE
  789.  
  790. // Local Variables:
  791. // mode:C++
  792. // End:
  793.