19 #ifndef RAPIDJSON_DTOA_
20 #define RAPIDJSON_DTOA_
31 RAPIDJSON_DIAG_OFF(effc++)
32 RAPIDJSON_DIAG_OFF(array-bounds)
36 while (rest < wp_w && delta - rest >= ten_kappa &&
37 (rest + ten_kappa < wp_w ||
38 wp_w - rest > rest + ten_kappa - wp_w)) {
47 if (n < 100)
return 2;
48 if (n < 1000)
return 3;
49 if (n < 10000)
return 4;
50 if (n < 100000)
return 5;
51 if (n < 1000000)
return 6;
52 if (n < 10000000)
return 7;
53 if (n < 100000000)
return 8;
61 static const uint64_t kPow10[] = { 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL, 10000000ULL, 100000000ULL,
62 1000000000ULL, 10000000000ULL, 100000000000ULL, 1000000000000ULL,
63 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
64 10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL,
65 10000000000000000000ULL };
67 const DiyFp wp_w = Mp - W;
76 case 9: d = p1 / 100000000; p1 %= 100000000;
break;
77 case 8: d = p1 / 10000000; p1 %= 10000000;
break;
78 case 7: d = p1 / 1000000; p1 %= 1000000;
break;
79 case 6: d = p1 / 100000; p1 %= 100000;
break;
80 case 5: d = p1 / 10000; p1 %= 10000;
break;
81 case 4: d = p1 / 1000; p1 %= 1000;
break;
82 case 3: d = p1 / 100; p1 %= 100;
break;
83 case 2: d = p1 / 10; p1 %= 10;
break;
84 case 1: d = p1; p1 = 0;
break;
88 buffer[(*len)++] =
static_cast<char>(
'0' +
static_cast<char>(d));
93 GrisuRound(buffer, *len, delta, tmp, kPow10[kappa] << -one.
e, wp_w.
f);
102 char d =
static_cast<char>(p2 >> -one.
e);
104 buffer[(*len)++] =
static_cast<char>(
'0' + d);
110 GrisuRound(buffer, *len, delta, p2, one.
f, wp_w.
f * (index < 20 ? kPow10[index] : 0));
116 inline void Grisu2(
double value,
char* buffer,
int* length,
int* K) {
123 DiyFp Wp = w_p * c_mk;
124 DiyFp Wm = w_m * c_mk;
127 DigitGen(W, Wp, Wp.
f - Wm.
f, buffer, length, K);
137 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K / 100));
149 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K));
154 inline char*
Prettify(
char* buffer,
int length,
int k,
int maxDecimalPlaces) {
155 const int kk = length + k;
157 if (0 <= k && kk <= 21) {
159 for (
int i = length; i < kk; i++)
162 buffer[kk + 1] =
'0';
163 return &buffer[kk + 2];
165 else if (0 < kk && kk <= 21) {
167 std::memmove(&buffer[kk + 1], &buffer[kk],
static_cast<size_t>(length - kk));
169 if (0 > k + maxDecimalPlaces) {
172 for (
int i = kk + maxDecimalPlaces; i > kk + 1; i--)
173 if (buffer[i] !=
'0')
174 return &buffer[i + 1];
175 return &buffer[kk + 2];
178 return &buffer[length + 1];
180 else if (-6 < kk && kk <= 0) {
182 const int offset = 2 - kk;
183 std::memmove(&buffer[offset], &buffer[0],
static_cast<size_t>(length));
186 for (
int i = 2; i < offset; i++)
188 if (length - kk > maxDecimalPlaces) {
191 for (
int i = maxDecimalPlaces + 1; i > 2; i--)
192 if (buffer[i] !=
'0')
193 return &buffer[i + 1];
197 return &buffer[length + offset];
199 else if (kk < -maxDecimalPlaces) {
206 else if (length == 1) {
213 std::memmove(&buffer[2], &buffer[1],
static_cast<size_t>(length - 1));
215 buffer[length + 1] =
'e';
220 inline char*
dtoa(
double value,
char* buffer,
int maxDecimalPlaces = 324) {
238 return Prettify(buffer, length, K, maxDecimalPlaces);
bool IsZero() const
Definition: ieee754.h:45
bool Sign() const
Definition: ieee754.h:37
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Definition: allocators.h:423
char * Prettify(char *buffer, int length, int k, int maxDecimalPlaces)
Definition: dtoa.h:154
void GrisuRound(char *buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w)
Definition: dtoa.h:35
char * dtoa(double value, char *buffer, int maxDecimalPlaces=324)
Definition: dtoa.h:220
void DigitGen(const DiyFp &W, const DiyFp &Mp, uint64_t delta, char *buffer, int *len, int *K)
Definition: dtoa.h:60
int CountDecimalDigit32(uint32_t n)
Definition: dtoa.h:44
const char * GetDigitsLut()
Definition: itoa.h:23
DiyFp GetCachedPower(int e, int *K)
Definition: diyfp.h:228
char * WriteExponent(int K, char *buffer)
Definition: dtoa.h:130
void Grisu2(double value, char *buffer, int *length, int *K)
Definition: dtoa.h:116
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1350
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
uint64_t f
Definition: diyfp.h:161
DiyFp Normalize() const
Definition: diyfp.h:106
void NormalizedBoundaries(DiyFp *minus, DiyFp *plus) const
Definition: diyfp.h:122
int e
Definition: diyfp.h:162