FixMath
Public Member Functions | Static Public Member Functions | Friends | List of all members
UFix< NI, NF, RANGE > Class Template Reference

#include <FixMath.h>

Public Member Functions

constexpr UFix ()
 
constexpr UFix (float fl)
 
constexpr UFix (double fl)
 
template<typename T >
constexpr UFix (T value, bool as_raw=false)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix (const UFix< _NI, _NF, _RANGE > &uf)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix (const SFix< _NI, _NF, _RANGE > &uf)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix< FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)>::UFixNIadj_t operator+ (const UFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix< FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)>::SFixNIadj_t operator+ (const SFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix< FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::FM_max(FixMathPrivate::shiftR(RANGE, FixMathPrivate::FM_max(NF, _NF) -NF), FixMathPrivate::shiftR(_RANGE, FixMathPrivate::FM_max(NF, _NF) -_NF))> operator- (const UFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix< FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)>::SFixNIadj_t operator- (const SFix< _NI, _NF, _RANGE > &op2) const
 
constexpr SFix< NI, NF, RANGE > operator- () const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix< NI+_NI, NF+_NF, RANGE *_RANGE >::UFixNIadj_t operator* (const UFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix< NI+_NI, NF+_NF, RANGE *_RANGE >::SFixNIadj_t operator* (const SFix< _NI, _NF, _RANGE > &op) const
 
constexpr UFix< NF, NI > invFast () const
 
template<int8_t _NF>
constexpr UFix< NF, _NF > inv () const
 
constexpr UFix< NF, FixMathPrivate::FM_min(NI *2+NF, 64-NF)> invFull () const
 
template<int8_t _NF = FixMathPrivate::FM_min(NI*2+NF-1,64-NF)>
constexpr UFix< NF, _NF > invAccurate () const
 
constexpr UFix< NI, NF > operator>> (const int8_t op) const
 
template<int8_t op>
constexpr UFix< FixMathPrivate::FM_max(NI-op, 0), NF+op, FixMathPrivate::rangeShift(NI, op, RANGE)> sR () const
 
template<int8_t op>
constexpr UFix< NI+op, FixMathPrivate::FM_max(NF-op, 0), FixMathPrivate::rangeShift(NF, op, RANGE)> sL () const
 
template<int8_t _NI, int8_t _NF>
constexpr bool operator> (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
constexpr bool operator< (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
constexpr bool operator== (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
constexpr bool operator!= (const UFix< _NI, _NF > &op) const
 
constexpr SFix< NI, NF, RANGE > asSFix () const
 
constexpr float asFloat () const
 
constexpr IntegerType< FixMathPrivate::uBitsToBytes(NI)>::unsigned_type asInt () const
 
constexpr internal_type asRaw () const
 

Static Public Member Functions

template<typename T >
static constexpr UFix< NI, NF > fromRaw (T raw)
 
static constexpr int8_t getNI ()
 
static constexpr int8_t getNF ()
 
template<int8_t BITS>
static constexpr void assertSize ()
 

Friends

template<int8_t , int8_t , uint64_t >
class SFix
 

Detailed Description

template<int8_t NI, int8_t NF, uint64_t RANGE>
class UFix< NI, NF, RANGE >

Instanciate an unsigned fixed point math number.

Parameters
NIThe number of bits encoding the integer part. The integral part can range into [0, 2^NI -1]
NFThe number of bits encoding the fractional part
RANGEA purely internal parameter that keeps track of the range used by the type. Safer to not define it.

Constructor & Destructor Documentation

◆ UFix() [1/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix< NI, NF, RANGE >::UFix ( )
inlineconstexpr

Constructor

◆ UFix() [2/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix< NI, NF, RANGE >::UFix ( float  fl)
inlineconstexpr

Constructor from a positive floating point value.

Parameters
flFloating point value
Returns
An unsigned fixed point number

◆ UFix() [3/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix< NI, NF, RANGE >::UFix ( double  fl)
inlineconstexpr

Constructor from a floating point value.

Parameters
flFloating point value
Returns
An unsigned fixed point number

◆ UFix() [4/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<typename T >
constexpr UFix< NI, NF, RANGE >::UFix ( value,
bool  as_raw = false 
)
inlineconstexpr

Constructor from an integer value which can be interpreted as both a resulting fixed point math number with a fractional part equals to 0, or as a number with decimal, ie as the underlying type behind the fixed point math number.

Parameters
valueInteger value
as_raw=falsewith false value will be interpreted as an integer, with true it will be interpreted as a number with decimals.
Returns
An unsigned fixed point number

◆ UFix() [5/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix< NI, NF, RANGE >::UFix ( const UFix< _NI, _NF, _RANGE > &  uf)
inlineconstexpr

Constructor from another UFix.

Parameters
ufAn unsigned fixed type number which value can be represented in this type.
Returns
A unsigned fixed type number

◆ UFix() [6/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix< NI, NF, RANGE >::UFix ( const SFix< _NI, _NF, _RANGE > &  uf)
inlineconstexpr

Constructor from a SFix.

Parameters
ufAn signed fixed type number which value can be represented in this type: sign is thus discarded.
Returns
A unsigned fixed type number

Member Function Documentation

◆ asFloat()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr float UFix< NI, NF, RANGE >::asFloat ( ) const
inlineconstexpr

Returns the value as floating point number.

Returns
The floating point value.

◆ asInt()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr IntegerType<FixMathPrivate::uBitsToBytes(NI)>::unsigned_type UFix< NI, NF, RANGE >::asInt ( ) const
inlineconstexpr

Return the integer part of the number, as a standard C type integer (uint8_t, uint16_t etc) depending on NI.

Returns
The integer part, as a C integer type.

◆ asRaw()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr internal_type UFix< NI, NF, RANGE >::asRaw ( ) const
inlineconstexpr

Returns the internal integer value

Returns
the internal value

◆ assertSize()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t BITS>
static constexpr void UFix< NI, NF, RANGE >::assertSize ( )
inlinestaticconstexpr

Check wether this number exceeds the given total size given in bits, and produce a compile time error, otherwise.

Note
In the "sibling" function SFix::assertSize(), the sign bit is counted as an extra bit, i.e. SFix<8,0>::assertSize<8>() will fail!

Useful as a mechanism to guard against unexpected bit size inflation. This is a compile-type check and will not incur any flash use or performance penalty.

Example:

auto a = toUInt((uint16_t) 42); // uint16_t is unecessarily large for this value, results in a UFix<16,0>
auto cubed = a*a*a; // therefore, cubed is a UFix<48,0>
cubed.assertSize<32>(); // Oops, already at 48 bits! Should have used a smaller data type, above!
constexpr UFix< sizeof(T) *8, 0 > toUInt(T val)
Definition: FixMath.h:700

◆ asSFix()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr SFix<NI,NF,RANGE> UFix< NI, NF, RANGE >::asSFix ( ) const
inlineconstexpr

Returns the number as a SFix of same range and precision. This is more optimized than a cast.

Returns
a SFix

◆ fromRaw()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<typename T >
static constexpr UFix<NI,NF> UFix< NI, NF, RANGE >::fromRaw ( raw)
inlinestaticconstexpr

Set the internal value of the fixed point math number.

Parameters
rawThe new internal value.
Returns
An UFixx

◆ getNF()

template<int8_t NI, int8_t NF, uint64_t RANGE>
static constexpr int8_t UFix< NI, NF, RANGE >::getNF ( )
inlinestaticconstexpr

The number of bits used to encode the fractional part.

Returns
The number of bits used to encode the fractional part.

◆ getNI()

template<int8_t NI, int8_t NF, uint64_t RANGE>
static constexpr int8_t UFix< NI, NF, RANGE >::getNI ( )
inlinestaticconstexpr

The number of bits used to encode the integral part.

Returns
The number of bits used to encode the integral part.

◆ inv()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NF>
constexpr UFix<NF,_NF> UFix< NI, NF, RANGE >::inv ( ) const
inlineconstexpr

Compute the inverse of a UFix<NI,NF>, as a UFix<NF,_NF>. _NF is the number of precision bits for the output. Can be any number but especially useful for case between invFast() (_NF=NI) and invFull() (_NF=2*NI+NF)

Returns
The inverse of the number.

◆ invAccurate()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NF = FixMathPrivate::FM_min(NI*2+NF-1,64-NF)>
constexpr UFix<NF,_NF> UFix< NI, NF, RANGE >::invAccurate ( ) const
inlineconstexpr

Compute the inverse of a UFix<NI,NF>, as a UFix<NF,NI*2+NF-1> (default) or as a UFix<NF,_NF> with _NF the template parameter of invAccurate<_NF>.

Note
This uses a number of width NF+_NF+1 internally. If you are seeking for performances, you should try to aim for this number to not require a too big a type internally. For instance UFix<0,16> a; a.invAccurate<16>() will use a 64 bits internally whereas a.invAccurate<15>() will only use a 32 bits. It can be called as a.invAccurate() (default) or a.invAccurate<16> for instance.

◆ invFast()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix<NF,NI> UFix< NI, NF, RANGE >::invFast ( ) const
inlineconstexpr

Compute the inverse of a UFix<NI,NF>, as a UFix<NF,NI> (not a typo). This inverse is able to represent accurately the inverse of the smallest and biggest of the initial number, but might lead to a lot of duplicates in between. Good if precision is not a premium but type conservation and speeds are. This is still slower than a multiplication, hence the suggested workflow is to compute the inverse when time is not critical, for instance in updateControl(), and multiply it afterward, for instance in updateAudio(), if you need a division.

Returns
The inverse of the number.

◆ invFull()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix<NF,FixMathPrivate::FM_min(NI*2+NF,64-NF)> UFix< NI, NF, RANGE >::invFull ( ) const
inlineconstexpr

Compute the inverse of a UFix<NI,NF>, as a UFix<NF,NI*2+NF>. This inverse is more accurate than invFast, and usually leads to non common values on the whole input range. This comes at the cost of a way bigger resulting type. This is still slower than a multiplication, hence the suggested workflow is to compute the inverse when time is not critical, for instance in updateControl(), and multiply it afterward, for instance in updateAudio(), if you need a division.

Returns
The inverse of the number.

◆ operator!=()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF>
constexpr bool UFix< NI, NF, RANGE >::operator!= ( const UFix< _NI, _NF > &  op) const
inlineconstexpr

Comparison with another UFix.

Parameters
opA UFix
Returns
true if this not equal to op, false otherwise

◆ operator*() [1/2]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix<NI+_NI, NF+_NF, RANGE*_RANGE>::SFixNIadj_t UFix< NI, NF, RANGE >::operator* ( const SFix< _NI, _NF, _RANGE > &  op) const
inlineconstexpr

Multiplication with a SFix. Safe.

Parameters
opThe SFix to be multiplied.
Returns
The result of the multiplication as a SFix.

◆ operator*() [2/2]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix<NI+_NI, NF+_NF, RANGE*_RANGE>::UFixNIadj_t UFix< NI, NF, RANGE >::operator* ( const UFix< _NI, _NF, _RANGE > &  op) const
inlineconstexpr

Multiplication with another UFix. Safe.

Parameters
opThe UFix to be multiplied.
Returns
The result of the multiplication as a UFix.

◆ operator+() [1/2]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix<FixMathPrivate::FM_max(NI,_NI), FixMathPrivate::FM_max(NF,_NF), FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)>::SFixNIadj_t UFix< NI, NF, RANGE >::operator+ ( const SFix< _NI, _NF, _RANGE > &  op) const
inlineconstexpr

Addition with a SFix. Safe.

Parameters
opThe UFix to be added.
Returns
The result of the addition as a SFix.

◆ operator+() [2/2]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr UFix<FixMathPrivate::FM_max(NI,_NI), FixMathPrivate::FM_max(NF,_NF), FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)>::UFixNIadj_t UFix< NI, NF, RANGE >::operator+ ( const UFix< _NI, _NF, _RANGE > &  op) const
inlineconstexpr

Addition with another UFix. Safe.

Parameters
opThe UFix to be added.
Returns
The result of the addition as a UFix.

◆ operator-() [1/3]

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr SFix<NI,NF,RANGE> UFix< NI, NF, RANGE >::operator- ( ) const
inlineconstexpr

Opposite of the number (unary minus operator).

Returns
The opposite number as a SFix.

◆ operator-() [2/3]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix<FixMathPrivate::FM_max(NI,_NI),FixMathPrivate::FM_max(NF,_NF),FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)>::SFixNIadj_t UFix< NI, NF, RANGE >::operator- ( const SFix< _NI, _NF, _RANGE > &  op2) const
inlineconstexpr

Subtraction between a UFix and a SFix. Safe.

Parameters
op1A UFix
op2A SFix
Returns
The result of the subtraction of op1 by op2. As a SFix

◆ operator-() [3/3]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
constexpr SFix<FixMathPrivate::FM_max(NI,_NI),FixMathPrivate::FM_max(NF,_NF), FixMathPrivate::FM_max(FixMathPrivate::shiftR(RANGE,FixMathPrivate::FM_max(NF,_NF)-NF), FixMathPrivate::shiftR(_RANGE,FixMathPrivate::FM_max(NF,_NF)-_NF))> UFix< NI, NF, RANGE >::operator- ( const UFix< _NI, _NF, _RANGE > &  op) const
inlineconstexpr

Subtraction with another UFix. Safe.

Parameters
opThe UFix to be subtracted.
Returns
The result of the subtraction as a SFix.

◆ operator<()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF>
constexpr bool UFix< NI, NF, RANGE >::operator< ( const UFix< _NI, _NF > &  op) const
inlineconstexpr

Comparison with another UFix.

Parameters
opA UFix
Returns
true if this is smaller than op, false otherwise

◆ operator==()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF>
constexpr bool UFix< NI, NF, RANGE >::operator== ( const UFix< _NI, _NF > &  op) const
inlineconstexpr

Comparison with another UFix.

Parameters
opA UFix
Returns
true if this equal to op, false otherwise

◆ operator>()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF>
constexpr bool UFix< NI, NF, RANGE >::operator> ( const UFix< _NI, _NF > &  op) const
inlineconstexpr

Comparison with another UFix.

Parameters
opA UFix
Returns
true if this is bigger than op, false otherwise

◆ operator>>()

template<int8_t NI, int8_t NF, uint64_t RANGE>
constexpr UFix<NI,NF> UFix< NI, NF, RANGE >::operator>> ( const int8_t  op) const
inlineconstexpr

Right shift. This won't overflow but will not leverage on the bits freed by the shift. Better to use .sR<shift>() if possible instead.

Parameters
opThe shift number
Returns
The result of the shift as a UFix.

◆ sL()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t op>
constexpr UFix<NI+op,FixMathPrivate::FM_max(NF-op,0),FixMathPrivate::rangeShift(NF,op,RANGE)> UFix< NI, NF, RANGE >::sL ( ) const
inlineconstexpr

Safe and optimal left shift. The returned type will be adjusted accordingly

Parameters
opThe shift number
Returns
The result of the shift as a UFix of bigger size.

◆ sR()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t op>
constexpr UFix<FixMathPrivate::FM_max(NI-op,0),NF+op, FixMathPrivate::rangeShift(NI,op,RANGE)> UFix< NI, NF, RANGE >::sR ( ) const
inlineconstexpr

Safe and optimal right shift. The returned type will be adjusted accordingly

Parameters
opThe shift number
Returns
The result of the shift as a UFix of smaller size.

The documentation for this class was generated from the following file: