dtoa.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #endif
  28. inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
  29. while (rest < wp_w && delta - rest >= ten_kappa &&
  30. (rest + ten_kappa < wp_w || /// closer
  31. wp_w - rest > rest + ten_kappa - wp_w)) {
  32. buffer[len - 1]--;
  33. rest += ten_kappa;
  34. }
  35. }
  36. inline unsigned CountDecimalDigit32(uint32_t n) {
  37. // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
  38. if (n < 10) return 1;
  39. if (n < 100) return 2;
  40. if (n < 1000) return 3;
  41. if (n < 10000) return 4;
  42. if (n < 100000) return 5;
  43. if (n < 1000000) return 6;
  44. if (n < 10000000) return 7;
  45. if (n < 100000000) return 8;
  46. // Will not reach 10 digits in DigitGen()
  47. //if (n < 1000000000) return 9;
  48. //return 10;
  49. return 9;
  50. }
  51. inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
  52. static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
  53. const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
  54. const DiyFp wp_w = Mp - W;
  55. uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
  56. uint64_t p2 = Mp.f & (one.f - 1);
  57. int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
  58. *len = 0;
  59. while (kappa > 0) {
  60. uint32_t d = 0;
  61. switch (kappa) {
  62. case 9: d = p1 / 100000000; p1 %= 100000000; break;
  63. case 8: d = p1 / 10000000; p1 %= 10000000; break;
  64. case 7: d = p1 / 1000000; p1 %= 1000000; break;
  65. case 6: d = p1 / 100000; p1 %= 100000; break;
  66. case 5: d = p1 / 10000; p1 %= 10000; break;
  67. case 4: d = p1 / 1000; p1 %= 1000; break;
  68. case 3: d = p1 / 100; p1 %= 100; break;
  69. case 2: d = p1 / 10; p1 %= 10; break;
  70. case 1: d = p1; p1 = 0; break;
  71. default:;
  72. }
  73. if (d || *len)
  74. buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
  75. kappa--;
  76. uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
  77. if (tmp <= delta) {
  78. *K += kappa;
  79. GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
  80. return;
  81. }
  82. }
  83. // kappa = 0
  84. for (;;) {
  85. p2 *= 10;
  86. delta *= 10;
  87. char d = static_cast<char>(p2 >> -one.e);
  88. if (d || *len)
  89. buffer[(*len)++] = static_cast<char>('0' + d);
  90. p2 &= one.f - 1;
  91. kappa--;
  92. if (p2 < delta) {
  93. *K += kappa;
  94. GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]);
  95. return;
  96. }
  97. }
  98. }
  99. inline void Grisu2(double value, char* buffer, int* length, int* K) {
  100. const DiyFp v(value);
  101. DiyFp w_m, w_p;
  102. v.NormalizedBoundaries(&w_m, &w_p);
  103. const DiyFp c_mk = GetCachedPower(w_p.e, K);
  104. const DiyFp W = v.Normalize() * c_mk;
  105. DiyFp Wp = w_p * c_mk;
  106. DiyFp Wm = w_m * c_mk;
  107. Wm.f++;
  108. Wp.f--;
  109. DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
  110. }
  111. inline char* WriteExponent(int K, char* buffer) {
  112. if (K < 0) {
  113. *buffer++ = '-';
  114. K = -K;
  115. }
  116. if (K >= 100) {
  117. *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
  118. K %= 100;
  119. const char* d = GetDigitsLut() + K * 2;
  120. *buffer++ = d[0];
  121. *buffer++ = d[1];
  122. }
  123. else if (K >= 10) {
  124. const char* d = GetDigitsLut() + K * 2;
  125. *buffer++ = d[0];
  126. *buffer++ = d[1];
  127. }
  128. else
  129. *buffer++ = static_cast<char>('0' + static_cast<char>(K));
  130. return buffer;
  131. }
  132. inline char* Prettify(char* buffer, int length, int k) {
  133. const int kk = length + k; // 10^(kk-1) <= v < 10^kk
  134. if (length <= kk && kk <= 21) {
  135. // 1234e7 -> 12340000000
  136. for (int i = length; i < kk; i++)
  137. buffer[i] = '0';
  138. buffer[kk] = '.';
  139. buffer[kk + 1] = '0';
  140. return &buffer[kk + 2];
  141. }
  142. else if (0 < kk && kk <= 21) {
  143. // 1234e-2 -> 12.34
  144. std::memmove(&buffer[kk + 1], &buffer[kk], length - kk);
  145. buffer[kk] = '.';
  146. return &buffer[length + 1];
  147. }
  148. else if (-6 < kk && kk <= 0) {
  149. // 1234e-6 -> 0.001234
  150. const int offset = 2 - kk;
  151. std::memmove(&buffer[offset], &buffer[0], length);
  152. buffer[0] = '0';
  153. buffer[1] = '.';
  154. for (int i = 2; i < offset; i++)
  155. buffer[i] = '0';
  156. return &buffer[length + offset];
  157. }
  158. else if (length == 1) {
  159. // 1e30
  160. buffer[1] = 'e';
  161. return WriteExponent(kk - 1, &buffer[2]);
  162. }
  163. else {
  164. // 1234e30 -> 1.234e33
  165. std::memmove(&buffer[2], &buffer[1], length - 1);
  166. buffer[1] = '.';
  167. buffer[length + 1] = 'e';
  168. return WriteExponent(kk - 1, &buffer[0 + length + 2]);
  169. }
  170. }
  171. inline char* dtoa(double value, char* buffer) {
  172. Double d(value);
  173. if (d.IsZero()) {
  174. if (d.Sign())
  175. *buffer++ = '-'; // -0.0, Issue #289
  176. buffer[0] = '0';
  177. buffer[1] = '.';
  178. buffer[2] = '0';
  179. return &buffer[3];
  180. }
  181. else {
  182. if (value < 0) {
  183. *buffer++ = '-';
  184. value = -value;
  185. }
  186. int length, K;
  187. Grisu2(value, buffer, &length, &K);
  188. return Prettify(buffer, length, K);
  189. }
  190. }
  191. #ifdef __GNUC__
  192. RAPIDJSON_DIAG_POP
  193. #endif
  194. } // namespace internal
  195. RAPIDJSON_NAMESPACE_END
  196. #endif // RAPIDJSON_DTOA_