From: Mikko Rasa Date: Sun, 29 Aug 2021 10:32:08 +0000 (+0300) Subject: Use in BinFloat X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0d925901d1446de6fe76616b9b2e731d7702571e;p=libs%2Fdatafile.git Use in BinFloat --- 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