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
* 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 "maths.hpp"
#include <cmath>
using namespace util;
/*
* Constructors
*/
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>::fixed (integral_type val):
m_value (val << FRAC)
///////////////////////////////////////////////////////////////////////////////
// Constructors
template <unsigned I, unsigned E>
fixed<I,E>::fixed (uint_t val):
m_value (val << E)
{
static_assert (INT > 0, "must use positive integer bits");
static_assert (FRAC > 0, "must use positive fractional bits");
static_assert (INT + FRAC == sizeof (m_value) * 8,
"underlying storage must be exactly int:frac sized");
static_assert (I > 0, "must use positive integer bits");
static_assert (E > 0, "must use positive fractional bits");
static_assert (I + E == sizeof (m_value) * 8,
"underlying storage must be exactly I+E sized");
}
/*
* Conversions
*/
///////////////////////////////////////////////////////////////////////////////
// Conversions
template <unsigned int INT, unsigned int FRAC>
template <unsigned I, unsigned E>
double
fixed<INT, FRAC>::to_double (void) const
{ return m_value / std::pow (2.0, FRAC); }
fixed<I,E>::to_double (void) const
{
return static_cast<double> (m_value) / pow (2, E);
}
template <unsigned int INT, unsigned int FRAC>
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
float
fixed<INT, FRAC>::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<I,E>::to_float (void) const
{
fixed<INT,FRAC> out (integral_type {0u});
out.m_value = i;
return out;
return static_cast<float> (m_value) / pow (2, E);
}
//-----------------------------------------------------------------------------
template <unsigned INT, unsigned FRAC>
typename fixed<INT,FRAC>::integral_type
fixed<INT,FRAC>::to_integral (integral_type v)
template <unsigned I, unsigned E>
typename fixed<I,E>::uint_t
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) {
m_value += val << FRAC;
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
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;
}
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>&
fixed<INT, FRAC>::operator -=(integral_type val) {
m_value -= val << FRAC;
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>&
fixed<I,E>::operator-= (uint_t val)
{
m_value -= val << E;
return *this;
}
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>&
fixed<INT, FRAC>::operator *=(integral_type val) {
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>&
fixed<I,E>::operator*= (uint_t val)
{
m_value *= val;
return *this;
}
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>&
fixed<INT, FRAC>::operator /=(integral_type val) {
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>&
fixed<I,E>::operator/= (uint_t val)
{
m_value /= val;
return *this;
}
template <unsigned int INT, unsigned int FRAC>
fixed<INT, FRAC>
fixed<INT, FRAC>::operator +(integral_type val) const
{ return fixed<INT, FRAC>(m_value + val << FRAC); }
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>
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>
fixed<INT, FRAC>::operator -(integral_type val) const
{ return fixed<INT, FRAC>(m_value - val << FRAC); }
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>
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>
fixed<INT, FRAC>::operator *(integral_type val) const
{ return fixed<INT, FRAC>(m_value * val); }
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>
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>
fixed<INT, FRAC>::operator /(integral_type val) const
{ return fixed<INT, FRAC>(m_value / val); }
//-----------------------------------------------------------------------------
template <unsigned I, unsigned E>
fixed<I,E>
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
* 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
@ -25,47 +25,46 @@
#include <cstdint>
namespace util {
template <unsigned int INT, unsigned int FRAC>
template <unsigned I, unsigned E>
class fixed {
public:
typedef typename bits_type<INT + FRAC>::uint combined_type;
typedef typename bits_type<INT + FRAC>::uint integral_type;
typedef typename bits_type<I+E>::uint uint_t;
combined_type m_value;
fixed (double);
fixed (float);
fixed (uint_t);
public:
explicit fixed(double);
explicit fixed(float);
fixed(integral_type);
double to_double (void) const;
float to_float (void) const;
uint_t to_integral (void) const;
uint_t to_native (void) const;
double to_double (void) const;
float to_float (void) const;
integral_type to_integral (void) const;
combined_type to_native (void) const;
static fixed<I,E> from_native (uint_t);
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<INT, FRAC>& operator -=(const fixed<INT, FRAC>);
fixed<INT, FRAC>& operator *=(const fixed<INT, FRAC>);
fixed<INT, FRAC>& operator /=(const fixed<INT, FRAC>);
fixed<I,E> operator +(const fixed<I,E>) const;
fixed<I,E> operator -(const fixed<I,E>) const;
fixed<I,E> operator *(const fixed<I,E>) const;
fixed<I,E> operator /(const fixed<I,E>) const;
fixed<INT, FRAC> operator +(const fixed<INT, FRAC>) const;
fixed<INT, FRAC> operator -(const fixed<INT, FRAC>) const;
fixed<INT, FRAC> operator *(const fixed<INT, FRAC>) const;
fixed<INT, FRAC> operator /(const fixed<INT, FRAC>) const;
fixed<I,E>& operator +=(uint_t);
fixed<I,E>& operator -=(uint_t);
fixed<I,E>& operator *=(uint_t);
fixed<I,E>& operator /=(uint_t);
fixed<INT, FRAC>& operator +=(integral_type);
fixed<INT, FRAC>& operator -=(integral_type);
fixed<INT, FRAC>& operator *=(integral_type);
fixed<INT, FRAC>& operator /=(integral_type);
fixed<I,E> operator +(uint_t) const;
fixed<I,E> operator -(uint_t) const;
fixed<I,E> operator *(uint_t) const;
fixed<I,E> operator /(uint_t) const;
fixed<INT, FRAC> operator +(integral_type) const;
fixed<INT, FRAC> operator -(integral_type) const;
fixed<INT, FRAC> operator *(integral_type) const;
fixed<INT, FRAC> operator /(integral_type) const;
private:
uint_t m_value;
};
}