dtoa.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright 2010-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "bx_p.h"
  6. #include <bx/cpu.h>
  7. #include <bx/math.h>
  8. #include <bx/string.h>
  9. #include <bx/uint32_t.h>
  10. #include <type_traits>
  11. namespace bx
  12. {
  13. /*
  14. * https://github.com/miloyip/dtoa-benchmark
  15. *
  16. * Copyright (C) 2014 Milo Yip
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this software and associated documentation files (the "Software"), to deal
  20. * in the Software without restriction, including without limitation the rights
  21. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. * copies of the Software, and to permit persons to whom the Software is
  23. * furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34. * THE SOFTWARE.
  35. *
  36. */
  37. struct DiyFp
  38. {
  39. DiyFp()
  40. {
  41. }
  42. DiyFp(uint64_t _f, int32_t _e)
  43. : f(_f)
  44. , e(_e)
  45. {
  46. }
  47. DiyFp(double d)
  48. {
  49. union
  50. {
  51. double d;
  52. uint64_t u64;
  53. } u = { d };
  54. int32_t biased_e = (u.u64 & kDpExponentMask) >> kDpSignificandSize;
  55. uint64_t significand = (u.u64 & kDpSignificandMask);
  56. if (biased_e != 0)
  57. {
  58. f = significand + kDpHiddenBit;
  59. e = biased_e - kDpExponentBias;
  60. }
  61. else
  62. {
  63. f = significand;
  64. e = kDpMinExponent + 1;
  65. }
  66. }
  67. DiyFp operator-(const DiyFp& rhs) const
  68. {
  69. BX_CHECK(e == rhs.e, "");
  70. BX_CHECK(f >= rhs.f, "");
  71. return DiyFp(f - rhs.f, e);
  72. }
  73. DiyFp operator*(const DiyFp& rhs) const
  74. {
  75. const uint64_t M32 = UINT32_MAX;
  76. const uint64_t a = f >> 32;
  77. const uint64_t b = f & M32;
  78. const uint64_t c = rhs.f >> 32;
  79. const uint64_t d = rhs.f & M32;
  80. const uint64_t ac = a * c;
  81. const uint64_t bc = b * c;
  82. const uint64_t ad = a * d;
  83. const uint64_t bd = b * d;
  84. uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
  85. tmp += 1U << 31; /// mult_round
  86. return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64);
  87. }
  88. DiyFp Normalize() const
  89. {
  90. uint32_t s = uint64_cntlz(f);
  91. return DiyFp(f << s, e - s);
  92. }
  93. DiyFp NormalizeBoundary() const
  94. {
  95. uint32_t index = uint64_cntlz(f);
  96. return DiyFp (f << index, e - index);
  97. }
  98. void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const
  99. {
  100. DiyFp pl = DiyFp( (f << 1) + 1, e - 1).NormalizeBoundary();
  101. DiyFp mi = (f == kDpHiddenBit) ? DiyFp( (f << 2) - 1, e - 2) : DiyFp( (f << 1) - 1, e - 1);
  102. mi.f <<= mi.e - pl.e;
  103. mi.e = pl.e;
  104. *plus = pl;
  105. *minus = mi;
  106. }
  107. #define UINT64_C2(h, l) ( (static_cast<uint64_t>(h) << 32) | static_cast<uint64_t>(l) )
  108. static const int32_t kDiySignificandSize = 64;
  109. static const int32_t kDpSignificandSize = 52;
  110. static const int32_t kDpExponentBias = 0x3FF + kDpSignificandSize;
  111. static const int32_t kDpMinExponent = -kDpExponentBias;
  112. static const uint64_t kDpExponentMask = UINT64_C2(0x7FF00000, 0x00000000);
  113. static const uint64_t kDpSignificandMask = UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
  114. static const uint64_t kDpHiddenBit = UINT64_C2(0x00100000, 0x00000000);
  115. uint64_t f;
  116. int32_t e;
  117. };
  118. // 10^-348, 10^-340, ..., 10^340
  119. static const uint64_t s_kCachedPowers_F[] =
  120. {
  121. UINT64_C2(0xfa8fd5a0, 0x081c0288), UINT64_C2(0xbaaee17f, 0xa23ebf76),
  122. UINT64_C2(0x8b16fb20, 0x3055ac76), UINT64_C2(0xcf42894a, 0x5dce35ea),
  123. UINT64_C2(0x9a6bb0aa, 0x55653b2d), UINT64_C2(0xe61acf03, 0x3d1a45df),
  124. UINT64_C2(0xab70fe17, 0xc79ac6ca), UINT64_C2(0xff77b1fc, 0xbebcdc4f),
  125. UINT64_C2(0xbe5691ef, 0x416bd60c), UINT64_C2(0x8dd01fad, 0x907ffc3c),
  126. UINT64_C2(0xd3515c28, 0x31559a83), UINT64_C2(0x9d71ac8f, 0xada6c9b5),
  127. UINT64_C2(0xea9c2277, 0x23ee8bcb), UINT64_C2(0xaecc4991, 0x4078536d),
  128. UINT64_C2(0x823c1279, 0x5db6ce57), UINT64_C2(0xc2109436, 0x4dfb5637),
  129. UINT64_C2(0x9096ea6f, 0x3848984f), UINT64_C2(0xd77485cb, 0x25823ac7),
  130. UINT64_C2(0xa086cfcd, 0x97bf97f4), UINT64_C2(0xef340a98, 0x172aace5),
  131. UINT64_C2(0xb23867fb, 0x2a35b28e), UINT64_C2(0x84c8d4df, 0xd2c63f3b),
  132. UINT64_C2(0xc5dd4427, 0x1ad3cdba), UINT64_C2(0x936b9fce, 0xbb25c996),
  133. UINT64_C2(0xdbac6c24, 0x7d62a584), UINT64_C2(0xa3ab6658, 0x0d5fdaf6),
  134. UINT64_C2(0xf3e2f893, 0xdec3f126), UINT64_C2(0xb5b5ada8, 0xaaff80b8),
  135. UINT64_C2(0x87625f05, 0x6c7c4a8b), UINT64_C2(0xc9bcff60, 0x34c13053),
  136. UINT64_C2(0x964e858c, 0x91ba2655), UINT64_C2(0xdff97724, 0x70297ebd),
  137. UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), UINT64_C2(0xf8a95fcf, 0x88747d94),
  138. UINT64_C2(0xb9447093, 0x8fa89bcf), UINT64_C2(0x8a08f0f8, 0xbf0f156b),
  139. UINT64_C2(0xcdb02555, 0x653131b6), UINT64_C2(0x993fe2c6, 0xd07b7fac),
  140. UINT64_C2(0xe45c10c4, 0x2a2b3b06), UINT64_C2(0xaa242499, 0x697392d3),
  141. UINT64_C2(0xfd87b5f2, 0x8300ca0e), UINT64_C2(0xbce50864, 0x92111aeb),
  142. UINT64_C2(0x8cbccc09, 0x6f5088cc), UINT64_C2(0xd1b71758, 0xe219652c),
  143. UINT64_C2(0x9c400000, 0x00000000), UINT64_C2(0xe8d4a510, 0x00000000),
  144. UINT64_C2(0xad78ebc5, 0xac620000), UINT64_C2(0x813f3978, 0xf8940984),
  145. UINT64_C2(0xc097ce7b, 0xc90715b3), UINT64_C2(0x8f7e32ce, 0x7bea5c70),
  146. UINT64_C2(0xd5d238a4, 0xabe98068), UINT64_C2(0x9f4f2726, 0x179a2245),
  147. UINT64_C2(0xed63a231, 0xd4c4fb27), UINT64_C2(0xb0de6538, 0x8cc8ada8),
  148. UINT64_C2(0x83c7088e, 0x1aab65db), UINT64_C2(0xc45d1df9, 0x42711d9a),
  149. UINT64_C2(0x924d692c, 0xa61be758), UINT64_C2(0xda01ee64, 0x1a708dea),
  150. UINT64_C2(0xa26da399, 0x9aef774a), UINT64_C2(0xf209787b, 0xb47d6b85),
  151. UINT64_C2(0xb454e4a1, 0x79dd1877), UINT64_C2(0x865b8692, 0x5b9bc5c2),
  152. UINT64_C2(0xc83553c5, 0xc8965d3d), UINT64_C2(0x952ab45c, 0xfa97a0b3),
  153. UINT64_C2(0xde469fbd, 0x99a05fe3), UINT64_C2(0xa59bc234, 0xdb398c25),
  154. UINT64_C2(0xf6c69a72, 0xa3989f5c), UINT64_C2(0xb7dcbf53, 0x54e9bece),
  155. UINT64_C2(0x88fcf317, 0xf22241e2), UINT64_C2(0xcc20ce9b, 0xd35c78a5),
  156. UINT64_C2(0x98165af3, 0x7b2153df), UINT64_C2(0xe2a0b5dc, 0x971f303a),
  157. UINT64_C2(0xa8d9d153, 0x5ce3b396), UINT64_C2(0xfb9b7cd9, 0xa4a7443c),
  158. UINT64_C2(0xbb764c4c, 0xa7a44410), UINT64_C2(0x8bab8eef, 0xb6409c1a),
  159. UINT64_C2(0xd01fef10, 0xa657842c), UINT64_C2(0x9b10a4e5, 0xe9913129),
  160. UINT64_C2(0xe7109bfb, 0xa19c0c9d), UINT64_C2(0xac2820d9, 0x623bf429),
  161. UINT64_C2(0x80444b5e, 0x7aa7cf85), UINT64_C2(0xbf21e440, 0x03acdd2d),
  162. UINT64_C2(0x8e679c2f, 0x5e44ff8f), UINT64_C2(0xd433179d, 0x9c8cb841),
  163. UINT64_C2(0x9e19db92, 0xb4e31ba9), UINT64_C2(0xeb96bf6e, 0xbadf77d9),
  164. UINT64_C2(0xaf87023b, 0x9bf0ee6b)
  165. };
  166. static const int16_t s_kCachedPowers_E[] =
  167. {
  168. -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
  169. -954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
  170. -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
  171. -422, -396, -369, -343, -316, -289, -263, -236, -210, -183,
  172. -157, -130, -103, -77, -50, -24, 3, 30, 56, 83,
  173. 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
  174. 375, 402, 428, 455, 481, 508, 534, 561, 588, 614,
  175. 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
  176. 907, 933, 960, 986, 1013, 1039, 1066
  177. };
  178. static const char s_cDigitsLut[200] =
  179. {
  180. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
  181. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
  182. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  183. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
  184. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
  185. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  186. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
  187. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
  188. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  189. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
  190. };
  191. static const uint32_t s_kPow10[] =
  192. {
  193. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
  194. };
  195. DiyFp GetCachedPower(int32_t e, int32_t* K)
  196. {
  197. double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive
  198. int32_t k = static_cast<int32_t>(dk);
  199. if (k != dk)
  200. {
  201. k++;
  202. }
  203. uint32_t index = static_cast<uint32_t>( (k >> 3) + 1);
  204. *K = -(-348 + static_cast<int32_t>(index << 3) ); // decimal exponent no need lookup table
  205. BX_CHECK(index < sizeof(s_kCachedPowers_F) / sizeof(s_kCachedPowers_F[0]), "");
  206. return DiyFp(s_kCachedPowers_F[index], s_kCachedPowers_E[index]);
  207. }
  208. void GrisuRound(char* buffer, int32_t len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w)
  209. {
  210. while (rest < wp_w
  211. && delta - rest >= ten_kappa
  212. && (rest + ten_kappa < wp_w || wp_w - rest > rest + ten_kappa - wp_w) )
  213. {
  214. buffer[len - 1]--;
  215. rest += ten_kappa;
  216. }
  217. }
  218. uint32_t CountDecimalDigit32(uint32_t n)
  219. {
  220. // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
  221. if (n < 10) return 1;
  222. if (n < 100) return 2;
  223. if (n < 1000) return 3;
  224. if (n < 10000) return 4;
  225. if (n < 100000) return 5;
  226. if (n < 1000000) return 6;
  227. if (n < 10000000) return 7;
  228. if (n < 100000000) return 8;
  229. if (n < 1000000000) return 9;
  230. return 10;
  231. }
  232. void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int32_t* len, int32_t* K)
  233. {
  234. const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
  235. const DiyFp wp_w = Mp - W;
  236. uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
  237. uint64_t p2 = Mp.f & (one.f - 1);
  238. int32_t kappa = static_cast<int32_t>(CountDecimalDigit32(p1) );
  239. *len = 0;
  240. while (kappa > 0)
  241. {
  242. uint32_t d;
  243. switch (kappa)
  244. {
  245. case 10: d = p1 / 1000000000; p1 %= 1000000000; break;
  246. case 9: d = p1 / 100000000; p1 %= 100000000; break;
  247. case 8: d = p1 / 10000000; p1 %= 10000000; break;
  248. case 7: d = p1 / 1000000; p1 %= 1000000; break;
  249. case 6: d = p1 / 100000; p1 %= 100000; break;
  250. case 5: d = p1 / 10000; p1 %= 10000; break;
  251. case 4: d = p1 / 1000; p1 %= 1000; break;
  252. case 3: d = p1 / 100; p1 %= 100; break;
  253. case 2: d = p1 / 10; p1 %= 10; break;
  254. case 1: d = p1; p1 = 0; break;
  255. default:
  256. d = 0;
  257. break;
  258. }
  259. if (d || *len)
  260. {
  261. buffer[(*len)++] = '0' + static_cast<char>(d);
  262. }
  263. kappa--;
  264. uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
  265. if (tmp <= delta)
  266. {
  267. *K += kappa;
  268. GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(s_kPow10[kappa]) << -one.e, wp_w.f);
  269. return;
  270. }
  271. }
  272. // kappa = 0
  273. for (;;)
  274. {
  275. p2 *= 10;
  276. delta *= 10;
  277. char d = static_cast<char>(p2 >> -one.e);
  278. if (d || *len)
  279. {
  280. buffer[(*len)++] = '0' + d;
  281. }
  282. p2 &= one.f - 1;
  283. kappa--;
  284. if (p2 < delta)
  285. {
  286. *K += kappa;
  287. const int index = -static_cast<int>(kappa);
  288. GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? s_kPow10[-static_cast<int>(kappa)] : 0));
  289. return;
  290. }
  291. }
  292. }
  293. void Grisu2(double value, char* buffer, int32_t* length, int32_t* K)
  294. {
  295. const DiyFp v(value);
  296. DiyFp w_m, w_p;
  297. v.NormalizedBoundaries(&w_m, &w_p);
  298. const DiyFp c_mk = GetCachedPower(w_p.e, K);
  299. const DiyFp W = v.Normalize() * c_mk;
  300. DiyFp Wp = w_p * c_mk;
  301. DiyFp Wm = w_m * c_mk;
  302. Wm.f++;
  303. Wp.f--;
  304. DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
  305. }
  306. int32_t WriteExponent(int32_t K, char* buffer)
  307. {
  308. const char* ptr = buffer;
  309. if (K < 0)
  310. {
  311. *buffer++ = '-';
  312. K = -K;
  313. }
  314. if (K >= 100)
  315. {
  316. *buffer++ = '0' + static_cast<char>(K / 100);
  317. K %= 100;
  318. const char* d = s_cDigitsLut + K * 2;
  319. *buffer++ = d[0];
  320. *buffer++ = d[1];
  321. }
  322. else if (K >= 10)
  323. {
  324. const char* d = s_cDigitsLut + K * 2;
  325. *buffer++ = d[0];
  326. *buffer++ = d[1];
  327. }
  328. else
  329. {
  330. *buffer++ = '0' + static_cast<char>(K);
  331. }
  332. *buffer = '\0';
  333. return int32_t(buffer - ptr);
  334. }
  335. int32_t Prettify(char* buffer, int32_t length, int32_t k)
  336. {
  337. const int32_t kk = length + k; // 10^(kk-1) <= v < 10^kk
  338. if (length <= kk && kk <= 21)
  339. {
  340. // 1234e7 -> 12340000000
  341. for (int32_t i = length; i < kk; i++)
  342. {
  343. buffer[i] = '0';
  344. }
  345. buffer[kk] = '.';
  346. buffer[kk + 1] = '0';
  347. buffer[kk + 2] = '\0';
  348. return kk + 2;
  349. }
  350. if (0 < kk && kk <= 21)
  351. {
  352. // 1234e-2 -> 12.34
  353. memMove(&buffer[kk + 1], &buffer[kk], length - kk);
  354. buffer[kk] = '.';
  355. buffer[length + 1] = '\0';
  356. return length + 1;
  357. }
  358. if (-6 < kk && kk <= 0)
  359. {
  360. // 1234e-6 -> 0.001234
  361. const int32_t offset = 2 - kk;
  362. memMove(&buffer[offset], &buffer[0], length);
  363. buffer[0] = '0';
  364. buffer[1] = '.';
  365. for (int32_t i = 2; i < offset; i++)
  366. {
  367. buffer[i] = '0';
  368. }
  369. buffer[length + offset] = '\0';
  370. return length + offset;
  371. }
  372. if (length == 1)
  373. {
  374. // 1e30
  375. buffer[1] = 'e';
  376. int32_t exp = WriteExponent(kk - 1, &buffer[2]);
  377. return 2 + exp;
  378. }
  379. // 1234e30 -> 1.234e33
  380. memMove(&buffer[2], &buffer[1], length - 1);
  381. buffer[1] = '.';
  382. buffer[length + 1] = 'e';
  383. int32_t exp = WriteExponent(kk - 1, &buffer[length + 2]);
  384. return length + 2 + exp;
  385. }
  386. int32_t toString(char* _dst, int32_t _max, double _value)
  387. {
  388. int32_t sign = 0 != (doubleToBits(_value) & (UINT64_C(1)<<63) ) ? 1 : 0;
  389. if (1 == sign)
  390. {
  391. *_dst++ = '-';
  392. --_max;
  393. _value = -_value;
  394. }
  395. if (isNan(_value) )
  396. {
  397. return (int32_t)strCopy(_dst, _max, "nan") + sign;
  398. }
  399. else if (isInfinite(_value) )
  400. {
  401. return (int32_t)strCopy(_dst, _max, "inf") + sign;
  402. }
  403. int32_t len;
  404. if (0.0 == _value)
  405. {
  406. len = (int32_t)strCopy(_dst, _max, "0.0");
  407. }
  408. else
  409. {
  410. int32_t kk;
  411. Grisu2(_value, _dst, &len, &kk);
  412. len = Prettify(_dst, len, kk);
  413. }
  414. return len + sign;
  415. }
  416. static void reverse(char* _dst, int32_t _len)
  417. {
  418. for (int32_t ii = 0, jj = _len - 1; ii < jj; ++ii, --jj)
  419. {
  420. swap(_dst[ii], _dst[jj]);
  421. }
  422. }
  423. template<typename Ty>
  424. int32_t toStringSigned(char* _dst, int32_t _max, Ty _value, uint32_t _base, char _separator)
  425. {
  426. if (_base == 10
  427. && _value < 0)
  428. {
  429. if (_max < 1)
  430. {
  431. return 0;
  432. }
  433. _max = toString(_dst + 1
  434. , _max - 1
  435. , typename std::make_unsigned<Ty>::type(-_value)
  436. , _base
  437. , _separator
  438. );
  439. if (_max == 0)
  440. {
  441. return 0;
  442. }
  443. *_dst = '-';
  444. return int32_t(_max + 1);
  445. }
  446. return toString(_dst
  447. , _max
  448. , typename std::make_unsigned<Ty>::type(_value)
  449. , _base
  450. , _separator
  451. );
  452. }
  453. int32_t toString(char* _dst, int32_t _max, int32_t _value, uint32_t _base, char _separator)
  454. {
  455. return toStringSigned(_dst, _max, _value, _base, _separator);
  456. }
  457. int32_t toString(char* _dst, int32_t _max, int64_t _value, uint32_t _base, char _separator)
  458. {
  459. return toStringSigned(_dst, _max, _value, _base, _separator);
  460. }
  461. template<typename Ty>
  462. int32_t toStringUnsigned(char* _dst, int32_t _max, Ty _value, uint32_t _base, char _separator)
  463. {
  464. char data[32];
  465. int32_t len = 0;
  466. if (_base > 16
  467. || _base < 2)
  468. {
  469. return 0;
  470. }
  471. uint32_t count = 1;
  472. do
  473. {
  474. const Ty rem = _value % _base;
  475. _value /= _base;
  476. if (rem < 10)
  477. {
  478. data[len++] = char('0' + rem);
  479. }
  480. else
  481. {
  482. data[len++] = char('a' + rem - 10);
  483. }
  484. if ('\0' != _separator
  485. && 0 == count%3
  486. && 0 != _value)
  487. {
  488. data[len++] = _separator;
  489. }
  490. ++count;
  491. }
  492. while (0 != _value);
  493. if (_max < len + 1)
  494. {
  495. return 0;
  496. }
  497. reverse(data, len);
  498. memCopy(_dst, data, len);
  499. _dst[len] = '\0';
  500. return int32_t(len);
  501. }
  502. int32_t toString(char* _dst, int32_t _max, uint32_t _value, uint32_t _base, char _separator)
  503. {
  504. return toStringUnsigned(_dst, _max, _value, _base, _separator);
  505. }
  506. int32_t toString(char* _dst, int32_t _max, uint64_t _value, uint32_t _base, char _separator)
  507. {
  508. return toStringUnsigned(_dst, _max, _value, _base, _separator);
  509. }
  510. /*
  511. * https://github.com/grzegorz-kraszewski/stringtofloat/
  512. *
  513. * MIT License
  514. *
  515. * Copyright (c) 2016 Grzegorz Kraszewski
  516. *
  517. * Permission is hereby granted, free of charge, to any person obtaining a copy
  518. * of this software and associated documentation files (the "Software"), to deal
  519. * in the Software without restriction, including without limitation the rights
  520. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  521. * copies of the Software, and to permit persons to whom the Software is
  522. * furnished to do so, subject to the following conditions:
  523. *
  524. * The above copyright notice and this permission notice shall be included in all
  525. * copies or substantial portions of the Software.
  526. *
  527. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  528. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  529. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  530. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  531. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  532. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  533. * SOFTWARE.
  534. */
  535. /*
  536. * IMPORTANT
  537. *
  538. * The code works in "round towards zero" mode. This is different from
  539. * GCC standard library strtod(), which uses "round half to even" rule.
  540. * Therefore it cannot be used as a direct drop-in replacement, as in
  541. * some cases results will be different on the least significant bit of
  542. * mantissa. Read more in the README.md file.
  543. */
  544. #define DIGITS 18
  545. #define DOUBLE_PLUS_ZERO UINT64_C(0x0000000000000000)
  546. #define DOUBLE_MINUS_ZERO UINT64_C(0x8000000000000000)
  547. #define DOUBLE_PLUS_INFINITY UINT64_C(0x7ff0000000000000)
  548. #define DOUBLE_MINUS_INFINITY UINT64_C(0xfff0000000000000)
  549. union HexDouble
  550. {
  551. double d;
  552. uint64_t u;
  553. };
  554. #define lsr96(s2, s1, s0, d2, d1, d0) \
  555. d0 = ( (s0) >> 1) | ( ( (s1) & 1) << 31); \
  556. d1 = ( (s1) >> 1) | ( ( (s2) & 1) << 31); \
  557. d2 = (s2) >> 1;
  558. #define lsl96(s2, s1, s0, d2, d1, d0) \
  559. d2 = ( (s2) << 1) | ( ( (s1) & (1 << 31) ) >> 31); \
  560. d1 = ( (s1) << 1) | ( ( (s0) & (1 << 31) ) >> 31); \
  561. d0 = (s0) << 1;
  562. /*
  563. * Undefine the below constant if your processor or compiler is slow
  564. * at 64-bit arithmetic. This is a rare case however. 64-bit macros are
  565. * better for deeply pipelined CPUs (no conditional execution), are
  566. * very efficient for 64-bit processors and also fast on 32-bit processors
  567. * featuring extended precision arithmetic (x86, PowerPC_32, M68k and probably
  568. * more).
  569. */
  570. #define USE_64BIT_FOR_ADDSUB_MACROS 0
  571. #if USE_64BIT_FOR_ADDSUB_MACROS
  572. #define add96(s2, s1, s0, d2, d1, d0) { \
  573. uint64_t w; \
  574. w = (uint64_t)(s0) + (uint64_t)(d0); \
  575. (s0) = w; \
  576. w >>= 32; \
  577. w += (uint64_t)(s1) + (uint64_t)(d1); \
  578. (s1) = w; \
  579. w >>= 32; \
  580. w += (uint64_t)(s2) + (uint64_t)(d2); \
  581. (s2) = w; }
  582. #define sub96(s2, s1, s0, d2, d1, d0) { \
  583. uint64_t w; \
  584. w = (uint64_t)(s0) - (uint64_t)(d0); \
  585. (s0) = w; \
  586. w >>= 32; \
  587. w += (uint64_t)(s1) - (uint64_t)(d1); \
  588. (s1) = w; \
  589. w >>= 32; \
  590. w += (uint64_t)(s2) - (uint64_t)(d2); \
  591. (s2) = w; }
  592. #else
  593. #define add96(s2, s1, s0, d2, d1, d0) { \
  594. uint32_t _x, _c; \
  595. _x = (s0); (s0) += (d0); \
  596. if ( (s0) < _x) _c = 1; else _c = 0; \
  597. _x = (s1); (s1) += (d1) + _c; \
  598. if ( ( (s1) < _x) || ( ( (s1) == _x) && _c) ) _c = 1; else _c = 0; \
  599. (s2) += (d2) + _c; }
  600. #define sub96(s2, s1, s0, d2, d1, d0) { \
  601. uint32_t _x, _c; \
  602. _x = (s0); (s0) -= (d0); \
  603. if ( (s0) > _x) _c = 1; else _c = 0; \
  604. _x = (s1); (s1) -= (d1) + _c; \
  605. if ( ( (s1) > _x) || ( ( (s1) == _x) && _c) ) _c = 1; else _c = 0; \
  606. (s2) -= (d2) + _c; }
  607. #endif /* USE_64BIT_FOR_ADDSUB_MACROS */
  608. /* parser state machine states */
  609. #define FSM_A 0
  610. #define FSM_B 1
  611. #define FSM_C 2
  612. #define FSM_D 3
  613. #define FSM_E 4
  614. #define FSM_F 5
  615. #define FSM_G 6
  616. #define FSM_H 7
  617. #define FSM_I 8
  618. #define FSM_STOP 9
  619. /* The structure is filled by parser, then given to converter. */
  620. struct PrepNumber
  621. {
  622. int negative; /* 0 if positive number, 1 if negative */
  623. int32_t exponent; /* power of 10 exponent */
  624. uint64_t mantissa; /* integer mantissa */
  625. };
  626. /* Possible parser return values. */
  627. #define PARSER_OK 0 // parser finished OK
  628. #define PARSER_PZERO 1 // no digits or number is smaller than +-2^-1022
  629. #define PARSER_MZERO 2 // number is negative, module smaller
  630. #define PARSER_PINF 3 // number is higher than +HUGE_VAL
  631. #define PARSER_MINF 4 // number is lower than -HUGE_VAL
  632. inline char next(const char*& _s, const char* _term)
  633. {
  634. return _s != _term
  635. ? *_s++
  636. : '\0'
  637. ;
  638. }
  639. static int parser(const char* _s, const char* _term, PrepNumber* _pn)
  640. {
  641. int state = FSM_A;
  642. int digx = 0;
  643. char c = ' '; /* initial value for kicking off the state machine */
  644. int result = PARSER_OK;
  645. int expneg = 0;
  646. int32_t expexp = 0;
  647. while (state != FSM_STOP) // && _s != _term)
  648. {
  649. switch (state)
  650. {
  651. case FSM_A:
  652. if (isSpace(c) )
  653. {
  654. c = next(_s, _term);
  655. }
  656. else
  657. {
  658. state = FSM_B;
  659. }
  660. break;
  661. case FSM_B:
  662. state = FSM_C;
  663. if (c == '+')
  664. {
  665. c = next(_s, _term);
  666. }
  667. else if (c == '-')
  668. {
  669. _pn->negative = 1;
  670. c = next(_s, _term);
  671. }
  672. else if (isNumeric(c) )
  673. {
  674. }
  675. else if (c == '.')
  676. {
  677. }
  678. else
  679. {
  680. state = FSM_STOP;
  681. }
  682. break;
  683. case FSM_C:
  684. if (c == '0')
  685. {
  686. c = next(_s, _term);
  687. }
  688. else if (c == '.')
  689. {
  690. c = next(_s, _term);
  691. state = FSM_D;
  692. }
  693. else
  694. {
  695. state = FSM_E;
  696. }
  697. break;
  698. case FSM_D:
  699. if (c == '0')
  700. {
  701. c = next(_s, _term);
  702. if (_pn->exponent > -2147483647) _pn->exponent--;
  703. }
  704. else
  705. {
  706. state = FSM_F;
  707. }
  708. break;
  709. case FSM_E:
  710. if (isNumeric(c) )
  711. {
  712. if (digx < DIGITS)
  713. {
  714. _pn->mantissa *= 10;
  715. _pn->mantissa += c - '0';
  716. digx++;
  717. }
  718. else if (_pn->exponent < 2147483647)
  719. {
  720. _pn->exponent++;
  721. }
  722. c = next(_s, _term);
  723. }
  724. else if (c == '.')
  725. {
  726. c = next(_s, _term);
  727. state = FSM_F;
  728. }
  729. else
  730. {
  731. state = FSM_F;
  732. }
  733. break;
  734. case FSM_F:
  735. if (isNumeric(c) )
  736. {
  737. if (digx < DIGITS)
  738. {
  739. _pn->mantissa *= 10;
  740. _pn->mantissa += c - '0';
  741. _pn->exponent--;
  742. digx++;
  743. }
  744. c = next(_s, _term);
  745. }
  746. else if ('e' == toLower(c) )
  747. {
  748. c = next(_s, _term);
  749. state = FSM_G;
  750. }
  751. else
  752. {
  753. state = FSM_G;
  754. }
  755. break;
  756. case FSM_G:
  757. if (c == '+')
  758. {
  759. c = next(_s, _term);
  760. }
  761. else if (c == '-')
  762. {
  763. expneg = 1;
  764. c = next(_s, _term);
  765. }
  766. state = FSM_H;
  767. break;
  768. case FSM_H:
  769. if (c == '0')
  770. {
  771. c = next(_s, _term);
  772. }
  773. else
  774. {
  775. state = FSM_I;
  776. }
  777. break;
  778. case FSM_I:
  779. if (isNumeric(c) )
  780. {
  781. if (expexp < 214748364)
  782. {
  783. expexp *= 10;
  784. expexp += c - '0';
  785. }
  786. c = next(_s, _term);
  787. }
  788. else
  789. {
  790. state = FSM_STOP;
  791. }
  792. break;
  793. }
  794. }
  795. if (expneg)
  796. {
  797. expexp = -expexp;
  798. }
  799. _pn->exponent += expexp;
  800. if (_pn->mantissa == 0)
  801. {
  802. if (_pn->negative)
  803. {
  804. result = PARSER_MZERO;
  805. }
  806. else
  807. {
  808. result = PARSER_PZERO;
  809. }
  810. }
  811. else if (_pn->exponent > 309)
  812. {
  813. if (_pn->negative)
  814. {
  815. result = PARSER_MINF;
  816. }
  817. else
  818. {
  819. result = PARSER_PINF;
  820. }
  821. }
  822. else if (_pn->exponent < -328)
  823. {
  824. if (_pn->negative)
  825. {
  826. result = PARSER_MZERO;
  827. }
  828. else
  829. {
  830. result = PARSER_PZERO;
  831. }
  832. }
  833. return result;
  834. }
  835. static double converter(PrepNumber* _pn)
  836. {
  837. int binexp = 92;
  838. HexDouble hd;
  839. uint32_t s2, s1, s0; /* 96-bit precision integer */
  840. uint32_t q2, q1, q0; /* 96-bit precision integer */
  841. uint32_t r2, r1, r0; /* 96-bit precision integer */
  842. uint32_t mask28 = UINT32_C(0xf) << 28;
  843. hd.u = 0;
  844. s0 = (uint32_t)(_pn->mantissa & UINT32_MAX);
  845. s1 = (uint32_t)(_pn->mantissa >> 32);
  846. s2 = 0;
  847. while (_pn->exponent > 0)
  848. {
  849. lsl96(s2, s1, s0, q2, q1, q0); // q = p << 1
  850. lsl96(q2, q1, q0, r2, r1, r0); // r = p << 2
  851. lsl96(r2, r1, r0, s2, s1, s0); // p = p << 3
  852. add96(s2, s1, s0, q2, q1, q0); // p = (p << 3) + (p << 1)
  853. _pn->exponent--;
  854. while (s2 & mask28)
  855. {
  856. lsr96(s2, s1, s0, q2, q1, q0);
  857. binexp++;
  858. s2 = q2;
  859. s1 = q1;
  860. s0 = q0;
  861. }
  862. }
  863. while (_pn->exponent < 0)
  864. {
  865. while (!(s2 & (1 << 31) ) )
  866. {
  867. lsl96(s2, s1, s0, q2, q1, q0);
  868. binexp--;
  869. s2 = q2;
  870. s1 = q1;
  871. s0 = q0;
  872. }
  873. q2 = s2 / 10;
  874. r1 = s2 % 10;
  875. r2 = (s1 >> 8) | (r1 << 24);
  876. q1 = r2 / 10;
  877. r1 = r2 % 10;
  878. r2 = ( (s1 & 0xFF) << 16) | (s0 >> 16) | (r1 << 24);
  879. r0 = r2 / 10;
  880. r1 = r2 % 10;
  881. q1 = (q1 << 8) | ( (r0 & 0x00FF0000) >> 16);
  882. q0 = r0 << 16;
  883. r2 = (s0 & UINT16_MAX) | (r1 << 16);
  884. q0 |= r2 / 10;
  885. s2 = q2;
  886. s1 = q1;
  887. s0 = q0;
  888. _pn->exponent++;
  889. }
  890. if (s2 || s1 || s0)
  891. {
  892. while (!(s2 & mask28) )
  893. {
  894. lsl96(s2, s1, s0, q2, q1, q0);
  895. binexp--;
  896. s2 = q2;
  897. s1 = q1;
  898. s0 = q0;
  899. }
  900. }
  901. binexp += 1023;
  902. if (binexp > 2046)
  903. {
  904. if (_pn->negative)
  905. {
  906. hd.u = DOUBLE_MINUS_INFINITY;
  907. }
  908. else
  909. {
  910. hd.u = DOUBLE_PLUS_INFINITY;
  911. }
  912. }
  913. else if (binexp < 1)
  914. {
  915. if (_pn->negative)
  916. {
  917. hd.u = DOUBLE_MINUS_ZERO;
  918. }
  919. }
  920. else if (s2)
  921. {
  922. uint64_t q;
  923. uint64_t binexs2 = (uint64_t)binexp;
  924. binexs2 <<= 52;
  925. q = ( (uint64_t)(s2 & ~mask28) << 24)
  926. | ( ( (uint64_t)s1 + 128) >> 8) | binexs2;
  927. if (_pn->negative)
  928. {
  929. q |= (1ULL << 63);
  930. }
  931. hd.u = q;
  932. }
  933. return hd.d;
  934. }
  935. int32_t toString(char* _out, int32_t _max, bool _value)
  936. {
  937. StringView str(_value ? "true" : "false");
  938. strCopy(_out, _max, str);
  939. return str.getLength();
  940. }
  941. bool fromString(bool* _out, const StringView& _str)
  942. {
  943. char ch = toLower(_str.getPtr()[0]);
  944. *_out = ch == 't' || ch == '1';
  945. return 0 != _str.getLength();
  946. }
  947. bool fromString(float* _out, const StringView& _str)
  948. {
  949. double dbl;
  950. bool result = fromString(&dbl, _str);
  951. *_out = float(dbl);
  952. return result;
  953. }
  954. bool fromString(double* _out, const StringView& _str)
  955. {
  956. PrepNumber pn;
  957. pn.mantissa = 0;
  958. pn.negative = 0;
  959. pn.exponent = 0;
  960. HexDouble hd;
  961. hd.u = DOUBLE_PLUS_ZERO;
  962. switch (parser(_str.getPtr(), _str.getTerm(), &pn) )
  963. {
  964. case PARSER_OK:
  965. *_out = converter(&pn);
  966. break;
  967. case PARSER_PZERO:
  968. *_out = hd.d;
  969. break;
  970. case PARSER_MZERO:
  971. hd.u = DOUBLE_MINUS_ZERO;
  972. *_out = hd.d;
  973. break;
  974. case PARSER_PINF:
  975. hd.u = DOUBLE_PLUS_INFINITY;
  976. *_out = hd.d;
  977. break;
  978. case PARSER_MINF:
  979. hd.u = DOUBLE_MINUS_INFINITY;
  980. *_out = hd.d;
  981. break;
  982. }
  983. return true;
  984. }
  985. bool fromString(int32_t* _out, const StringView& _str)
  986. {
  987. StringView str = bx::strLTrimSpace(_str);
  988. const char* ptr = str.getPtr();
  989. const char* term = str.getTerm();
  990. char ch = *ptr++;
  991. bool neg = false;
  992. switch (ch)
  993. {
  994. case '-':
  995. case '+': neg = '-' == ch;
  996. break;
  997. default:
  998. --ptr;
  999. break;
  1000. }
  1001. int32_t result = 0;
  1002. for (ch = *ptr++; isNumeric(ch) && ptr <= term; ch = *ptr++)
  1003. {
  1004. result = 10*result - (ch - '0');
  1005. }
  1006. *_out = neg ? result : -result;
  1007. return true;
  1008. }
  1009. bool fromString(uint32_t* _out, const StringView& _str)
  1010. {
  1011. fromString( (int32_t*)_out, _str);
  1012. return true;
  1013. }
  1014. } // namespace bx