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)<<bits.mantissa)-1;
+ uint64_t mantissa_mask = (1ULL<<bits.mantissa)-1;
int exponent_mask = (1<<bits.exponent)-1;
BinFloat bf;
else
{
// Extract mantissa, add the implied one and align it to high bits
- bf.mantissa = (value&mantissa_mask) | (UInt64(1)<<bits.mantissa);
+ bf.mantissa = (value&mantissa_mask) | (uint64_t(1)<<bits.mantissa);
bf.mantissa <<= 63-bits.mantissa;
}
return bf;
}
-UInt64 BinFloat::compose(const Bits &bits)
+uint64_t BinFloat::compose(const Bits &bits)
{
- UInt64 mantissa_mask = (UInt64(1)<<bits.mantissa)-1;
+ uint64_t mantissa_mask = (1ULL<<bits.mantissa)-1;
int exponent_mask = (1<<bits.exponent)-1;
int biased_exponent = exponent+(exponent_mask>>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<<bits.exponent | exponent_mask)<<bits.mantissa;
+ return uint64_t(sign<<bits.exponent | exponent_mask)<<bits.mantissa;
else if(biased_exponent<=0 || !mantissa)
// Underflow, return zero
return 0;
else
{
- UInt64 value = rounded_mantissa&mantissa_mask;
- value |= UInt64(biased_exponent)<<bits.mantissa;
- value |= UInt64(sign)<<(bits.mantissa+bits.exponent);
+ uint64_t value = rounded_mantissa&mantissa_mask;
+ value |= uint64_t(biased_exponent)<<bits.mantissa;
+ value |= uint64_t(sign)<<(bits.mantissa+bits.exponent);
return value;
}
}
#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;
+
+ 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