From 201062cadefa0d244ab13071e2a397eb23ed0339 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 18 Sep 2014 10:19:40 +0200 Subject: [PATCH] make width of computation depend on base type, i.e. if its 64bit then use 64bit math --- DataStructures/FixedPointNumber.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/DataStructures/FixedPointNumber.h b/DataStructures/FixedPointNumber.h index 8ad687afb..45655fd1f 100644 --- a/DataStructures/FixedPointNumber.h +++ b/DataStructures/FixedPointNumber.h @@ -49,9 +49,8 @@ class FixedPointNumber static_assert(FractionalBitSize > 0, "FractionalBitSize must be greater than 0"); static_assert(FractionalBitSize <= 32, "FractionalBitSize must at most 32"); - constexpr static const int32_t PRECISION = 1 << FractionalBitSize; - typename std::conditional::type m_fixed_point_state; + constexpr static const decltype(m_fixed_point_state) PRECISION = 1 << FractionalBitSize; // state signage encapsulates whether the state should either represent a // signed or an unsigned floating point number @@ -65,13 +64,14 @@ class FixedPointNumber // the type is either initialized with a floating point value or an // integral state. Anything else will throw at compile-time. - template FixedPointNumber(const T &&input) noexcept + template + constexpr FixedPointNumber(const T &&input) noexcept + : m_fixed_point_state(static_cast( + std::round(std::forward(input) * PRECISION))) { static_assert( std::is_floating_point::value || std::is_integral::value, "FixedPointNumber needs to be initialized with floating point or integral value"); - m_fixed_point_state = - static_cast(std::round(std::forward(input) * PRECISION)); } // get max value @@ -160,7 +160,7 @@ class FixedPointNumber } temp >>= FractionalBitSize; FixedPointNumber tmp; - tmp.m_fixed_point_state = static_cast(temp); + tmp.m_fixed_point_state = static_cast(temp); return tmp; } @@ -172,7 +172,7 @@ class FixedPointNumber // rounding! temp = temp + ((temp & 1 << (FractionalBitSize - 1)) << 1); temp >>= FractionalBitSize; - this->m_fixed_point_state = static_cast(temp); + this->m_fixed_point_state = static_cast(temp); return *this; } @@ -182,7 +182,7 @@ class FixedPointNumber temp <<= FractionalBitSize; temp /= static_cast(other.m_fixed_point_state); FixedPointNumber tmp; - tmp.m_fixed_point_state = static_cast(temp); + tmp.m_fixed_point_state = static_cast(temp); return tmp; } @@ -192,7 +192,7 @@ class FixedPointNumber temp <<= FractionalBitSize; temp /= static_cast(other.m_fixed_point_state); FixedPointNumber tmp; - this->m_fixed_point_state = static_cast(temp); + this->m_fixed_point_state = static_cast(temp); return *this; } };