num_get_float.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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) && !defined (__hpux)) || \
  23. defined (__DMC__)
  24. # include <stdint.h>
  25. #endif
  26. #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
  27. defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
  28. # if defined (__BORLANDC__)
  29. typedef unsigned int uint32_t;
  30. typedef unsigned __int64 uint64_t;
  31. # endif
  32. union _ll {
  33. uint64_t i64;
  34. struct {
  35. # if defined (_STLP_BIG_ENDIAN)
  36. uint32_t hi;
  37. uint32_t lo;
  38. # elif defined (_STLP_LITTLE_ENDIAN)
  39. uint32_t lo;
  40. uint32_t hi;
  41. # else
  42. # error Unknown endianess
  43. # endif
  44. } i32;
  45. };
  46. # if defined (__linux__)
  47. # include <ieee754.h>
  48. # else
  49. union ieee854_long_double {
  50. long double d;
  51. /* This is the IEEE 854 double-extended-precision format. */
  52. struct {
  53. unsigned int mantissa1:32;
  54. unsigned int mantissa0:32;
  55. unsigned int exponent:15;
  56. unsigned int negative:1;
  57. unsigned int empty:16;
  58. } ieee;
  59. };
  60. # define IEEE854_LONG_DOUBLE_BIAS 0x3fff
  61. # endif
  62. #endif
  63. _STLP_BEGIN_NAMESPACE
  64. _STLP_MOVE_TO_PRIV_NAMESPACE
  65. //----------------------------------------------------------------------
  66. // num_get
  67. // Helper functions for _M_do_get_float.
  68. #if !defined (_STLP_NO_WCHAR_T)
  69. void _STLP_CALL
  70. _Initialize_get_float( const ctype<wchar_t>& ct,
  71. wchar_t& Plus, wchar_t& Minus,
  72. wchar_t& pow_e, wchar_t& pow_E,
  73. wchar_t* digits) {
  74. char ndigits[11] = "0123456789";
  75. Plus = ct.widen('+');
  76. Minus = ct.widen('-');
  77. pow_e = ct.widen('e');
  78. pow_E = ct.widen('E');
  79. ct.widen(ndigits + 0, ndigits + 10, digits);
  80. }
  81. #endif /* WCHAR_T */
  82. /*
  83. * __string_to_double is just lifted from atof, the difference being
  84. * that we just use '.' for the decimal point, rather than let it
  85. * be taken from the current C locale, which of course is not accessible
  86. * to us.
  87. */
  88. #if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
  89. typedef unsigned long uint32;
  90. typedef unsigned __int64 uint64;
  91. # define ULL(x) x##Ui64
  92. #elif defined (__unix) || defined (__MINGW32__) || \
  93. (defined (__DMC__) && (__LONGLONG)) || defined (__WATCOMC__)
  94. typedef uint32_t uint32;
  95. typedef uint64_t uint64;
  96. # define ULL(x) x##ULL
  97. #else
  98. # error There should be some unsigned 64-bit integer on the system!
  99. #endif
  100. // Multiplication of two 64-bit integers, giving a 128-bit result.
  101. // Taken from Algorithm M in Knuth section 4.3.1, with the loop
  102. // hand-unrolled.
  103. static void _Stl_mult64(const uint64 u, const uint64 v,
  104. uint64& high, uint64& low) {
  105. const uint64 low_mask = ULL(0xffffffff);
  106. const uint64 u0 = u & low_mask;
  107. const uint64 u1 = u >> 32;
  108. const uint64 v0 = v & low_mask;
  109. const uint64 v1 = v >> 32;
  110. uint64 t = u0 * v0;
  111. low = t & low_mask;
  112. t = u1 * v0 + (t >> 32);
  113. uint64 w1 = t & low_mask;
  114. uint64 w2 = t >> 32;
  115. uint64 x = u0 * v1 + w1;
  116. low += (x & low_mask) << 32;
  117. high = u1 * v1 + w2 + (x >> 32);
  118. }
  119. #ifndef __linux__
  120. # define bit11 ULL(0x7ff)
  121. # define exponent_mask (bit11 << 52)
  122. # if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
  123. (!defined (__CYGWIN__) && !defined (__MINGW32__))
  124. //Generate bad code when compiled with -O2 option.
  125. inline
  126. # endif
  127. void _Stl_set_exponent(uint64 &val, uint64 exp)
  128. { val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
  129. #endif // __linux__
  130. /* Power of ten fractions for tenscale*/
  131. /* The constants are factored so that at most two constants
  132. * and two multiplies are needed. Furthermore, one of the constants
  133. * is represented exactly - 10**n where 1<= n <= 27.
  134. */
  135. static const uint64 _Stl_tenpow[80] = {
  136. ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
  137. ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
  138. ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
  139. ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
  140. ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
  141. ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
  142. ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
  143. ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
  144. ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
  145. ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
  146. ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
  147. ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
  148. ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
  149. ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
  150. ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
  151. ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
  152. ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
  153. ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
  154. ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
  155. ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
  156. ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
  157. ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
  158. ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
  159. ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
  160. ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
  161. ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
  162. ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
  163. ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
  164. ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
  165. ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
  166. ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
  167. ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
  168. ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
  169. ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
  170. ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
  171. ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
  172. ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
  173. // /* _Stl_tenpow[36]=(10**335)/(2**) */
  174. // /* _Stl_tenpow[36]=(10**335)/(2**) */
  175. ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
  176. ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
  177. ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
  178. ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
  179. ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
  180. ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
  181. ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
  182. ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
  183. ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
  184. ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
  185. ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
  186. ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
  187. ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
  188. };
  189. static const short _Stl_twoexp[80] = {
  190. 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,
  191. 183,276,369,462,555,648,741,834,927,1020,
  192. -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
  193. };
  194. #define TEN_1 0 /* offset to 10 ** 1 */
  195. #define TEN_27 26 /* offset to 10 ** 27 */
  196. #define TEN_M28 37 /* offset to 10 ** -28 */
  197. #define NUM_HI_P 11
  198. #define NUM_HI_N 13
  199. #define _Stl_HIBITULL (ULL(1) << 63)
  200. static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
  201. norm = 0;
  202. if ((prodhi & _Stl_HIBITULL) == 0) {
  203. /* leading bit is a zero
  204. * may have to normalize
  205. */
  206. if ((prodhi == ~_Stl_HIBITULL) &&
  207. ((prodlo >> 62) == 0x3)) { /* normalization followed by round
  208. * would cause carry to create
  209. * extra bit, so don't normalize
  210. */
  211. p = _Stl_HIBITULL;
  212. return;
  213. }
  214. p = (prodhi << 1) | (prodlo >> 63); /* normalize */
  215. norm = 1;
  216. prodlo <<= 1;
  217. }
  218. else {
  219. p = prodhi;
  220. }
  221. if ((prodlo & _Stl_HIBITULL) != 0) { /* first guard bit a one */
  222. if (((p & 0x1) != 0) ||
  223. prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */
  224. /* round */
  225. ++p;
  226. if (p == 0)
  227. ++p;
  228. }
  229. }
  230. }
  231. // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
  232. // p: 64-bit fraction
  233. // exp: base-10 exponent
  234. // bexp: base-2 exponent (output parameter)
  235. static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
  236. bexp = 0;
  237. if ( exp == 0 ) { /* no scaling needed */
  238. return;
  239. }
  240. int exp_hi = 0, exp_lo = exp; /* exp = exp_hi*32 + exp_lo */
  241. int tlo = TEN_1, thi; /* offsets in power of ten table */
  242. int num_hi; /* number of high exponent powers */
  243. if (exp > 0) { /* split exponent */
  244. if (exp_lo > 27) {
  245. exp_lo++;
  246. while (exp_lo > 27) {
  247. exp_hi++;
  248. exp_lo -= 28;
  249. }
  250. }
  251. thi = TEN_27;
  252. num_hi = NUM_HI_P;
  253. } else { // exp < 0
  254. while (exp_lo < 0) {
  255. exp_hi++;
  256. exp_lo += 28;
  257. }
  258. thi = TEN_M28;
  259. num_hi = NUM_HI_N;
  260. }
  261. uint64 prodhi, prodlo; /* 128b product */
  262. int norm; /* number of bits of normalization */
  263. int hi, lo; /* offsets in power of ten table */
  264. while (exp_hi) { /* scale */
  265. hi = (min) (exp_hi, num_hi); /* only a few large powers of 10 */
  266. exp_hi -= hi; /* could iterate in extreme case */
  267. hi += thi-1;
  268. _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
  269. _Stl_norm_and_round(p, norm, prodhi, prodlo);
  270. bexp += _Stl_twoexp[hi] - norm;
  271. }
  272. if (exp_lo) {
  273. lo = tlo + exp_lo -1;
  274. _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
  275. _Stl_norm_and_round(p, norm, prodhi, prodlo);
  276. bexp += _Stl_twoexp[lo] - norm;
  277. }
  278. return;
  279. }
  280. // First argument is a buffer of values from 0 to 9, NOT ascii.
  281. // Second argument is number of digits in buffer, 1 <= digits <= 17.
  282. // Third argument is base-10 exponent.
  283. /* IEEE representation */
  284. #if !defined (__linux__)
  285. union _Double_rep {
  286. uint64 ival;
  287. double val;
  288. };
  289. static double _Stl_atod(char *buffer, ptrdiff_t ndigit, int dexp) {
  290. typedef numeric_limits<double> limits;
  291. _Double_rep drep;
  292. uint64 &value = drep.ival; /* Value develops as follows:
  293. * 1) decimal digits as an integer
  294. * 2) left adjusted fraction
  295. * 3) right adjusted fraction
  296. * 4) exponent and fraction
  297. */
  298. uint32 guard; /* First guard bit */
  299. uint64 rest; /* Remaining guard bits */
  300. int bexp; /* binary exponent */
  301. int nzero; /* number of non-zero bits */
  302. int sexp; /* scaling exponent */
  303. char *bufferend; /* pointer to char after last digit */
  304. /* Convert the decimal digits to a binary integer. */
  305. bufferend = buffer + ndigit;
  306. value = 0;
  307. while (buffer < bufferend) {
  308. value *= 10;
  309. value += *buffer++;
  310. }
  311. /* Check for zero and treat it as a special case */
  312. if (value == 0) {
  313. return 0.0;
  314. }
  315. /* Normalize value */
  316. bexp = 64; /* convert from 64b int to fraction */
  317. /* Count number of non-zeroes in value */
  318. nzero = 0;
  319. if ((value >> 32) != 0) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
  320. if ((value >> (16 + nzero)) != 0) { nzero += 16; }
  321. if ((value >> ( 8 + nzero)) != 0) { nzero += 8; }
  322. if ((value >> ( 4 + nzero)) != 0) { nzero += 4; }
  323. if ((value >> ( 2 + nzero)) != 0) { nzero += 2; }
  324. if ((value >> ( 1 + nzero)) != 0) { nzero += 1; }
  325. if ((value >> ( nzero)) != 0) { nzero += 1; }
  326. /* Normalize */
  327. value <<= /*(uint64)*/ (64 - nzero); //*TY 03/25/2000 - removed extraneous cast to uint64
  328. bexp -= 64 - nzero;
  329. /* At this point we have a 64b fraction and a binary exponent
  330. * but have yet to incorporate the decimal exponent.
  331. */
  332. /* multiply by 10^dexp */
  333. _Stl_tenscale(value, dexp, sexp);
  334. bexp += sexp;
  335. if (bexp <= -1022) { /* HI denorm or underflow */
  336. bexp += 1022;
  337. if (bexp < -53) { /* guaranteed underflow */
  338. value = 0;
  339. }
  340. else { /* denorm or possible underflow */
  341. int lead0 = 12 - bexp; /* 12 sign and exponent bits */
  342. /* we must special case right shifts of more than 63 */
  343. if (lead0 > 64) {
  344. rest = value;
  345. guard = 0;
  346. value = 0;
  347. }
  348. else if (lead0 == 64) {
  349. rest = value & ((ULL(1)<< 63)-1);
  350. guard = (uint32) ((value>> 63) & 1 );
  351. value = 0;
  352. }
  353. else {
  354. rest = value & (((ULL(1) << lead0)-1)-1);
  355. guard = (uint32) (((value>> lead0)-1) & 1);
  356. value >>= /*(uint64)*/ lead0; /* exponent is zero */
  357. }
  358. /* Round */
  359. if (guard && ((value & 1) || rest) ) {
  360. ++value;
  361. if (value == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
  362. value = 0;
  363. _Stl_set_exponent(value, 1);
  364. }
  365. }
  366. }
  367. }
  368. else { /* not zero or denorm */
  369. /* Round to 53 bits */
  370. rest = value & ((1 << 10) - 1);
  371. value >>= 10;
  372. guard = (uint32) value & 1;
  373. value >>= 1;
  374. /* value&1 guard rest Action
  375. *
  376. * dc 0 dc none
  377. * 1 1 dc round
  378. * 0 1 0 none
  379. * 0 1 !=0 round
  380. */
  381. if (guard) {
  382. if (((value&1)!=0) || (rest!=0)) {
  383. ++value; /* round */
  384. if ((value >> 53) != 0) { /* carry all the way across */
  385. value >>= 1; /* renormalize */
  386. ++bexp;
  387. }
  388. }
  389. }
  390. /*
  391. * Check for overflow
  392. * IEEE Double Precision Format
  393. * (From Table 7-8 of Kane and Heinrich)
  394. *
  395. * Fraction bits 52
  396. * Emax +1023
  397. * Emin -1022
  398. * Exponent bias +1023
  399. * Exponent bits 11
  400. * Integer bit hidden
  401. * Total width in bits 64
  402. */
  403. if (bexp > limits::max_exponent) { /* overflow */
  404. return limits::infinity();
  405. }
  406. else { /* value is normal */
  407. value &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */
  408. _Stl_set_exponent(value, bexp + 1022); /* add bias */
  409. }
  410. }
  411. _STLP_STATIC_ASSERT(sizeof(uint64) >= sizeof(double))
  412. return drep.val;
  413. }
  414. #endif
  415. #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
  416. defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
  417. template <class D, class IEEE, int M, int BIAS>
  418. D _Stl_atodT(char *buffer, ptrdiff_t ndigit, int dexp)
  419. {
  420. typedef numeric_limits<D> limits;
  421. /* Convert the decimal digits to a binary integer. */
  422. char *bufferend = buffer + ndigit; /* pointer to char after last digit */
  423. _ll vv;
  424. vv.i64 = 0L;
  425. while ( buffer < bufferend ) {
  426. vv.i64 *= 10;
  427. vv.i64 += *buffer++;
  428. }
  429. if ( vv.i64 == ULL(0) ) { /* Check for zero and treat it as a special case */
  430. return D(0.0);
  431. }
  432. /* Normalize value */
  433. int bexp = 64; /* convert from 64b int to fraction */
  434. /* Count number of non-zeroes in value */
  435. int nzero = 0;
  436. if ((vv.i64 >> 32) != 0) { nzero = 32; }
  437. if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; }
  438. if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero += 8; }
  439. if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero += 4; }
  440. if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero += 2; }
  441. if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero += 1; }
  442. if ((vv.i64 >> ( nzero)) != 0) { nzero += 1; }
  443. /* Normalize */
  444. nzero = 64 - nzero;
  445. vv.i64 <<= nzero; // * TY 03/25/2000 - removed extraneous cast to uint64
  446. bexp -= nzero;
  447. /* At this point we have a 64b fraction and a binary exponent
  448. * but have yet to incorporate the decimal exponent.
  449. */
  450. /* multiply by 10^dexp */
  451. int sexp;
  452. _Stl_tenscale(vv.i64, dexp, sexp);
  453. bexp += sexp;
  454. if ( bexp >= limits::min_exponent ) { /* not zero or denorm */
  455. if ( limits::digits < 64 ) {
  456. /* Round to (64 - M + 1) bits */
  457. uint64_t rest = vv.i64 & ((~ULL(0) / ULL(2)) >> (limits::digits - 1));
  458. vv.i64 >>= M - 2;
  459. uint32_t guard = (uint32) vv.i64 & 1;
  460. vv.i64 >>= 1;
  461. /* value&1 guard rest Action
  462. *
  463. * dc 0 dc none
  464. * 1 1 dc round
  465. * 0 1 0 none
  466. * 0 1 !=0 round
  467. */
  468. if (guard) {
  469. if ( ((vv.i64 & 1) != 0) || (rest != 0) ) {
  470. vv.i64++; /* round */
  471. if ( (vv.i64 >> (limits::digits < 64 ? limits::digits : 0)) != 0 ) { /* carry all the way across */
  472. vv.i64 >>= 1; /* renormalize */
  473. ++bexp;
  474. }
  475. }
  476. }
  477. vv.i64 &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */
  478. }
  479. /*
  480. * Check for overflow
  481. * IEEE Double Precision Format
  482. * (From Table 7-8 of Kane and Heinrich)
  483. *
  484. * Fraction bits 52
  485. * Emax +1023
  486. * Emin -1022
  487. * Exponent bias +1023
  488. * Exponent bits 11
  489. * Integer bit hidden
  490. * Total width in bits 64
  491. */
  492. if (bexp > limits::max_exponent) { /* overflow */
  493. return limits::infinity();
  494. }
  495. /* value is normal */
  496. IEEE v;
  497. v.ieee.mantissa0 = vv.i32.hi;
  498. v.ieee.mantissa1 = vv.i32.lo;
  499. v.ieee.negative = 0;
  500. v.ieee.exponent = bexp + BIAS - 1;
  501. return v.d;
  502. }
  503. /* HI denorm or underflow */
  504. bexp += BIAS - 1;
  505. if (bexp < -limits::digits) { /* guaranteed underflow */
  506. vv.i64 = 0;
  507. } else { /* denorm or possible underflow */
  508. /*
  509. * Problem point for long double: looks like this code reflect shareing of mantissa
  510. * and exponent in 64b int; not so for long double
  511. */
  512. int lead0 = M - bexp; /* M = 12 sign and exponent bits */
  513. uint64_t rest;
  514. uint32_t guard;
  515. /* we must special case right shifts of more than 63 */
  516. if (lead0 > 64) {
  517. rest = vv.i64;
  518. guard = 0;
  519. vv.i64 = 0;
  520. } else if (lead0 == 64) {
  521. rest = vv.i64 & ((ULL(1) << 63)-1);
  522. guard = (uint32) ((vv.i64 >> 63) & 1 );
  523. vv.i64 = 0;
  524. } else {
  525. rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
  526. guard = (uint32) (((vv.i64 >> lead0)-1) & 1);
  527. vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */
  528. }
  529. /* Round */
  530. if (guard && ( (vv.i64 & 1) || rest)) {
  531. vv.i64++;
  532. if (vv.i64 == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
  533. IEEE v;
  534. v.ieee.mantissa0 = 0;
  535. v.ieee.mantissa1 = 0;
  536. v.ieee.negative = 0;
  537. v.ieee.exponent = 1;
  538. return v.d;
  539. }
  540. }
  541. }
  542. IEEE v;
  543. v.ieee.mantissa0 = vv.i32.hi;
  544. v.ieee.mantissa1 = vv.i32.lo;
  545. v.ieee.negative = 0;
  546. v.ieee.exponent = 0;
  547. return v.d;
  548. }
  549. #endif // __linux__
  550. #ifndef __linux__
  551. static double _Stl_string_to_double(const char *s) {
  552. typedef numeric_limits<double> limits;
  553. const int max_digits = limits::digits10 + 2;
  554. unsigned c;
  555. unsigned Negate, decimal_point;
  556. char *d;
  557. int exp;
  558. int dpchar;
  559. char digits[max_digits];
  560. c = *s++;
  561. /* process sign */
  562. Negate = 0;
  563. if (c == '+') {
  564. c = *s++;
  565. } else if (c == '-') {
  566. Negate = 1;
  567. c = *s++;
  568. }
  569. d = digits;
  570. dpchar = '.' - '0';
  571. decimal_point = 0;
  572. exp = 0;
  573. for (;;) {
  574. c -= '0';
  575. if (c < 10) {
  576. if (d == digits + max_digits) {
  577. /* ignore more than max_digits digits, but adjust exponent */
  578. exp += (decimal_point ^ 1);
  579. } else {
  580. if (c == 0 && d == digits) {
  581. /* ignore leading zeros */
  582. } else {
  583. *d++ = (char) c;
  584. }
  585. exp -= decimal_point;
  586. }
  587. } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
  588. decimal_point = 1;
  589. } else {
  590. break;
  591. }
  592. c = *s++;
  593. }
  594. /* strtod cant return until it finds the end of the exponent */
  595. if (d == digits) {
  596. return 0.0;
  597. }
  598. if (c == 'e' - '0' || c == 'E' - '0') {
  599. register unsigned negate_exp = 0;
  600. register int e = 0;
  601. c = *s++;
  602. if (c == '+' || c == ' ') {
  603. c = *s++;
  604. } else if (c == '-') {
  605. negate_exp = 1;
  606. c = *s++;
  607. }
  608. if (c -= '0', c < 10) {
  609. do {
  610. e = e * 10 + (int)c;
  611. c = *s++;
  612. } while (c -= '0', c < 10);
  613. if (negate_exp) {
  614. e = -e;
  615. }
  616. exp += e;
  617. }
  618. }
  619. double x;
  620. ptrdiff_t n = d - digits;
  621. if ((exp + n - 1) < limits::min_exponent10) {
  622. x = 0;
  623. }
  624. else if ((exp + n - 1) > limits::max_exponent10) {
  625. x = limits::infinity();
  626. }
  627. else {
  628. /* Let _Stl_atod diagnose under- and over-flows.
  629. * If the input was == 0.0, we have already returned,
  630. * so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW */
  631. x = _Stl_atod(digits, n, exp);
  632. }
  633. if (Negate) {
  634. x = -x;
  635. }
  636. return x;
  637. }
  638. #endif
  639. #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
  640. defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
  641. template <class D, class IEEE, int M, int BIAS>
  642. D _Stl_string_to_doubleT(const char *s)
  643. {
  644. typedef numeric_limits<D> limits;
  645. const int max_digits = limits::digits10; /* + 2 17 */;
  646. unsigned c;
  647. unsigned decimal_point;
  648. char *d;
  649. int exp;
  650. D x;
  651. int dpchar;
  652. char digits[max_digits];
  653. c = *s++;
  654. /* process sign */
  655. bool Negate = false;
  656. if (c == '+') {
  657. c = *s++;
  658. } else if (c == '-') {
  659. Negate = true;
  660. c = *s++;
  661. }
  662. d = digits;
  663. dpchar = '.' - '0';
  664. decimal_point = 0;
  665. exp = 0;
  666. for (;;) {
  667. c -= '0';
  668. if (c < 10) {
  669. if (d == digits + max_digits) {
  670. /* ignore more than max_digits digits, but adjust exponent */
  671. exp += (decimal_point ^ 1);
  672. } else {
  673. if (c == 0 && d == digits) {
  674. /* ignore leading zeros */
  675. } else {
  676. *d++ = (char) c;
  677. }
  678. exp -= decimal_point;
  679. }
  680. } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
  681. decimal_point = 1;
  682. } else {
  683. break;
  684. }
  685. c = *s++;
  686. }
  687. /* strtod cant return until it finds the end of the exponent */
  688. if (d == digits) {
  689. return D(0.0);
  690. }
  691. if (c == 'e'-'0' || c == 'E'-'0') {
  692. bool negate_exp = false;
  693. register int e = 0;
  694. c = *s++;
  695. if (c == '+' || c == ' ') {
  696. c = *s++;
  697. } else if (c == '-') {
  698. negate_exp = true;
  699. c = *s++;
  700. }
  701. if (c -= '0', c < 10) {
  702. do {
  703. e = e * 10 + (int)c;
  704. c = *s++;
  705. } while (c -= '0', c < 10);
  706. if (negate_exp) {
  707. e = -e;
  708. }
  709. exp += e;
  710. }
  711. }
  712. ptrdiff_t n = d - digits;
  713. if ((exp + n - 1) < limits::min_exponent10) {
  714. return D(0.0); // +0.0 is the same as -0.0
  715. } else if ((exp + n - 1) > limits::max_exponent10 ) {
  716. // not good, because of x = -x below; this may lead to portability problems
  717. x = limits::infinity();
  718. } else {
  719. /* let _Stl_atod diagnose under- and over-flows */
  720. /* if the input was == 0.0, we have already returned,
  721. so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
  722. */
  723. x = _Stl_atodT<D,IEEE,M,BIAS>(digits, n, exp);
  724. }
  725. return Negate ? -x : x;
  726. }
  727. #endif // __linux__
  728. void _STLP_CALL
  729. __string_to_float(const __iostring& v, float& val)
  730. {
  731. #if !defined (__linux__)
  732. val = (float)_Stl_string_to_double(v.c_str());
  733. #else
  734. val = (float)_Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
  735. #endif
  736. }
  737. void _STLP_CALL
  738. __string_to_float(const __iostring& v, double& val)
  739. {
  740. #if !defined (__linux__)
  741. val = _Stl_string_to_double(v.c_str());
  742. #else
  743. val = _Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
  744. #endif
  745. }
  746. #if !defined (_STLP_NO_LONG_DOUBLE)
  747. void _STLP_CALL
  748. __string_to_float(const __iostring& v, long double& val) {
  749. #if !defined (__linux__) && !defined (__MINGW32__) && !defined (__CYGWIN__) && \
  750. !defined (__BORLANDC__) && !defined (__DMC__) && !defined (__HP_aCC)
  751. //The following function is valid only if long double is an alias for double.
  752. _STLP_STATIC_ASSERT( sizeof(long double) <= sizeof(double) )
  753. val = _Stl_string_to_double(v.c_str());
  754. #else
  755. val = _Stl_string_to_doubleT<long double,ieee854_long_double,16,IEEE854_LONG_DOUBLE_BIAS>(v.c_str());
  756. #endif
  757. }
  758. #endif
  759. _STLP_MOVE_TO_STD_NAMESPACE
  760. _STLP_END_NAMESPACE
  761. // Local Variables:
  762. // mode:C++
  763. // End: