PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gameedit/CellGameEdit/CellSetResourceCPP/STLport/src/num_get_float.cpp

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