MCPcopy Create free account
hub / github.com/bytedance/Fastbot_Android / grisu2_digit_gen

Function grisu2_digit_gen

native/thirdpart/json/json.hpp:13231–13464  ·  view source on GitHub ↗

! 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. */

Source from the content-addressed store, hash-verified

13229M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
13230*/
13231inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
13232 diyfp M_minus, diyfp w, diyfp M_plus)
13233{
13234 static_assert(kAlpha >= -60, "internal error");
13235 static_assert(kGamma <= -32, "internal error");
13236
13237 // Generates the digits (and the exponent) of a decimal floating-point
13238 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
13239 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
13240 //
13241 // <--------------------------- delta ---->
13242 // <---- dist --------->
13243 // --------------[------------------+-------------------]--------------
13244 // M- w M+
13245 //
13246 // Grisu2 generates the digits of M+ from left to right and stops as soon as
13247 // V is in [M-,M+].
13248
13249 assert(M_plus.e >= kAlpha);
13250 assert(M_plus.e <= kGamma);
13251
13252 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
13253 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
13254
13255 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
13256 //
13257 // M+ = f * 2^e
13258 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
13259 // = ((p1 ) * 2^-e + (p2 )) * 2^e
13260 // = p1 + p2 * 2^e
13261
13262 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
13263
13264 auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
13265 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
13266
13267 // 1)
13268 //
13269 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
13270
13271 assert(p1 > 0);
13272
13273 std::uint32_t pow10;
13274 const int k = find_largest_pow10(p1, pow10);
13275
13276 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
13277 //
13278 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
13279 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
13280 //
13281 // M+ = p1 + p2 * 2^e
13282 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
13283 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
13284 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
13285 //
13286 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
13287 //
13288 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]

Callers 1

grisu2Function · 0.85

Calls 2

find_largest_pow10Function · 0.85
grisu2_roundFunction · 0.85

Tested by

no test coverage detected