36 template <
typename Target,
typename Source>
37 Target reinterpret_bits(
const Source
source)
39 static_assert(
sizeof(Target) ==
sizeof(Source),
"size mismatch");
42 std::memcpy(&target, &
source,
sizeof(Source));
48 static constexpr
int kPrecision = 64;
53 constexpr
diyfp() noexcept : f(0), e(0) {}
54 constexpr
diyfp(
uint64_t f_,
int e_) noexcept : f(f_), e(e_) {}
65 return diyfp(x.f - y.f, x.e);
74 static_assert(kPrecision == 64,
"internal error");
99 const uint64_t u_lo = x.f & 0xFFFFFFFF;
101 const uint64_t v_lo = y.f & 0xFFFFFFFF;
110 const uint64_t p1_lo = p1 & 0xFFFFFFFF;
112 const uint64_t p2_lo = p2 & 0xFFFFFFFF;
128 const uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32);
130 return diyfp(h, x.e + y.e + 64);
141 while ((x.f >> 63) == 0)
156 const int delta = x.e - target_exponent;
159 assert(((x.f << delta) >> delta) == x.f);
161 return diyfp(x.f << delta, target_exponent);
178 template <
typename FloatType>
181 assert(std::isfinite(value));
191 static_assert(std::numeric_limits<FloatType>::is_iec559,
192 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
194 constexpr
int kPrecision = std::numeric_limits<FloatType>::digits;
195 constexpr
int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
196 constexpr
int kMinExp = 1 - kBias;
199 using bits_type =
typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type;
201 const uint64_t bits = reinterpret_bits<bits_type>(value);
202 const uint64_t E = bits >> (kPrecision - 1);
203 const uint64_t F = bits & (kHiddenBit - 1);
205 const bool is_denormal = (E == 0);
206 const diyfp v = is_denormal
208 :
diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
231 const bool lower_boundary_is_closer = (F == 0 and E > 1);
232 const diyfp m_plus =
diyfp(2 * v.f + 1, v.e - 1);
233 const diyfp m_minus = lower_boundary_is_closer
234 ?
diyfp(4 * v.f - 1, v.e - 2)
235 :
diyfp(2 * v.f - 1, v.e - 1);
301 constexpr
int kAlpha = -60;
302 constexpr
int kGamma = -32;
370 constexpr
int kCachedPowersSize = 79;
371 constexpr
int kCachedPowersMinDecExp = -300;
372 constexpr
int kCachedPowersDecStep = 8;
376 { 0xAB70FE17C79AC6CA, -1060, -300 },
377 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
378 { 0xBE5691EF416BD60C, -1007, -284 },
379 { 0x8DD01FAD907FFC3C, -980, -276 },
380 { 0xD3515C2831559A83, -954, -268 },
381 { 0x9D71AC8FADA6C9B5, -927, -260 },
382 { 0xEA9C227723EE8BCB, -901, -252 },
383 { 0xAECC49914078536D, -874, -244 },
384 { 0x823C12795DB6CE57, -847, -236 },
385 { 0xC21094364DFB5637, -821, -228 },
386 { 0x9096EA6F3848984F, -794, -220 },
387 { 0xD77485CB25823AC7, -768, -212 },
388 { 0xA086CFCD97BF97F4, -741, -204 },
389 { 0xEF340A98172AACE5, -715, -196 },
390 { 0xB23867FB2A35B28E, -688, -188 },
391 { 0x84C8D4DFD2C63F3B, -661, -180 },
392 { 0xC5DD44271AD3CDBA, -635, -172 },
393 { 0x936B9FCEBB25C996, -608, -164 },
394 { 0xDBAC6C247D62A584, -582, -156 },
395 { 0xA3AB66580D5FDAF6, -555, -148 },
396 { 0xF3E2F893DEC3F126, -529, -140 },
397 { 0xB5B5ADA8AAFF80B8, -502, -132 },
398 { 0x87625F056C7C4A8B, -475, -124 },
399 { 0xC9BCFF6034C13053, -449, -116 },
400 { 0x964E858C91BA2655, -422, -108 },
401 { 0xDFF9772470297EBD, -396, -100 },
402 { 0xA6DFBD9FB8E5B88F, -369, -92 },
403 { 0xF8A95FCF88747D94, -343, -84 },
404 { 0xB94470938FA89BCF, -316, -76 },
405 { 0x8A08F0F8BF0F156B, -289, -68 },
406 { 0xCDB02555653131B6, -263, -60 },
407 { 0x993FE2C6D07B7FAC, -236, -52 },
408 { 0xE45C10C42A2B3B06, -210, -44 },
409 { 0xAA242499697392D3, -183, -36 },
410 { 0xFD87B5F28300CA0E, -157, -28 },
411 { 0xBCE5086492111AEB, -130, -20 },
412 { 0x8CBCCC096F5088CC, -103, -12 },
413 { 0xD1B71758E219652C, -77, -4 },
414 { 0x9C40000000000000, -50, 4 },
415 { 0xE8D4A51000000000, -24, 12 },
416 { 0xAD78EBC5AC620000, 3, 20 },
417 { 0x813F3978F8940984, 30, 28 },
418 { 0xC097CE7BC90715B3, 56, 36 },
419 { 0x8F7E32CE7BEA5C70, 83, 44 },
420 { 0xD5D238A4ABE98068, 109, 52 },
421 { 0x9F4F2726179A2245, 136, 60 },
422 { 0xED63A231D4C4FB27, 162, 68 },
423 { 0xB0DE65388CC8ADA8, 189, 76 },
424 { 0x83C7088E1AAB65DB, 216, 84 },
425 { 0xC45D1DF942711D9A, 242, 92 },
426 { 0x924D692CA61BE758, 269, 100 },
427 { 0xDA01EE641A708DEA, 295, 108 },
428 { 0xA26DA3999AEF774A, 322, 116 },
429 { 0xF209787BB47D6B85, 348, 124 },
430 { 0xB454E4A179DD1877, 375, 132 },
431 { 0x865B86925B9BC5C2, 402, 140 },
432 { 0xC83553C5C8965D3D, 428, 148 },
433 { 0x952AB45CFA97A0B3, 455, 156 },
434 { 0xDE469FBD99A05FE3, 481, 164 },
435 { 0xA59BC234DB398C25, 508, 172 },
436 { 0xF6C69A72A3989F5C, 534, 180 },
437 { 0xB7DCBF5354E9BECE, 561, 188 },
438 { 0x88FCF317F22241E2, 588, 196 },
439 { 0xCC20CE9BD35C78A5, 614, 204 },
440 { 0x98165AF37B2153DF, 641, 212 },
441 { 0xE2A0B5DC971F303A, 667, 220 },
442 { 0xA8D9D1535CE3B396, 694, 228 },
443 { 0xFB9B7CD9A4A7443C, 720, 236 },
444 { 0xBB764C4CA7A44410, 747, 244 },
445 { 0x8BAB8EEFB6409C1A, 774, 252 },
446 { 0xD01FEF10A657842C, 800, 260 },
447 { 0x9B10A4E5E9913129, 827, 268 },
448 { 0xE7109BFBA19C0C9D, 853, 276 },
449 { 0xAC2820D9623BF429, 880, 284 },
450 { 0x80444B5E7AA7CF85, 907, 292 },
451 { 0xBF21E44003ACDD2D, 933, 300 },
452 { 0x8E679C2F5E44FF8F, 960, 308 },
453 { 0xD433179D9C8CB841, 986, 316 },
454 { 0x9E19DB92B4E31BA9, 1013, 324 },
463 const int f = kAlpha - e - 1;
464 const int k = (f * 78913) / (1 << 18) + (f > 0);
466 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
468 assert(index < kCachedPowersSize);
469 static_cast<void>(kCachedPowersSize);
472 assert(kAlpha <= cached.e + e + 64);
473 assert(kGamma >= cached.e + e + 64);
491 else if (n >= 100000000)
496 else if (n >= 10000000)
501 else if (n >= 1000000)
506 else if (n >= 100000)
542 assert(dist <= delta);
543 assert(rest <= delta);
566 and delta - rest >= ten_k
567 and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
569 assert(buf[len - 1] !=
'0');
582 static_assert(kAlpha >= -60,
"internal error");
583 static_assert(kGamma <= -32,
"internal error");
597 assert(M_plus.e >= kAlpha);
598 assert(M_plus.e <= kGamma);
612 uint32_t p1 = static_cast<uint32_t>(M_plus.f >> -one.e);
613 uint64_t p2 = M_plus.f & (one.f - 1);
656 buffer[length++] = static_cast<char>(
'0' + d);
680 decimal_exponent += n;
692 grisu2_round(buffer, length, dist, delta, rest, ten_n);
753 assert(p2 <= UINT64_MAX / 10);
756 const uint64_t r = p2 & (one.f - 1);
763 buffer[length++] = static_cast<char>(
'0' + d);
788 decimal_exponent -= m;
797 grisu2_round(buffer, length, dist, delta, p2, ten_m);
819 inline void grisu2(
char* buf,
int& len,
int& decimal_exponent,
822 assert(m_plus.e == m_minus.e);
823 assert(m_plus.e == v.e);
836 const diyfp c_minus_k(cached.f, cached.e);
864 const diyfp M_minus(w_minus.f + 1, w_minus.e);
865 const diyfp M_plus (w_plus.f - 1, w_plus.e );
867 decimal_exponent = -cached.k;
877 template <
typename FloatType>
878 void grisu2(
char* buf,
int& len,
int& decimal_exponent, FloatType value)
880 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
881 "internal error: not enough precision");
883 assert(std::isfinite(value));
908 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
931 uint32_t k = static_cast<uint32_t>(e);
937 *buf++ = static_cast<char>(
'0' + k);
941 *buf++ = static_cast<char>(
'0' + k / 10);
943 *buf++ = static_cast<char>(
'0' + k);
947 *buf++ = static_cast<char>(
'0' + k / 100);
949 *buf++ = static_cast<char>(
'0' + k / 10);
951 *buf++ = static_cast<char>(
'0' + k);
967 int min_exp,
int max_exp)
973 const int n = len + decimal_exponent;
979 if (k <= n and n <= max_exp)
984 std::memset(buf + k,
'0', static_cast<size_t>(n - k));
988 return buf + (n + 2);
991 if (0 < n and n <= max_exp)
998 std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
1000 return buf + (k + 1);
1003 if (min_exp < n and n <= 0)
1008 std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
1011 std::memset(buf + 2,
'0', static_cast<size_t>(-n));
1012 return buf + (2 + (-n) + k);
1027 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k - 1));
1048 template <
typename FloatType>
1049 char*
to_chars(
char* first,
char* last, FloatType value)
1051 static_cast<void>(last);
1052 assert(std::isfinite(value));
1055 if (std::signbit(value))
1070 assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
1077 int decimal_exponent = 0;
1080 assert(len <= std::numeric_limits<FloatType>::max_digits10);
1083 constexpr
int kMinExp = -4;
1085 constexpr
int kMaxExp = std::numeric_limits<FloatType>::digits10;
1087 assert(last - first >= kMaxExp + 2);
1088 assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
1089 assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);