arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
soft_float_t Class Reference

Software "pseudo-float": a real number represented as a signed 16-bit mantissa plus a 16-bit exponent (value = mantissa * 2^exponent), computed with integer multiply/shift/compare only – no hardware floating-point instructions. This generalizes speexdsp's classic fixed-point trick of tracking a handful of wide-dynamic-range control variables as a normalized mantissa+exponent pair into a full, operator-overloaded numeric type usable as a drop-in replacement for float (see MDFFixedPoint in MDFEchoCancellation.h, which uses this class as the element type for the MDF echo canceller's sample/spectrum arrays and internal state, so the whole algorithm runs without native float arithmetic). More...

#include <soft_float_t.h>

Public Member Functions

constexpr soft_float_t ()=default
 
 soft_float_t (double d)
 
 soft_float_t (float f)
 
 soft_float_t (int i)
 
constexpr soft_float_t (int16_t mantissa, int16_t exponent)
 
int16_t exponent () const
 
int16_t mantissa () const
 
 operator float () const
 
 operator int () const
 truncates toward zero, like (int)someFloat
 
bool operator!= (double o) const
 
bool operator!= (float o) const
 
bool operator!= (int o) const
 
bool operator!= (soft_float_t o) const
 
soft_float_t operator* (double o) const
 
soft_float_t operator* (float o) const
 
soft_float_t operator* (int o) const
 
soft_float_t operator* (soft_float_t o) const
 
soft_float_toperator*= (soft_float_t o)
 
soft_float_t operator+ (double o) const
 
soft_float_t operator+ (float o) const
 
soft_float_t operator+ (int o) const
 
soft_float_t operator+ (soft_float_t o) const
 
soft_float_toperator+= (soft_float_t o)
 
soft_float_t operator- () const
 
soft_float_t operator- (double o) const
 
soft_float_t operator- (float o) const
 
soft_float_t operator- (int o) const
 
soft_float_t operator- (soft_float_t o) const
 
soft_float_toperator-= (soft_float_t o)
 
soft_float_t operator/ (double o) const
 
soft_float_t operator/ (float o) const
 
soft_float_t operator/ (int o) const
 
soft_float_t operator/ (soft_float_t o) const
 
soft_float_toperator/= (soft_float_t o)
 
bool operator< (double o) const
 
bool operator< (float o) const
 
bool operator< (int o) const
 
bool operator< (soft_float_t o) const
 
bool operator<= (double o) const
 
bool operator<= (float o) const
 
bool operator<= (int o) const
 
bool operator<= (soft_float_t o) const
 
bool operator== (double o) const
 
bool operator== (float o) const
 
bool operator== (int o) const
 
bool operator== (soft_float_t o) const
 
bool operator> (double o) const
 
bool operator> (float o) const
 
bool operator> (int o) const
 
bool operator> (soft_float_t o) const
 
bool operator>= (double o) const
 
bool operator>= (float o) const
 
bool operator>= (int o) const
 
bool operator>= (soft_float_t o) const
 
int16_t scale (int16_t sample) const
 scales a 16-bit PCM sample by this factor
 
int24_t scale (int24_t sample) const
 scales a 24-bit PCM sample by this factor
 
int32_t scale (int32_t sample) const
 scales a 32-bit PCM sample by this factor
 
int16_t toInt16 () const
 converts to a 16-bit PCM sample (-32768..32767)
 
int24_t toInt24 () const
 converts to a 24-bit PCM sample (-8388608..8388607)
 
int32_t toInt32 () const
 converts to a 32-bit PCM sample (-2147483648..2147483647)
 

Static Public Member Functions

static soft_float_t fromInt16 (int16_t sample)
 builds a soft_float_t from a 16-bit PCM sample (-32768..32767)
 
static soft_float_t fromInt24 (int24_t sample)
 builds a soft_float_t from a 24-bit PCM sample (-8388608..8388607)
 
