19 #ifndef RAPIDJSON_DTOA_
20 #define RAPIDJSON_DTOA_
26 RAPIDJSON_NAMESPACE_BEGIN
31 RAPIDJSON_DIAG_OFF(effc++)
34 inline void GrisuRound(
char* buffer,
int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
35 while (rest < wp_w && delta - rest >= ten_kappa &&
36 (rest + ten_kappa < wp_w ||
37 wp_w - rest > rest + ten_kappa - wp_w)) {
43 inline unsigned CountDecimalDigit32(uint32_t n) {
46 if (n < 100)
return 2;
47 if (n < 1000)
return 3;
48 if (n < 10000)
return 4;
49 if (n < 100000)
return 5;
50 if (n < 1000000)
return 6;
51 if (n < 10000000)
return 7;
52 if (n < 100000000)
return 8;
59 inline void DigitGen(
const DiyFp& W,
const DiyFp& Mp, uint64_t delta,
char* buffer,
int* len,
int* K) {
60 static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
61 const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
62 const DiyFp wp_w = Mp - W;
63 uint32_t p1 =
static_cast<uint32_t
>(Mp.f >> -one.e);
64 uint64_t p2 = Mp.f & (one.f - 1);
65 unsigned kappa = CountDecimalDigit32(p1);
71 case 9: d = p1 / 100000000; p1 %= 100000000;
break;
72 case 8: d = p1 / 10000000; p1 %= 10000000;
break;
73 case 7: d = p1 / 1000000; p1 %= 1000000;
break;
74 case 6: d = p1 / 100000; p1 %= 100000;
break;
75 case 5: d = p1 / 10000; p1 %= 10000;
break;
76 case 4: d = p1 / 1000; p1 %= 1000;
break;
77 case 3: d = p1 / 100; p1 %= 100;
break;
78 case 2: d = p1 / 10; p1 %= 10;
break;
79 case 1: d = p1; p1 = 0;
break;
83 buffer[(*len)++] =
static_cast<char>(
'0' +
static_cast<char>(d));
85 uint64_t tmp = (
static_cast<uint64_t
>(p1) << -one.e) + p2;
88 GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
97 char d =
static_cast<char>(p2 >> -one.e);
99 buffer[(*len)++] =
static_cast<char>(
'0' + d);
104 GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-static_cast<int>(kappa)]);
110 inline void Grisu2(
double value,
char* buffer,
int* length,
int* K) {
111 const DiyFp v(value);
113 v.NormalizedBoundaries(&w_m, &w_p);
115 const DiyFp c_mk = GetCachedPower(w_p.e, K);
116 const DiyFp W = v.Normalize() * c_mk;
117 DiyFp Wp = w_p * c_mk;
118 DiyFp Wm = w_m * c_mk;
121 DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
124 inline char* WriteExponent(
int K,
char* buffer) {
131 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K / 100));
133 const char* d = GetDigitsLut() + K * 2;
138 const char* d = GetDigitsLut() + K * 2;
143 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K));
148 inline char* Prettify(
char* buffer,
int length,
int k) {
149 const int kk = length + k;
151 if (length <= kk && kk <= 21) {
153 for (
int i = length; i < kk; i++)
156 buffer[kk + 1] =
'0';
157 return &buffer[kk + 2];
159 else if (0 < kk && kk <= 21) {
161 std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
163 return &buffer[length + 1];
165 else if (-6 < kk && kk <= 0) {
167 const int offset = 2 - kk;
168 std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
171 for (
int i = 2; i < offset; i++)
173 return &buffer[length + offset];
175 else if (length == 1) {
178 return WriteExponent(kk - 1, &buffer[2]);
182 std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
184 buffer[length + 1] =
'e';
185 return WriteExponent(kk - 1, &buffer[0 + length + 2]);
189 inline char* dtoa(
double value,
char* buffer) {
205 Grisu2(value, buffer, &length, &K);
206 return Prettify(buffer, length, K);
215 RAPIDJSON_NAMESPACE_END
217 #endif // RAPIDJSON_DTOA_