From 0d925901d1446de6fe76616b9b2e731d7702571e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 29 Aug 2021 13:32:08 +0300 Subject: [PATCH] Use in BinFloat --- source/binfloat.cpp | 20 +++++++-------- source/binfloat.h | 62 ++++++++++++++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/source/binfloat.cpp b/source/binfloat.cpp index fa4be73..50fe5c5 100644 --- a/source/binfloat.cpp +++ b/source/binfloat.cpp @@ -7,9 +7,9 @@ using namespace std; namespace Msp { namespace DataFile { -BinFloat BinFloat::explode(UInt64 value, const Bits &bits) +BinFloat BinFloat::explode(uint64_t value, const Bits &bits) { - UInt64 mantissa_mask = (UInt64(1)<>1); // Shift down and round the mantissa - UInt64 rounded_mantissa = ((mantissa>>(62-bits.mantissa))+1)>>1; + uint64_t rounded_mantissa = ((mantissa>>(62-bits.mantissa))+1)>>1; // If the integer part is greater than one, we need to use a higher exponent if((rounded_mantissa>>bits.mantissa)>1) ++biased_exponent; if(biased_exponent>=exponent_mask || infinity) // Overflow, return infinity - return UInt64(sign< +#include +#include namespace Msp { namespace DataFile { @@ -25,38 +27,56 @@ struct BinFloat }; template - union Conversion - { - T f; - typename MatchingInt::UnsignedType i; - }; + struct MatchingInt; + + template + union Conversion; bool sign; bool infinity; int exponent; - UInt64 mantissa; + std::uint64_t mantissa; - static BinFloat explode(UInt64, const Bits &); + static BinFloat explode(std::uint64_t, const Bits &); template - static BinFloat explode_iec559(T v) - { - Conversion c; - c.f = v; - return explode(c.i, sizeof(T)*CHAR_BIT); - } + static BinFloat explode_iec559(T v); - UInt64 compose(const Bits &); + std::uint64_t compose(const Bits &); template - T compose_iec559() - { - Conversion c; - c.i = compose(sizeof(T)*CHAR_BIT); - return c.f; - } + T compose_iec559(); }; +template<> +struct BinFloat::MatchingInt { typedef std::uint32_t Type; }; + +template<> +struct BinFloat::MatchingInt { typedef std::uint64_t Type; }; + +template +union BinFloat::Conversion +{ + T f; + typename MatchingInt::Type i; +}; + +template +inline BinFloat BinFloat::explode_iec559(T v) +{ + Conversion c; + c.f = v; + return explode(c.i, sizeof(T)*CHAR_BIT); +} + +template +inline T BinFloat::compose_iec559() +{ + Conversion c; + c.i = compose(sizeof(T)*CHAR_BIT); + return c.f; +} + } // namespace DataFile } // namespace Msp -- 2.43.0