]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/binfloat.h
Use <cstdint> in BinFloat
[libs/datafile.git] / source / binfloat.h
index d16570fc057fa296a349f2b69bb167d68a960ceb..2c74441ad333888f1d47f98b1c3e449b40f98f6f 100644 (file)
@@ -1,7 +1,9 @@
 #ifndef MSP_DATAFILE_BINFLOAT_H_
 #define MSP_DATAFILE_BINFLOAT_H_
 
-#include "type.h"
+#include <climits>
+#include <cstdint>
+#include <cstring>
 
 namespace Msp {
 namespace DataFile {
@@ -25,38 +27,56 @@ struct BinFloat
        };
 
        template<typename T>
-       union Conversion
-       {
-               T f;
-               typename MatchingInt<T>::UnsignedType i;
-       };
+       struct MatchingInt;
+
+       template<typename T>
+       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<typename T>
-       static BinFloat explode_iec559(T v)
-       {
-               Conversion<T> 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<typename T>
-       T compose_iec559()
-       {
-               Conversion<T> c;
-               c.i = compose(sizeof(T)*CHAR_BIT);
-               return c.f;
-       }
+       T compose_iec559();
 };
 
+template<>
+struct BinFloat::MatchingInt<float> { typedef std::uint32_t Type; };
+
+template<>
+struct BinFloat::MatchingInt<double> { typedef std::uint64_t Type; };
+
+template<typename T>
+union BinFloat::Conversion
+{
+       T f;
+       typename MatchingInt<T>::Type i;
+};
+
+template<typename T>
+inline BinFloat BinFloat::explode_iec559(T v)
+{
+       Conversion<T> c;
+       c.f = v;
+       return explode(c.i, sizeof(T)*CHAR_BIT);
+}
+
+template<typename T>
+inline T BinFloat::compose_iec559()
+{
+       Conversion<T> c;
+       c.i = compose(sizeof(T)*CHAR_BIT);
+       return c.f;
+}
+
 } // namespace DataFile
 } // namespace Msp