From 1a4671024df651f79c745f38ecaaf45b677d460c Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 6 Jul 2023 15:07:45 +0200 Subject: [PATCH] Improve rounding to 1 ULP --- include/boost/charconv/detail/bit_layouts.hpp | 15 --------------- .../boost/charconv/detail/compute_float80.hpp | 16 ++++------------ 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/include/boost/charconv/detail/bit_layouts.hpp b/include/boost/charconv/detail/bit_layouts.hpp index f4013aa..ae60363 100644 --- a/include/boost/charconv/detail/bit_layouts.hpp +++ b/include/boost/charconv/detail/bit_layouts.hpp @@ -87,21 +87,6 @@ struct IEEEl2bits #endif }; -struct IEEEl2bits_oneman -{ -#if BOOST_CHARCONV_ENDIAN_LITTLE_BYTE - std::uint64_t mantissa : 64; - std::uint32_t exponent : 15; - std::uint32_t sign : 1; - std::uint32_t pad : 32; -#else // Big endian - std::uint32_t pad : 32; - std::uint32_t sign : 1; - std::uint32_t exponent : 15; - std::uint64_t mantissa : 64 -#endif -}; - struct ieee754_binary80 { static constexpr int significand_bits = 63; diff --git a/include/boost/charconv/detail/compute_float80.hpp b/include/boost/charconv/detail/compute_float80.hpp index 6fc736c..2000582 100644 --- a/include/boost/charconv/detail/compute_float80.hpp +++ b/include/boost/charconv/detail/compute_float80.hpp @@ -347,19 +347,11 @@ inline ResultType compute_float80(std::int64_t q, Unsigned_Integer w, bool negat return_val = negative ? -return_val : return_val; // Do we need to round? - IEEEl2bits_oneman bits; - std::memcpy(&bits, &return_val, sizeof(return_val)); - if ((man & 1) != (bits.mantissa & 1)) + if (!(man & 1)) { - // Yes we should round - if (bits.mantissa & 1) - { - ++bits.mantissa; - } - else - { - --bits.mantissa; - } + IEEEl2bits bits; + std::memcpy(&bits, &return_val, sizeof(return_val)); + ++bits.mantissa_l; std::memcpy(&return_val, &bits, sizeof(return_val)); } success = std::errc();