Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
nlohmann::detail::dtoa_impl Namespace Reference

implements the Grisu2 algorithm for binary to decimal floating-point conversion. More...

Classes

struct  boundaries
 
struct  cached_power
 
struct  diyfp
 

Functions

template<typename Target , typename Source >
Target reinterpret_bits (const Source source)
 
template<typename FloatType >
boundaries compute_boundaries (FloatType value)
 
cached_power get_cached_power_for_binary_exponent (int e)
 
int find_largest_pow10 (const uint32_t n, uint32_t &pow10)
 
void grisu2_round (char *buf, int len, uint64_t dist, uint64_t delta, uint64_t rest, uint64_t ten_k)
 
void grisu2_digit_gen (char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
 
void grisu2 (char *buf, int &len, int &decimal_exponent, diyfp m_minus, diyfp v, diyfp m_plus)
 
template<typename FloatType >
void grisu2 (char *buf, int &len, int &decimal_exponent, FloatType value)
 
char * append_exponent (char *buf, int e)
 appends a decimal representation of e to buf More...
 
char * format_buffer (char *buf, int len, int decimal_exponent, int min_exp, int max_exp)
 prettify v = buf * 10^decimal_exponent More...
 

Variables

constexpr int kAlpha = -60
 
constexpr int kGamma = -32
 

Detailed Description

implements the Grisu2 algorithm for binary to decimal floating-point conversion.

This implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz).

The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.

For a detailed description of the algorithm see:

