19#if defined(_MSC_VER) && defined(_M_X64)
56#define P(T) T, '0', T, '1', T, '2', T, '3', T, '4', T, '5', T, '6', T, '7', T, '8', T, '9'
58static const pair s_pairs[] = {
P(
'0'),
P(
'1'),
P(
'2'),
P(
'3'),
P(
'4'),
P(
'5'),
P(
'6'),
P(
'7'),
P(
'8'),
P(
'9') };
60#define W(N, I) *(pair*)&b[N] = s_pairs[I]
62#define A(N) t = (uint64_t(1) << (32 + N / 5 * N * 53 / 16)) / uint32_t(1e##N) + 1 + N / 6 - N / 8, t *= u, t >>= N / 5 * N * 53 / 16, t += N / 6 * 4, W(0, t >> 32)
64#define S(N) b[N] = char(uint64_t(10) * uint32_t(t) >> 32) + '0'
66#define D(N) t = uint64_t(100) * uint32_t(t), W(N, t >> 32)
68#define L0 b[0] = char(u) + '0'
72#define L4 A(3), D(2), S(4)
73#define L5 A(4), D(2), D(4)
74#define L6 A(5), D(2), D(4), S(6)
75#define L7 A(6), D(2), D(4), D(6)
76#define L8 A(7), D(2), D(4), D(6), S(8)
77#define L9 A(8), D(2), D(4), D(6), D(8)
79#define LN(N) (L##N, b += N + 1)
85 (u < 100 ? u < 10 ? F(0) : F(1) : \
86 u < 1000000 ? u < 10000 ? u < 1000 ? F(2) : F(3) : \
89 u < 100000000 ? u < 10000000 ? F(6) : F(7) : \
90 u < 1000000000 ? F(8) : \
98 if (uint32_t(n >> 32) == 0)
104 uint64_t a = n / 100000000;
106 if (uint32_t(a >> 32) == 0)
113 u = uint32_t(a / 100000000);
153template <
typename Target,
typename Source>
156 static_assert(
sizeof(Target) ==
sizeof(Source),
"size mismatch");
159 std::memcpy(&target, &source,
sizeof(Source));
170 constexpr diyfp(std::uint64_t f_,
int e_) noexcept
183 return { x.
f - y.f, x.e };
192 static_assert(
kPrecision == 64,
"internal error");
198#if defined(_MSC_VER) && defined(_M_X64)
201 uint64_t l = _umul128(x.f, y.f, &h);
204 return { h, x.e + y.e + 64 };
206#elif defined(__GNUC__) && defined(__SIZEOF_INT128__)
208 __extension__
using Uint128 =
unsigned __int128;
210 Uint128
const p = Uint128{x.f} * Uint128{y.f};
212 uint64_t h =
static_cast<uint64_t
>(p >> 64);
213 uint64_t l =
static_cast<uint64_t
>(p);
216 return { h, x.e + y.e + 64 };
241 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
242 const std::uint64_t u_hi = x.f >> 32u;
243 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
244 const std::uint64_t v_hi = y.f >> 32u;
246 const std::uint64_t p0 = u_lo * v_lo;
247 const std::uint64_t p1 = u_lo * v_hi;
248 const std::uint64_t p2 = u_hi * v_lo;
249 const std::uint64_t p3 = u_hi * v_hi;
251 const std::uint64_t p0_hi = p0 >> 32u;
252 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
253 const std::uint64_t p1_hi = p1 >> 32u;
254 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
255 const std::uint64_t p2_hi = p2 >> 32u;
257 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
268 Q += std::uint64_t { 1 } << (64u - 32u - 1u);
270 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
272 return { h, x.e + y.e + 64 };
283 while ((x.f >> 63u) == 0)
298 const int delta = x.
e - target_exponent;
300 return { x.f << delta, target_exponent };
327 std::numeric_limits<FloatType>::is_iec559,
328 "internal error: dtoa_short requires an IEEE-754 "
329 "floating-point implementation");
331 constexpr int kPrecision =
332 std::numeric_limits<FloatType>::digits;
333 constexpr int kBias =
334 std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
335 constexpr int kMinExp = 1 - kBias;
336 constexpr std::uint64_t kHiddenBit = std::uint64_t { 1 }
339 using bits_type = std::conditional_t<
340 kPrecision == 24, std::uint32_t, std::uint64_t>;
342 const std::uint64_t bits = reinterpret_bits<bits_type>(value);
343 const std::uint64_t E = bits >> (kPrecision - 1);
344 const std::uint64_t F = bits & (kHiddenBit - 1);
346 const bool is_denormal = E == 0;
347 const diyfp v = is_denormal ?
349 diyfp(F + kHiddenBit,
static_cast<int>(E) - kBias);
372 const bool lower_boundary_is_closer = F == 0 && E > 1;
374 const diyfp m_minus = lower_boundary_is_closer ?
511 constexpr int kCachedPowersMinDecExp = -300;
512 constexpr int kCachedPowersDecStep = 8;
514 static constexpr std::array<cached_power, 79> kCachedPowers = { {
515 { 0xAB70FE17C79AC6CA, -1060, -300 }, { 0xFF77B1FCBEBCDC4F, -1034, -292 },
516 { 0xBE5691EF416BD60C, -1007, -284 }, { 0x8DD01FAD907FFC3C, -980, -276 },
517 { 0xD3515C2831559A83, -954, -268 }, { 0x9D71AC8FADA6C9B5, -927, -260 },
518 { 0xEA9C227723EE8BCB, -901, -252 }, { 0xAECC49914078536D, -874, -244 },
519 { 0x823C12795DB6CE57, -847, -236 }, { 0xC21094364DFB5637, -821, -228 },
520 { 0x9096EA6F3848984F, -794, -220 }, { 0xD77485CB25823AC7, -768, -212 },
521 { 0xA086CFCD97BF97F4, -741, -204 }, { 0xEF340A98172AACE5, -715, -196 },
522 { 0xB23867FB2A35B28E, -688, -188 }, { 0x84C8D4DFD2C63F3B, -661, -180 },
523 { 0xC5DD44271AD3CDBA, -635, -172 }, { 0x936B9FCEBB25C996, -608, -164 },
524 { 0xDBAC6C247D62A584, -582, -156 }, { 0xA3AB66580D5FDAF6, -555, -148 },
525 { 0xF3E2F893DEC3F126, -529, -140 }, { 0xB5B5ADA8AAFF80B8, -502, -132 },
526 { 0x87625F056C7C4A8B, -475, -124 }, { 0xC9BCFF6034C13053, -449, -116 },
527 { 0x964E858C91BA2655, -422, -108 }, { 0xDFF9772470297EBD, -396, -100 },
528 { 0xA6DFBD9FB8E5B88F, -369, -92 }, { 0xF8A95FCF88747D94, -343, -84 },
529 { 0xB94470938FA89BCF, -316, -76 }, { 0x8A08F0F8BF0F156B, -289, -68 },
530 { 0xCDB02555653131B6, -263, -60 }, { 0x993FE2C6D07B7FAC, -236, -52 },
531 { 0xE45C10C42A2B3B06, -210, -44 }, { 0xAA242499697392D3, -183, -36 },
532 { 0xFD87B5F28300CA0E, -157, -28 }, { 0xBCE5086492111AEB, -130, -20 },
533 { 0x8CBCCC096F5088CC, -103, -12 }, { 0xD1B71758E219652C, -77, -4 },
534 { 0x9C40000000000000, -50, 4 }, { 0xE8D4A51000000000, -24, 12 },
535 { 0xAD78EBC5AC620000, 3, 20 }, { 0x813F3978F8940984, 30, 28 },
536 { 0xC097CE7BC90715B3, 56, 36 }, { 0x8F7E32CE7BEA5C70, 83, 44 },
537 { 0xD5D238A4ABE98068, 109, 52 }, { 0x9F4F2726179A2245, 136, 60 },
538 { 0xED63A231D4C4FB27, 162, 68 }, { 0xB0DE65388CC8ADA8, 189, 76 },
539 { 0x83C7088E1AAB65DB, 216, 84 }, { 0xC45D1DF942711D9A, 242, 92 },
540 { 0x924D692CA61BE758, 269, 100 }, { 0xDA01EE641A708DEA, 295, 108 },
541 { 0xA26DA3999AEF774A, 322, 116 }, { 0xF209787BB47D6B85, 348, 124 },
542 { 0xB454E4A179DD1877, 375, 132 }, { 0x865B86925B9BC5C2, 402, 140 },
543 { 0xC83553C5C8965D3D, 428, 148 }, { 0x952AB45CFA97A0B3, 455, 156 },
544 { 0xDE469FBD99A05FE3, 481, 164 }, { 0xA59BC234DB398C25, 508, 172 },
545 { 0xF6C69A72A3989F5C, 534, 180 }, { 0xB7DCBF5354E9BECE, 561, 188 },
546 { 0x88FCF317F22241E2, 588, 196 }, { 0xCC20CE9BD35C78A5, 614, 204 },
547 { 0x98165AF37B2153DF, 641, 212 }, { 0xE2A0B5DC971F303A, 667, 220 },
548 { 0xA8D9D1535CE3B396, 694, 228 }, { 0xFB9B7CD9A4A7443C, 720, 236 },
549 { 0xBB764C4CA7A44410, 747, 244 }, { 0x8BAB8EEFB6409C1A, 774, 252 },
550 { 0xD01FEF10A657842C, 800, 260 }, { 0x9B10A4E5E9913129, 827, 268 },
551 { 0xE7109BFBA19C0C9D, 853, 276 }, { 0xAC2820D9623BF429, 880, 284 },
552 { 0x80444B5E7AA7CF85, 907, 292 }, { 0xBF21E44003ACDD2D, 933, 300 },
553 { 0x8E679C2F5E44FF8F, 960, 308 }, { 0xD433179D9C8CB841, 986, 316 },
554 { 0x9E19DB92B4E31BA9, 1013, 324 },
561 const int f =
kAlpha - e - 1;
562 const int k = (f * 78913) / (1 << 18) +
static_cast<int>(f > 0);
565 (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) /
566 kCachedPowersDecStep;
568 const cached_power cached = kCachedPowers[
static_cast<std::size_t
>(index)];
586 else if (n >= 100000000)
591 else if (n >= 10000000)
596 else if (n >= 1000000)
601 else if (n >= 100000)
634 char* buf,
int len, std::uint64_t dist, std::uint64_t delta,
635 std::uint64_t rest, std::uint64_t ten_k)
657 while (rest < dist && delta - rest >= ten_k &&
658 (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
670 char* buffer,
char* last,
int& length,
int& decimal_exponent,
diyfp M_minus,
diyfp w,
673 static_assert(
kAlpha >= -60,
"internal error");
674 static_assert(
kGamma <= -32,
"internal error");
676 const int max_length =
static_cast<int>(last - buffer);
691 std::uint64_t delta =
705 const diyfp one(std::uint64_t { 1 } << -M_plus.
e, M_plus.
e);
707 auto p1 =
static_cast<std::uint32_t
>(
710 std::uint64_t p2 = M_plus.
f & (one.f - 1);
741 if (length >= max_length)
748 const std::uint32_t d = p1 / pow10;
749 const std::uint32_t r = p1 % pow10;
755 static_cast<char>(
'0' + d);
774 const std::uint64_t rest = (std::uint64_t { p1 } << -one.e) + p2;
779 decimal_exponent += n;
790 const std::uint64_t ten_n = std::uint64_t { pow10 } << -one.e;
845 if (length >= max_length)
857 const std::uint64_t d = p2 >> -one.e;
858 const std::uint64_t r = p2 & (one.f - 1);
865 static_cast<char>(
'0' + d);
890 decimal_exponent -= m;
898 const std::uint64_t ten_m = one.f;
924 char* buf,
char* last,
int& len,
int& decimal_exponent,
diyfp m_minus,
diyfp v,
939 const diyfp c_minus_k(cached.
f, cached.
e);
968 const diyfp M_minus(w_minus.
f + 1, w_minus.
e);
969 const diyfp M_plus(w_plus.
f - 1, w_plus.
e);
971 decimal_exponent = -cached.
k;
973 return grisu2_digit_gen(buf, last, len, decimal_exponent, M_minus, w, M_plus);
981template <
typename FloatType>
982bool grisu2(
char* buf,
char* last,
int& len,
int& decimal_exponent, FloatType value)
986 "internal error: not enough precision");
1024 return { last, std::errc::value_too_large };
1036 auto k =
static_cast<std::uint32_t
>(e);
1038 const int requiredSymbolsCount = k < 100 ? 2 : 3;
1039 char* requiredLast = buf + requiredSymbolsCount + 1;
1041 if (requiredLast > last)
1042 return { last, std::errc::value_too_large };
1049 *buf++ =
static_cast<char>(
'0' + k);
1053 *buf++ =
static_cast<char>(
'0' + k / 10);
1055 *buf++ =
static_cast<char>(
'0' + k);
1059 *buf++ =
static_cast<char>(
'0' + k / 100);
1061 *buf++ =
static_cast<char>(
'0' + k / 10);
1063 *buf++ =
static_cast<char>(
'0' + k);
1066 return { buf, std::errc() };
1077 char* buf,
char* last,
int len,
int decimal_exponent,
int min_exp,
int max_exp)
1080 const int n = len + decimal_exponent;
1087 if (k <= n && n <= max_exp)
1089 char* requiredLast = buf + (
static_cast<size_t>(n));
1090 if (requiredLast > last)
1091 return { last, std::errc::value_too_large };
1096 buf + k,
'0',
static_cast<size_t>(n) -
static_cast<size_t>(k));
1100 return { requiredLast, std::errc() };
1103 if (0 < n && n <= max_exp)
1105 char* requiredLast = buf + (
static_cast<size_t>(k) + 1U);
1107 if (requiredLast > last)
1108 return { last, std::errc::value_too_large };
1112 buf + (
static_cast<size_t>(n) + 1), buf + n,
1113 static_cast<size_t>(k) -
static_cast<size_t>(n));
1116 return { requiredLast, std::errc() };
1119 if (min_exp < n && n <= 0)
1121 char* requiredLast =
1122 buf + (2U +
static_cast<size_t>(-n) +
static_cast<size_t>(k));
1124 if (requiredLast > last)
1125 return { last, std::errc::value_too_large };
1130 buf + (2 +
static_cast<size_t>(-n)), buf,
static_cast<size_t>(k));
1133 std::memset(buf + 2,
'0',
static_cast<size_t>(-n));
1135 return { requiredLast, std::errc() };
1140 char* requiredLast = buf + 1;
1142 if (requiredLast > last)
1143 return { last, std::errc::value_too_large };
1151 char* requiredLast = buf + 1 +
static_cast<size_t>(k);
1153 if (requiredLast > last)
1154 return { last, std::errc::value_too_large };
1158 std::memmove(buf + 2, buf + 1,
static_cast<size_t>(k) - 1);
1161 buf += 1 +
static_cast<size_t>(k);
1179 char* first,
char* last, T value,
int digitsAfterDecimalPoint)
1181 if (first >= last || first ==
nullptr)
1182 return { last, std::errc::value_too_large };
1188 return { first, std::errc() };
1191 if (std::signbit(value))
1201 int decimal_exponent = 0;
1203 return { last, std::errc::value_too_large };
1205 const int kMinExp = digitsAfterDecimalPoint < 0 ? -4 : -digitsAfterDecimalPoint;
1206 constexpr int kMaxExp = std::numeric_limits<double>::digits10;
1210 if (digitsAfterDecimalPoint >= 0)
1212 if (len + decimal_exponent > 0 && -decimal_exponent > digitsAfterDecimalPoint)
1214 const int difference = digitsAfterDecimalPoint + decimal_exponent;
1215 decimal_exponent = -digitsAfterDecimalPoint;
1221 first, last, len, decimal_exponent, kMinExp, kMaxExp);
1226 char* buffer,
char* last,
float value,
1227 int digitsAfterDecimalPoint)
noexcept
1230 buffer, last, value, digitsAfterDecimalPoint);
1234 char* buffer,
char* last,
double value,
1235 int digitsAfterDecimalPoint)
noexcept
1238 buffer, last, value, digitsAfterDecimalPoint);
1242ToChars(
char* buffer,
char* last,
long long value)
noexcept
1244 if (buffer >= last || buffer ==
nullptr)
1245 return { last, std::errc::value_too_large };
1253 return ToChars(buffer, last,
static_cast<unsigned long long>(value));
1258 if (buffer >= last || buffer ==
nullptr)
1259 return { last, std::errc::value_too_large };
1264 return { buffer, std::errc() };
1267 constexpr size_t safeSize =
1268 std::numeric_limits<unsigned long long>::digits10 + 2;
1270 const size_t bufferSize =
static_cast<size_t>(last - buffer);
1272 if (bufferSize >= safeSize)
1275 char tempBuffer[safeSize];
1278 const size_t resultSize =
static_cast<size_t>(tempLast - tempBuffer);
1280 if (resultSize > bufferSize)
1281 return { last, std::errc::value_too_large };
1283 std::copy(tempBuffer, tempLast, buffer);
1285 return { buffer + resultSize, std::errc() };
STRING_UTILS_API ToCharsResult ToChars(char *buffer, char *last, float value, int digitsAfterDecimalPoint) noexcept
Convert a single precision floating point number to a string, always uses the dot as decimal.
Define functions to convert numeric types to string representation.
bool grisu2(char *buf, char *last, int &len, int &decimal_exponent, diyfp m_minus, diyfp v, diyfp m_plus)
boundaries compute_boundaries(FloatType value)
int find_largest_pow10(const std::uint32_t n, std::uint32_t &pow10)
ToCharsResult append_exponent(char *buf, char *last, int e)
appends a decimal representation of e to buf
bool grisu2_digit_gen(char *buffer, char *last, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
Target reinterpret_bits(const Source source)
ToCharsResult format_buffer(char *buf, char *last, int len, int decimal_exponent, int min_exp, int max_exp)
prettify v = buf * 10^decimal_exponent If v is in the range [10^min_exp, 10^max_exp) it will be print...
cached_power get_cached_power_for_binary_exponent(int e)
void grisu2_round(char *buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k)
static const pair s_pairs[]
char * u64toa_jeaiii(uint64_t n, char *b)
ToCharsResult float_to_chars(char *first, char *last, T value, int digitsAfterDecimalPoint)
void copy(const T *src, T *dst, int32_t n)
Result of the conversion, similar to std::to_chars_result.
static diyfp normalize(diyfp x) noexcept
normalize x such that the significand is >= 2^(q-1)
constexpr diyfp(std::uint64_t f_, int e_) noexcept
static diyfp normalize_to(const diyfp &x, const int target_exponent) noexcept
normalize x such that the result has the exponent E
static diyfp mul(const diyfp &x, const diyfp &y) noexcept
returns x * y
static diyfp sub(const diyfp &x, const diyfp &y) noexcept
returns x - y
static constexpr int kPrecision