PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/project/jni/third_party/stlport/src/num_get_float.cpp

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