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
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "types/bits.hpp"
|
|
|
|
#include "maths.hpp"
|
|
|
|
#include "endian.hpp"
|
2011-08-08 16:44:30 +10:00
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <iosfwd>
|
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>
|
2017-01-04 22:38:41 +11:00
|
|
|
class [[gnu::packed]] fixed {
|
|
|
|
public:
|
|
|
|
static_assert (I > 0);
|
|
|
|
static_assert (E > 0);
|
|
|
|
static_assert ((I + E) % 8 == 0);
|
|
|
|
|
|
|
|
using sint_t = typename bits_type<I+E>::sint;
|
|
|
|
using uint_t = typename bits_type<I+E>::uint;
|
|
|
|
|
|
|
|
using native_type = typename std::conditional<
|
|
|
|
std::is_signed<T>::value,
|
|
|
|
sint_t, uint_t
|
|
|
|
>::type;
|
|
|
|
|
|
|
|
using integer_type = typename std::conditional<
|
|
|
|
std::is_signed<T>::value,
|
|
|
|
typename bits_type<divup (I, 8u) * 8u>::sint,
|
|
|
|
typename bits_type<divup (I, 8u) * 8u>::uint
|
|
|
|
>::type;
|
|
|
|
|
|
|
|
explicit fixed (double);
|
|
|
|
explicit fixed (float);
|
|
|
|
|
|
|
|
double to_double (void) const;
|
|
|
|
float to_float (void) const;
|
|
|
|
integer_type to_integer (void) const;
|
|
|
|
native_type to_native (void) const;
|
|
|
|
|
|
|
|
static fixed<T,I,E> from_native (native_type);
|
|
|
|
static fixed<T,I,E> from_integer (integer_type);
|
|
|
|
|
|
|
|
static integer_type to_integer (native_type);
|
|
|
|
|
|
|
|
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>);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
fixed<T,I,E>& operator +=(integer_type);
|
|
|
|
fixed<T,I,E>& operator -=(integer_type);
|
|
|
|
fixed<T,I,E>& operator *=(integer_type);
|
|
|
|
fixed<T,I,E>& operator /=(integer_type);
|
|
|
|
|
|
|
|
fixed<T,I,E> operator +(integer_type) const;
|
|
|
|
fixed<T,I,E> operator -(integer_type) const;
|
|
|
|
fixed<T,I,E> operator *(integer_type) const;
|
|
|
|
fixed<T,I,E> operator /(integer_type) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
fixed () = default;
|
|
|
|
|
|
|
|
native_type m_value;
|
2014-12-05 13:20:05 +11:00
|
|
|
};
|
2015-02-06 16:35:11 +11:00
|
|
|
|
|
|
|
|
2017-01-04 22:38:41 +11:00
|
|
|
template <typename T, unsigned I, unsigned E>
|
|
|
|
constexpr
|
|
|
|
fixed<T,I,E>
|
|
|
|
bswap (fixed<T,I,E> val)
|
|
|
|
{
|
|
|
|
return fixed<T,I,E>::from_native (
|
|
|
|
bswap (val.to_native ())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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>);
|
|
|
|
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
|
|
|
|
2017-01-04 22:38:41 +11:00
|
|
|
template <typename T, unsigned I, unsigned E> bool operator== (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator!= (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator< (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator<= (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator> (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
|
|
|
template <typename T, unsigned I, unsigned E> bool operator>= (util::fixed<T,I,E>, typename util::fixed<T,I,E>::integer_type);
|
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
|