pdtoa.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. See pdtoa.h for explanation.
  3. Copyright (C) 2014 Milo Yip
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "pdtoa.h"
  21. #include "cmath.h"
  22. #include <assert.h>
  23. #include <math.h>
  24. #include <stdint.h>
  25. #if defined(_MSC_VER)
  26. #include <intrin.h>
  27. #include <float.h>
  28. #define copysign _copysign
  29. #pragma float_control(precise, on, push)
  30. #endif
  31. #define UINT64_C2(h, l) ((static_cast<uint64_t>(h) << 32) | static_cast<uint64_t>(l))
  32. struct DiyFp {
  33. DiyFp() {}
  34. DiyFp(uint64_t f, int e) : f(f), e(e) {}
  35. DiyFp(double d) {
  36. union {
  37. double d;
  38. uint64_t u64;
  39. } u = { d };
  40. int biased_e = (u.u64 & kDpExponentMask) >> kDpSignificandSize;
  41. uint64_t significand = (u.u64 & kDpSignificandMask);
  42. if (biased_e != 0) {
  43. f = significand + kDpHiddenBit;
  44. e = biased_e - kDpExponentBias;
  45. }
  46. else {
  47. f = significand;
  48. e = kDpMinExponent + 1;
  49. }
  50. }
  51. DiyFp operator-(const DiyFp& rhs) const {
  52. assert(e == rhs.e);
  53. assert(f >= rhs.f);
  54. return DiyFp(f - rhs.f, e);
  55. }
  56. DiyFp operator*(const DiyFp& rhs) const {
  57. #if defined(_MSC_VER) && defined(_M_AMD64)
  58. uint64_t h;
  59. uint64_t l = _umul128(f, rhs.f, &h);
  60. if (l & (uint64_t(1) << 63)) // rounding
  61. h++;
  62. return DiyFp(h, e + rhs.e + 64);
  63. #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
  64. unsigned __int128 p = static_cast<unsigned __int128>(f) * static_cast<unsigned __int128>(rhs.f);
  65. uint64_t h = p >> 64;
  66. uint64_t l = static_cast<uint64_t>(p);
  67. if (l & (uint64_t(1) << 63)) // rounding
  68. h++;
  69. return DiyFp(h, e + rhs.e + 64);
  70. #else
  71. const uint64_t M32 = 0xFFFFFFFF;
  72. const uint64_t a = f >> 32;
  73. const uint64_t b = f & M32;
  74. const uint64_t c = rhs.f >> 32;
  75. const uint64_t d = rhs.f & M32;
  76. const uint64_t ac = a * c;
  77. const uint64_t bc = b * c;
  78. const uint64_t ad = a * d;
  79. const uint64_t bd = b * d;
  80. uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
  81. tmp += 1U << 31; /// mult_round
  82. return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64);
  83. #endif
  84. }
  85. DiyFp Normalize() const {
  86. #if defined(_MSC_VER) && defined(_M_AMD64)
  87. unsigned long index;
  88. _BitScanReverse64(&index, f);
  89. return DiyFp(f << (63 - index), e - (63 - index));
  90. #elif defined(__GNUC__)
  91. int s = __builtin_clzll(f);
  92. return DiyFp(f << s, e - s);
  93. #else
  94. DiyFp res = *this;
  95. while (!(res.f & kDpHiddenBit)) {
  96. res.f <<= 1;
  97. res.e--;
  98. }
  99. res.f <<= (kDiySignificandSize - kDpSignificandSize - 1);
  100. res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 1);
  101. return res;
  102. #endif
  103. }
  104. DiyFp NormalizeBoundary() const {
  105. #if defined(_MSC_VER) && defined(_M_AMD64)
  106. unsigned long index;
  107. _BitScanReverse64(&index, f);
  108. return DiyFp (f << (63 - index), e - (63 - index));
  109. #else
  110. DiyFp res = *this;
  111. while (!(res.f & (kDpHiddenBit << 1))) {
  112. res.f <<= 1;
  113. res.e--;
  114. }
  115. res.f <<= (kDiySignificandSize - kDpSignificandSize - 2);
  116. res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2);
  117. return res;
  118. #endif
  119. }
  120. void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const {
  121. DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary();
  122. DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1);
  123. mi.f <<= mi.e - pl.e;
  124. mi.e = pl.e;
  125. *plus = pl;
  126. *minus = mi;
  127. }
  128. static const int kDiySignificandSize = 64;
  129. static const int kDpSignificandSize = 52;
  130. static const int kDpExponentBias = 0x3FF + kDpSignificandSize;
  131. static const int kDpMinExponent = -kDpExponentBias;
  132. static const uint64_t kDpExponentMask = UINT64_C2(0x7FF00000, 0x00000000);
  133. static const uint64_t kDpSignificandMask = UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
  134. static const uint64_t kDpHiddenBit = UINT64_C2(0x00100000, 0x00000000);
  135. uint64_t f;
  136. int e;
  137. };
  138. inline static DiyFp GetCachedPower(int e, int* K) {
  139. // 10^-348, 10^-340, ..., 10^340
  140. static const uint64_t kCachedPowers_F[] = {
  141. UINT64_C2(0xfa8fd5a0, 0x081c0288), UINT64_C2(0xbaaee17f, 0xa23ebf76),
  142. UINT64_C2(0x8b16fb20, 0x3055ac76), UINT64_C2(0xcf42894a, 0x5dce35ea),
  143. UINT64_C2(0x9a6bb0aa, 0x55653b2d), UINT64_C2(0xe61acf03, 0x3d1a45df),
  144. UINT64_C2(0xab70fe17, 0xc79ac6ca), UINT64_C2(0xff77b1fc, 0xbebcdc4f),
  145. UINT64_C2(0xbe5691ef, 0x416bd60c), UINT64_C2(0x8dd01fad, 0x907ffc3c),
  146. UINT64_C2(0xd3515c28, 0x31559a83), UINT64_C2(0x9d71ac8f, 0xada6c9b5),
  147. UINT64_C2(0xea9c2277, 0x23ee8bcb), UINT64_C2(0xaecc4991, 0x4078536d),
  148. UINT64_C2(0x823c1279, 0x5db6ce57), UINT64_C2(0xc2109436, 0x4dfb5637),
  149. UINT64_C2(0x9096ea6f, 0x3848984f), UINT64_C2(0xd77485cb, 0x25823ac7),
  150. UINT64_C2(0xa086cfcd, 0x97bf97f4), UINT64_C2(0xef340a98, 0x172aace5),
  151. UINT64_C2(0xb23867fb, 0x2a35b28e), UINT64_C2(0x84c8d4df, 0xd2c63f3b),
  152. UINT64_C2(0xc5dd4427, 0x1ad3cdba), UINT64_C2(0x936b9fce, 0xbb25c996),
  153. UINT64_C2(0xdbac6c24, 0x7d62a584), UINT64_C2(0xa3ab6658, 0x0d5fdaf6),
  154. UINT64_C2(0xf3e2f893, 0xdec3f126), UINT64_C2(0xb5b5ada8, 0xaaff80b8),
  155. UINT64_C2(0x87625f05, 0x6c7c4a8b), UINT64_C2(0xc9bcff60, 0x34c13053),
  156. UINT64_C2(0x964e858c, 0x91ba2655), UINT64_C2(0xdff97724, 0x70297ebd),
  157. UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), UINT64_C2(0xf8a95fcf, 0x88747d94),
  158. UINT64_C2(0xb9447093, 0x8fa89bcf), UINT64_C2(0x8a08f0f8, 0xbf0f156b),
  159. UINT64_C2(0xcdb02555, 0x653131b6), UINT64_C2(0x993fe2c6, 0xd07b7fac),
  160. UINT64_C2(0xe45c10c4, 0x2a2b3b06), UINT64_C2(0xaa242499, 0x697392d3),
  161. UINT64_C2(0xfd87b5f2, 0x8300ca0e), UINT64_C2(0xbce50864, 0x92111aeb),
  162. UINT64_C2(0x8cbccc09, 0x6f5088cc), UINT64_C2(0xd1b71758, 0xe219652c),
  163. UINT64_C2(0x9c400000, 0x00000000), UINT64_C2(0xe8d4a510, 0x00000000),
  164. UINT64_C2(0xad78ebc5, 0xac620000), UINT64_C2(0x813f3978, 0xf8940984),
  165. UINT64_C2(0xc097ce7b, 0xc90715b3), UINT64_C2(0x8f7e32ce, 0x7bea5c70),
  166. UINT64_C2(0xd5d238a4, 0xabe98068), UINT64_C2(0x9f4f2726, 0x179a2245),
  167. UINT64_C2(0xed63a231, 0xd4c4fb27), UINT64_C2(0xb0de6538, 0x8cc8ada8),
  168. UINT64_C2(0x83c7088e, 0x1aab65db), UINT64_C2(0xc45d1df9, 0x42711d9a),
  169. UINT64_C2(0x924d692c, 0xa61be758), UINT64_C2(0xda01ee64, 0x1a708dea),
  170. UINT64_C2(0xa26da399, 0x9aef774a), UINT64_C2(0xf209787b, 0xb47d6b85),
  171. UINT64_C2(0xb454e4a1, 0x79dd1877), UINT64_C2(0x865b8692, 0x5b9bc5c2),
  172. UINT64_C2(0xc83553c5, 0xc8965d3d), UINT64_C2(0x952ab45c, 0xfa97a0b3),
  173. UINT64_C2(0xde469fbd, 0x99a05fe3), UINT64_C2(0xa59bc234, 0xdb398c25),
  174. UINT64_C2(0xf6c69a72, 0xa3989f5c), UINT64_C2(0xb7dcbf53, 0x54e9bece),
  175. UINT64_C2(0x88fcf317, 0xf22241e2), UINT64_C2(0xcc20ce9b, 0xd35c78a5),
  176. UINT64_C2(0x98165af3, 0x7b2153df), UINT64_C2(0xe2a0b5dc, 0x971f303a),
  177. UINT64_C2(0xa8d9d153, 0x5ce3b396), UINT64_C2(0xfb9b7cd9, 0xa4a7443c),
  178. UINT64_C2(0xbb764c4c, 0xa7a44410), UINT64_C2(0x8bab8eef, 0xb6409c1a),
  179. UINT64_C2(0xd01fef10, 0xa657842c), UINT64_C2(0x9b10a4e5, 0xe9913129),
  180. UINT64_C2(0xe7109bfb, 0xa19c0c9d), UINT64_C2(0xac2820d9, 0x623bf429),
  181. UINT64_C2(0x80444b5e, 0x7aa7cf85), UINT64_C2(0xbf21e440, 0x03acdd2d),
  182. UINT64_C2(0x8e679c2f, 0x5e44ff8f), UINT64_C2(0xd433179d, 0x9c8cb841),
  183. UINT64_C2(0x9e19db92, 0xb4e31ba9), UINT64_C2(0xeb96bf6e, 0xbadf77d9),
  184. UINT64_C2(0xaf87023b, 0x9bf0ee6b)
  185. };
  186. static const int16_t kCachedPowers_E[] = {
  187. -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
  188. -954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
  189. -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
  190. -422, -396, -369, -343, -316, -289, -263, -236, -210, -183,
  191. -157, -130, -103, -77, -50, -24, 3, 30, 56, 83,
  192. 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
  193. 375, 402, 428, 455, 481, 508, 534, 561, 588, 614,
  194. 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
  195. 907, 933, 960, 986, 1013, 1039, 1066
  196. };
  197. //int k = static_cast<int>(ceil((-61 - e) * 0.30102999566398114)) + 374;
  198. double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive
  199. int k = static_cast<int>(dk);
  200. if (k != dk)
  201. k++;
  202. unsigned index = static_cast<unsigned>((k >> 3) + 1);
  203. *K = -(-348 + static_cast<int>(index << 3)); // decimal exponent no need lookup table
  204. assert(index < sizeof(kCachedPowers_F) / sizeof(kCachedPowers_F[0]));
  205. return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
  206. }
  207. inline static void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
  208. while (rest < wp_w && delta - rest >= ten_kappa &&
  209. (rest + ten_kappa < wp_w || /// closer
  210. wp_w - rest > rest + ten_kappa - wp_w)) {
  211. buffer[len - 1]--;
  212. rest += ten_kappa;
  213. }
  214. }
  215. inline static unsigned CountDecimalDigit32(uint32_t n) {
  216. // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
  217. if (n < 10) return 1;
  218. if (n < 100) return 2;
  219. if (n < 1000) return 3;
  220. if (n < 10000) return 4;
  221. if (n < 100000) return 5;
  222. if (n < 1000000) return 6;
  223. if (n < 10000000) return 7;
  224. if (n < 100000000) return 8;
  225. if (n < 1000000000) return 9;
  226. return 10;
  227. }
  228. inline static void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
  229. static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 0, 0, 0, 0, 0 };
  230. const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
  231. const DiyFp wp_w = Mp - W;
  232. uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
  233. uint64_t p2 = Mp.f & (one.f - 1);
  234. int kappa = static_cast<int>(CountDecimalDigit32(p1));
  235. *len = 0;
  236. while (kappa > 0) {
  237. uint32_t d = 0;
  238. switch (kappa) {
  239. case 10: d = p1 / 1000000000; p1 %= 1000000000; break;
  240. case 9: d = p1 / 100000000; p1 %= 100000000; break;
  241. case 8: d = p1 / 10000000; p1 %= 10000000; break;
  242. case 7: d = p1 / 1000000; p1 %= 1000000; break;
  243. case 6: d = p1 / 100000; p1 %= 100000; break;
  244. case 5: d = p1 / 10000; p1 %= 10000; break;
  245. case 4: d = p1 / 1000; p1 %= 1000; break;
  246. case 3: d = p1 / 100; p1 %= 100; break;
  247. case 2: d = p1 / 10; p1 %= 10; break;
  248. case 1: d = p1; p1 = 0; break;
  249. NODEFAULT
  250. }
  251. if (d || *len)
  252. buffer[(*len)++] = '0' + static_cast<char>(d);
  253. kappa--;
  254. uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
  255. if (tmp <= delta) {
  256. *K += kappa;
  257. GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
  258. return;
  259. }
  260. }
  261. // kappa = 0
  262. for (;;) {
  263. p2 *= 10;
  264. delta *= 10;
  265. char d = static_cast<char>(p2 >> -one.e);
  266. if (d || *len)
  267. buffer[(*len)++] = '0' + d;
  268. p2 &= one.f - 1;
  269. kappa--;
  270. if (p2 < delta) {
  271. *K += kappa;
  272. GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]);
  273. return;
  274. }
  275. }
  276. }
  277. inline static void Grisu2(double value, char* buffer, int* length, int* K) {
  278. const DiyFp v(value);
  279. DiyFp w_m, w_p;
  280. v.NormalizedBoundaries(&w_m, &w_p);
  281. const DiyFp c_mk = GetCachedPower(w_p.e, K);
  282. const DiyFp W = v.Normalize() * c_mk;
  283. DiyFp Wp = w_p * c_mk;
  284. DiyFp Wm = w_m * c_mk;
  285. Wm.f++;
  286. Wp.f--;
  287. DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
  288. }
  289. static const char cDigitsLut[200] = {
  290. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
  291. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
  292. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  293. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
  294. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
  295. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  296. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
  297. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
  298. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  299. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
  300. };
  301. inline void WriteExponent(int K, char* buffer) {
  302. if (K < 0) {
  303. *buffer++ = '-';
  304. K = -K;
  305. }
  306. if (K >= 100) {
  307. *buffer++ = '0' + static_cast<char>(K / 100);
  308. K %= 100;
  309. const char* d = cDigitsLut + K * 2;
  310. *buffer++ = d[0];
  311. *buffer++ = d[1];
  312. }
  313. else if (K >= 10) {
  314. const char* d = cDigitsLut + K * 2;
  315. *buffer++ = d[0];
  316. *buffer++ = d[1];
  317. }
  318. else
  319. *buffer++ = '0' + static_cast<char>(K);
  320. *buffer = '\0';
  321. }
  322. inline static void Prettify(char* buffer, int length, int k) {
  323. const int kk = length + k; // 10^(kk-1) <= v < 10^kk
  324. if (length <= kk && kk <= 21) {
  325. // 1234e7 -> 12340000000
  326. for (int i = length; i < kk; i++)
  327. buffer[i] = '0';
  328. buffer[kk] = '.';
  329. buffer[kk + 1] = '0';
  330. buffer[kk + 2] = '\0';
  331. }
  332. else if (0 < kk && kk <= 21) {
  333. // 1234e-2 -> 12.34
  334. memmove(&buffer[kk + 1], &buffer[kk], length - kk);
  335. buffer[kk] = '.';
  336. buffer[length + 1] = '\0';
  337. }
  338. else if (-6 < kk && kk <= 0) {
  339. // 1234e-6 -> 0.001234
  340. const int offset = 2 - kk;
  341. memmove(&buffer[offset], &buffer[0], length);
  342. buffer[0] = '0';
  343. buffer[1] = '.';
  344. for (int i = 2; i < offset; i++)
  345. buffer[i] = '0';
  346. buffer[length + offset] = '\0';
  347. }
  348. else if (length == 1) {
  349. // 1e30
  350. buffer[1] = 'e';
  351. WriteExponent(kk - 1, &buffer[2]);
  352. }
  353. else {
  354. // 1234e30 -> 1.234e33
  355. memmove(&buffer[2], &buffer[1], length - 1);
  356. buffer[1] = '.';
  357. buffer[length + 1] = 'e';
  358. WriteExponent(kk - 1, &buffer[0 + length + 2]);
  359. }
  360. }
  361. void pdtoa(double value, char *buffer) {
  362. #ifdef _MSC_VER
  363. if (copysign(1.0, value) < 0) {
  364. #else
  365. if (std::signbit(value)) {
  366. #endif
  367. *buffer++ = '-';
  368. value = -value;
  369. }
  370. if (cinf(value)) {
  371. buffer[0] = 'i';
  372. buffer[1] = 'n';
  373. buffer[2] = 'f';
  374. buffer[3] = '\0';
  375. } else if (cnan(value)) {
  376. buffer[0] = 'n';
  377. buffer[1] = 'a';
  378. buffer[2] = 'n';
  379. buffer[3] = '\0';
  380. } else if (value == 0.0) {
  381. buffer[0] = '0';
  382. buffer[1] = '.';
  383. buffer[2] = '0';
  384. buffer[3] = '\0';
  385. } else if (value == 1.0) {
  386. buffer[0] = '1';
  387. buffer[1] = '.';
  388. buffer[2] = '0';
  389. buffer[3] = '\0';
  390. } else {
  391. int length, K;
  392. Grisu2(value, buffer, &length, &K);
  393. Prettify(buffer, length, K);
  394. }
  395. }
  396. /**
  397. * Version of pdtoa that tries hard to find the minimal string representation
  398. * for a single-precision floating-point number.
  399. */
  400. void pftoa(float value, char *buffer) {
  401. #ifdef _MSC_VER
  402. if (copysign(1.0f, value) < 0) {
  403. #else
  404. if (std::signbit(value)) {
  405. #endif
  406. *buffer++ = '-';
  407. value = -value;
  408. }
  409. if (cinf(value)) {
  410. buffer[0] = 'i';
  411. buffer[1] = 'n';
  412. buffer[2] = 'f';
  413. buffer[3] = '\0';
  414. } else if (cnan(value)) {
  415. buffer[0] = 'n';
  416. buffer[1] = 'a';
  417. buffer[2] = 'n';
  418. buffer[3] = '\0';
  419. } else if (value == 0.0f) {
  420. buffer[0] = '0';
  421. buffer[1] = '.';
  422. buffer[2] = '0';
  423. buffer[3] = '\0';
  424. } else if (value == 1.0f) {
  425. buffer[0] = '1';
  426. buffer[1] = '.';
  427. buffer[2] = '0';
  428. buffer[3] = '\0';
  429. } else {
  430. int length, k;
  431. Grisu2(value, buffer, &length, &k);
  432. const int kk = length + k; // 10^(kk-1) <= v < 10^kk
  433. if (length <= kk && kk <= 21) {
  434. // 1234e7 -> 12340000000
  435. for (int i = length; i < kk; i++)
  436. buffer[i] = '0';
  437. buffer[kk] = '.';
  438. buffer[kk + 1] = '0';
  439. buffer[kk + 2] = '\0';
  440. }
  441. else if (0 < kk && kk <= 21) {
  442. // 1234e-2 -> 12.34
  443. memmove(&buffer[kk + 1], &buffer[kk], length - kk);
  444. // We want the shortest possible representation, so keep reading digits
  445. // until strtod would give the correct float value.
  446. buffer[kk] = '\0';
  447. double v = (double)atoi(buffer);
  448. buffer[kk] = '.';
  449. double multiplicand = 0.1;
  450. for (int i = kk + 1; i <= length; ++i) {
  451. double vplus = v + (buffer[i] - '0' + 1) * multiplicand;
  452. v += (buffer[i] - '0') * multiplicand;
  453. multiplicand *= 0.1;
  454. if ((float)v == value) {
  455. length = i;
  456. break;
  457. }
  458. if (buffer[i] < '9' && (float)vplus == value) {
  459. ++buffer[i];
  460. length = i;
  461. break;
  462. }
  463. }
  464. buffer[length + 1] = '\0';
  465. }
  466. else if (-6 < kk && kk <= 0) {
  467. // 1234e-6 -> 0.001234
  468. const int offset = 2 - kk;
  469. memmove(&buffer[offset], &buffer[0], length);
  470. buffer[0] = '0';
  471. buffer[1] = '.';
  472. // We want the shortest possible representation, so keep reading digits
  473. // until strtod would give the correct float value.
  474. double multiplicand = 1.0;
  475. for (int i = 2; i < offset; i++) {
  476. buffer[i] = '0';
  477. multiplicand *= 0.1;
  478. }
  479. if ((float)multiplicand == value) {
  480. length = 0;
  481. buffer[offset - 1] = '1';
  482. } else {
  483. multiplicand *= 0.1;
  484. double v = 0.0;
  485. for (int i = offset; i < length + offset; ++i) {
  486. double vplus = v + (buffer[i] - '0' + 1) * multiplicand;
  487. v += (buffer[i] - '0') * multiplicand;
  488. multiplicand *= 0.1;
  489. if ((float)v == value) {
  490. buffer[i + 1] = '\0';
  491. break;
  492. }
  493. if (buffer[i] < '9' && (float)vplus == value) {
  494. buffer[i]++;
  495. buffer[i + 1] = '\0';
  496. break;
  497. }
  498. }
  499. }
  500. buffer[length + offset] = '\0';
  501. }
  502. else if (length == 1) {
  503. // 1e30
  504. buffer[1] = 'e';
  505. WriteExponent(kk - 1, &buffer[2]);
  506. }
  507. else {
  508. // 1234e30 -> 1.234e33
  509. memmove(&buffer[2], &buffer[1], length - 1);
  510. buffer[1] = '.';
  511. buffer[length + 1] = 'e';
  512. double e_mult = pow(10.0, kk - 1);
  513. if ((float)(10.0 * e_mult) == value) {
  514. buffer[0] = '1';
  515. buffer[1] = 'e';
  516. WriteExponent(kk, &buffer[2]);
  517. } else {
  518. // We want the shortest possible representation, so keep reading
  519. // digits until strtod would give the correct float value.
  520. double v = buffer[0] - '0';
  521. double multiplicand = 0.1;
  522. for (int i = 2; i < length + 2; ++i) {
  523. double vplus = v + (buffer[i] - '0' + 1) * multiplicand;
  524. v += (buffer[i] - '0') * multiplicand;
  525. multiplicand *= 0.1;
  526. if ((float)(v * e_mult) == value) {
  527. length = i;
  528. buffer[i + 1] = 'e';
  529. break;
  530. }
  531. if (buffer[i] < '9' && (float)(vplus * e_mult) == value) {
  532. buffer[i]++;
  533. length = i;
  534. buffer[i + 1] = 'e';
  535. break;
  536. }
  537. }
  538. WriteExponent(kk - 1, &buffer[0 + length + 2]);
  539. }
  540. }
  541. }
  542. }
  543. #ifdef _MSC_VER
  544. #pragma float_control(pop)
  545. #endif