libcruft-util/fixed.cpp

206 lines
6.4 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
2015-02-06 16:34:30 +11:00
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "fixed.hpp"
2015-02-06 16:34:30 +11:00
#include "maths.hpp"
2014-12-05 13:20:05 +11:00
using namespace util;
2015-02-06 16:34:30 +11:00
///////////////////////////////////////////////////////////////////////////////
// Constructors
2015-02-06 20:01:26 +11:00
template <typename T, unsigned I, unsigned E>
fixed<T,I,E>::fixed (native_t val):
2015-02-06 16:34:30 +11:00
m_value (val << E)
{
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");
}
2015-02-06 16:34:30 +11:00
///////////////////////////////////////////////////////////////////////////////
// Conversions
2015-02-06 20:01:26 +11:00
template <typename T, unsigned I, unsigned E>
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);
}
2015-02-06 16:34:30 +11:00
//-----------------------------------------------------------------------------
2015-02-06 20:01:26 +11:00
template <typename T, unsigned I, unsigned E>
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);
}
2015-02-06 16:34:30 +11:00
//-----------------------------------------------------------------------------
2015-02-06 20:01:26 +11:00
template <typename T, unsigned I, unsigned E>
typename fixed<T,I,E>::native_t
fixed<T,I,E>::to_integer (void) const
2015-02-06 16:34:30 +11:00
{
return m_value >> E;
}
2015-02-06 16:34:30 +11:00
//-----------------------------------------------------------------------------
2015-02-06 20:01:26 +11:00
template <typename T, unsigned I, unsigned E>
typename fixed<T,I,E>::native_t
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>
fixed<T,I,E>::from_native (native_t i)
2015-01-15 13:59:28 +11:00
{
2015-02-06 20:01:26 +11:00
fixed<T,I,E> v;
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>
typename fixed<T,I,E>::native_t
fixed<T,I,E>::to_integer (native_t n)
2015-01-15 13:59:28 +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> \
util::fixed<T,I,E>& \
util::fixed<T,I,E>::operator OP (const fixed<T,I,E> rhs) \
{ \
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> \
util::fixed<T,I,E> \
util::fixed<T,I,E>::operator OP (const fixed<T,I,E> rhs) const \
{ \
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
2015-02-06 20:01:26 +11:00
#define SIMPLE_INTEGER_REF(OP) \
template <typename T, unsigned I, unsigned E> \
fixed<T,I,E>& \
fixed<T,I,E>::operator OP (native_t val) \
{ \
m_value OP val << E; \
return *this; \
}
2015-02-06 20:01:26 +11:00
SIMPLE_INTEGER_REF(+=)
SIMPLE_INTEGER_REF(-=)
SIMPLE_INTEGER_REF(*=)
SIMPLE_INTEGER_REF(/=)
2015-02-06 16:34:30 +11:00
//-----------------------------------------------------------------------------
2015-02-06 20:01:26 +11:00
#define SIMPLE_INTEGER_LIT(OP) \
template <typename T, unsigned I, unsigned E> \
fixed<T,I,E> \
fixed<T,I,E>::operator OP (native_t val) const \
{ \
return fixed<T,I,E>::from_native (m_value OP val << E); \
2015-02-06 16:34:30 +11:00
}
2015-02-06 20:01:26 +11:00
SIMPLE_INTEGER_LIT(+)
SIMPLE_INTEGER_LIT(-)
SIMPLE_INTEGER_LIT(*)
SIMPLE_INTEGER_LIT(/)
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 \
2015-02-06 20:01:26 +11:00
util::operator OP (util::fixed<T,I,E> a, \
util::fixed<T,I,E> b) \
2015-02-06 16:35:11 +11:00
{ \
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
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&
2015-02-06 20:01:26 +11:00
util::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) \
template class util::fixed<T,I,E>; \
template std::ostream& util::operator<< (std::ostream&, fixed<T,I,E>); \
template bool util::operator== (util::fixed<T,I,E>, util::fixed<T,I,E>); \
template bool util::operator!= (util::fixed<T,I,E>, util::fixed<T,I,E>); \
template bool util::operator< (util::fixed<T,I,E>, util::fixed<T,I,E>); \
template bool util::operator<= (util::fixed<T,I,E>, util::fixed<T,I,E>); \
template bool util::operator> (util::fixed<T,I,E>, util::fixed<T,I,E>); \
template bool util::operator>= (util::fixed<T,I,E>, util::fixed<T,I,E>);
template class util::fixed<signed,8,8>;
INSTANTIATE(signed,16,16)
INSTANTIATE(signed,26, 6)
INSTANTIATE(signed,32,32)
INSTANTIATE(unsigned,16,16)
INSTANTIATE(unsigned,26, 6)
INSTANTIATE(unsigned,32,32)