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

#include <FixMath.h>

Public Member Functions

 UFix ()
 
 UFix (float fl)
 
 UFix (double fl)
 
template<typename T >
 UFix (T value, bool as_raw=false)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
 UFix (const UFix< _NI, _NF, _RANGE > &uf)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
 UFix (const SFix< _NI, _NF, _RANGE > &uf)
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix< FixMathPrivate::neededNIExtra(FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)> operator+ (const UFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
SFix< FixMathPrivate::neededSNIExtra(FixMathPrivate::FM_max(NI, _NI), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)), FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF, _NF, RANGE, _RANGE)> operator+ (const SFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
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
 
SFix< NI, NF, RANGE > operator- () const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix< FixMathPrivate::neededNIExtra(NI+_NI, NF+_NF, RANGE *_RANGE), NF+_NF, RANGE *_RANGE > operator* (const UFix< _NI, _NF, _RANGE > &op) const
 
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
SFix< FixMathPrivate::neededSNIExtra(NI+_NI, NF+_NF, RANGE *_RANGE), NF+_NF, RANGE *_RANGE > operator* (const SFix< _NI, _NF, _RANGE > &op) const
 
UFix< NF, NI > invFast () const
 
template<int8_t _NF>
UFix< NF, _NF > inv () const
 
UFix< NF, FixMathPrivate::FM_min(NI *2+NF, 63-NF)> invAccurate () const
 
UFix< NI, NF > operator>> (const int8_t op) const
 
template<int8_t op>
UFix< FixMathPrivate::FM_max(NI-op, 0), NF+op, FixMathPrivate::rangeShift(NI, op, RANGE)> sR ()
 
template<int8_t op>
UFix< NI+op, FixMathPrivate::FM_max(NF-op, 0), FixMathPrivate::rangeShift(NF, op, RANGE)> sL ()
 
template<int8_t _NI, int8_t _NF>
bool operator> (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
bool operator< (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
bool operator== (const UFix< _NI, _NF > &op) const
 
template<int8_t _NI, int8_t _NF>
bool operator!= (const UFix< _NI, _NF > &op) const
 
SFix< NI, NF, RANGE > asSFix () const
 
float asFloat () const
 
internal_type asRaw () const
 
int8_t getNI () const
 
int8_t getNF () const
 

Static Public Member Functions

template<typename T >
static UFix< NI, NF > fromRaw (T raw)
 

Detailed Description

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
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 = FixMathPrivate::uFullRange(NI+NF)>
UFix< NI, NF, RANGE >::UFix ( )
inline

Constructor

◆ UFix() [2/6]

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
UFix< NI, NF, RANGE >::UFix ( float  fl)
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
UFix< NI, NF, RANGE >::UFix ( double  fl)
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<typename T >
UFix< NI, NF, RANGE >::UFix ( value,
bool  as_raw = false 
)
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix< NI, NF, RANGE >::UFix ( const UFix< _NI, _NF, _RANGE > &  uf)
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix< NI, NF, RANGE >::UFix ( const SFix< _NI, _NF, _RANGE > &  uf)
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
float UFix< NI, NF, RANGE >::asFloat ( ) const
inline

Returns the value as floating point number.

Returns
The floating point value.

◆ asRaw()

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
internal_type UFix< NI, NF, RANGE >::asRaw ( ) const
inline

Returns the internal integer value

Returns
the internal value

◆ asSFix()

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
SFix<NI,NF,RANGE> UFix< NI, NF, RANGE >::asSFix ( ) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<typename T >
static UFix<NI,NF> UFix< NI, NF, RANGE >::fromRaw ( raw)
inlinestatic

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 = FixMathPrivate::uFullRange(NI+NF)>
int8_t UFix< NI, NF, RANGE >::getNF ( ) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
int8_t UFix< NI, NF, RANGE >::getNI ( ) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NF>
UFix<NF,_NF> UFix< NI, NF, RANGE >::inv ( ) const
inline

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 invAccurate() (_NF=2*NI+NF)

Returns
The inverse of the number.

◆ invAccurate()

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
UFix<NF,FixMathPrivate::FM_min(NI*2+NF,63-NF)> UFix< NI, NF, RANGE >::invAccurate ( ) const
inline

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.

◆ invFast()

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
UFix<NF,NI> UFix< NI, NF, RANGE >::invFast ( ) const
inline

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.

◆ operator!=()

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF>
bool UFix< NI, NF, RANGE >::operator!= ( const UFix< _NI, _NF > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
SFix<FixMathPrivate::neededSNIExtra(NI+_NI, NF+_NF, RANGE*_RANGE),NF+_NF, RANGE*_RANGE> UFix< NI, NF, RANGE >::operator* ( const SFix< _NI, _NF, _RANGE > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix<FixMathPrivate::neededNIExtra(NI+_NI, NF+_NF, RANGE*_RANGE),NF+_NF, RANGE*_RANGE> UFix< NI, NF, RANGE >::operator* ( const UFix< _NI, _NF, _RANGE > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
SFix<FixMathPrivate::neededSNIExtra(FixMathPrivate::FM_max(NI,_NI),FixMathPrivate::FM_max(NF,_NF),FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)),FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)> UFix< NI, NF, RANGE >::operator+ ( const SFix< _NI, _NF, _RANGE > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
UFix<FixMathPrivate::neededNIExtra(FixMathPrivate::FM_max(NI,_NI),FixMathPrivate::FM_max(NF,_NF),FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)),FixMathPrivate::FM_max(NF, _NF), FixMathPrivate::rangeAdd(NF,_NF,RANGE,_RANGE)> UFix< NI, NF, RANGE >::operator+ ( const UFix< _NI, _NF, _RANGE > &  op) const
inline

Addition with another UFix. Safe.

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

◆ operator-() [1/2]

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
SFix<NI,NF,RANGE> UFix< NI, NF, RANGE >::operator- ( ) const
inline

Opposite of the number.

Returns
The opposite number as a SFix.

◆ operator-() [2/2]

template<int8_t NI, int8_t NF, uint64_t RANGE = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
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
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF>
bool UFix< NI, NF, RANGE >::operator< ( const UFix< _NI, _NF > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF>
bool UFix< NI, NF, RANGE >::operator== ( const UFix< _NI, _NF > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t _NI, int8_t _NF>
bool UFix< NI, NF, RANGE >::operator> ( const UFix< _NI, _NF > &  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
UFix<NI,NF> UFix< NI, NF, RANGE >::operator>> ( const int8_t  op) const
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t op>
UFix<NI+op,FixMathPrivate::FM_max(NF-op,0),FixMathPrivate::rangeShift(NF,op,RANGE)> UFix< NI, NF, RANGE >::sL ( )
inline

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 = FixMathPrivate::uFullRange(NI+NF)>
template<int8_t op>
UFix<FixMathPrivate::FM_max(NI-op,0),NF+op, FixMathPrivate::rangeShift(NI,op,RANGE)> UFix< NI, NF, RANGE >::sR ( )
inline

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: