X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbinfloat.h;h=f6e6d1fb5b493a045022fefc7af2c1ecc2aec335;hb=d3c55a54b4c6ed0f740a87e5446e4054888bdcdf;hp=d16570fc057fa296a349f2b69bb167d68a960ceb;hpb=19179a622c1de88de5ed7047643eec79f285bf2a;p=libs%2Fdatafile.git diff --git a/source/binfloat.h b/source/binfloat.h index d16570f..f6e6d1f 100644 --- a/source/binfloat.h +++ b/source/binfloat.h @@ -1,7 +1,9 @@ #ifndef MSP_DATAFILE_BINFLOAT_H_ #define MSP_DATAFILE_BINFLOAT_H_ -#include "type.h" +#include +#include +#include namespace Msp { namespace DataFile { @@ -25,38 +27,47 @@ struct BinFloat }; template - union Conversion - { - T f; - typename MatchingInt::UnsignedType i; - }; + struct MatchingInt; 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 +inline BinFloat BinFloat::explode_iec559(T v) +{ + typename MatchingInt::Type i; + memcpy(&i, &v, sizeof(T)); + return explode(i, sizeof(T)*CHAR_BIT); +} + +template +inline T BinFloat::compose_iec559() +{ + typename MatchingInt::Type i = compose(sizeof(T)*CHAR_BIT); + T v; + memcpy(&v, &i, sizeof(T)); + return v; +} + } // namespace DataFile } // namespace Msp