]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/binfloat.h
Cosmetic changes
[libs/datafile.git] / source / binfloat.h
index 2c74441ad333888f1d47f98b1c3e449b40f98f6f..db3e92431823627249365d83f814b9f991a751c0 100644 (file)
@@ -20,8 +20,8 @@ struct BinFloat
 {
        struct Bits
        {
-               unsigned exponent;
-               unsigned mantissa;
+               unsigned exponent = 8;
+               unsigned mantissa = 23;
 
                Bits(unsigned);
        };
@@ -29,13 +29,10 @@ struct BinFloat
        template<typename T>
        struct MatchingInt;
 
-       template<typename T>
-       union Conversion;
-
-       bool sign;
-       bool infinity;
-       int exponent;
-       std::uint64_t mantissa;
+       bool sign = false;
+       bool infinity = false;
+       int exponent = 0;
+       std::uint64_t mantissa = 0;
 
        static BinFloat explode(std::uint64_t, const Bits &);
 
@@ -54,27 +51,21 @@ 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);
+       typename MatchingInt<T>::Type i;
+       memcpy(&i, &v, sizeof(T));
+       return explode(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;
+       typename MatchingInt<T>::Type i = compose(sizeof(T)*CHAR_BIT);
+       T v;
+       memcpy(&v, &i, sizeof(T));
+       return v;
 }
 
 } // namespace DataFile