fixed_point (deprecated)  rev.2
Binary Fixed-Point Arithmetic Library in C++
Namespaces | Classes | Typedefs | Functions
sg14 Namespace Reference

study group 14 of the C++ working group More...

Namespaces

 literals
 generate an sg14::elastic_fixed_point object using a literal
 

Classes

class  const_integer
 a compile-time-only integer type like a std::integral_constant with arithmetic support More...
 
class  elastic_integer
 literal integer type that encodes its width in bits within its type More...
 
class  fixed_point
 literal real number approximation that uses fixed-point arithmetic More...
 

Typedefs

template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = signed>
using elastic_fixed_point = fixed_point< elastic_integer< IntegerDigits+FractionalDigits, Narrowest >, -FractionalDigits >
 literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow More...
 
template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = signed>
using make_fixed = fixed_point< set_digits_t< Narrowest, IntegerDigits+FractionalDigits >, -FractionalDigits >
 specializes fixed_point with the given number of integer and fractional digits More...
 
template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = unsigned>
using make_ufixed = make_fixed< IntegerDigits, FractionalDigits, typename make_unsigned< Narrowest >::type >
 specializes fixed_point with the given number of integer and fractional digits; produces an unsigned type More...
 

Functions

template<class Lhs , class Rhs >
constexpr auto add (const Lhs &lhs, const Rhs &rhs) -> decltype(_impl::fp::operate< _impl::fp::named_function_tag >(lhs, rhs, _impl::add_tag))
 calculates the sum of two fixed_point values More...
 
template<class Lhs , class Rhs >
constexpr auto divide (const Lhs &lhs, const Rhs &rhs) -> decltype(_impl::fp::operate< _impl::fp::division_named_function_tag >(lhs, rhs, _impl::divide_tag))
 calculates the quotient of two fixed_point values More...
 
template<class Rep , int Exponent>
constexpr fixed_point< Rep, Exponent > exp2 (fixed_point< Rep, Exponent > x)
 Calculates exp2(x), i.e. 2^xAccurate to 1LSB for up to 32 bit underlying representation. More...
 
template<typename Narrowest = int, typename Integral = int, Integral Value = 0>
constexpr elastic_fixed_point< _impl::max(_impl::used_bits_symmetric(Value), 1), -trailing_bits(Value), Narrowest > make_elastic_fixed_point (const_integer< Integral, Value >=const_integer< Integral, Value >{})
 generate an sg14::elastic_fixed_point object of given value More...
 
template<class Narrowest = int, class Integral = int>
constexpr elastic_fixed_point< std::numeric_limits< Integral >::digits, 0, Narrowest > make_elastic_fixed_point (Integral value)
 generate an sg14::elastic_fixed_point object of given value More...
 
template<class Lhs , class Rhs >
constexpr auto multiply (const Lhs &lhs, const Rhs &rhs) -> decltype(_impl::fp::operate< _impl::fp::named_function_tag >(lhs, rhs, _impl::multiply_tag))
 calculates the product of two fixed_point factors More...
 
template<class RhsRep , int RhsExponent>
constexpr auto negate (const fixed_point< RhsRep, RhsExponent > &rhs) -> fixed_point< decltype(-rhs.data()), RhsExponent >
 calculates the negative of a fixed_point value More...
 
template<class Rep , int Exponent>
constexpr fixed_point< Rep, Exponent > sqrt (const fixed_point< Rep, Exponent > &x)
 calculates the square root of a fixed_point value More...
 
template<class Lhs , class Rhs >
constexpr auto subtract (const Lhs &lhs, const Rhs &rhs) -> decltype(_impl::fp::operate< _impl::fp::named_function_tag >(lhs, rhs, _impl::subtract_tag))
 calculates the difference of two fixed_point values More...
 

Detailed Description

study group 14 of the C++ working group

Typedef Documentation

◆ elastic_fixed_point

template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = signed>
using sg14::elastic_fixed_point = typedef fixed_point<elastic_integer<IntegerDigits+FractionalDigits, Narrowest>, -FractionalDigits>

literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow

Template Parameters
IntegerDigitsthe number of integer digits that can be stored
FractionalDigitsthe number of fractional digits that can be stored
Narrowestthe most narrow integer type to use to represent values
See also
elastic_integer

◆ make_fixed

template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = signed>
using sg14::make_fixed = typedef fixed_point< set_digits_t<Narrowest, IntegerDigits+FractionalDigits>, -FractionalDigits>

specializes fixed_point with the given number of integer and fractional digits

Template Parameters
IntegerDigitsspecifies minimum value of fixed_point::integer_digits
FractionalDigitsspecifies the exact value of fixed_point::fractional_digits
Narrowesthints at the type of fixed_point::rep
Remarks
The signage of Narrowest specifies signage of the resultant fixed-point type.
Typical choices for Narrowest, signed and unsigned, result in a type that uses built-in integers for fixed_point::rep.
Resultant type is signed by default.
Example:

To generate a fixed-point type with a sign bit, 8 fractional bits and at least 7 integer bits:

static_assert(std::is_same<make_fixed<7, 8, signed>, fixed_point<int16_t, -8>>::value, "failed to use make_fixed");
See also
make_ufixed

◆ make_ufixed

template<int IntegerDigits, int FractionalDigits = 0, class Narrowest = unsigned>
using sg14::make_ufixed = typedef make_fixed< IntegerDigits, FractionalDigits, typename make_unsigned<Narrowest>::type>

specializes fixed_point with the given number of integer and fractional digits; produces an unsigned type

See also
make_fixed

Function Documentation

◆ add()

template<class Lhs , class Rhs >
constexpr auto sg14::add ( const Lhs &  lhs,
const Rhs &  rhs 
) -> decltype(_impl::fp::operate<_impl::fp::named_function_tag>(lhs, rhs, _impl::add_tag))

calculates the sum of two fixed_point values

Parameters
lhs,rhsaugend and addend
Returns
sum: lhs + rhs
Note
This function add the values without performing any additional scaling or conversion.
See also
negate, subtract, multiply, divide

◆ divide()

template<class Lhs , class Rhs >
constexpr auto sg14::divide ( const Lhs &  lhs,
const Rhs &  rhs 
) -> decltype(_impl::fp::operate<_impl::fp::division_named_function_tag>(lhs, rhs, _impl::divide_tag))

calculates the quotient of two fixed_point values

Parameters
lhs,rhsdividend and divisor
Returns
quotient: lhs / rhs
Note
This function divides the values without performing any additional scaling or conversion.
See also
negate, add, subtract, multiply

◆ exp2()

template<class Rep , int Exponent>
constexpr fixed_point<Rep, Exponent> sg14::exp2 ( fixed_point< Rep, Exponent >  x)

Calculates exp2(x), i.e. 2^xAccurate to 1LSB for up to 32 bit underlying representation.

Template Parameters
xthe input value as a fixed_point
Returns
the result of the exponential, in the same representation as x

◆ make_elastic_fixed_point() [1/2]

template<typename Narrowest = int, typename Integral = int, Integral Value = 0>
constexpr elastic_fixed_point<_impl::max(_impl::used_bits_symmetric(Value), 1), -trailing_bits(Value), Narrowest> sg14::make_elastic_fixed_point ( const_integer< Integral, Value >  = const_integer<Integral, Value>{})

generate an sg14::elastic_fixed_point object of given value

Template Parameters
Narrowestthe narrowest type to use as storage in the resultant sg14::elastic_fixed_point object
Integralthe type of Value
Valuethe integer number to be represented
Returns
the given value to be represented using an sg14::elastic_fixed_point type
Note
The return type is guaranteed to be no larger than is necessary to represent the value.
Example

To define an int-sized object using make_elastic_fixed_point and const_integer:

// std::uint8_t specifies the type of const_integer - not elastic
constexpr auto n = make_elastic_fixed_point(const_integer<std::uint8_t, 0xAA>{});
static_assert(n==0xAA, "n now has the value, 1024");
static_assert(std::is_same<decltype(n), const elastic_fixed_point<8, -1, int>>::value, "by default make_elastic_fixed_point uses the most efficient type it can");

Referenced by make_elastic_fixed_point().

◆ make_elastic_fixed_point() [2/2]

template<class Narrowest = int, class Integral = int>
constexpr elastic_fixed_point<std::numeric_limits<Integral>::digits, 0, Narrowest> sg14::make_elastic_fixed_point ( Integral  value)

generate an sg14::elastic_fixed_point object of given value

Template Parameters
Narrowestthe most narrow storage type of the resultant sg14::elastic_fixed_point object
Integralthe type of value
Note
The return type is guaranteed to be no larger than is necessary to represent the value.
Example

To define a byte-sized object using make_elastic_fixed_point and _c:

constexpr auto n = make_elastic_fixed_point<char>(const_integer<short, 1536>{});
static_assert(n==1536, "n now has the value, 1536");
static_assert(std::is_same<decltype(n), const elastic_fixed_point<11, -9, char>>::value, "by default make_elastic_fixed_point uses the most efficient type it can");

References make_elastic_fixed_point().

◆ multiply()

template<class Lhs , class Rhs >
constexpr auto sg14::multiply ( const Lhs &  lhs,
const Rhs &  rhs 
) -> decltype(_impl::fp::operate<_impl::fp::named_function_tag>(lhs, rhs, _impl::multiply_tag))

calculates the product of two fixed_point factors

Parameters
lhs,rhsthe factors
Returns
product: lhs * rhs
Note
This function multiplies the values without performing any additional scaling or conversion.
See also
negate, add, subtract, divide

◆ negate()

template<class RhsRep , int RhsExponent>
constexpr auto sg14::negate ( const fixed_point< RhsRep, RhsExponent > &  rhs) -> fixed_point<decltype(-rhs.data()), RhsExponent>

calculates the negative of a fixed_point value

Parameters
rhsinput value
Returns
negative: - rhs
Note
This function negates the value without performing any additional scaling or conversion.
See also
add, subtract, multiply, divide

◆ sqrt()

template<class Rep , int Exponent>
constexpr fixed_point<Rep, Exponent> sg14::sqrt ( const fixed_point< Rep, Exponent > &  x)

calculates the square root of a fixed_point value

Parameters
xinput parameter
Returns
square root of x
Note
This function is a placeholder implementation with poor run-time performance characteristics.
It uses divides the values without performing any additional scaling or conversion.
See also
negate, add, subtract, multiply

References sg14::fixed_point< Rep, Exponent >::from_data().

◆ subtract()

template<class Lhs , class Rhs >
constexpr auto sg14::subtract ( const Lhs &  lhs,
const Rhs &  rhs 
) -> decltype(_impl::fp::operate<_impl::fp::named_function_tag>(lhs, rhs, _impl::subtract_tag))

calculates the difference of two fixed_point values

Parameters
lhs,rhsminuend and subtrahend
Returns
difference: lhs - rhs
Note
This function subtracts the values without performing any additional scaling or conversion.
See also
negate, add, multiply, divide