2011-08-08 16:44:30 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-08-08 16:44:30 +10:00
|
|
|
*
|
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"
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <ostream>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using namespace cruft;
|
2014-12-05 13:20:05 +11:00
|
|
|
|
2016-10-11 23:47:57 +11:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2011-08-08 16:44:30 +10:00
|
|
|
double
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E>::to_double (void) const
|
2015-02-06 16:34:30 +11:00
|
|
|
{
|
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2011-08-08 16:44:30 +10:00
|
|
|
float
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E>::to_float (void) const
|
2015-02-06 16:34:30 +11:00
|
|
|
{
|
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2017-01-04 22:38:41 +11:00
|
|
|
typename fixed<T,I,E>::integer_type
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,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
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2017-01-04 22:38:41 +11:00
|
|
|
typename fixed<T,I,E>::native_type
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E>::to_native (void) const
|
2015-02-06 16:34:30 +11:00
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
2014-12-05 13:20:23 +11:00
|
|
|
|
|
|
|
|
2015-01-15 13:59:28 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
|
|
|
fixed<T,I,E>
|
2017-01-04 22:38:41 +11:00
|
|
|
fixed<T,I,E>::from_native (native_type i)
|
2015-01-15 13:59:28 +11:00
|
|
|
{
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E> v;
|
2015-02-06 19:01:06 +11:00
|
|
|
v.m_value = i;
|
|
|
|
return v;
|
2015-01-15 13:59:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2017-01-04 22:38:41 +11:00
|
|
|
fixed<T,I,E>
|
|
|
|
fixed<T,I,E>::from_integer (integer_type val)
|
|
|
|
{
|
|
|
|
constexpr auto rshift = sizeof (val) * 8 - I;
|
|
|
|
constexpr auto lshift = E - rshift;
|
|
|
|
|
|
|
|
fixed<T,I,E> res;
|
|
|
|
res.m_value = static_cast<native_type> (val) << lshift;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T, unsigned I, unsigned E>
|
|
|
|
typename fixed<T,I,E>::integer_type
|
|
|
|
fixed<T,I,E>::to_integer (native_type 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 19:01:38 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fixed operators
|
2015-02-06 20:01:26 +11:00
|
|
|
#define SIMPLE_FIXED_REF(OP) \
|
|
|
|
template <typename T, unsigned I, unsigned E> \
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::fixed<T,I,E>& \
|
|
|
|
cruft::fixed<T,I,E>::operator OP (const fixed<T,I,E> rhs) \
|
2015-02-06 20:01:26 +11:00
|
|
|
{ \
|
|
|
|
m_value OP rhs.m_value; \
|
|
|
|
return *this; \
|
2015-02-06 19:01:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SIMPLE_FIXED_REF(-=)
|
|
|
|
SIMPLE_FIXED_REF(+=)
|
|
|
|
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
#define SIMPLE_FIXED_LIT(OP) \
|
|
|
|
template <typename T, unsigned I, unsigned E> \
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::fixed<T,I,E> \
|
|
|
|
cruft::fixed<T,I,E>::operator OP (const fixed<T,I,E> rhs) const \
|
2015-02-06 20:01:26 +11:00
|
|
|
{ \
|
|
|
|
fixed<T,I,E> v; \
|
|
|
|
v.m_value = m_value OP rhs.m_value; \
|
|
|
|
return v; \
|
2015-02-06 19:01:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SIMPLE_FIXED_LIT(-)
|
|
|
|
SIMPLE_FIXED_LIT(+)
|
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Integer operators
|
2017-01-04 22:38:41 +11:00
|
|
|
#define SIMPLE_INTEGER_REF(OP) \
|
|
|
|
template <typename T, unsigned I, unsigned E> \
|
|
|
|
fixed<T,I,E>& \
|
|
|
|
fixed<T,I,E>::operator OP (integer_type val) \
|
|
|
|
{ \
|
|
|
|
m_value OP (static_cast<native_type> (val) << E); \
|
|
|
|
return *this; \
|
2011-08-08 16:44:30 +10:00
|
|
|
}
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
SIMPLE_INTEGER_REF(+=)
|
|
|
|
SIMPLE_INTEGER_REF(-=)
|
|
|
|
SIMPLE_INTEGER_REF(*=)
|
|
|
|
SIMPLE_INTEGER_REF(/=)
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-01-04 22:38:41 +11:00
|
|
|
#define SIMPLE_INTEGER_LIT(OP) \
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E> \
|
|
|
|
fixed<T,I,E> \
|
2017-01-04 22:38:41 +11:00
|
|
|
fixed<T,I,E>::operator OP (integer_type val) const \
|
2015-02-06 20:01:26 +11:00
|
|
|
{ \
|
2018-03-27 20:16:32 +11:00
|
|
|
return fixed<T,I,E>::from_native (m_value OP (native_type{val} << E)); \
|
2015-02-06 16:34:30 +11:00
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
SIMPLE_INTEGER_LIT(+)
|
|
|
|
SIMPLE_INTEGER_LIT(-)
|
|
|
|
SIMPLE_INTEGER_LIT(*)
|
|
|
|
SIMPLE_INTEGER_LIT(/)
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
|
2015-02-06 16:35:11 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// logical operators
|
|
|
|
|
|
|
|
#define LOGIC_OP(OP) \
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E> \
|
2015-02-06 16:35:11 +11:00
|
|
|
bool \
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::operator OP (cruft::fixed<T,I,E> a, \
|
|
|
|
cruft::fixed<T,I,E> b) \
|
2015-02-06 16:35:11 +11:00
|
|
|
{ \
|
|
|
|
return a.to_native () OP b.to_native (); \
|
2017-01-04 22:38:41 +11:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
template <typename T, unsigned I, unsigned E> \
|
|
|
|
bool \
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::operator OP (cruft::fixed<T,I,E> a, \
|
|
|
|
typename cruft::fixed<T,I,E>::integer_type b) \
|
2017-01-04 22:38:41 +11:00
|
|
|
{ \
|
2018-08-05 14:42:02 +10:00
|
|
|
return a OP cruft::fixed<T,I,E>::from_integer (b); \
|
2015-02-06 16:35:11 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGIC_OP(==)
|
|
|
|
LOGIC_OP(!=)
|
|
|
|
LOGIC_OP(<)
|
|
|
|
LOGIC_OP(<=)
|
|
|
|
LOGIC_OP(>)
|
|
|
|
LOGIC_OP(>=)
|
|
|
|
|
2017-01-04 22:38:41 +11:00
|
|
|
|
2015-02-06 16:35:23 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// iostream operators
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2015-02-06 16:35:23 +11:00
|
|
|
std::ostream&
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::operator<< (std::ostream &os, fixed<T,I,E> v)
|
2015-02-06 16:35:23 +11:00
|
|
|
{
|
|
|
|
return os << v.to_double ();
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:35:11 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Instantiations
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
#define INSTANTIATE(T,I,E) \
|
2018-08-05 14:42:02 +10:00
|
|
|
template class cruft::fixed<T,I,E>; \
|
|
|
|
template std::ostream& cruft::operator<< (std::ostream&, fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator== (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator!= (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator< (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator<= (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator> (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator>= (cruft::fixed<T,I,E>, cruft::fixed<T,I,E>); \
|
|
|
|
template bool cruft::operator== (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type); \
|
|
|
|
template bool cruft::operator!= (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type); \
|
|
|
|
template bool cruft::operator< (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type); \
|
|
|
|
template bool cruft::operator<= (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type); \
|
|
|
|
template bool cruft::operator> (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type); \
|
|
|
|
template bool cruft::operator>= (cruft::fixed<T,I,E>, typename cruft::fixed<T,I,E>::integer_type);
|
|
|
|
|
|
|
|
template class cruft::fixed<signed,8,8>;
|
2015-02-06 20:01:26 +11:00
|
|
|
|
2017-01-04 22:38:41 +11:00
|
|
|
INSTANTIATE(signed, 2,14)
|
2015-02-06 20:01:26 +11:00
|
|
|
INSTANTIATE(signed,16,16)
|
|
|
|
INSTANTIATE(signed,26, 6)
|
|
|
|
INSTANTIATE(signed,32,32)
|
|
|
|
INSTANTIATE(unsigned,16,16)
|
|
|
|
INSTANTIATE(unsigned,26, 6)
|
|
|
|
INSTANTIATE(unsigned,32,32)
|