PageRenderTime 114ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 1ms

/stlport/src/num_get_float.cpp

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