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

#include <FixMath.h>

Public Member Functions

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

Static Public Member Functions

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

Detailed Description

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

Instanciate an signed fixed point math number.

Parameters
NIThe number of bits encoding the integer part. The integral part can range into [-2^NI, 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.
Note
The total number of the underlying int will be NI+NF+1 in order to accomodate the sign. It follows that, if you want something that reproduces the behavior of a int8_t, it should be declared as SFix<7,0>.

Constructor & Destructor Documentation

◆ SFix() [1/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
SFix< NI, NF, RANGE >::SFix ( )
inline

Constructor

◆ SFix() [2/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
SFix< NI, NF, RANGE >::SFix ( float  fl)
inline

Constructor from a floating point value.

Parameters
flFloating point value
Returns
An signed fixed point number

◆ SFix() [3/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
SFix< NI, NF, RANGE >::SFix ( double  fl)
inline

Constructor from a floating point value.

Parameters
flFloating point value
Returns
An signed fixed point number

◆ SFix() [4/6]

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<typename T >
SFix< NI, NF, RANGE >::SFix ( 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 signed fixed point number

◆ SFix() [5/6]

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

Constructor from another SFix.

Parameters
ufA signed fixed type number which value can be represented in this type.
Returns
A signed fixed type number

◆ SFix() [6/6]

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

Constructor from an UFix.

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

Member Function Documentation

◆ asFloat()

template<int8_t NI, int8_t NF, uint64_t RANGE>
float SFix< 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>
internal_type SFix< NI, NF, RANGE >::asRaw ( ) const
inline

Returns the internal integer value

Returns
the internal value

◆ asUFix()

template<int8_t NI, int8_t NF, uint64_t RANGE>
UFix<NI,NF,RANGE> SFix< NI, NF, RANGE >::asUFix ( ) const
inline

Returns the number as a UFix of same (positive) range and precision. The initial value has to be positive to return something correct. This is more optimized than a cast.

Returns
a UFix

◆ fromRaw()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<typename T >
static SFix<NI,NF> SFix< NI, NF, RANGE >::fromRaw ( raw)
inlinestatic

Set the internal value of the fixed point math number.

Parameters
rawThe new internal value.
Returns
A SFix.

◆ getNF()

template<int8_t NI, int8_t NF, uint64_t RANGE>
int8_t SFix< 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>
int8_t SFix< 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>
template<int8_t _NF>
SFix<NF,_NF> SFix< NI, NF, RANGE >::inv ( ) const
inline

Compute the inverse of a SFix<NI,NF>, as a SFix<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>
SFix<NF,FixMathPrivate::FM_min(NI*2+NF,62-NF)> SFix< NI, NF, RANGE >::invAccurate ( ) const
inline

Compute the inverse of a SFix<NI,NF>, as a SFix<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>
SFix<NF,NI> SFix< NI, NF, RANGE >::invFast ( ) const
inline

Compute the inverse of a SFix<NI,NF>, as a SFix<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>
template<int8_t _NI, int8_t _NF>
bool SFix< NI, NF, RANGE >::operator!= ( const SFix< _NI, _NF > &  op) const
inline

Comparison with another SFix.

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

◆ operator*()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF, uint64_t _RANGE>
SFix<FixMathPrivate::neededSNIExtra(NI+_NI, NF+_NF, RANGE*_RANGE),NF+_NF, RANGE*_RANGE> SFix< NI, NF, RANGE >::operator* ( const SFix< _NI, _NF, _RANGE > &  op) const
inline

Multiplication with another SFix. Safe.

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

◆ operator+()

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

Addition with another SFix. Safe.

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

◆ operator-() [1/3]

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

Opposite of the number.

Returns
The opposite numberas 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>
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)> SFix< NI, NF, RANGE >::operator- ( const SFix< _NI, _NF, _RANGE > &  op) const
inline

Subtraction with another SFix. Safe.

Parameters
opThe SFix to be subtracted.
Returns
The result of the subtraction 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>
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)> SFix< NI, NF, RANGE >::operator- ( const UFix< _NI, _NF, _RANGE > &  op) const
inline

Subtraction with a UFix. Safe.

Parameters
opThe SFix 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>
bool SFix< NI, NF, RANGE >::operator< ( const SFix< _NI, _NF > &  op) const
inline

Comparison with another SFix.

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>
bool SFix< NI, NF, RANGE >::operator== ( const SFix< _NI, _NF > &  op) const
inline

Comparison with another SFix.

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

◆ operator>()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t _NI, int8_t _NF>
bool SFix< NI, NF, RANGE >::operator> ( const SFix< _NI, _NF > &  op) const
inline

Comparison with another SFix.

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

◆ operator>>()

template<int8_t NI, int8_t NF, uint64_t RANGE>
SFix<NI,NF> SFix< 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 SFix.

◆ sL()

template<int8_t NI, int8_t NF, uint64_t RANGE>
template<int8_t op>
SFix<NI+op,FixMathPrivate::FM_max(NF-op,0),FixMathPrivate::rangeShift(NF,op,RANGE)> SFix< 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>
template<int8_t op>
SFix<FixMathPrivate::FM_max(NI-op,0), NF+op, FixMathPrivate::rangeShift(NI,op,RANGE)> SFix< 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: