Skip to content

Instantly share code, notes, and snippets.

@johnmcfarlane
Created July 9, 2016 20:44
Show Gist options
  • Save johnmcfarlane/e9d9aac94c849b393aa584190d21412c to your computer and use it in GitHub Desktop.
Save johnmcfarlane/e9d9aac94c849b393aa584190d21412c to your computer and use it in GitHub Desktop.

Header file fixed_point.h

#define SG14_FIXED_POINT_H 1

#include "type_traits.h"

#include "bits/common.h"

#define SG14_FIXED_POINT_EXCEPTIONS_ENABLED 

#include <stdexcept>

#include "bits/fixed_point_extras.h"

namespace sg14
{
    namespace _fixed_point_impl
    {
        template <typename T>
        using digits = std::integral_constant<int, width<T>::value-is_signed<T>::value>;
        
        template <typename T>
        using float_of_same_size = set_width_t<float, width<T>::value>;
        
        template <typename IntType>
        using next_size = typename sg14::set_width_t<IntType, width<IntType>::value*2>;
        
        template <typename IntType>
        using previous_size = typename sg14::set_width_t<IntType, width<IntType>::value/2>;
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        (Exponent==0),
        int>::type Dummy = 0>
        constexpr Output shift_left(Input i);
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        Exponent==0,
        int>::type Dummy = 0>
        constexpr Output shift_right(Input i);
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        !(Exponent<=0),
        int>::type Dummy = 0>
        constexpr Output shift_left(Input i);
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        !(Exponent<=0),
        int>::type Dummy = 0>
        constexpr Output shift_right(Input i);
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        (Exponent<0),
        int>::type Dummy = 0>
        constexpr Output shift_left(Input i);
        
        template <int Exponent, typename Output, typename Input, typename std::enable_if<
        Exponent<0,
        int>::type Dummy = 0>
        constexpr Output shift_right(Input i);
        
        template <typename S, int Exponent, typename std::enable_if<Exponent==0, int>::type Dummy = 0>
        constexpr S pow2();
        
        template <typename S, int Exponent, typename std::enable_if<!(Exponent<=0) && (Exponent<8), int>::type Dummy = 0>
        constexpr S pow2();
        
        template <typename S, int Exponent, typename std::enable_if<(Exponent>=8), int>::type Dummy = 0>
        constexpr S pow2();
        
        template <typename S, int Exponent, typename std::enable_if<!(Exponent>=0) && (Exponent>-8), int>::type Dummy = 0>
        constexpr S pow2();
        
        template <typename S, int Exponent, typename std::enable_if<(Exponent<=-8), int>::type Dummy = 0>
        constexpr S pow2();
        
        template <>
        struct capacity<0>;
        
        template <unsigned N>
        struct capacity;}
    
    template <typename Rep = int, int Exponent = 0>
    class fixed_point;
    
    template <int IntegerDigits, int FractionalDigits = 0, typename Archetype = signed>
    using make_fixed = fixed_point<
    set_width_t<Archetype, IntegerDigits+FractionalDigits+is_signed<Archetype>::value>,
    -FractionalDigits>;
    
    template <int IntegerDigits, int FractionalDigits = 0, typename Archetype = unsigned>
    using make_ufixed = make_fixed<
    IntegerDigits,
    FractionalDigits,
    typename make_unsigned<Archetype>::type>;
    
    namespace _fixed_point_impl
    {
        template <typename T>
        struct is_fixed_point;
        
        template <typename Rep, int Exponent>
        struct is_fixed_point;
        
        template <typename FixedPoint>
        struct widen_integer_result;
        
        template <typename FixedPoint>
        using widen_integer_result_t = typename widen_integer_result<FixedPoint>::type;
        
        template <typename FixedPoint>
        constexpr widen_integer_result_t<FixedPoint> widen_integer(const FixedPoint& from);
        
        template <typename FixedPoint>
        struct widen_fractional_result;
        
        template <typename FixedPoint>
        using widen_fractional_result_t = typename widen_fractional_result<FixedPoint>::type;
        
