! 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. */
| 17492 | M- and M+ must be normalized and share the same exponent -60 <= e <= -32. |
| 17493 | */ |
| 17494 | inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, |
| 17495 | diyfp M_minus, diyfp w, diyfp M_plus) |
| 17496 | { |
| 17497 | static_assert(kAlpha >= -60, "internal error"); |
| 17498 | static_assert(kGamma <= -32, "internal error"); |
| 17499 | |
| 17500 | // Generates the digits (and the exponent) of a decimal floating-point |
| 17501 | // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's |
| 17502 | // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma. |
| 17503 | // |
| 17504 | // <--------------------------- delta ----> |
| 17505 | // <---- dist ---------> |
| 17506 | // --------------[------------------+-------------------]-------------- |
| 17507 | // M- w M+ |
| 17508 | // |
| 17509 | // Grisu2 generates the digits of M+ from left to right and stops as soon as |
| 17510 | // V is in [M-,M+]. |
| 17511 | |
| 17512 | JSON_ASSERT(M_plus.e >= kAlpha); |
| 17513 | JSON_ASSERT(M_plus.e <= kGamma); |
| 17514 | |
| 17515 | std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e) |
| 17516 | std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e) |
| 17517 | |
| 17518 | // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0): |
| 17519 | // |
| 17520 | // M+ = f * 2^e |
| 17521 | // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e |
| 17522 | // = ((p1 ) * 2^-e + (p2 )) * 2^e |
| 17523 | // = p1 + p2 * 2^e |
| 17524 | |
| 17525 | const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e); |
| 17526 | |
| 17527 | 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.) |
| 17528 | std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e |
| 17529 | |
| 17530 | // 1) |
| 17531 | // |
| 17532 | // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0] |
| 17533 | |
| 17534 | JSON_ASSERT(p1 > 0); |
| 17535 | |
| 17536 | std::uint32_t pow10{}; |
| 17537 | const int k = find_largest_pow10(p1, pow10); |
| 17538 | |
| 17539 | // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) |
| 17540 | // |
| 17541 | // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1)) |
| 17542 | // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1)) |
| 17543 | // |
| 17544 | // M+ = p1 + p2 * 2^e |
| 17545 | // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e |
| 17546 | // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e |
| 17547 | // = d[k-1] * 10^(k-1) + ( rest) * 2^e |
| 17548 | // |
| 17549 | // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0) |
| 17550 | // |
| 17551 | // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0] |
no test coverage detected