[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming Language Design and Implementation, PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, PLDI 1996

Function Documentation

char* nlohmann::detail::dtoa_impl::append_exponent ( char *  buf,
int  e 
)
inline

appends a decimal representation of e to buf

Returns
a pointer to the element following the exponent.
Precondition
-1000 < e < 1000

Definition at line 10594 of file json.hpp.

Referenced by format_buffer().

10595 {
10596  assert(e > -1000);
10597  assert(e < 1000);
10598 
10599  if (e < 0)
10600  {
10601  e = -e;
10602  *buf++ = '-';
10603  }
10604  else
10605  {
10606  *buf++ = '+';
10607  }
10608 
10609  auto k = static_cast<uint32_t>(e);
10610  if (k < 10)
10611  {
10612  // Always print at least two digits in the exponent.
10613  // This is for compatibility with printf("%g").
10614  *buf++ = '0';
10615  *buf++ = static_cast<char>('0' + k);
10616  }
10617  else if (k < 100)
10618  {
10619  *buf++ = static_cast<char>('0' + k / 10);
10620  k %= 10;
10621  *buf++ = static_cast<char>('0' + k);
10622  }
10623  else
10624  {
10625  *buf++ = static_cast<char>('0' + k / 100);
10626  k %= 100;
10627  *buf++ = static_cast<char>('0' + k / 10);
10628  k %= 10;
10629  *buf++ = static_cast<char>('0' + k);
10630  }
10631 
10632  return buf;
10633 }
template<typename FloatType >
boundaries nlohmann::detail::dtoa_impl::compute_boundaries ( FloatType  value)

Compute the (normalized) diyfp representing the input number 'value' and its boundaries.

Precondition
value must be finite and positive

Definition at line 9857 of file json.hpp.

References nlohmann::detail::dtoa_impl::diyfp::e, nlohmann::detail::dtoa_impl::diyfp::f, and Catch::Generators::value().

Referenced by grisu2().

9858 {
9859  assert(std::isfinite(value));
9860  assert(value > 0);
9861 
9862  // Convert the IEEE representation into a diyfp.
9863  //
9864  // If v is denormal:
9865  // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
9866  // If v is normalized:
9867  // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
9868 
9869  static_assert(std::numeric_limits<FloatType>::is_iec559,
9870  "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
9871 
9872  constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
9873  constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
9874  constexpr int kMinExp = 1 - kBias;
9875  constexpr uint64_t kHiddenBit = uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
9876 
9877  using bits_type = typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type;
9878 
9879  const uint64_t bits = reinterpret_bits<bits_type>(value);
9880  const uint64_t E = bits >> (kPrecision - 1);
9881  const uint64_t F = bits & (kHiddenBit - 1);
9882 
9883  const bool is_denormal = (E == 0);
9884  const diyfp v = is_denormal
9885  ? diyfp(F, kMinExp)
9886  : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
9887 
9888  // Compute the boundaries m- and m+ of the floating-point value
9889  // v = f * 2^e.
9890  //
9891  // Determine v- and v+, the floating-point predecessor and successor if v,
9892  // respectively.
9893  //
9894  // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
9895  // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
9896  //
9897  // v+ = v + 2^e
9898  //
9899  // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
9900  // between m- and m+ round to v, regardless of how the input rounding
9901  // algorithm breaks ties.
9902  //
9903  // ---+-------------+-------------+-------------+-------------+--- (A)
9904  // v- m- v m+ v+
9905  //
9906  // -----------------+------+------+-------------+-------------+--- (B)
9907  // v- m- v m+ v+
9908 
9909  const bool lower_boundary_is_closer = (F == 0 and E > 1);
9910  const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
9911  const diyfp m_minus = lower_boundary_is_closer
9912  ? diyfp(4 * v.f - 1, v.e - 2) // (B)
9913  : diyfp(2 * v.f - 1, v.e - 1); // (A)
9914 
9915  // Determine the normalized w+ = m+.
9916  const diyfp w_plus = diyfp::normalize(m_plus);
9917 
9918  // Determine w- = m- such that e_(w-) = e_(w+).
9919  const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
9920 
9921  return {diyfp::normalize(v), w_minus, w_plus};
9922 }
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
int nlohmann::detail::dtoa_impl::find_largest_pow10 ( const uint32_t  n,
uint32_t &  pow10 
)
inline

For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. For n == 0, returns 1 and sets pow10 := 1.

Definition at line 10160 of file json.hpp.

Referenced by grisu2_digit_gen().

10161 {
10162  // LCOV_EXCL_START
10163  if (n >= 1000000000)
10164  {
10165  pow10 = 1000000000;
10166  return 10;
10167  }
10168  // LCOV_EXCL_STOP
10169  else if (n >= 100000000)
10170  {
10171  pow10 = 100000000;
10172  return 9;
10173  }
10174  else if (n >= 10000000)
10175  {
10176  pow10 = 10000000;
10177  return 8;
10178  }
10179  else if (n >= 1000000)
10180  {
10181  pow10 = 1000000;
10182  return 7;
10183  }
10184  else if (n >= 100000)
10185  {
10186  pow10 = 100000;
10187  return 6;
10188  }
10189  else if (n >= 10000)
10190  {
10191  pow10 = 10000;
10192  return 5;
10193  }
10194  else if (n >= 1000)
10195  {
10196  pow10 = 1000;
10197  return 4;
10198  }
10199  else if (n >= 100)
10200  {
10201  pow10 = 100;
10202  return 3;
10203  }
10204  else if (n >= 10)
10205  {
10206  pow10 = 10;
10207  return 2;
10208  }
10209  else
10210  {
10211  pow10 = 1;
10212  return 1;
10213  }
10214 }
char* nlohmann::detail::dtoa_impl::format_buffer ( char *  buf,
int  len,
int  decimal_exponent,
int  min_exp,
int  max_exp 
)
inline

prettify v = buf * 10^decimal_exponent

If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point notation. Otherwise it will be printed in exponential notation.

Precondition
min_exp < 0
max_exp > 0

Definition at line 10644 of file json.hpp.

References append_exponent().

Referenced by nlohmann::detail::to_chars().

10646 {
10647  assert(min_exp < 0);
10648  assert(max_exp > 0);
10649 
10650  const int k = len;
10651  const int n = len + decimal_exponent;
10652 
10653  // v = buf * 10^(n-k)
10654  // k is the length of the buffer (number of decimal digits)
10655  // n is the position of the decimal point relative to the start of the buffer.
10656 
10657  if (k <= n and n <= max_exp)
10658  {
10659  // digits[000]
10660  // len <= max_exp + 2
10661 
10662  std::memset(buf + k, '0', static_cast<size_t>(n - k));
10663  // Make it look like a floating-point number (#362, #378)
10664  buf[n + 0] = '.';
10665  buf[n + 1] = '0';
10666  return buf + (n + 2);
10667  }
10668 
10669  if (0 < n and n <= max_exp)
10670  {
10671  // dig.its
10672  // len <= max_digits10 + 1
10673 
10674  assert(k > n);
10675 
10676  std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
10677  buf[n] = '.';
10678  return buf + (k + 1);
10679  }
10680 
10681  if (min_exp < n and n <= 0)
10682  {
10683  // 0.[000]digits
10684  // len <= 2 + (-min_exp - 1) + max_digits10
10685 
10686  std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
10687  buf[0] = '0';
10688  buf[1] = '.';
10689  std::memset(buf + 2, '0', static_cast<size_t>(-n));
10690  return buf + (2 + (-n) + k);
10691  }
10692 
10693  if (k == 1)
10694  {
10695  // dE+123
10696  // len <= 1 + 5
10697 
10698  buf += 1;
10699  }
10700  else
10701  {
10702  // d.igitsE+123
10703  // len <= max_digits10 + 1 + 5
10704 
10705  std::memmove(buf + 2, buf + 1, static_cast<size_t>(k - 1));
10706  buf[1] = '.';
10707  buf += 1 + k;
10708  }
10709 
10710  *buf++ = 'e';
10711  return append_exponent(buf, n - 1);
10712 }
char * append_exponent(char *buf, int e)
appends a decimal representation of e to buf
Definition: json.hpp:10594
cached_power nlohmann::detail::dtoa_impl::get_cached_power_for_binary_exponent ( int  e)
inline

For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c satisfies (Definition 3.2 from [1])

 alpha <= e_c + e + q <= gamma.

Definition at line 9996 of file json.hpp.

References nlohmann::detail::dtoa_impl::cached_power::e.

Referenced by grisu2().

9997 {
9998  // Now
9999  //
10000  // alpha <= e_c + e + q <= gamma (1)
10001  // ==> f_c * 2^alpha <= c * 2^e * 2^q
10002  //
10003  // and since the c's are normalized, 2^(q-1) <= f_c,
10004  //
10005  // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
10006  // ==> 2^(alpha - e - 1) <= c
10007  //
10008  // If c were an exakt power of ten, i.e. c = 10^k, one may determine k as
10009  //
10010  // k = ceil( log_10( 2^(alpha - e - 1) ) )
10011  // = ceil( (alpha - e - 1) * log_10(2) )
10012  //
10013  // From the paper:
10014  // "In theory the result of the procedure could be wrong since c is rounded,
10015  // and the computation itself is approximated [...]. In practice, however,
10016  // this simple function is sufficient."
10017  //
10018  // For IEEE double precision floating-point numbers converted into
10019  // normalized diyfp's w = f * 2^e, with q = 64,
10020  //
10021  // e >= -1022 (min IEEE exponent)
10022  // -52 (p - 1)
10023  // -52 (p - 1, possibly normalize denormal IEEE numbers)
10024  // -11 (normalize the diyfp)
10025  // = -1137
10026  //
10027  // and
10028  //
10029  // e <= +1023 (max IEEE exponent)
10030  // -52 (p - 1)
10031  // -11 (normalize the diyfp)
10032  // = 960
10033  //
10034  // This binary exponent range [-1137,960] results in a decimal exponent
10035  // range [-307,324]. One does not need to store a cached power for each
10036  // k in this range. For each such k it suffices to find a cached power
10037  // such that the exponent of the product lies in [alpha,gamma].
10038  // This implies that the difference of the decimal exponents of adjacent
10039  // table entries must be less than or equal to
10040  //
10041  // floor( (gamma - alpha) * log_10(2) ) = 8.
10042  //
10043  // (A smaller distance gamma-alpha would require a larger table.)
10044 
10045  // NB:
10046  // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
10047 
10048  constexpr int kCachedPowersSize = 79;
10049  constexpr int kCachedPowersMinDecExp = -300;
10050  constexpr int kCachedPowersDecStep = 8;
10051 
10052  static constexpr cached_power kCachedPowers[] =
10053  {
10054  { 0xAB70FE17C79AC6CA, -1060, -300 },
10055  { 0xFF77B1FCBEBCDC4F, -1034, -292 },
10056  { 0xBE5691EF416BD60C, -1007, -284 },
10057  { 0x8DD01FAD907FFC3C, -980, -276 },
10058  { 0xD3515C2831559A83, -954, -268 },
10059  { 0x9D71AC8FADA6C9B5, -927, -260 },
10060  { 0xEA9C227723EE8BCB, -901, -252 },
10061  { 0xAECC49914078536D, -874, -244 },
10062  { 0x823C12795DB6CE57, -847, -236 },
10063  { 0xC21094364DFB5637, -821, -228 },
10064  { 0x9096EA6F3848984F, -794, -220 },
10065  { 0xD77485CB25823AC7, -768, -212 },
10066  { 0xA086CFCD97BF97F4, -741, -204 },
10067  { 0xEF340A98172AACE5, -715, -196 },
10068  { 0xB23867FB2A35B28E, -688, -188 },
10069  { 0x84C8D4DFD2C63F3B, -661, -180 },
10070  { 0xC5DD44271AD3CDBA, -635, -172 },
10071  { 0x936B9FCEBB25C996, -608, -164 },
10072  { 0xDBAC6C247D62A584, -582, -156 },
10073  { 0xA3AB66580D5FDAF6, -555, -148 },
10074  { 0xF3E2F893DEC3F126, -529, -140 },
10075  { 0xB5B5ADA8AAFF80B8, -502, -132 },
10076  { 0x87625F056C7C4A8B, -475, -124 },
10077  { 0xC9BCFF6034C13053, -449, -116 },
10078  { 0x964E858C91BA2655, -422, -108 },
10079  { 0xDFF9772470297EBD, -396, -100 },
10080  { 0xA6DFBD9FB8E5B88F, -369, -92 },
10081  { 0xF8A95FCF88747D94, -343, -84 },
10082  { 0xB94470938FA89BCF, -316, -76 },
10083  { 0x8A08F0F8BF0F156B, -289, -68 },
10084  { 0xCDB02555653131B6, -263, -60 },
10085  { 0x993FE2C6D07B7FAC, -236, -52 },
10086  { 0xE45C10C42A2B3B06, -210, -44 },
10087  { 0xAA242499697392D3, -183, -36 },
10088  { 0xFD87B5F28300CA0E, -157, -28 },
10089  { 0xBCE5086492111AEB, -130, -20 },
10090  { 0x8CBCCC096F5088CC, -103, -12 },
10091  { 0xD1B71758E219652C, -77, -4 },
10092  { 0x9C40000000000000, -50, 4 },
10093  { 0xE8D4A51000000000, -24, 12 },
10094  { 0xAD78EBC5AC620000, 3, 20 },
10095  { 0x813F3978F8940984, 30, 28 },
10096  { 0xC097CE7BC90715B3, 56, 36 },
10097  { 0x8F7E32CE7BEA5C70, 83, 44 },
10098  { 0xD5D238A4ABE98068, 109, 52 },
10099  { 0x9F4F2726179A2245, 136, 60 },
10100  { 0xED63A231D4C4FB27, 162, 68 },
10101  { 0xB0DE65388CC8ADA8, 189, 76 },
10102  { 0x83C7088E1AAB65DB, 216, 84 },
10103  { 0xC45D1DF942711D9A, 242, 92 },
10104  { 0x924D692CA61BE758, 269, 100 },
10105  { 0xDA01EE641A708DEA, 295, 108 },
10106  { 0xA26DA3999AEF774A, 322, 116 },
10107  { 0xF209787BB47D6B85, 348, 124 },
10108  { 0xB454E4A179DD1877, 375, 132 },
10109  { 0x865B86925B9BC5C2, 402, 140 },
10110  { 0xC83553C5C8965D3D, 428, 148 },
10111  { 0x952AB45CFA97A0B3, 455, 156 },
10112  { 0xDE469FBD99A05FE3, 481, 164 },
10113  { 0xA59BC234DB398C25, 508, 172 },
10114  { 0xF6C69A72A3989F5C, 534, 180 },
10115  { 0xB7DCBF5354E9BECE, 561, 188 },
10116  { 0x88FCF317F22241E2, 588, 196 },
10117  { 0xCC20CE9BD35C78A5, 614, 204 },
10118  { 0x98165AF37B2153DF, 641, 212 },
10119  { 0xE2A0B5DC971F303A, 667, 220 },
10120  { 0xA8D9D1535CE3B396, 694, 228 },
10121  { 0xFB9B7CD9A4A7443C, 720, 236 },
10122  { 0xBB764C4CA7A44410, 747, 244 },
10123  { 0x8BAB8EEFB6409C1A, 774, 252 },
10124  { 0xD01FEF10A657842C, 800, 260 },
10125  { 0x9B10A4E5E9913129, 827, 268 },
10126  { 0xE7109BFBA19C0C9D, 853, 276 },
10127  { 0xAC2820D9623BF429, 880, 284 },
10128  { 0x80444B5E7AA7CF85, 907, 292 },
10129  { 0xBF21E44003ACDD2D, 933, 300 },
10130  { 0x8E679C2F5E44FF8F, 960, 308 },
10131  { 0xD433179D9C8CB841, 986, 316 },
10132  { 0x9E19DB92B4E31BA9, 1013, 324 },
10133  };
10134 
10135  // This computation gives exactly the same results for k as
10136  // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
10137  // for |e| <= 1500, but doesn't require floating-point operations.
10138  // NB: log_10(2) ~= 78913 / 2^18
10139  assert(e >= -1500);
10140  assert(e <= 1500);
10141  const int f = kAlpha - e - 1;
10142  const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
10143 
10144  const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
10145  assert(index >= 0);
10146  assert(index < kCachedPowersSize);
10147  static_cast<void>(kCachedPowersSize); // Fix warning.
10148 
10149  const cached_power cached = kCachedPowers[index];
10150  assert(kAlpha <= cached.e + e + 64);
10151  assert(kGamma >= cached.e + e + 64);
10152 
10153  return cached;
10154 }
constexpr int kAlpha
Definition: json.hpp:9979
constexpr int kGamma
Definition: json.hpp:9980
void nlohmann::detail::dtoa_impl::grisu2 ( char *  buf,
int &  len,
int &  decimal_exponent,
diyfp  m_minus,
diyfp  v,
diyfp  m_plus 
)
inline

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10.

Definition at line 10497 of file json.hpp.

References nlohmann::detail::dtoa_impl::diyfp::e, nlohmann::detail::dtoa_impl::cached_power::e, nlohmann::detail::dtoa_impl::diyfp::f, nlohmann::detail::dtoa_impl::cached_power::f, get_cached_power_for_binary_exponent(), grisu2_digit_gen(), nlohmann::detail::dtoa_impl::cached_power::k, and w.

Referenced by nlohmann::detail::to_chars().

10499 {
10500  assert(m_plus.e == m_minus.e);
10501  assert(m_plus.e == v.e);
10502 
10503  // --------(-----------------------+-----------------------)-------- (A)
10504  // m- v m+
10505  //
10506  // --------------------(-----------+-----------------------)-------- (B)
10507  // m- v m+
10508  //
10509  // First scale v (and m- and m+) such that the exponent is in the range
10510  // [alpha, gamma].
10511 
10512  const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
10513 
10514  const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
10515 
10516  // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
10517  const diyfp w = diyfp::mul(v, c_minus_k);
10518  const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
10519  const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
10520 
10521  // ----(---+---)---------------(---+---)---------------(---+---)----
10522  // w- w w+
10523  // = c*m- = c*v = c*m+
10524  //
10525  // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
10526  // w+ are now off by a small amount.
10527  // In fact:
10528  //
10529  // w - v * 10^k < 1 ulp
10530  //
10531  // To account for this inaccuracy, add resp. subtract 1 ulp.
10532  //
10533  // --------+---[---------------(---+---)---------------]---+--------
10534  // w- M- w M+ w+
10535  //
10536  // Now any number in [M-, M+] (bounds included) will round to w when input,
10537  // regardless of how the input rounding algorithm breaks ties.
10538  //
10539  // And digit_gen generates the shortest possible such number in [M-, M+].
10540  // Note that this does not mean that Grisu2 always generates the shortest
10541  // possible number in the interval (m-, m+).
10542  const diyfp M_minus(w_minus.f + 1, w_minus.e);
10543  const diyfp M_plus (w_plus.f - 1, w_plus.e );
10544 
10545  decimal_exponent = -cached.k; // = -(-k) = k
10546 
10547  grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
10548 }
void grisu2_digit_gen(char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
Definition: json.hpp:10257
cached_power get_cached_power_for_binary_exponent(int e)
Definition: json.hpp:9996
static double w[2][28][111]
Definition: ccl_emu17.c:33
template<typename FloatType >
void nlohmann::detail::dtoa_impl::grisu2 ( char *  buf,
int &  len,
int &  decimal_exponent,
FloatType  value 
)

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10.

Definition at line 10556 of file json.hpp.

References compute_boundaries(), nlohmann::detail::dtoa_impl::boundaries::minus, nlohmann::detail::dtoa_impl::boundaries::plus, w, and nlohmann::detail::dtoa_impl::boundaries::w.

10557 {
10558  static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
10559  "internal error: not enough precision");
10560 
10561  assert(std::isfinite(value));
10562  assert(value > 0);
10563 
10564  // If the neighbors (and boundaries) of 'value' are always computed for double-precision
10565  // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
10566  // decimal representations are not exactly "short".
10567  //
10568  // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
10569  // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
10570  // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
10571  // does.
10572  // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
10573  // representation using the corresponding std::from_chars function recovers value exactly". That
10574  // indicates that single precision floating-point numbers should be recovered using
10575  // 'std::strtof'.
10576  //
10577  // NB: If the neighbors are computed for single-precision numbers, there is a single float
10578  // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
10579  // value is off by 1 ulp.
10580 #if 0
10581  const boundaries w = compute_boundaries(static_cast<double>(value));
10582 #else
10583  const boundaries w = compute_boundaries(value);
10584 #endif
10585 
10586  grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
10587 }
boundaries compute_boundaries(FloatType value)
Definition: json.hpp:9857
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value)
Definition: json.hpp:10556
static double w[2][28][111]
Definition: ccl_emu17.c:33
void nlohmann::detail::dtoa_impl::grisu2_digit_gen ( char *  buffer,
int &  length,
int &  decimal_exponent,
diyfp  M_minus,
diyfp  w,
diyfp  M_plus 
)
inline

Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.

Definition at line 10257 of file json.hpp.

References nlohmann::detail::dtoa_impl::diyfp::e, nlohmann::detail::dtoa_impl::diyfp::f, find_largest_pow10(), grisu2_round(), and m.

Referenced by grisu2().

10259 {
10260  static_assert(kAlpha >= -60, "internal error");
10261  static_assert(kGamma <= -32, "internal error");
10262 
10263  // Generates the digits (and the exponent) of a decimal floating-point
10264  // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
10265  // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
10266  //
10267  // <--------------------------- delta ---->
10268  // <---- dist --------->
10269  // --------------[------------------+-------------------]--------------
10270  // M- w M+
10271  //
10272  // Grisu2 generates the digits of M+ from left to right and stops as soon as
10273  // V is in [M-,M+].
10274 
10275  assert(M_plus.e >= kAlpha);
10276  assert(M_plus.e <= kGamma);
10277 
10278  uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
10279  uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
10280 
10281  // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
10282  //
10283  // M+ = f * 2^e
10284  // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
10285  // = ((p1 ) * 2^-e + (p2 )) * 2^e
10286  // = p1 + p2 * 2^e
10287 
10288  const diyfp one(uint64_t{1} << -M_plus.e, M_plus.e);
10289 
10290  auto p1 = static_cast<uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
10291  uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
10292 
10293  // 1)
10294  //
10295  // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
10296 
10297  assert(p1 > 0);
10298 
10299  uint32_t pow10;
10300  const int k = find_largest_pow10(p1, pow10);
10301 
10302  // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
10303  //
10304  // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
10305  // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
10306  //
10307  // M+ = p1 + p2 * 2^e
10308  // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
10309  // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
10310  // = d[k-1] * 10^(k-1) + ( rest) * 2^e
10311  //
10312  // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
10313  //
10314  // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
10315  //
10316  // but stop as soon as
10317  //
10318  // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
10319 
10320  int n = k;
10321  while (n > 0)
10322  {
10323  // Invariants:
10324  // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
10325  // pow10 = 10^(n-1) <= p1 < 10^n
10326  //
10327  const uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
10328  const uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
10329  //
10330  // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
10331  // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
10332  //
10333  assert(d <= 9);
10334  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
10335  //
10336  // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
10337  //
10338  p1 = r;
10339  n--;
10340  //
10341  // M+ = buffer * 10^n + (p1 + p2 * 2^e)
10342  // pow10 = 10^n
10343  //
10344 
10345  // Now check if enough digits have been generated.
10346  // Compute
10347  //
10348  // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
10349  //
10350  // Note:
10351  // Since rest and delta share the same exponent e, it suffices to
10352  // compare the significands.
10353  const uint64_t rest = (uint64_t{p1} << -one.e) + p2;
10354  if (rest <= delta)
10355  {
10356  // V = buffer * 10^n, with M- <= V <= M+.
10357 
10358  decimal_exponent += n;
10359 
10360  // We may now just stop. But instead look if the buffer could be
10361  // decremented to bring V closer to w.
10362  //
10363  // pow10 = 10^n is now 1 ulp in the decimal representation V.
10364  // The rounding procedure works with diyfp's with an implicit
10365  // exponent of e.
10366  //
10367  // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
10368  //
10369  const uint64_t ten_n = uint64_t{pow10} << -one.e;
10370  grisu2_round(buffer, length, dist, delta, rest, ten_n);
10371 
10372  return;
10373  }
10374 
10375  pow10 /= 10;
10376  //
10377  // pow10 = 10^(n-1) <= p1 < 10^n
10378  // Invariants restored.
10379  }
10380 
10381  // 2)
10382  //
10383  // The digits of the integral part have been generated:
10384  //
10385  // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
10386  // = buffer + p2 * 2^e
10387  //
10388  // Now generate the digits of the fractional part p2 * 2^e.
10389  //
10390  // Note:
10391  // No decimal point is generated: the exponent is adjusted instead.
10392  //
10393  // p2 actually represents the fraction
10394  //
10395  // p2 * 2^e
10396  // = p2 / 2^-e
10397  // = d[-1] / 10^1 + d[-2] / 10^2 + ...
10398  //
10399  // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
10400  //
10401  // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
10402  // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
10403  //
10404  // using
10405  //
10406  // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
10407  // = ( d) * 2^-e + ( r)
10408  //
10409  // or
10410  // 10^m * p2 * 2^e = d + r * 2^e
10411  //
10412  // i.e.
10413  //
10414  // M+ = buffer + p2 * 2^e
10415  // = buffer + 10^-m * (d + r * 2^e)
10416  // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
10417  //
10418  // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
10419 
10420  assert(p2 > delta);
10421 
10422  int m = 0;
10423  for (;;)
10424  {
10425  // Invariant:
10426  // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
10427  // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
10428  // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
10429  // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
10430  //
10431  assert(p2 <= UINT64_MAX / 10);
10432  p2 *= 10;
10433  const uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
10434  const uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
10435  //
10436  // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
10437  // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
10438  // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
10439  //
10440  assert(d <= 9);
10441  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
10442  //
10443  // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
10444  //
10445  p2 = r;
10446  m++;
10447  //
10448  // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
10449  // Invariant restored.
10450 
10451  // Check if enough digits have been generated.
10452  //
10453  // 10^-m * p2 * 2^e <= delta * 2^e
10454  // p2 * 2^e <= 10^m * delta * 2^e
10455  // p2 <= 10^m * delta
10456  delta *= 10;
10457  dist *= 10;
10458  if (p2 <= delta)
10459  {
10460  break;
10461  }
10462  }
10463 
10464  // V = buffer * 10^-m, with M- <= V <= M+.
10465 
10466  decimal_exponent -= m;
10467 
10468  // 1 ulp in the decimal representation is now 10^-m.
10469  // Since delta and dist are now scaled by 10^m, we need to do the
10470  // same with ulp in order to keep the units in sync.
10471  //
10472  // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
10473  //
10474  const uint64_t ten_m = one.f;
10475  grisu2_round(buffer, length, dist, delta, p2, ten_m);
10476 
10477  // By construction this algorithm generates the shortest possible decimal
10478  // number (Loitsch, Theorem 6.2) which rounds back to w.
10479  // For an input number of precision p, at least
10480  //
10481  // N = 1 + ceil(p * log_10(2))
10482  //
10483  // decimal digits are sufficient to identify all binary floating-point
10484  // numbers (Matula, "In-and-Out conversions").
10485  // This implies that the algorithm does not produce more than N decimal
10486  // digits.
10487  //
10488  // N = 17 for p = 53 (IEEE double precision)
10489  // N = 9 for p = 24 (IEEE single precision)
10490 }
constexpr int kAlpha
Definition: json.hpp:9979
constexpr int kGamma
Definition: json.hpp:9980
static int m[2]
Definition: ccl_emu17.c:25
static double w[2][28][111]
Definition: ccl_emu17.c:33
void grisu2_round(char *buf, int len, uint64_t dist, uint64_t delta, uint64_t rest, uint64_t ten_k)
Definition: json.hpp:10216
int find_largest_pow10(const uint32_t n, uint32_t &pow10)
Definition: json.hpp:10160
void nlohmann::detail::dtoa_impl::grisu2_round ( char *  buf,
int  len,
uint64_t  dist,
uint64_t  delta,
uint64_t  rest,
uint64_t  ten_k 
)
inline

Definition at line 10216 of file json.hpp.

Referenced by grisu2_digit_gen().

10218 {
10219  assert(len >= 1);
10220  assert(dist <= delta);
10221  assert(rest <= delta);
10222  assert(ten_k > 0);
10223 
10224  // <--------------------------- delta ---->
10225  // <---- dist --------->
10226  // --------------[------------------+-------------------]--------------
10227  // M- w M+
10228  //
10229  // ten_k
10230  // <------>
10231  // <---- rest ---->
10232  // --------------[------------------+----+--------------]--------------
10233  // w V
10234  // = buf * 10^k
10235  //
10236  // ten_k represents a unit-in-the-last-place in the decimal representation
10237  // stored in buf.
10238  // Decrement buf by ten_k while this takes buf closer to w.
10239 
10240  // The tests are written in this order to avoid overflow in unsigned
10241  // integer arithmetic.
10242 
10243  while (rest < dist
10244  and delta - rest >= ten_k
10245  and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
10246  {
10247  assert(buf[len - 1] != '0');
10248  buf[len - 1]--;
10249  rest += ten_k;
10250  }
10251 }
template<typename Target , typename Source >
Target nlohmann::detail::dtoa_impl::reinterpret_bits ( const Source  source)

Definition at line 9716 of file json.hpp.

9717 {
9718  static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
9719 
9720  Target target;
9721  std::memcpy(&target, &source, sizeof(Source));
9722  return target;
9723 }

Variable Documentation

constexpr int nlohmann::detail::dtoa_impl::kAlpha = -60

Definition at line 9979 of file json.hpp.

constexpr int nlohmann::detail::dtoa_impl::kGamma = -32

Definition at line 9980 of file json.hpp.