2011-08-08 16:44:30 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2015-02-06 16:34:30 +11:00
|
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
2011-08-08 16:44:30 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fixed.hpp"
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
#include "maths.hpp"
|
|
|
|
|
2011-08-08 16:44:30 +10:00
|
|
|
#include <cmath>
|
|
|
|
|
2014-12-05 13:20:05 +11:00
|
|
|
using namespace util;
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>::fixed (uint_t val):
|
|
|
|
m_value (val << E)
|
2011-08-08 16:44:30 +10:00
|
|
|
{
|
2015-02-06 16:34:30 +11:00
|
|
|
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");
|
2011-08-08 16:44:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Conversions
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
template <unsigned I, unsigned E>
|
2011-08-08 16:44:30 +10:00
|
|
|
double
|
2015-02-06 16:34:30 +11:00
|
|
|
fixed<I,E>::to_double (void) const
|
|
|
|
{
|
|
|
|
return static_cast<double> (m_value) / pow (2, E);
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
2011-08-08 16:44:30 +10:00
|
|
|
float
|
2015-02-06 16:34:30 +11:00
|
|
|
fixed<I,E>::to_float (void) const
|
|
|
|
{
|
|
|
|
return static_cast<float> (m_value) / pow (2, E);
|
2014-07-02 15:47:09 +10:00
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
typename fixed<I,E>::uint_t
|
2015-02-06 19:00:47 +11:00
|
|
|
fixed<I,E>::to_integer (void) const
|
2015-02-06 16:34:30 +11:00
|
|
|
{
|
|
|
|
return m_value >> E;
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
typename fixed<I,E>::uint_t
|
|
|
|
fixed<I,E>::to_native (void) const
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
2014-12-05 13:20:23 +11:00
|
|
|
|
|
|
|
|
2015-01-15 13:59:28 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 16:34:30 +11:00
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>
|
|
|
|
fixed<I,E>::from_native (uint_t i)
|
2015-01-15 13:59:28 +11:00
|
|
|
{
|
2015-02-06 16:34:30 +11:00
|
|
|
return fixed<I,E> {i};
|
2015-01-15 13:59:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 16:34:30 +11:00
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
typename fixed<I,E>::uint_t
|
2015-02-06 19:00:47 +11:00
|
|
|
fixed<I,E>::to_integer (uint_t n)
|
2015-01-15 13:59:28 +11:00
|
|
|
{
|
2015-02-06 19:00:47 +11:00
|
|
|
return n >> E;
|
2015-01-15 13:59:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Integer operators
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>&
|
|
|
|
fixed<I,E>::operator+= (uint_t val)
|
|
|
|
{
|
|
|
|
m_value += val << E;
|
2011-08-08 16:44:30 +10:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>&
|
|
|
|
fixed<I,E>::operator-= (uint_t val)
|
|
|
|
{
|
|
|
|
m_value -= val << E;
|
2011-08-08 16:44:30 +10:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>&
|
|
|
|
fixed<I,E>::operator*= (uint_t val)
|
|
|
|
{
|
2011-08-08 16:44:30 +10:00
|
|
|
m_value *= val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>&
|
|
|
|
fixed<I,E>::operator/= (uint_t val)
|
|
|
|
{
|
2011-08-08 16:44:30 +10:00
|
|
|
m_value /= val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>
|
|
|
|
fixed<I,E>::operator+ (uint_t val) const
|
|
|
|
{
|
|
|
|
return fixed<I,E> (m_value + val << E);
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>
|
|
|
|
fixed<I,E>::operator- (uint_t val) const
|
|
|
|
{
|
|
|
|
return fixed<I,E> (m_value - val << E);
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>
|
|
|
|
fixed<I,E>::operator* (uint_t val) const
|
|
|
|
{
|
|
|
|
return fixed<I,E> (m_value * val);
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
fixed<I,E>
|
|
|
|
fixed<I,E>::operator /(uint_t val) const
|
|
|
|
{
|
|
|
|
return fixed<I,E> (m_value / val);
|
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:35:11 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// logical operators
|
|
|
|
|
|
|
|
#define LOGIC_OP(OP) \
|
|
|
|
template <unsigned I, unsigned E> \
|
|
|
|
bool \
|
|
|
|
util::operator OP (util::fixed<I,E> a, \
|
|
|
|
util::fixed<I,E> b) \
|
|
|
|
{ \
|
|
|
|
return a.to_native () OP b.to_native (); \
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGIC_OP(==)
|
|
|
|
LOGIC_OP(!=)
|
|
|
|
LOGIC_OP(<)
|
|
|
|
LOGIC_OP(<=)
|
|
|
|
LOGIC_OP(>)
|
|
|
|
LOGIC_OP(>=)
|
|
|
|
|
2015-02-06 16:35:23 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// iostream operators
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
std::ostream&
|
|
|
|
util::operator<< (std::ostream &os, fixed<I,E> v)
|
|
|
|
{
|
|
|
|
return os << v.to_double ();
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:35:11 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Instantiations
|
|
|
|
|
|
|
|
#define INSTANTIATE(I,E) \
|
|
|
|
template class util::fixed<(I),(E)>; \
|
2015-02-06 16:35:23 +11:00
|
|
|
template std::ostream& util::operator<< (std::ostream&, fixed<(I),(E)>); \
|
2015-02-06 16:35:11 +11:00
|
|
|
template bool util::operator== (util::fixed<(I),(E)>, util::fixed<(I),(E)>); \
|
|
|
|
template bool util::operator!= (util::fixed<(I),(E)>, util::fixed<(I),(E)>); \
|
|
|
|
template bool util::operator< (util::fixed<(I),(E)>, util::fixed<(I),(E)>); \
|
|
|
|
template bool util::operator<= (util::fixed<(I),(E)>, util::fixed<(I),(E)>); \
|
|
|
|
template bool util::operator> (util::fixed<(I),(E)>, util::fixed<(I),(E)>); \
|
|
|
|
template bool util::operator>= (util::fixed<(I),(E)>, util::fixed<(I),(E)>);
|
|
|
|
|
|
|
|
INSTANTIATE(16,16)
|
|
|
|
INSTANTIATE(26, 6)
|
|
|
|
INSTANTIATE(32,32)
|