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-2014 Danny Robson <danny@nerdcruft.net>
|
2011-08-08 16:44:30 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_FIXED_HPP
|
|
|
|
#define __UTIL_FIXED_HPP
|
|
|
|
|
2012-05-25 15:19:07 +10:00
|
|
|
#include "types/bits.hpp"
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
#include <cstdint>
|
2015-02-06 16:35:23 +11:00
|
|
|
#include <iostream>
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2014-12-05 13:20:05 +11:00
|
|
|
namespace util {
|
2015-02-06 16:34:30 +11:00
|
|
|
template <unsigned I, unsigned E>
|
2014-12-05 13:20:05 +11:00
|
|
|
class fixed {
|
|
|
|
public:
|
2015-02-06 16:34:30 +11:00
|
|
|
typedef typename bits_type<I+E>::uint uint_t;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
fixed (double);
|
|
|
|
fixed (float);
|
|
|
|
fixed (uint_t);
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
double to_double (void) const;
|
|
|
|
float to_float (void) const;
|
|
|
|
uint_t to_integral (void) const;
|
|
|
|
uint_t to_native (void) const;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
static fixed<I,E> from_native (uint_t);
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
static uint_t to_integral (uint_t);
|
2015-01-15 13:59:28 +11:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
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>);
|
2015-01-15 13:59:28 +11:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
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;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
fixed<I,E>& operator +=(uint_t);
|
|
|
|
fixed<I,E>& operator -=(uint_t);
|
|
|
|
fixed<I,E>& operator *=(uint_t);
|
|
|
|
fixed<I,E>& operator /=(uint_t);
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
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;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
private:
|
|
|
|
uint_t m_value;
|
2014-12-05 13:20:05 +11:00
|
|
|
};
|
2015-02-06 16:35:11 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <unsigned I, unsigned E> bool operator== (util::fixed<I,E>, util::fixed<I,E>);
|
|
|
|
template <unsigned I, unsigned E> bool operator!= (util::fixed<I,E>, util::fixed<I,E>);
|
|
|
|
|
|
|
|
template <unsigned I, unsigned E> bool operator< (util::fixed<I,E>, util::fixed<I,E>);
|
|
|
|
template <unsigned I, unsigned E> bool operator<= (util::fixed<I,E>, util::fixed<I,E>);
|
|
|
|
template <unsigned I, unsigned E> bool operator> (util::fixed<I,E>, util::fixed<I,E>);
|
|
|
|
template <unsigned I, unsigned E> bool operator>= (util::fixed<I,E>, util::fixed<I,E>);
|
2015-02-06 16:35:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <unsigned I, unsigned E>
|
|
|
|
std::ostream& operator<< (std::ostream&, fixed<I,E>);
|
2014-12-05 13:20:05 +11:00
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
|
|
|
#endif // __UTIL_FIXED_HPP
|