11 #define UINT64_C2(h, l) ((static_cast<uint64_t>(h) << 32) | static_cast<uint64_t>(l)) 49 #if defined(_MSC_VER) && defined(_M_AMD64) 51 uint64_t l = _umul128(
f, rhs.
f, &h);
52 if (l & (uint64_t(1) << 63))
54 return DiyFp(h,
e + rhs.
e + 64);
55 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) 56 unsigned __int128 p = static_cast<unsigned __int128>(
f) * static_cast<unsigned __int128>(rhs.
f);
58 uint64_t l = static_cast<uint64_t>(p);
59 if (l & (uint64_t(1) << 63))
61 return DiyFp(h,
e + rhs.
e + 64);
63 const uint64_t M32 = 0xFFFFFFFF;
64 const uint64_t a =
f >> 32;
65 const uint64_t b =
f & M32;
66 const uint64_t c = rhs.
f >> 32;
67 const uint64_t d = rhs.
f & M32;
68 const uint64_t ac = a * c;
69 const uint64_t bc = b * c;
70 const uint64_t ad = a * d;
71 const uint64_t bd = b * d;
72 uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
74 return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32),
e + rhs.
e + 64);
80 #if defined(_MSC_VER) && defined(_M_AMD64) 82 _BitScanReverse64(&index,
f);
83 return DiyFp(
f << (63 - index),
e - (63 - index));
84 #elif defined(__GNUC__) 85 int s = __builtin_clzll(
f);
102 #if defined(_MSC_VER) && defined(_M_AMD64) 104 _BitScanReverse64(&index,
f);
105 return DiyFp(
f << (63 - index),
e - (63 - index));
121 DiyFp pl =
DiyFp((
f << 1) + 1,
e - 1).NormalizeBoundary();
123 mi.
f <<= mi.
e - pl.
e;
144 static const uint64_t kCachedPowers_F[] = {
167 static const int16_t kCachedPowers_E[] = {
168 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, -688, -661,
169 -635, -608, -582, -555, -529, -502, -475, -449, -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77,
170 -50, -24, 3, 30, 56, 83, 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, 375, 402, 428, 455, 481, 508,
171 534, 561, 588, 614, 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066};
174 double dk = (-61 - e) * 0.30102999566398114 + 347;
175 int k = static_cast<int>(dk);
179 unsigned index = static_cast<unsigned>((k >> 3) + 1);
180 *K = -(-348 + static_cast<int>(index << 3));
182 assert(index <
sizeof(kCachedPowers_F) /
sizeof(kCachedPowers_F[0]));
183 return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
186 inline void GrisuRound(
char* buffer,
int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w)
188 while (rest < wp_w && delta - rest >= ten_kappa &&
189 (rest + ten_kappa < wp_w ||
190 wp_w - rest > rest + ten_kappa - wp_w))
221 inline void DigitGen(
const DiyFp& W,
const DiyFp& Mp, uint64_t delta,
char* buffer,
int* len,
int* K)
223 static const uint32_t kPow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
224 const DiyFp one(uint64_t(1) << -Mp.
e, Mp.
e);
225 const DiyFp wp_w = Mp - W;
226 uint32_t p1 = static_cast<uint32_t>(Mp.
f >> -one.
e);
227 uint64_t p2 = Mp.
f & (one.
f - 1);
277 #if defined(_MSC_VER) 279 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 280 __builtin_unreachable();
286 buffer[(*len)++] =
'0' + static_cast<char>(d);
288 uint64_t tmp = (static_cast<uint64_t>(p1) << -one.
e) + p2;
292 GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.
e, wp_w.
f);
302 char d = static_cast<char>(p2 >> -one.
e);
304 buffer[(*len)++] =
'0' + d;
310 GrisuRound(buffer, *len, delta, p2, one.
f, wp_w.
f * kPow10[-kappa]);
316 inline void Grisu2(
double value,
char* buffer,
int* length,
int* K)
318 const DiyFp v(value);
324 DiyFp Wp = w_p * c_mk;
325 DiyFp Wm = w_m * c_mk;
328 DigitGen(W, Wp, Wp.
f - Wm.
f, buffer, length, K);
333 static const char cDigitsLut[200] = {
334 '0',
'0',
'0',
'1',
'0',
'2',
'0',
'3',
'0',
'4',
'0',
'5',
'0',
'6',
'0',
'7',
'0',
'8',
'0',
'9',
'1',
'0',
'1',
'1',
'1',
335 '2',
'1',
'3',
'1',
'4',
'1',
'5',
'1',
'6',
'1',
'7',
'1',
'8',
'1',
'9',
'2',
'0',
'2',
'1',
'2',
'2',
'2',
'3',
'2',
'4',
336 '2',
'5',
'2',
'6',
'2',
'7',
'2',
'8',
'2',
'9',
'3',
'0',
'3',
'1',
'3',
'2',
'3',
'3',
'3',
'4',
'3',
'5',
'3',
'6',
'3',
337 '7',
'3',
'8',
'3',
'9',
'4',
'0',
'4',
'1',
'4',
'2',
'4',
'3',
'4',
'4',
'4',
'5',
'4',
'6',
'4',
'7',
'4',
'8',
'4',
'9',
338 '5',
'0',
'5',
'1',
'5',
'2',
'5',
'3',
'5',
'4',
'5',
'5',
'5',
'6',
'5',
'7',
'5',
'8',
'5',
'9',
'6',
'0',
'6',
'1',
'6',
339 '2',
'6',
'3',
'6',
'4',
'6',
'5',
'6',
'6',
'6',
'7',
'6',
'8',
'6',
'9',
'7',
'0',
'7',
'1',
'7',
'2',
'7',
'3',
'7',
'4',
340 '7',
'5',
'7',
'6',
'7',
'7',
'7',
'8',
'7',
'9',
'8',
'0',
'8',
'1',
'8',
'2',
'8',
'3',
'8',
'4',
'8',
'5',
'8',
'6',
'8',
341 '7',
'8',
'8',
'8',
'9',
'9',
'0',
'9',
'1',
'9',
'2',
'9',
'3',
'9',
'4',
'9',
'5',
'9',
'6',
'9',
'7',
'9',
'8',
'9',
'9'};
355 *buffer++ =
'0' + static_cast<char>(K / 100);
368 *buffer++ =
'0' + static_cast<char>(K);
373 inline void Prettify(
char* buffer,
int length,
int k)
375 const int kk = length + k;
377 if (length <= kk && kk <= 21)
380 for (
int i = length; i < kk; i++)
383 buffer[kk + 1] =
'0';
384 buffer[kk + 2] =
'\0';
386 else if (0 < kk && kk <= 21)
389 memmove(&buffer[kk + 1], &buffer[kk], length - kk);
391 buffer[length + 1] =
'\0';
393 else if (-6 < kk && kk <= 0)
396 const int offset = 2 - kk;
397 memmove(&buffer[offset], &buffer[0], length);
400 for (
int i = 2; i < offset; i++)
402 buffer[length + offset] =
'\0';
404 else if (length == 1)
413 memmove(&buffer[2], &buffer[1], length - 1);
415 buffer[length + 1] =
'e';
423 assert(!isnan(value));
424 assert(!isinf(value));
441 Grisu2(value, buffer, &length, &K);
static const uint64_t kDpHiddenBit
Definition: dtoa.h:135
static const int kDiySignificandSize
Definition: dtoa.h:129
DiyFp operator-(const DiyFp &rhs) const
Definition: dtoa.h:40
static const uint64_t kDpExponentMask
Definition: dtoa.h:133
int e
Definition: dtoa.h:138
void DigitGen(const DiyFp &W, const DiyFp &Mp, uint64_t delta, char *buffer, int *len, int *K)
Definition: dtoa.h:221
static const int kDpSignificandSize
Definition: dtoa.h:130
DiyFp Normalize() const
Definition: dtoa.h:78
void dtoa_milo(double value, char *buffer)
Definition: dtoa.h:420
void Grisu2(double value, char *buffer, int *length, int *K)
Definition: dtoa.h:316
void GrisuRound(char *buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w)
Definition: dtoa.h:186
DiyFp(uint64_t f, int e)
Definition: dtoa.h:17
void NormalizedBoundaries(DiyFp *minus, DiyFp *plus) const
Definition: dtoa.h:119
static const uint64_t kDpSignificandMask
Definition: dtoa.h:134
#define UINT64_C2(h, l)
Definition: dtoa.h:11
static const int kDpExponentBias
Definition: dtoa.h:131
DiyFp()
Definition: dtoa.h:15
unsigned CountDecimalDigit32(uint32_t n)
Definition: dtoa.h:197
DiyFp operator *(const DiyFp &rhs) const
Definition: dtoa.h:47
void Prettify(char *buffer, int length, int k)
Definition: dtoa.h:373
void WriteExponent(int K, char *buffer)
Definition: dtoa.h:345
uint64_t f
Definition: dtoa.h:137
const char * GetDigitsLut()
Definition: dtoa.h:331
DiyFp(double d)
Definition: dtoa.h:19
static const int kDpMinExponent
Definition: dtoa.h:132
DiyFp GetCachedPower(int e, int *K)
Definition: dtoa.h:141
DiyFp NormalizeBoundary() const
Definition: dtoa.h:100