! 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. */
| 17368 | M- and M+ must be normalized and share the same exponent -60 <= e <= -32. |
| 17369 | */ |
| 17370 | inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, |
| 17371 | diyfp M_minus, diyfp w, diyfp M_plus) |
| 17372 | { |
| 17373 | static_assert(kAlpha >= -60, "internal error"); |
| 17374 | static_assert(kGamma <= -32, "internal error"); |
| 17375 | |
| 17376 | // Generates the digits (and the exponent) of a decimal floating-point |
| 17377 | // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's |
| 17378 | // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma. |
| 17379 | // |
| 17380 | // <--------------------------- delta ----> |
| 17381 | // <---- dist ---------> |
| 17382 | // --------------[------------------+-------------------]-------------- |
| 17383 | // M- w M+ |
| 17384 | // |
| 17385 | // Grisu2 generates the digits of M+ from left to right and stops as soon as |
| 17386 | // V is in [M-,M+]. |
| 17387 | |
| 17388 | JSON_ASSERT(M_plus.e >= kAlpha); |
| 17389 | JSON_ASSERT(M_plus.e <= kGamma); |
| 17390 | |
| 17391 | std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e) |
| 17392 | std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e) |
| 17393 | |
| 17394 | // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0): |
| 17395 | // |
| 17396 | // M+ = f * 2^e |
| 17397 | // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e |
| 17398 | // = ((p1 ) * 2^-e + (p2 )) * 2^e |
| 17399 | // = p1 + p2 * 2^e |
| 17400 | |
| 17401 | const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e); |
| 17402 | |
| 17403 | 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.) |
| 17404 | std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e |
| 17405 | |
| 17406 | // 1) |
| 17407 | // |
| 17408 | // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0] |
| 17409 | |
| 17410 | JSON_ASSERT(p1 > 0); |
| 17411 | |
| 17412 | std::uint32_t pow10{}; |
| 17413 | const int k = find_largest_pow10(p1, pow10); |
| 17414 | |
| 17415 | // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) |
| 17416 | // |
| 17417 | // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1)) |
| 17418 | // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1)) |
| 17419 | // |
| 17420 | // M+ = p1 + p2 * 2^e |
| 17421 | // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e |
| 17422 | // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e |
| 17423 | // = d[k-1] * 10^(k-1) + ( rest) * 2^e |
| 17424 | // |
| 17425 | // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0) |
| 17426 | // |
| 17427 | // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0] |
no test coverage detected