From dc6ce63e01d06e705f7eb065b2b28a6fee27164f Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Fri, 30 Jun 2023 17:27:52 +0200 Subject: [PATCH] Fix 256-bit high bit calculation --- include/boost/charconv/detail/compute_float80.hpp | 7 ++++--- include/boost/charconv/detail/emulated256.hpp | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/boost/charconv/detail/compute_float80.hpp b/include/boost/charconv/detail/compute_float80.hpp index 6c67ad8..1daf794 100644 --- a/include/boost/charconv/detail/compute_float80.hpp +++ b/include/boost/charconv/detail/compute_float80.hpp @@ -177,8 +177,9 @@ inline uint128 mask_mantissa(uint256 z) noexcept; template inline uint128 mask_mantissa(uint256 z) noexcept { - static constexpr uint128 mask {0x1FFFFFFFFFFFF, UINT64_MAX}; - return (z >> 113) & mask; + //static constexpr uint128 mask {0x1FFFFFFFFFFFF, UINT64_MAX}; + //return static_cast((z >> 113) & mask); + return z.high; } #elif BOOST_CHARCONV_LDBL_BITS == 128 @@ -337,7 +338,7 @@ inline ResultType compute_float80(std::int64_t q, Unsigned_Integer w, bool negat #endif // Step 8: Value of the most significant bit of z - const auto u = significant_bit(z) == m ? 0 : 1; + const auto u = high_bit(z) == 255 ? 0 : 1; #ifdef BOOST_CHARCONV_DEBUG_FLOAT128 std::cerr << "u: " << u << std::endl; #endif diff --git a/include/boost/charconv/detail/emulated256.hpp b/include/boost/charconv/detail/emulated256.hpp index 50bfbe8..7a7c299 100644 --- a/include/boost/charconv/detail/emulated256.hpp +++ b/include/boost/charconv/detail/emulated256.hpp @@ -242,11 +242,11 @@ int high_bit(uint256 v) noexcept { if (v.high != 0) { - return high_bit(v.high); + return 255 - high_bit(v.high); } else if (v.low != 0) { - return high_bit(v.low); + return 127 - high_bit(v.low); } return 0;