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