        template <typename FixedPoint>
        constexpr widen_fractional_result_t<FixedPoint> widen_fractional(const FixedPoint& from);
        
        struct default_arithmetic_policy;
        
        template <typename LhsRep, int LhsExponent, typename RhsInteger>
        struct common_type_mixed<
        fixed_point<LhsRep, LhsExponent>,
        RhsInteger,
        typename std::enable_if<is_integral<RhsInteger>::value>::type>;
        
        template <typename LhsRep, int LhsExponent, typename Float>
        struct common_type_mixed<
        fixed_point<LhsRep, LhsExponent>,
        Float,
        typename std::enable_if<std::is_floating_point<Float>::value>::type>;}}

namespace std{}

namespace sg14
{
    namespace _fixed_point_type
    {
        template <typename LhsFixedPoint, typename RhsFixedPoint>
        using subtract_result_rep = typename make_signed<typename std::common_type<LhsFixedPoint, RhsFixedPoint>::type>::type;
        
        template <typename Rep>
        using square_result_rep = typename make_unsigned<Rep>::type;
        
        template <typename FixedPoint>
        using sqrt_result_rep = typename make_unsigned<FixedPoint>::type;}
    
    template <typename Result, typename Rhs>
    constexpr Result negate(const Rhs& rhs);
    
