MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / compute_boundaries

Function compute_boundaries

Source/Utils/json.hpp:14520–14585  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14518 */
14519 template<typename FloatType>
14520 boundaries compute_boundaries(FloatType value)
14521 {
14522 JSON_ASSERT(std::isfinite(value));
14523 JSON_ASSERT(value > 0);
14524
14525 // Convert the IEEE representation into a diyfp.
14526 //
14527 // If v is denormal:
14528 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
14529 // If v is normalized:
14530 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
14531
14532 static_assert(std::numeric_limits<FloatType>::is_iec559,
14533 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
14534
14535 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
14536 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
14537 constexpr int kMinExp = 1 - kBias;
14538 constexpr std::uint64_t kHiddenBit = std::uint64_t{ 1 } << (kPrecision - 1); // = 2^(p-1)
14539
14540 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
14541
14542 const std::uint64_t bits = reinterpret_bits<bits_type>(value);
14543 const std::uint64_t E = bits >> (kPrecision - 1);
14544 const std::uint64_t F = bits & (kHiddenBit - 1);
14545
14546 const bool is_denormal = E == 0;
14547 const diyfp v = is_denormal
14548 ? diyfp(F, kMinExp)
14549 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
14550
14551 // Compute the boundaries m- and m+ of the floating-point value
14552 // v = f * 2^e.
14553 //
14554 // Determine v- and v+, the floating-point predecessor and successor if v,
14555 // respectively.
14556 //
14557 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
14558 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
14559 //
14560 // v+ = v + 2^e
14561 //
14562 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
14563 // between m- and m+ round to v, regardless of how the input rounding
14564 // algorithm breaks ties.
14565 //
14566 // ---+-------------+-------------+-------------+-------------+--- (A)
14567 // v- m- v m+ v+
14568 //
14569 // -----------------+------+------+-------------+-------------+--- (B)
14570 // v- m- v m+ v+
14571
14572 const bool lower_boundary_is_closer = F == 0 && E > 1;
14573 const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
14574 const diyfp m_minus = lower_boundary_is_closer
14575 ? diyfp(4 * v.f - 1, v.e - 2) // (B)
14576 : diyfp(2 * v.f - 1, v.e - 1); // (A)
14577

Callers 1

json.hppFile · 0.85

Calls 1

diyfpClass · 0.85

Tested by

no test coverage detected