47template<
typename Target,
typename Source>
50 static_assert(
sizeof(Target) ==
sizeof(Source),
"size mismatch");
53 std::memcpy(&target, &
source,
sizeof(Source));
64 constexpr diyfp(std::uint64_t f_,
int e_) noexcept :
f(f_),
e(e_) {}
75 return {x.f - y.f, x.e};
84 static_assert(
kPrecision == 64,
"internal error");
109 const std::uint64_t u_lo = x.
f & 0xFFFFFFFFu;
110 const std::uint64_t u_hi = x.f >> 32u;
111 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
112 const std::uint64_t v_hi = y.f >> 32u;
114 const std::uint64_t p0 = u_lo * v_lo;
115 const std::uint64_t p1 = u_lo * v_hi;
116 const std::uint64_t p2 = u_hi * v_lo;
117 const std::uint64_t p3 = u_hi * v_hi;
119 const std::uint64_t p0_hi = p0 >> 32u;
120 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
121 const std::uint64_t p1_hi = p1 >> 32u;
122 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
123 const std::uint64_t p2_hi = p2 >> 32u;
125 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
136 Q += std::uint64_t{1} << (64u - 32u - 1u);
138 const std::uint64_t
h = p3 + p2_hi + p1_hi + (Q >> 32u);
140 return {
h, x.e + y.e + 64};
151 while ((x.f >> 63u) == 0)
166 const int delta = x.
e - target_exponent;
171 return {x.f << delta, target_exponent};
188template<
typename FloatType>
201 static_assert(std::numeric_limits<FloatType>::is_iec559,
202 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
204 constexpr int kPrecision = std::numeric_limits<FloatType>::digits;
205 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
206 constexpr int kMinExp = 1 - kBias;
207 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1);
211 const auto bits =
static_cast<std::uint64_t
>(reinterpret_bits<bits_type>(
value));
212 const std::uint64_t E =
bits >> (kPrecision - 1);
213 const std::uint64_t
F =
bits & (kHiddenBit - 1);
215 const bool is_denormal = E == 0;
216 const diyfp v = is_denormal
218 :
diyfp(
F + kHiddenBit,
static_cast<int>(E) - kBias);
241 const bool lower_boundary_is_closer =
F == 0 && E > 1;
243 const diyfp m_minus = lower_boundary_is_closer
380 constexpr int kCachedPowersMinDecExp = -300;
381 constexpr int kCachedPowersDecStep = 8;
383 static constexpr std::array<cached_power, 79> kCachedPowers =
386 { 0xAB70FE17C79AC6CA, -1060, -300 },
387 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
388 { 0xBE5691EF416BD60C, -1007, -284 },
389 { 0x8DD01FAD907FFC3C, -980, -276 },
390 { 0xD3515C2831559A83, -954, -268 },
391 { 0x9D71AC8FADA6C9B5, -927, -260 },
392 { 0xEA9C227723EE8BCB, -901, -252 },
393 { 0xAECC49914078536D, -874, -244 },
394 { 0x823C12795DB6CE57, -847, -236 },
395 { 0xC21094364DFB5637, -821, -228 },
396 { 0x9096EA6F3848984F, -794, -220 },
397 { 0xD77485CB25823AC7, -768, -212 },
398 { 0xA086CFCD97BF97F4, -741, -204 },
399 { 0xEF340A98172AACE5, -715, -196 },
400 { 0xB23867FB2A35B28E, -688, -188 },
401 { 0x84C8D4DFD2C63F3B, -661, -180 },
402 { 0xC5DD44271AD3CDBA, -635, -172 },
403 { 0x936B9FCEBB25C996, -608, -164 },
404 { 0xDBAC6C247D62A584, -582, -156 },
405 { 0xA3AB66580D5FDAF6, -555, -148 },
406 { 0xF3E2F893DEC3F126, -529, -140 },
407 { 0xB5B5ADA8AAFF80B8, -502, -132 },
408 { 0x87625F056C7C4A8B, -475, -124 },
409 { 0xC9BCFF6034C13053, -449, -116 },
410 { 0x964E858C91BA2655, -422, -108 },
411 { 0xDFF9772470297EBD, -396, -100 },
412 { 0xA6DFBD9FB8E5B88F, -369, -92 },
413 { 0xF8A95FCF88747D94, -343, -84 },
414 { 0xB94470938FA89BCF, -316, -76 },
415 { 0x8A08F0F8BF0F156B, -289, -68 },
416 { 0xCDB02555653131B6, -263, -60 },
417 { 0x993FE2C6D07B7FAC, -236, -52 },
418 { 0xE45C10C42A2B3B06, -210, -44 },
419 { 0xAA242499697392D3, -183, -36 },
420 { 0xFD87B5F28300CA0E, -157, -28 },
421 { 0xBCE5086492111AEB, -130, -20 },
422 { 0x8CBCCC096F5088CC, -103, -12 },
423 { 0xD1B71758E219652C, -77, -4 },
424 { 0x9C40000000000000, -50, 4 },
425 { 0xE8D4A51000000000, -24, 12 },
426 { 0xAD78EBC5AC620000, 3, 20 },
427 { 0x813F3978F8940984, 30, 28 },
428 { 0xC097CE7BC90715B3, 56, 36 },
429 { 0x8F7E32CE7BEA5C70, 83, 44 },
430 { 0xD5D238A4ABE98068, 109, 52 },
431 { 0x9F4F2726179A2245, 136, 60 },
432 { 0xED63A231D4C4FB27, 162, 68 },
433 { 0xB0DE65388CC8ADA8, 189, 76 },
434 { 0x83C7088E1AAB65DB, 216, 84 },
435 { 0xC45D1DF942711D9A, 242, 92 },
436 { 0x924D692CA61BE758, 269, 100 },
437 { 0xDA01EE641A708DEA, 295, 108 },
438 { 0xA26DA3999AEF774A, 322, 116 },
439 { 0xF209787BB47D6B85, 348, 124 },
440 { 0xB454E4A179DD1877, 375, 132 },
441 { 0x865B86925B9BC5C2, 402, 140 },
442 { 0xC83553C5C8965D3D, 428, 148 },
443 { 0x952AB45CFA97A0B3, 455, 156 },
444 { 0xDE469FBD99A05FE3, 481, 164 },
445 { 0xA59BC234DB398C25, 508, 172 },
446 { 0xF6C69A72A3989F5C, 534, 180 },
447 { 0xB7DCBF5354E9BECE, 561, 188 },
448 { 0x88FCF317F22241E2, 588, 196 },
449 { 0xCC20CE9BD35C78A5, 614, 204 },
450 { 0x98165AF37B2153DF, 641, 212 },
451 { 0xE2A0B5DC971F303A, 667, 220 },
452 { 0xA8D9D1535CE3B396, 694, 228 },
453 { 0xFB9B7CD9A4A7443C, 720, 236 },
454 { 0xBB764C4CA7A44410, 747, 244 },
455 { 0x8BAB8EEFB6409C1A, 774, 252 },
456 { 0xD01FEF10A657842C, 800, 260 },
457 { 0x9B10A4E5E9913129, 827, 268 },
458 { 0xE7109BFBA19C0C9D, 853, 276 },
459 { 0xAC2820D9623BF429, 880, 284 },
460 { 0x80444B5E7AA7CF85, 907, 292 },
461 { 0xBF21E44003ACDD2D, 933, 300 },
462 { 0x8E679C2F5E44FF8F, 960, 308 },
463 { 0xD433179D9C8CB841, 986, 316 },
464 { 0x9E19DB92B4E31BA9, 1013, 324 },
475 const int k = (f * 78913) / (1 << 18) +
static_cast<int>(f > 0);
477 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
546inline void grisu2_round(
char* buf,
int len, std::uint64_t dist, std::uint64_t delta,
547 std::uint64_t rest, std::uint64_t ten_k)
574 && delta - rest >= ten_k
575 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
590 static_assert(
kAlpha >= -60,
"internal error");
591 static_assert(
kGamma <= -32,
"internal error");
608 std::uint64_t delta =
diyfp::sub(M_plus, M_minus).
f;
618 const diyfp one(std::uint64_t{1} << -M_plus.
e, M_plus.
e);
620 auto p1 =
static_cast<std::uint32_t
>(M_plus.
f >> -one.e);
621 std::uint64_t p2 = M_plus.
f & (one.f - 1);
629 std::uint32_t
pow10{};
657 const std::uint32_t d = p1 /
pow10;
658 const std::uint32_t r = p1 %
pow10;
664 buffer[length++] =
static_cast<char>(
'0' + d);
683 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
688 decimal_exponent += n;
699 const std::uint64_t ten_n = std::uint64_t{
pow10} << -one.e;
763 const std::uint64_t d = p2 >> -one.e;
764 const std::uint64_t r = p2 & (one.f - 1);
771 buffer[length++] =
static_cast<char>(
'0' + d);
796 decimal_exponent -= m;
804 const std::uint64_t ten_m = one.f;
828inline
void grisu2(
char* buf,
int& len,
int& decimal_exponent,
845 const diyfp c_minus_k(cached.
f, cached.
e);
873 const diyfp M_minus(w_minus.
f + 1, w_minus.
e);
874 const diyfp M_plus (w_plus.
f - 1, w_plus.
e );
876 decimal_exponent = -cached.
k;
886template<
typename FloatType>
888void
grisu2(
char* buf,
int& len,
int& decimal_exponent, FloatType
value)
891 "internal error: not enough precision");
943 auto k =
static_cast<std::uint32_t
>(
e);
949 *buf++ =
static_cast<char>(
'0' + k);
953 *buf++ =
static_cast<char>(
'0' + k / 10);
955 *buf++ =
static_cast<char>(
'0' + k);
959 *buf++ =
static_cast<char>(
'0' + k / 100);
961 *buf++ =
static_cast<char>(
'0' + k / 10);
963 *buf++ =
static_cast<char>(
'0' + k);
981 int min_exp,
int max_exp)
987 const int n = len + decimal_exponent;
993 if (k <= n && n <= max_exp)
998 std::memset(buf + k,
'0',
static_cast<size_t>(n) -
static_cast<size_t>(k));
1002 return buf + (
static_cast<size_t>(n) + 2);
1005 if (0 < n && n <= max_exp)
1012 std::memmove(buf + (
static_cast<size_t>(n) + 1), buf + n,
static_cast<size_t>(k) -
static_cast<size_t>(n));
1014 return buf + (
static_cast<size_t>(k) + 1U);
1017 if (min_exp < n && n <= 0)
1022 std::memmove(buf + (2 +
static_cast<size_t>(-n)), buf,
static_cast<size_t>(k));
1025 std::memset(buf + 2,
'0',
static_cast<size_t>(-n));
1026 return buf + (2U +
static_cast<size_t>(-n) +
static_cast<size_t>(k));
1041 std::memmove(buf + 2, buf + 1,
static_cast<size_t>(k) - 1);
1043 buf += 1 +
static_cast<size_t>(k);
1062template<
typename FloatType>
1067 static_cast<void>(last);
1078#pragma GCC diagnostic push
1079#pragma GCC diagnostic ignored "-Wfloat-equal"
1090#pragma GCC diagnostic pop
1100 int decimal_exponent = 0;
1103 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
1106 constexpr int kMinExp = -4;
1111 JSON_ASSERT(last -
first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
1112 JSON_ASSERT(last -
first >= std::numeric_limits<FloatType>::max_digits10 + 6);
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:111
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
\rst A contiguous memory buffer with an optional growing ability.
Definition: core.h:798
#define JSON_HEDLEY_NON_NULL(...)
Definition: hedley.h:1288
#define JSON_HEDLEY_RETURNS_NON_NULL
Definition: hedley.h:1729
#define JSON_ASSERT(x)
Definition: macro_scope.h:192
int find_largest_pow10(const std::uint32_t n, std::uint32_t &pow10)
Definition: to_chars.h:492
cached_power get_cached_power_for_binary_exponent(int e)
Definition: to_chars.h:328
Target reinterpret_bits(const Source source)
Definition: to_chars.h:48
boundaries compute_boundaries(FloatType value)
Definition: to_chars.h:189
void grisu2_round(char *buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k)
Definition: to_chars.h:546
constexpr int kAlpha
Definition: to_chars.h:311
void grisu2(char *buf, int &len, int &decimal_exponent, diyfp m_minus, diyfp v, diyfp m_plus)
Definition: to_chars.h:828
constexpr int kGamma
Definition: to_chars.h:312
void grisu2_digit_gen(char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
Definition: to_chars.h:587
JSON_HEDLEY_RETURNS_NON_NULL char * append_exponent(char *buf, int e)
appends a decimal representation of e to buf
Definition: to_chars.h:928
JSON_HEDLEY_RETURNS_NON_NULL char * format_buffer(char *buf, int len, int decimal_exponent, int min_exp, int max_exp)
prettify v = buf * 10^decimal_exponent
Definition: to_chars.h:980
detail namespace with internal helper functions
Definition: xchar.h:20
constexpr long long pow10(std::uint32_t n)
Definition: chrono.h:1021
constexpr auto digits10() noexcept -> int
Definition: format.h:1283
JSON_HEDLEY_RETURNS_NON_NULL char * to_chars(char *first, const char *last, FloatType value)
generates a decimal representation of the floating-point number value in [first, last).
Definition: to_chars.h:1065
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
bool isfinite(T)
Definition: chrono.h:1600
type
Definition: core.h:556
constexpr bool signbit(const T x) noexcept
Compile-time sign bit detection function.
Definition: signbit.hpp:40
static constexpr const unit_t< compound_unit< energy::joule, time::seconds > > h(6.626070040e-34)
Planck constant.
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3417
bits
Definition: data.h:44
Definition: to_chars.h:176
diyfp w
Definition: to_chars.h:177
diyfp minus
Definition: to_chars.h:178
diyfp plus
Definition: to_chars.h:179
Definition: to_chars.h:315
std::uint64_t f
Definition: to_chars.h:316
int e
Definition: to_chars.h:317
int k
Definition: to_chars.h:318
Definition: to_chars.h:58
static diyfp mul(const diyfp &x, const diyfp &y) noexcept
returns x * y
Definition: to_chars.h:82
static diyfp normalize_to(const diyfp &x, const int target_exponent) noexcept
normalize x such that the result has the exponent E
Definition: to_chars.h:164
static diyfp normalize(diyfp x) noexcept
normalize x such that the significand is >= 2^(q-1)
Definition: to_chars.h:147
static diyfp sub(const diyfp &x, const diyfp &y) noexcept
returns x - y
Definition: to_chars.h:70
constexpr diyfp(std::uint64_t f_, int e_) noexcept
Definition: to_chars.h:64
static constexpr int kPrecision
Definition: to_chars.h:59
std::uint64_t f
Definition: to_chars.h:61
int e
Definition: to_chars.h:62