/* * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. * * Copyright 2011-2014 Danny Robson */ #ifndef __UTIL_FIXED_HPP #define __UTIL_FIXED_HPP #include "types/bits.hpp" #include "maths.hpp" #include "endian.hpp" #include namespace util { template < // whether the type is signed or unsigned. supply an appropriately // signed type here; it won't be used for anything else. typename T, // how many bits total are used for the integral component unsigned I, // how many bits are used for the fractional component unsigned E > 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::sint; using uint_t = typename bits_type::uint; using native_type = typename std::conditional< std::is_signed::value, sint_t, uint_t >::type; using integer_type = typename std::conditional< std::is_signed::value, typename bits_type::sint, typename bits_type::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 from_native (native_type); static fixed from_integer (integer_type); static integer_type to_integer (native_type); fixed& operator +=(const fixed); fixed& operator -=(const fixed); fixed& operator *=(const fixed); fixed& operator /=(const fixed); fixed operator +(const fixed) const; fixed operator -(const fixed) const; fixed operator *(const fixed) const; fixed operator /(const fixed) const; fixed& operator +=(integer_type); fixed& operator -=(integer_type); fixed& operator *=(integer_type); fixed& operator /=(integer_type); fixed operator +(integer_type) const; fixed operator -(integer_type) const; fixed operator *(integer_type) const; fixed operator /(integer_type) const; private: fixed () = default; native_type m_value; }; template constexpr fixed bswap (fixed val) { return fixed::from_native ( bswap (val.to_native ()) ); } template bool operator== (util::fixed, util::fixed); template bool operator!= (util::fixed, util::fixed); template bool operator< (util::fixed, util::fixed); template bool operator<= (util::fixed, util::fixed); template bool operator> (util::fixed, util::fixed); template bool operator>= (util::fixed, util::fixed); template bool operator== (util::fixed, typename util::fixed::integer_type); template bool operator!= (util::fixed, typename util::fixed::integer_type); template bool operator< (util::fixed, typename util::fixed::integer_type); template bool operator<= (util::fixed, typename util::fixed::integer_type); template bool operator> (util::fixed, typename util::fixed::integer_type); template bool operator>= (util::fixed, typename util::fixed::integer_type); template std::ostream& operator<< (std::ostream&, fixed); } #endif // __UTIL_FIXED_HPP