fixed: update style

This commit is contained in:
Danny Robson 2015-02-06 16:34:30 +11:00
parent 9619b5fee9
commit 1a1bb23a8b
2 changed files with 135 additions and 112 deletions

186
fixed.cpp
View File

@ -14,138 +14,162 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>. * along with libgim. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2011, 2014 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#include "fixed.hpp" #include "fixed.hpp"
#include "maths.hpp"
#include <cmath> #include <cmath>
using namespace util; using namespace util;
/* ///////////////////////////////////////////////////////////////////////////////
* Constructors // Constructors
*/ template <unsigned I, unsigned E>
template <unsigned int INT, unsigned int FRAC> fixed<I,E>::fixed (uint_t val):
fixed<INT, FRAC>::fixed (integral_type val): m_value (val << E)
m_value (val << FRAC)
{ {
static_assert (INT > 0, "must use positive integer bits"); static_assert (I > 0, "must use positive integer bits");
static_assert (FRAC > 0, "must use positive fractional bits"); static_assert (E > 0, "must use positive fractional bits");
static_assert (INT + FRAC == sizeof (m_value) * 8, static_assert (I + E == sizeof (m_value) * 8,
"underlying storage must be exactly int:frac sized"); "underlying storage must be exactly I+E sized");
} }
/* ///////////////////////////////////////////////////////////////////////////////
* Conversions // Conversions
*/
template <unsigned int INT, unsigned int FRAC> template <unsigned I, unsigned E>
double double
fixed<INT, FRAC>::to_double (void) const fixed<I,E>::to_double (void) const
{ return m_value / std::pow (2.0, FRAC); } {
return static_cast<double> (m_value) / pow (2, E);
}
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
float float
fixed<INT, FRAC>::to_float (void) const { fixed<I,E>::to_float (void) const
return static_cast<float> (
m_value / std::pow (2.0f, FRAC)
);
}
template <unsigned int INT, unsigned int FRAC>
typename fixed<INT, FRAC>::integral_type
fixed<INT, FRAC>::to_integral (void) const
{ return m_value >> FRAC; }
template <unsigned int INT, unsigned int FRAC>
typename fixed<INT, FRAC>::combined_type
fixed<INT, FRAC>::to_native (void) const
{ return m_value; }
//-----------------------------------------------------------------------------
template <unsigned INT, unsigned FRAC>
fixed<INT,FRAC>
fixed<INT,FRAC>::from_native (integral_type i)
{ {
fixed<INT,FRAC> out (integral_type {0u}); return static_cast<float> (m_value) / pow (2, E);
out.m_value = i;
return out;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <unsigned INT, unsigned FRAC> template <unsigned I, unsigned E>
typename fixed<INT,FRAC>::integral_type typename fixed<I,E>::uint_t
fixed<INT,FRAC>::to_integral (integral_type v) fixed<I,E>::to_integral (void) const
{ {
return v >> FRAC; return m_value >> E;
} }
/* //-----------------------------------------------------------------------------
* Integral operators template <unsigned I, unsigned E>
*/ typename fixed<I,E>::uint_t
fixed<I,E>::to_native (void) const
{
return m_value;
}
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>& //-----------------------------------------------------------------------------
fixed<INT, FRAC>::operator +=(integral_type val) { template <unsigned I, unsigned E>
m_value += val << FRAC; fixed<I,E>
fixed<I,E>::from_native (uint_t i)
{
return fixed<I,E> {i};
}
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
typename fixed<I,E>::uint_t
fixed<I,E>::to_integral (uint_t v)
{
return v >> E;
}
///////////////////////////////////////////////////////////////////////////////
// Integer operators
template <unsigned I, unsigned E>
fixed<I,E>&
fixed<I,E>::operator+= (uint_t val)
{
m_value += val << E;
return *this; return *this;
} }
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC>& template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator -=(integral_type val) { fixed<I,E>&
m_value -= val << FRAC; fixed<I,E>::operator-= (uint_t val)
{
m_value -= val << E;
return *this; return *this;
} }
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC>& template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator *=(integral_type val) { fixed<I,E>&
fixed<I,E>::operator*= (uint_t val)
{
m_value *= val; m_value *= val;
return *this; return *this;
} }
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC>& template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator /=(integral_type val) { fixed<I,E>&
fixed<I,E>::operator/= (uint_t val)
{
m_value /= val; m_value /= val;
return *this; return *this;
} }
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC> template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator +(integral_type val) const fixed<I,E>
{ return fixed<INT, FRAC>(m_value + val << FRAC); } fixed<I,E>::operator+ (uint_t val) const
{
return fixed<I,E> (m_value + val << E);
}
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC> template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator -(integral_type val) const fixed<I,E>
{ return fixed<INT, FRAC>(m_value - val << FRAC); } fixed<I,E>::operator- (uint_t val) const
{
return fixed<I,E> (m_value - val << E);
}
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC> template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator *(integral_type val) const fixed<I,E>
{ return fixed<INT, FRAC>(m_value * val); } fixed<I,E>::operator* (uint_t val) const
{
return fixed<I,E> (m_value * val);
}
template <unsigned int INT, unsigned int FRAC> //-----------------------------------------------------------------------------
fixed<INT, FRAC> template <unsigned I, unsigned E>
fixed<INT, FRAC>::operator /(integral_type val) const fixed<I,E>
{ return fixed<INT, FRAC>(m_value / val); } fixed<I,E>::operator /(uint_t val) const
{
return fixed<I,E> (m_value / val);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>. * along with libgim. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2011, 2014 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2014 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_FIXED_HPP #ifndef __UTIL_FIXED_HPP
@ -25,47 +25,46 @@
#include <cstdint> #include <cstdint>
namespace util { namespace util {
template <unsigned int INT, unsigned int FRAC> template <unsigned I, unsigned E>
class fixed { class fixed {
public: public:
typedef typename bits_type<INT + FRAC>::uint combined_type; typedef typename bits_type<I+E>::uint uint_t;
typedef typename bits_type<INT + FRAC>::uint integral_type;
combined_type m_value; fixed (double);
fixed (float);
fixed (uint_t);
public: double to_double (void) const;
explicit fixed(double); float to_float (void) const;
explicit fixed(float); uint_t to_integral (void) const;
fixed(integral_type); uint_t to_native (void) const;
double to_double (void) const; static fixed<I,E> from_native (uint_t);
float to_float (void) const;
integral_type to_integral (void) const;
combined_type to_native (void) const;
static fixed<INT,FRAC> from_native (integral_type); static uint_t to_integral (uint_t);
static integral_type to_integral (integral_type); fixed<I,E>& operator +=(const fixed<I,E>);
fixed<I,E>& operator -=(const fixed<I,E>);
fixed<I,E>& operator *=(const fixed<I,E>);
fixed<I,E>& operator /=(const fixed<I,E>);
fixed<INT, FRAC>& operator +=(const fixed<INT, FRAC>); fixed<I,E> operator +(const fixed<I,E>) const;
fixed<INT, FRAC>& operator -=(const fixed<INT, FRAC>); fixed<I,E> operator -(const fixed<I,E>) const;
fixed<INT, FRAC>& operator *=(const fixed<INT, FRAC>); fixed<I,E> operator *(const fixed<I,E>) const;
fixed<INT, FRAC>& operator /=(const fixed<INT, FRAC>); fixed<I,E> operator /(const fixed<I,E>) const;
fixed<INT, FRAC> operator +(const fixed<INT, FRAC>) const; fixed<I,E>& operator +=(uint_t);
fixed<INT, FRAC> operator -(const fixed<INT, FRAC>) const; fixed<I,E>& operator -=(uint_t);
fixed<INT, FRAC> operator *(const fixed<INT, FRAC>) const; fixed<I,E>& operator *=(uint_t);
fixed<INT, FRAC> operator /(const fixed<INT, FRAC>) const; fixed<I,E>& operator /=(uint_t);
fixed<INT, FRAC>& operator +=(integral_type); fixed<I,E> operator +(uint_t) const;
fixed<INT, FRAC>& operator -=(integral_type); fixed<I,E> operator -(uint_t) const;
fixed<INT, FRAC>& operator *=(integral_type); fixed<I,E> operator *(uint_t) const;
fixed<INT, FRAC>& operator /=(integral_type); fixed<I,E> operator /(uint_t) const;
fixed<INT, FRAC> operator +(integral_type) const; private:
fixed<INT, FRAC> operator -(integral_type) const; uint_t m_value;
fixed<INT, FRAC> operator *(integral_type) const;
fixed<INT, FRAC> operator /(integral_type) const;
}; };
} }