static soft_float_t fromInt32 (int32_t sample)
 builds a soft_float_t from a 32-bit PCM sample (-2147483648..2147483647)
 

Detailed Description

Software "pseudo-float": a real number represented as a signed 16-bit mantissa plus a 16-bit exponent (value = mantissa * 2^exponent), computed with integer multiply/shift/compare only – no hardware floating-point instructions. This generalizes speexdsp's classic fixed-point trick of tracking a handful of wide-dynamic-range control variables as a normalized mantissa+exponent pair into a full, operator-overloaded numeric type usable as a drop-in replacement for float (see MDFFixedPoint in MDFEchoCancellation.h, which uses this class as the element type for the MDF echo canceller's sample/spectrum arrays and internal state, so the whole algorithm runs without native float arithmetic).

Unlike a plain fixed Q-format (a fixed number of fractional bits, which only covers a fixed range/precision trade-off and would over/underflow badly across this algorithm's actual value range – audio samples, FFT magnitudes in the thousands, and adaptation-rate fractions near zero, all flowing through the same arrays), this type renormalizes on every operation, so it tolerates the same wide dynamic range as float, at roughly the precision of a 16-bit mantissa (a relative error on the order of 2^-15, i.e. about 4-5 significant decimal digits – comparable to a compressed/half-precision float).

Note
Comparison operators (<, >, etc.) convert both sides to float and compare natively – they're scalar, infrequent control-flow checks, not the per-element array math this class exists to keep off the FPU, so trading a little purity there for simpler, more obviously correct code is a reasonable trade. Arithmetic that mixes a soft_float_t with a plain float/double literal (e.g. 0.7f * pseudoFloatValue) also still executes that specific operation using native float arithmetic via the implicit conversions below – avoiding all native float instructions everywhere would require rewriting every numeric literal in the algorithm as a soft_float_t, which isn't done here. What this class does guarantee is that soft_float_t-to-soft_float_t arithmetic – the tight inner loops over whole arrays (FFT bins, filter weights, correlations) that dominate the algorithm's cost – runs on integer mantissa/exponent math throughout.

Constructor & Destructor Documentation

◆ soft_float_t() [1/5]

constexpr soft_float_t ( )
constexprdefault

◆ soft_float_t() [2/5]

constexpr soft_float_t ( int16_t  mantissa,
int16_t  exponent 
)
inlineconstexpr

◆ soft_float_t() [3/5]

soft_float_t ( float  f)
inline

◆ soft_float_t() [4/5]

soft_float_t ( double  d)
inline

◆ soft_float_t() [5/5]

soft_float_t ( int  i)
inline

Member Function Documentation

◆ exponent()

int16_t exponent ( ) const
inline

◆ fromInt16()

static soft_float_t fromInt16 ( int16_t  sample)
inlinestatic

builds a soft_float_t from a 16-bit PCM sample (-32768..32767)

◆ fromInt24()

static soft_float_t fromInt24 ( int24_t  sample)
inlinestatic

builds a soft_float_t from a 24-bit PCM sample (-8388608..8388607)

◆ fromInt32()

static soft_float_t fromInt32 ( int32_t  sample)
inlinestatic

builds a soft_float_t from a 32-bit PCM sample (-2147483648..2147483647)

◆ mantissa()

int16_t mantissa ( ) const
inline

◆ operator float()

operator float ( ) const
inline

◆ operator int()

operator int ( ) const
inlineexplicit

truncates toward zero, like (int)someFloat

◆ operator!=() [1/4]

bool operator!= ( double  o) const
inline

◆ operator!=() [2/4]

bool operator!= ( float  o) const
inline

◆ operator!=() [3/4]

bool operator!= ( int  o) const
inline

◆ operator!=() [4/4]

bool operator!= ( soft_float_t  o) const
inline

◆ operator*() [1/4]

soft_float_t operator* ( double  o) const
inline

◆ operator*() [2/4]

soft_float_t operator* ( float  o) const
inline

◆ operator*() [3/4]

soft_float_t operator* ( int  o) const
inline

◆ operator*() [4/4]

soft_float_t operator* ( soft_float_t  o) const
inline

◆ operator*=()

soft_float_t & operator*= ( soft_float_t  o)
inline

◆ operator+() [1/4]

soft_float_t operator+ ( double  o) const
inline

◆ operator+() [2/4]

soft_float_t operator+ ( float  o) const
inline

◆ operator+() [3/4]

soft_float_t operator+ ( int  o) const
inline

◆ operator+() [4/4]

soft_float_t operator+ ( soft_float_t  o) const
inline

◆ operator+=()

soft_float_t & operator+= ( soft_float_t  o)
inline

◆ operator-() [1/5]

soft_float_t operator- ( ) const
inline

◆ operator-() [2/5]

soft_float_t operator- ( double  o) const
inline

◆ operator-() [3/5]

soft_float_t operator- ( float  o) const
inline

◆ operator-() [4/5]

soft_float_t operator- ( int  o) const
inline

◆ operator-() [5/5]

soft_float_t operator- ( soft_float_t  o) const
inline

◆ operator-=()

soft_float_t & operator-= ( soft_float_t  o)
inline

◆ operator/() [1/4]

soft_float_t operator/ ( double  o) const
inline

◆ operator/() [2/4]

soft_float_t operator/ ( float  o) const
inline

◆ operator/() [3/4]

soft_float_t operator/ ( int  o) const
inline

◆ operator/() [4/4]

soft_float_t operator/ ( soft_float_t  o) const
inline

◆ operator/=()

soft_float_t & operator/= ( soft_float_t  o)
inline

◆ operator<() [1/4]

bool operator< ( double  o) const
inline

◆ operator<() [2/4]

bool operator< ( float  o) const
inline

◆ operator<() [3/4]

bool operator< ( int  o) const
inline

◆ operator<() [4/4]

bool operator< ( soft_float_t  o) const
inline

◆ operator<=() [1/4]

bool operator<= ( double  o) const
inline

◆ operator<=() [2/4]

bool operator<= ( float  o) const
inline

◆ operator<=() [3/4]

bool operator<= ( int  o) const
inline

◆ operator<=() [4/4]

bool operator<= ( soft_float_t  o) const
inline

◆ operator==() [1/4]

bool operator== ( double  o) const
inline

◆ operator==() [2/4]

bool operator== ( float  o) const
inline

◆ operator==() [3/4]

bool operator== ( int  o) const
inline

◆ operator==() [4/4]

bool operator== ( soft_float_t  o) const
inline

◆ operator>() [1/4]

bool operator> ( double  o) const
inline

◆ operator>() [2/4]

bool operator> ( float  o) const
inline

◆ operator>() [3/4]

bool operator> ( int  o) const
inline

◆ operator>() [4/4]

bool operator> ( soft_float_t  o) const
inline

◆ operator>=() [1/4]

bool operator>= ( double  o) const
inline

◆ operator>=() [2/4]

bool operator>= ( float  o) const
inline

◆ operator>=() [3/4]

bool operator>= ( int  o) const
inline

◆ operator>=() [4/4]

bool operator>= ( soft_float_t  o) const
inline

◆ scale() [1/3]

int16_t scale ( int16_t  sample) const
inline

scales a 16-bit PCM sample by this factor

◆ scale() [2/3]

int24_t scale ( int24_t  sample) const
inline

scales a 24-bit PCM sample by this factor

◆ scale() [3/3]

int32_t scale ( int32_t  sample) const
inline

scales a 32-bit PCM sample by this factor

◆ toInt16()

int16_t toInt16 ( ) const
inline

converts to a 16-bit PCM sample (-32768..32767)

◆ toInt24()

int24_t toInt24 ( ) const
inline

converts to a 24-bit PCM sample (-8388608..8388607)

◆ toInt32()

int32_t toInt32 ( ) const
inline

converts to a 32-bit PCM sample (-2147483648..2147483647)


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