    template <typename Result, typename Lhs, typename Rhs>
    constexpr Result add(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Result, typename Lhs, typename Rhs>
    constexpr Result subtract(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Result, typename Lhs, typename Rhs>
    constexpr Result multiply(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Result, typename Lhs, typename Rhs>
    constexpr Result divide(const Lhs& lhs, const Rhs& rhs);
    
    namespace _fixed_point_impl
    {
        template <typename Policy, typename Rhs>
        constexpr typename Policy::template negate<Rhs>::result_type policy_negate(const Rhs& rhs);
        
        template <typename Policy, typename Lhs, typename Rhs>
        constexpr typename Policy::template add<Lhs, Rhs>::result_type policy_add(const Lhs& lhs, const Rhs& rhs);
        
        template <typename Policy, typename Lhs, typename Rhs>
        constexpr typename Policy::template subtract<Lhs, Rhs>::result_type policy_subtract(const Lhs& lhs, const Rhs& rhs);
        
        template <typename Policy, typename Lhs, typename Rhs>
        constexpr typename Policy::template multiply<Lhs, Rhs>::result_type policy_multiply(const Lhs& lhs, const Rhs& rhs);
        
        template <typename Policy, typename Lhs, typename Rhs>
        constexpr typename Policy::template divide<Lhs, Rhs>::result_type policy_divide(const Lhs& lhs, const Rhs& rhs);}
    
    template <typename Rep, int Exponent>
    constexpr bool operator==(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Rep, int Exponent>
    constexpr bool operator!=(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Rep, int Exponent>
    constexpr bool operator<(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Rep, int Exponent>
    constexpr bool operator>(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Rep, int Exponent>
    constexpr bool operator>=(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Rep, int Exponent>
    constexpr bool operator<=(const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename RhsRep, int RhsExponent>
    constexpr typename _fixed_point_impl::default_arithmetic_policy::negate<
    fixed_point<RhsRep, RhsExponent>>::result_type operator-(const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsRep, int RhsExponent>
    constexpr typename _fixed_point_impl::default_arithmetic_policy::add<
    fixed_point<LhsRep, LhsExponent>,
    fixed_point<RhsRep, RhsExponent>>::result_type operator+(const fixed_point<LhsRep, LhsExponent>& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsRep, int RhsExponent>
    constexpr typename _fixed_point_impl::default_arithmetic_policy::subtract<
    fixed_point<LhsRep, LhsExponent>,
    fixed_point<RhsRep, RhsExponent>>::result_type operator-(const fixed_point<LhsRep, LhsExponent>& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsRep, int RhsExponent>
    constexpr typename _fixed_point_impl::default_arithmetic_policy::multiply<
    fixed_point<LhsRep, LhsExponent>,
    fixed_point<RhsRep, RhsExponent>>::result_type operator*(const fixed_point<LhsRep, LhsExponent>& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsRep, int RhsExponent>
    constexpr typename _fixed_point_impl::default_arithmetic_policy::divide<
    fixed_point<LhsRep, LhsExponent>,
    fixed_point<RhsRep, RhsExponent>>::result_type operator/(const fixed_point<LhsRep, LhsExponent>& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator==(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator!=(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator<(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator>(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator>=(const Lhs& lhs, const Rhs& rhs);
    
    template <typename Lhs, typename Rhs>
    constexpr typename std::enable_if<
    _fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator<=(const Lhs& lhs, const Rhs& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsInteger, typename = typename std::enable_if<is_integral<RhsInteger>::value>::type>
    constexpr decltype(lhs + fixed_point<RhsInteger, 0> operator+(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsInteger& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsInteger, typename = typename std::enable_if<is_integral<RhsInteger>::value>::type>
    constexpr decltype(lhs - fixed_point<RhsInteger, 0> operator-(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsInteger& rhs);
    
    template <typename Rep, int Exponent, typename Integer, typename = typename std::enable_if<is_integral<Integer>::value>::type>
    constexpr fixed_point<decltype(std::declval<Rep>()*std::declval<Integer>()), Exponent> operator*(const fixed_point<Rep, Exponent>& lhs, const Integer& rhs);
    
    template <typename Rep, int Exponent, typename Integer, typename = typename std::enable_if<is_integral<Integer>::value>::type>
    constexpr fixed_point<decltype(std::declval<Rep>()/std::declval<Integer>()), Exponent> operator/(const fixed_point<Rep, Exponent>& lhs, const Integer& rhs);
    
    template <typename LhsInteger, typename RhsRep, int RhsExponent, typename = typename std::enable_if<is_integral<LhsInteger>::value>::type>
    constexpr decltype(fixed_point<LhsInteger, 0> operator+(const LhsInteger& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsInteger, typename RhsRep, int RhsExponent, typename = typename std::enable_if<is_integral<LhsInteger>::value>::type>
    constexpr decltype(fixed_point<LhsInteger, 0> operator-(const LhsInteger& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename Integer, typename Rep, int Exponent, typename = typename std::enable_if<is_integral<Integer>::value>::type>
    constexpr fixed_point<decltype(std::declval<Integer>()*std::declval<Rep>()), Exponent> operator*(const Integer& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename Integer, typename Rep, int Exponent, typename = typename std::enable_if<is_integral<Integer>::value>::type>
    constexpr fixed_point<decltype(std::declval<Integer>()/std::declval<Rep>()), Exponent> operator/(const Integer& lhs, const fixed_point<Rep, Exponent>& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsFloat, typename = typename std::enable_if<std::is_floating_point<RhsFloat>::value>::type>
    constexpr _impl::common_type_t<fixed_point<LhsRep, LhsExponent>, RhsFloat> operator+(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsFloat& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsFloat, typename = typename std::enable_if<std::is_floating_point<RhsFloat>::value>::type>
    constexpr _impl::common_type_t<fixed_point<LhsRep, LhsExponent>, RhsFloat> operator-(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsFloat& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsFloat>
    constexpr _impl::common_type_t<
    fixed_point<LhsRep, LhsExponent>,
    typename std::enable_if<std::is_floating_point<RhsFloat>::value, RhsFloat>::type> operator*(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsFloat& rhs);
    
    template <typename LhsRep, int LhsExponent, typename RhsFloat>
    constexpr _impl::common_type_t<
    fixed_point<LhsRep, LhsExponent>,
    typename std::enable_if<std::is_floating_point<RhsFloat>::value, RhsFloat>::type> operator/(const fixed_point<LhsRep, LhsExponent>& lhs, const RhsFloat& rhs);
    
    template <typename LhsFloat, typename RhsRep, int RhsExponent, typename = typename std::enable_if<std::is_floating_point<LhsFloat>::value>::type>
    constexpr _impl::common_type_t<LhsFloat, fixed_point<RhsRep, RhsExponent>> operator+(const LhsFloat& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsFloat, typename RhsRep, int RhsExponent, typename = typename std::enable_if<std::is_floating_point<LhsFloat>::value>::type>
    constexpr _impl::common_type_t<LhsFloat, fixed_point<RhsRep, RhsExponent>> operator-(const LhsFloat& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsFloat, typename RhsRep, int RhsExponent>
    constexpr _impl::common_type_t<
    typename std::enable_if<std::is_floating_point<LhsFloat>::value, LhsFloat>::type,
    fixed_point<RhsRep, RhsExponent>> operator*(const LhsFloat& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsFloat, typename RhsRep, int RhsExponent>
    constexpr _impl::common_type_t<
    typename std::enable_if<std::is_floating_point<LhsFloat>::value, LhsFloat>::type,
    fixed_point<RhsRep, RhsExponent>> operator/(const LhsFloat& lhs, const fixed_point<RhsRep, RhsExponent>& rhs);
    
    template <typename LhsRep, int Exponent, typename Rhs>
    fixed_point<LhsRep, Exponent>& operator+=(fixed_point<LhsRep, Exponent>& lhs, const Rhs& rhs);
    
    template <typename LhsRep, int Exponent, typename Rhs>
    fixed_point<LhsRep, Exponent>& operator-=(fixed_point<LhsRep, Exponent>& lhs, const Rhs& rhs);
    
    template <typename LhsRep, int LhsExponent, typename Rhs>
    constexpr fixed_point<LhsRep, LhsExponent> operator<<(const fixed_point<LhsRep, LhsExponent>& lhs, const Rhs& rhs);
    
    template <typename LhsRep, int LhsExponent, typename Rhs>
    constexpr fixed_point<LhsRep, LhsExponent> operator>>(const fixed_point<LhsRep, LhsExponent>& lhs, const Rhs& rhs);}

Macro SG14_FIXED_POINT_H

#define SG14_FIXED_POINT_H 1

Macro SG14_FIXED_POINT_EXCEPTIONS_ENABLED

#define SG14_FIXED_POINT_EXCEPTIONS_ENABLED 

Alias template sg14::_fixed_point_impl::digits<T>

template <typename T>
using digits = std::integral_constant<int, width<T>::value-is_signed<T>::value>;

Alias template sg14::_fixed_point_impl::float_of_same_size<T>

template <typename T>
using float_of_same_size = set_width_t<float, width<T>::value>;

Alias template sg14::_fixed_point_impl::next_size<IntType>

template <typename IntType>
using next_size = typename sg14::set_width_t<IntType, width<IntType>::value*2>;

Alias template sg14::_fixed_point_impl::previous_size<IntType>

template <typename IntType>
using previous_size = typename sg14::set_width_t<IntType, width<IntType>::value/2>;

Function template sg14::_fixed_point_impl::shift_left<Exponent, Output, Input, Dummy>

template <int Exponent, typename Output, typename Input, typename std::enable_if<
(Exponent==0),
int>::type Dummy = 0>
constexpr Output shift_left(Input i);

Function template sg14::_fixed_point_impl::pow2<S, Exponent, Dummy>

template <typename S, int Exponent, typename std::enable_if<Exponent==0, int>::type Dummy = 0>
constexpr S pow2();

Class template sg14::_fixed_point_impl::capacity<N>

template <unsigned N>
struct capacity
{
    static constexpr int value = capacity<N/2>::+1;
};

Class template sg14::fixed_point<Rep, Exponent>

template <typename Rep = int, int Exponent = 0>
class fixed_point
{
public:
    using rep = Rep;
    static constexpr int exponent = Exponent;
    static constexpr int digits = _fixed_point_impl::<Rep>::value;
    static constexpr int integer_digits = digits+exponent;
    static constexpr int fractional_digits = -exponent;
    
public:
    fixed_point();
    template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
    explicit constexpr fixed_point(S s);
    template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
    explicit constexpr fixed_point(S s);
    template <typename FromRep, int FromExponent>
    explicit constexpr fixed_point(const fixed_point<FromRep, FromExponent>& rhs);
    template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
    fixed_point& operator=(S s);
    template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
    fixed_point& operator=(S s);
    template <typename FromRep, int FromExponent>
    fixed_point& operator=(const fixed_point<FromRep, FromExponent>& rhs);
    template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
    explicit constexpr operator S() const;
    template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
    explicit constexpr operator S() const;
    explicit constexpr operator bool() const;
    template <typename Rhs>
    fixed_point& operator*=(const Rhs& rhs);
    template <typename Rhs>
    fixed_point& operator/=(const Rhs& rhs);
    constexpr rep data() const;
    static constexpr fixed_point from_data(rep r);
};

literal real number approximation that uses fixed-point arithmetic

To define a fixed-point value 1 byte in size with a sign bit, 3 integer bits and 4 fractional bits: \snippet snippets.cpp define a fixed_point value

Type alias sg14::rep

using rep = Rep;

alias to template parameter, \a Rep

Variable sg14::exponent

static constexpr int exponent = Exponent;

value of template parameter, \a Exponent

Variable sg14::digits

static constexpr int digits = _fixed_point_impl::<Rep>::value;

number of binary digits this type can represent; equivalent to std::numeric_limits::digits

Variable sg14::integer_digits

static constexpr int integer_digits = digits+exponent;

number of binary digits devoted to integer part of value; can be negative for specializations with especially small ranges

Variable sg14::fractional_digits

static constexpr int fractional_digits = -exponent;

number of binary digits devoted to fractional part of value; can be negative for specializations with especially large ranges

Constructor sg14::fixed_point

fixed_point();

default constructor

Function template sg14::fixed_point<S, Dummy>

template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
explicit constexpr fixed_point(S s);

constructor taking an integer type

Function template sg14::fixed_point<S, Dummy>

template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
explicit constexpr fixed_point(S s);

constructor taking a floating-point type

Function template sg14::fixed_point<FromRep, FromExponent>

template <typename FromRep, int FromExponent>
explicit constexpr fixed_point(const fixed_point<FromRep, FromExponent>& rhs);

constructor taking a fixed-point type

Function template sg14::operator=<S, Dummy>

template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
fixed_point& operator=(S s);

copy assignment operator taking an integer type

Function template sg14::operator=<S, Dummy>

template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
fixed_point& operator=(S s);

copy assignment operator taking a floating-point type

Function template sg14::operator=<FromRep, FromExponent>

template <typename FromRep, int FromExponent>
fixed_point& operator=(const fixed_point<FromRep, FromExponent>& rhs);

copy assignement operator taking a fixed-point type

Function template sg14::operator S<S, Dummy>

template <typename S, typename std::enable_if<is_integral<S>::value, int>::type Dummy = 0>
explicit constexpr operator S() const;

returns value represented as integral

Function template sg14::operator S<S, Dummy>

template <typename S, typename std::enable_if<std::is_floating_point<S>::value, int>::type Dummy = 0>
explicit constexpr operator S() const;

returns value represented as floating-point

Conversion operator sg14::operator bool

explicit constexpr operator bool() const;

returns non-zeroness represented as boolean

Function sg14::data

constexpr rep data() const;

returns internal representation of value

Function sg14::from_data

static constexpr fixed_point from_data(rep r);

creates an instance given the underlying representation value


Alias template sg14::make_fixed<IntegerDigits, FractionalDigits, Archetype>

template <int IntegerDigits, int FractionalDigits = 0, typename Archetype = signed>
using make_fixed = fixed_point<
set_width_t<Archetype, IntegerDigits+FractionalDigits+is_signed<Archetype>::value>,
-FractionalDigits>;

Produce a fixed-point type with the given number of integer and fractional digits.

Remarks: The signage of \a Archetype specifies signage of the resultant fixed-point type. \remarks Typical choices for \a Archetype, signed and unsigned, result in a type that uses built-in integers for \a fixed_point::rep. \remarks Resultant type is signed by default.

To generate a fixed-point type with a sign bit, 8 fractional bits and at least 7 integer bits: \snippet snippets.cpp use make_fixed

Alias template sg14::make_ufixed<IntegerDigits, FractionalDigits, Archetype>

template <int IntegerDigits, int FractionalDigits = 0, typename Archetype = unsigned>
using make_ufixed = make_fixed<
IntegerDigits,
FractionalDigits,
typename make_unsigned<Archetype>::type>;

Produce an unsigned fixed-point type with the given number of integer and fractional digits.

Class template sg14::_fixed_point_impl::is_fixed_point<T>

template <typename T>
struct is_fixed_point
: std::false_type
{};

Class template sg14::_fixed_point_impl::widen_fractional_result<FixedPoint>

template <typename FixedPoint>
struct widen_fractional_result
{
    using prev_rep = typename FixedPoint::rep;
    using next_rep = _fixed_point_impl::next_size<prev_rep>;
    using type = fixed_point<
    next_rep,
    FixedPoint::exponent+static_cast<int>(width<prev_rep>::value)
    -static_cast<int>(width<next_rep>::value)>;
};

Class sg14::_fixed_point_impl::default_arithmetic_policy

struct default_arithmetic_policy
{
    template <typename Lhs, typename Rhs>
    struct common_type;
    template <typename Lhs, typename Rhs>
    struct operator_base;
    template <typename Rhs>
    struct negate;
    template <typename Lhs, typename Rhs>
    struct add;
    template <typename Lhs, typename Rhs>
    struct subtract;
    template <typename Lhs, typename Rhs>
    struct multiply;
    template <typename Lhs, typename Rhs>
    struct divide;
};

Alias template sg14::_fixed_point_type::subtract_result_rep<LhsFixedPoint, RhsFixedPoint>

template <typename LhsFixedPoint, typename RhsFixedPoint>
using subtract_result_rep = typename make_signed<typename std::common_type<LhsFixedPoint, RhsFixedPoint>::type>::type;

Function template sg14::negate<Result, Rhs>

template <typename Result, typename Rhs>
constexpr Result negate(const Rhs& rhs);

calculates the negative of a \ref fixed_point value

Function template sg14::add<Result, Lhs, Rhs>

template <typename Result, typename Lhs, typename Rhs>
constexpr Result add(const Lhs& lhs, const Rhs& rhs);

calculates the sum of two \ref fixed_point values

Function template sg14::subtract<Result, Lhs, Rhs>

template <typename Result, typename Lhs, typename Rhs>
constexpr Result subtract(const Lhs& lhs, const Rhs& rhs);

calculates the difference of two \ref fixed_point values

Function template sg14::multiply<Result, Lhs, Rhs>

template <typename Result, typename Lhs, typename Rhs>
constexpr Result multiply(const Lhs& lhs, const Rhs& rhs);

calculates the product of two \ref fixed_point factors

Function template sg14::divide<Result, Lhs, Rhs>

template <typename Result, typename Lhs, typename Rhs>
constexpr Result divide(const Lhs& lhs, const Rhs& rhs);

calculates the quotient of two \ref fixed_point values

Function template sg14::_fixed_point_impl::policy_negate<Policy, Rhs>

template <typename Policy, typename Rhs>
constexpr typename Policy::template negate<Rhs>::result_type policy_negate(const Rhs& rhs);

Function template sg14::operator==<Lhs, Rhs>

template <typename Lhs, typename Rhs>
constexpr typename std::enable_if<
_fixed_point_impl::is_fixed_point<Lhs>::value || _fixed_point_impl::is_fixed_point<Rhs>::value, bool>::type operator==(const Lhs& lhs, const Rhs& rhs);

Function template sg14::operator<<<LhsRep, LhsExponent, Rhs>

template <typename LhsRep, int LhsExponent, typename Rhs>
constexpr fixed_point<LhsRep, LhsExponent> operator<<(const fixed_point<LhsRep, LhsExponent>& lhs, const Rhs& rhs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment