2011-08-08 16:44:30 +10:00
|
|
|
/*
|
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
|
2011-08-08 16:44:30 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-08 16:44:30 +10:00
|
|
|
*
|
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.
|
2011-08-08 16:44:30 +10:00
|
|
|
*
|
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 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
2014-12-05 13:20:05 +11:00
|
|
|
class fixed {
|
|
|
|
public:
|
2015-02-06 20:01:26 +11:00
|
|
|
typedef typename std::conditional<
|
|
|
|
std::is_signed<T>::value,
|
|
|
|
typename bits_type<I+E>::sint,
|
|
|
|
typename bits_type<I+E>::uint
|
|
|
|
>::type native_t;
|
|
|
|
|
|
|
|
typedef native_t integer_t;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
fixed (double);
|
|
|
|
fixed (float);
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed (native_t);
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
double to_double (void) const;
|
|
|
|
float to_float (void) const;
|
|
|
|
integer_t to_integer (void) const;
|
|
|
|
native_t to_native (void) const;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
static fixed<T,I,E> from_native (native_t);
|
|
|
|
static integer_t to_integer (native_t);
|
2015-01-15 13:59:28 +11:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E>& operator +=(const fixed<T,I,E>);
|
|
|
|
fixed<T,I,E>& operator -=(const fixed<T,I,E>);
|
|
|
|
fixed<T,I,E>& operator *=(const fixed<T,I,E>);
|
|
|
|
fixed<T,I,E>& operator /=(const fixed<T,I,E>);
|
2015-01-15 13:59:28 +11:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E> operator +(const fixed<T,I,E>) const;
|
|
|
|
fixed<T,I,E> operator -(const fixed<T,I,E>) const;
|
|
|
|
fixed<T,I,E> operator *(const fixed<T,I,E>) const;
|
|
|
|
fixed<T,I,E> operator /(const fixed<T,I,E>) const;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E>& operator +=(integer_t);
|
|
|
|
fixed<T,I,E>& operator -=(integer_t);
|
|
|
|
fixed<T,I,E>& operator *=(integer_t);
|
|
|
|
fixed<T,I,E>& operator /=(integer_t);
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
fixed<T,I,E> operator +(integer_t) const;
|
|
|
|
fixed<T,I,E> operator -(integer_t) const;
|
|
|
|
fixed<T,I,E> operator *(integer_t) const;
|
|
|
|
fixed<T,I,E> operator /(integer_t) const;
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:34:30 +11:00
|
|
|
private:
|
2015-02-06 19:01:06 +11:00
|
|
|
fixed () = default;
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
native_t m_value;
|
2014-12-05 13:20:05 +11:00
|
|
|
};
|
2015-02-06 16:35:11 +11:00
|
|
|
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E> bool operator== (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator!= (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
2015-02-06 16:35:11 +11:00
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E> bool operator< (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator<= (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator> (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator>= (util::fixed<T,I,E>, util::fixed<T,I,E>);
|
2015-02-06 16:35:23 +11:00
|
|
|
|
|
|
|
|
2015-02-06 20:01:26 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
|
|
|
std::ostream& operator<< (std::ostream&, fixed<T,I,E>);
|
2014-12-05 13:20:05 +11:00
|
|
|
}
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2015-02-06 16:36:05 +11:00
|
|
|
#endif // __UTIL_FIXED_HPP
|