12 #define UINT64_C2(h, l) ((static_cast<uint64_t>(h) << 32) | static_cast<uint64_t>(l))
50 #if defined(_MSC_VER) && defined(_M_AMD64)
52 uint64_t l = _umul128(
f, rhs.
f, &h);
53 if (l & (uint64_t(1) << 63))
55 return DiyFp(h,
e + rhs.
e + 64);
56 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
57 unsigned __int128 p =
static_cast<unsigned __int128
>(
f) *
static_cast<unsigned __int128
>(rhs.
f);
59 uint64_t l =
static_cast<uint64_t
>(p);
60 if (l & (uint64_t(1) << 63))
62 return DiyFp(h,
e + rhs.
e + 64);
64 const uint64_t M32 = 0xFFFFFFFF;
65 const uint64_t a =
f >> 32;
66 const uint64_t b =
f & M32;
67 const uint64_t c = rhs.
f >> 32;
68 const uint64_t d = rhs.
f & M32;
69 const uint64_t ac = a * c;
70 const uint64_t bc = b * c;
71 const uint64_t ad = a * d;
72 const uint64_t bd = b * d;
73 uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
75 return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32),
e + rhs.
e + 64);
81 #if defined(_MSC_VER) && defined(_M_AMD64)
83 _BitScanReverse64(&index,
f);
84 return DiyFp(
f << (63 - index),
e - (63 - index));
85 #elif defined(__GNUC__)
86 int s = __builtin_clzll(
f);
103 #if defined(_MSC_VER) && defined(_M_AMD64)
105 _BitScanReverse64(&index,
f);
106 return DiyFp(
f << (63 - index),
e - (63 - index));
122 DiyFp pl =
DiyFp((
f << 1) + 1,
e - 1).NormalizeBoundary();
124 mi.
f <<= mi.
e - pl.
e;
145 static const uint64_t kCachedPowers_F[] = {
168 static const int16_t kCachedPowers_E[] = {
169 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, -688, -661,
170 -635, -608, -582, -555, -529, -502, -475, -449, -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77,
171 -50, -24, 3, 30, 56, 83, 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, 375, 402, 428, 455, 481, 508,
172 534, 561, 588, 614, 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066};
175 double dk = (-61 - e) * 0.30102999566398114 + 347;
176 int k =
static_cast<int>(dk);
180 unsigned index =
static_cast<unsigned>((k >> 3) + 1);
181 *K = -(-348 +
static_cast<int>(index << 3));
183 assert(index <
sizeof(kCachedPowers_F) /
sizeof(kCachedPowers_F[0]));
184 return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
187 inline void GrisuRound(
char* buffer,
int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w)
189 while (rest < wp_w && delta - rest >= ten_kappa &&
190 (rest + ten_kappa < wp_w ||
191 wp_w - rest > rest + ten_kappa - wp_w))
222 inline void DigitGen(
const DiyFp& W,
const DiyFp& Mp, uint64_t delta,
char* buffer,
int* len,
int* K)
224 static const uint32_t kPow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
225 const DiyFp one(uint64_t(1) << -Mp.
e, Mp.
e);
226 const DiyFp wp_w = Mp - W;
227 uint32_t p1 =
static_cast<uint32_t
>(Mp.
f >> -one.
e);
228 uint64_t p2 = Mp.
f & (one.
f - 1);
278 #if defined(_MSC_VER)
280 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
281 __builtin_unreachable();
287 buffer[(*len)++] =
'0' +
static_cast<char>(d);
289 uint64_t tmp = (
static_cast<uint64_t
>(p1) << -one.
e) + p2;
293 GrisuRound(buffer, *len, delta, tmp,
static_cast<uint64_t
>(kPow10[kappa]) << -one.
e, wp_w.
f);
303 char d =
static_cast<char>(p2 >> -one.
e);
305 buffer[(*len)++] =
'0' + d;
311 GrisuRound(buffer, *len, delta, p2, one.
f, wp_w.
f * kPow10[-kappa]);
317 inline void Grisu2(
double value,
char* buffer,
int* length,
int* K)
319 const DiyFp v(value);
325 DiyFp Wp = w_p * c_mk;
326 DiyFp Wm = w_m * c_mk;
329 DigitGen(W, Wp, Wp.
f - Wm.
f, buffer, length, K);
334 static const char cDigitsLut[200] = {
335 '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',
336 '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',
337 '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',
338 '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',
339 '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',
340 '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',
341 '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',
342 '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'};
356 *buffer++ =
'0' +
static_cast<char>(K / 100);
369 *buffer++ =
'0' +
static_cast<char>(K);
374 inline void Prettify(
char* buffer,
int length,
int k)
376 const int kk = length + k;
378 if (length <= kk && kk <= 21)
381 for (
int i = length; i < kk; i++)
384 buffer[kk + 1] =
'0';
385 buffer[kk + 2] =
'\0';
387 else if (0 < kk && kk <= 21)
390 memmove(&buffer[kk + 1], &buffer[kk], length - kk);
392 buffer[length + 1] =
'\0';
394 else if (-6 < kk && kk <= 0)
397 const int offset = 2 - kk;
398 memmove(&buffer[offset], &buffer[0], length);
401 for (
int i = 2; i < offset; i++)
403 buffer[length + offset] =
'\0';
405 else if (length == 1)
414 memmove(&buffer[2], &buffer[1], length - 1);
416 buffer[length + 1] =
'e';
424 assert(!isnan(value));
425 assert(!isinf(value));
442 Grisu2(value, buffer, &length, &K);