dtoa.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
  15. // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
  16. // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
  17. #ifndef RAPIDJSON_DTOA_
  18. #define RAPIDJSON_DTOA_
  19. #include "itoa.h" // GetDigitsLut()
  20. #include "diyfp.h"
  21. #include "ieee754.h"
  22. RAPIDJSON_NAMESPACE_BEGIN
  23. namespace internal {
  24. #ifdef __GNUC__
  25. RAPIDJSON_DIAG_PUSH
  26. RAPIDJSON_DIAG_OFF(effc++)
  27. RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
  28. #endif
  29. inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
  30. while (rest < wp_w && delta - rest >= ten_kappa &&
  31. (rest + ten_kappa < wp_w || /// closer
  32. wp_w - rest > rest + ten_kappa - wp_w)) {
  33. buffer[len - 1]--;
  34. rest += ten_kappa;
  35. }
  36. }
  37. inline int CountDecimalDigit32(uint32_t n) {
  38. // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
  39. if (n < 10) return 1;
  40. if (n < 100) return 2;
  41. if (n < 1000) return 3;
  42. if (n < 10000) return 4;
  43. if (n < 100000) return 5;
  44. if (n < 1000000) return 6;
  45. if (n < 10000000) return 7;
  46. if (n < 100000000) return 8;
  47. // Will not reach 10 digits in DigitGen()
  48. //if (n < 1000000000) return 9;
  49. //return 10;
  50. return 9;
  51. }
  52. inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
  53. static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
  54. const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
  55. const DiyFp wp_w = Mp - W;
  56. uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
  57. uint64_t p2 = Mp.f & (one.f - 1);
  58. int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
  59. *len = 0;
  60. while (kappa > 0) {
  61. uint32_t d = 0;
  62. switch (kappa) {
  63. case 9: d = p1 / 100000000; p1 %= 100000000; break;
  64. case 8: d = p1 / 10000000; p1 %= 10000000; break;
  65. case 7: d = p1 / 1000000; p1 %= 1000000; break;
  66. case 6: d = p1 / 100000; p1 %= 100000; break;
  67. case 5: d = p1 / 10000; p1 %= 10000; break;
  68. case 4: d = p1 / 1000; p1 %= 1000; break;
  69. case 3: d = p1 / 100; p1 %= 100; break;
  70. case 2: d = p1 / 10; p1 %= 10; break;
  71. case 1: d = p1; p1 = 0; break;
  72. default:;
  73. }
  74. if (d || *len)
  75. buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
  76. kappa--;
  77. uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
  78. if (tmp <= delta) {
  79. *K += kappa;
  80. GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
  81. return;
  82. }
  83. }
  84. // kappa = 0
  85. for (;;) {
  86. p2 *= 10;
  87. delta *= 10;
  88. char d = static_cast<char>(p2 >> -one.e);
  89. if (d || *len)
  90. buffer[(*len)++] = static_cast<char>('0' + d);
  91. p2 &= one.f - 1;
  92. kappa--;
  93. if (p2 < delta) {
  94. *K += kappa;
  95. int index = -kappa;
  96. GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0));
  97. return;
  98. }
  99. }
  100. }
  101. inline void Grisu2(double value, char* buffer, int* length, int* K) {
  102. const DiyFp v(value);
  103. DiyFp w_m, w_p;
  104. v.NormalizedBoundaries(&w_m, &w_p);
  105. const DiyFp c_mk = GetCachedPower(w_p.e, K);
  106. const DiyFp W = v.Normalize() * c_mk;
  107. DiyFp Wp = w_p * c_mk;
  108. DiyFp Wm = w_m * c_mk;
  109. Wm.f++;
  110. Wp.f--;
  111. DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
  112. }
  113. inline char* WriteExponent(int K, char* buffer) {
  114. if (K < 0) {
  115. *buffer++ = '-';
  116. K = -K;
  117. }
  118. if (K >= 100) {
  119. *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
  120. K %= 100;
  121. const char* d = GetDigitsLut() + K * 2;
  122. *buffer++ = d[0];
  123. *buffer++ = d[1];
  124. }
  125. else if (K >= 10) {
  126. const char* d = GetDigitsLut() + K * 2;
  127. *buffer++ = d[0];
  128. *buffer++ = d[1];
  129. }
  130. else
  131. *buffer++ = static_cast<char>('0' + static_cast<char>(K));
  132. return buffer;
  133. }
  134. inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
  135. const int kk = length + k; // 10^(kk-1) <= v < 10^kk
  136. if (0 <= k && kk <= 21) {
  137. // 1234e7 -> 12340000000
  138. for (int i = length; i < kk; i++)
  139. buffer[i] = '0';
  140. buffer[kk] = '.';
  141. buffer[kk + 1] = '0';
  142. return &buffer[kk + 2];
  143. }
  144. else if (0 < kk && kk <= 21) {
  145. // 1234e-2 -> 12.34
  146. std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
  147. buffer[kk] = '.';
  148. if (0 > k + maxDecimalPlaces) {
  149. // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
  150. // Remove extra trailing zeros (at least one) after truncation.
  151. for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
  152. if (buffer[i] != '0')
  153. return &buffer[i + 1];
  154. return &buffer[kk + 2]; // Reserve one zero
  155. }
  156. else
  157. return &buffer[length + 1];
  158. }
  159. else if (-6 < kk && kk <= 0) {
  160. // 1234e-6 -> 0.001234
  161. const int offset = 2 - kk;
  162. std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
  163. buffer[0] = '0';
  164. buffer[1] = '.';
  165. for (int i = 2; i < offset; i++)
  166. buffer[i] = '0';
  167. if (length - kk > maxDecimalPlaces) {
  168. // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
  169. // Remove extra trailing zeros (at least one) after truncation.
  170. for (int i = maxDecimalPlaces + 1; i > 2; i--)
  171. if (buffer[i] != '0')
  172. return &buffer[i + 1];
  173. return &buffer[3]; // Reserve one zero
  174. }
  175. else
  176. return &buffer[length + offset];
  177. }
  178. else if (kk < -maxDecimalPlaces) {
  179. // Truncate to zero
  180. buffer[0] = '0';
  181. buffer[1] = '.';
  182. buffer[2] = '0';
  183. return &buffer[3];
  184. }
  185. else if (length == 1) {
  186. // 1e30
  187. buffer[1] = 'e';
  188. return WriteExponent(kk - 1, &buffer[2]);
  189. }
  190. else {
  191. // 1234e30 -> 1.234e33
  192. std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
  193. buffer[1] = '.';
  194. buffer[length + 1] = 'e';
  195. return WriteExponent(kk - 1, &buffer[0 + length + 2]);
  196. }
  197. }
  198. inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
  199. RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
  200. Double d(value);
  201. if (d.IsZero()) {
  202. if (d.Sign())
  203. *buffer++ = '-'; // -0.0, Issue #289
  204. buffer[0] = '0';
  205. buffer[1] = '.';
  206. buffer[2] = '0';
  207. return &buffer[3];
  208. }
  209. else {
  210. if (value < 0) {
  211. *buffer++ = '-';
  212. value = -value;
  213. }
  214. int length, K;
  215. Grisu2(value, buffer, &length, &K);
  216. return Prettify(buffer, length, K, maxDecimalPlaces);
  217. }
  218. }
  219. #ifdef __GNUC__
  220. RAPIDJSON_DIAG_POP
  221. #endif
  222. } // namespace internal
  223. RAPIDJSON_NAMESPACE_END
  224. #endif // RAPIDJSON_DTOA_