#ifndef MSP_DATAFILE_BINFLOAT_H_
#define MSP_DATAFILE_BINFLOAT_H_
-#include "type.h"
+#include <climits>
+#include <cstdint>
+#include <cstring>
namespace Msp {
namespace DataFile {
};
template<typename T>
- union Conversion
- {
- T f;
- typename MatchingInt<T>::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<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>
+inline BinFloat BinFloat::explode_iec559(T v)
+{
+ typename MatchingInt<T>::Type i;
+ memcpy(&v, &i, sizeof(T));
+ return explode(i, sizeof(T)*CHAR_BIT);
+}
+
+template<typename T>
+inline T BinFloat::compose_iec559()
+{
+ typename MatchingInt<T>::Type i = compose(sizeof(T)*CHAR_BIT);
+ T v;
+ memcpy(&v, &i, sizeof(T));
+ return v;
+}
+
} // namespace DataFile
} // namespace Msp