PebbleOS
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
Fixed-point math

Signed fixed-point types and a generic recursive filter. More...

Data Structures

union  Fixed_S16_3
 Fixed-point number with 1 sign bit, 12 integer bits and 3 fraction bits. More...
 
union  Fixed_S32_16
 Fixed-point number with 1 sign bit, 15 integer bits and 16 fraction bits. More...
 
union  Fixed_S64_32
 Fixed-point number with 1 sign bit, 31 integer bits and 32 fraction bits. More...
 
struct  Fixed_S16_3.__unnamed102__
 
struct  Fixed_S32_16.__unnamed104__
 
struct  Fixed_S64_32.__unnamed106__
 

Macros

#define Fixed_S16_3(raw)   ((Fixed_S16_3){.raw_value = (raw)})
 Make a Fixed_S16_3 from its raw value.
 
#define FIXED_S16_3_PRECISION   3
 Number of fraction bits of Fixed_S16_3.
 
#define FIXED_S16_3_FACTOR   (1 << FIXED_S16_3_PRECISION)
 Scale factor of Fixed_S16_3.
 
#define FIXED_S16_3_ZERO   ((Fixed_S16_3){.integer = 0, .fraction = 0})
 Fixed_S16_3 zero.
 
#define FIXED_S16_3_ONE   ((Fixed_S16_3){.integer = 1, .fraction = 0})
 Fixed_S16_3 one.
 
#define FIXED_S16_3_HALF   ((Fixed_S16_3){.raw_value = FIXED_S16_3_ONE.raw_value / 2})
 Fixed_S16_3 one half.
 
#define Fixed_S32_16(raw)   ((Fixed_S32_16){.raw_value = (raw)})
 Make a Fixed_S32_16 from its raw value.
 
#define FIXED_S32_16_PRECISION   16
 Number of fraction bits of Fixed_S32_16.
 
#define FIXED_S32_16_ONE   ((Fixed_S32_16){.integer = 1, .fraction = 0})
 Fixed_S32_16 one.
 
#define FIXED_S32_16_ZERO   ((Fixed_S32_16){.integer = 0, .fraction = 0})
 Fixed_S32_16 zero.
 
#define FIXED_S64_32_PRECISION   32
 Number of fraction bits of Fixed_S64_32.
 
#define FIXED_S64_32_ONE   ((Fixed_S64_32){.integer = 1, .fraction = 0})
 Fixed_S64_32 one.
 
#define FIXED_S64_32_ZERO   ((Fixed_S64_32){.integer = 0, .fraction = 0})
 Fixed_S64_32 zero.
 
#define FIXED_S64_32_FROM_RAW(raw)   ((Fixed_S64_32){.raw_value = (raw)})
 Make a Fixed_S64_32 from its raw value.
 
#define FIXED_S64_32_FROM_INT(x)   ((Fixed_S64_32){.integer = x, .fraction = 0})
 Make a Fixed_S64_32 from an integer.
 
#define FIXED_S64_32_TO_INT(x)   (x.integer)
 Get the integer part of a Fixed_S64_32, rounded towards negative infinity.
 

Typedefs

typedef Fixed_S32_16 Fixed_S32_16Return
 Alias of Fixed_S32_16 for function return types.
 

Functions

static __inline__ Fixed_S16_3 Fixed_S16_3_mul (Fixed_S16_3 a, Fixed_S16_3 b)
 Multiply two Fixed_S16_3 values.
 
static __inline__ Fixed_S16_3 Fixed_S16_3_add (Fixed_S16_3 a, Fixed_S16_3 b)
 Add two Fixed_S16_3 values.
 
static __inline__ Fixed_S16_3 Fixed_S16_3_sub (Fixed_S16_3 a, Fixed_S16_3 b)
 Subtract two Fixed_S16_3 values.
 
static __inline__ Fixed_S16_3 Fixed_S16_3_add3 (Fixed_S16_3 a, Fixed_S16_3 b, Fixed_S16_3 c)
 Add three Fixed_S16_3 values.
 
static __inline__ bool Fixed_S16_3_equal (Fixed_S16_3 a, Fixed_S16_3 b)
 Compare two Fixed_S16_3 values.
 
static __inline__ int16_t Fixed_S16_3_rounded_int (Fixed_S16_3 a)
 Round a Fixed_S16_3 value to the nearest integer, halves away from zero.
 
static __inline__ Fixed_S32_16 Fixed_S32_16_mul (Fixed_S32_16 a, Fixed_S32_16 b)
 Multiply two Fixed_S32_16 values.
 
static __inline__ Fixed_S32_16 Fixed_S32_16_add (Fixed_S32_16 a, Fixed_S32_16 b)
 Add two Fixed_S32_16 values.
 
static __inline__ Fixed_S32_16 Fixed_S32_16_add3 (Fixed_S32_16 a, Fixed_S32_16 b, Fixed_S32_16 c)
 Add three Fixed_S32_16 values.
 
static __inline__ Fixed_S32_16 Fixed_S32_16_sub (Fixed_S32_16 a, Fixed_S32_16 b)
 Subtract two Fixed_S32_16 values.
 
static __inline__ Fixed_S64_32 Fixed_S64_32_mul (Fixed_S64_32 a, Fixed_S64_32 b)
 Multiply two Fixed_S64_32 values.
 
static __inline__ Fixed_S64_32 Fixed_S64_32_add (Fixed_S64_32 a, Fixed_S64_32 b)
 Add two Fixed_S64_32 values.
 
static __inline__ Fixed_S64_32 Fixed_S64_32_add3 (Fixed_S64_32 a, Fixed_S64_32 b, Fixed_S64_32 c)
 Add three Fixed_S64_32 values.
 
static __inline__ Fixed_S64_32 Fixed_S64_32_sub (Fixed_S64_32 a, Fixed_S64_32 b)
 Subtract two Fixed_S64_32 values.
 
static __inline__ Fixed_S16_3 Fixed_S16_3_S32_16_mul (Fixed_S16_3 a, Fixed_S32_16 b)
 Multiply a Fixed_S16_3 by a Fixed_S32_16.
 
Fixed_S64_32 math_fixed_recursive_filter (Fixed_S64_32 x, int num_input_coefficients, int num_output_coefficients, const Fixed_S64_32 *cb, const Fixed_S64_32 *ca, Fixed_S64_32 *state_x, Fixed_S64_32 *state_y)
 Run a value through an Nth order linear recursive (IIR) filter.
 

Detailed Description

Signed fixed-point types and a generic recursive filter.

In every type the fraction is unsigned and adds to the integer part, so -1.125 in Fixed_S16_3 is stored as integer -2 and fraction 7 (-2 + 7 * 0.125), and 1.125 as integer 1 and fraction 1. This keeps raw_value a plain two's complement number, so values can be added and multiplied directly.

Fixed_S16_3 a = Fixed_S16_3(12); // 1.5
int16_t r = Fixed_S16_3_rounded_int(Fixed_S16_3_add(a, b)); // 3
Fixed-point number with 1 sign bit, 12 integer bits and 3 fraction bits.
Definition math_fixed.h:29
#define Fixed_S16_3(raw)
Make a Fixed_S16_3 from its raw value.
Definition math_fixed.h:45
static __inline__ Fixed_S16_3 Fixed_S16_3_add(Fixed_S16_3 a, Fixed_S16_3 b)
Add two Fixed_S16_3 values.
Definition math_fixed.h:76
#define FIXED_S16_3_ONE
Fixed_S16_3 one.
Definition math_fixed.h:54
static __inline__ int16_t Fixed_S16_3_rounded_int(Fixed_S16_3 a)
Round a Fixed_S16_3 value to the nearest integer, halves away from zero.
Definition math_fixed.h:120

Data Structure Documentation

◆ Fixed_S16_3

union Fixed_S16_3

Fixed-point number with 1 sign bit, 12 integer bits and 3 fraction bits.

Data Fields
struct Fixed_S16_3.__unnamed102__ __unnamed__
int16_t raw_value Value scaled by 8.

◆ Fixed_S32_16

union Fixed_S32_16

Fixed-point number with 1 sign bit, 15 integer bits and 16 fraction bits.

Data Fields
struct Fixed_S32_16.__unnamed104__ __unnamed__
int32_t raw_value Value scaled by 65536.

◆ Fixed_S64_32

union Fixed_S64_32

Fixed-point number with 1 sign bit, 31 integer bits and 32 fraction bits.

Data Fields
struct Fixed_S64_32.__unnamed106__ __unnamed__
int64_t raw_value Value scaled by 2^32.

◆ Fixed_S16_3.__unnamed102__

struct Fixed_S16_3.__unnamed102__
Data Fields
uint16_t fraction: 3 Fraction, in eighths.
int16_t integer: 13 Integer part, rounded towards negative infinity.

◆ Fixed_S32_16.__unnamed104__

struct Fixed_S32_16.__unnamed104__
Data Fields
uint16_t fraction: 16 Fraction, in 1/65536 units.
int16_t integer: 16 Integer part, rounded towards negative infinity.

◆ Fixed_S64_32.__unnamed106__

struct Fixed_S64_32.__unnamed106__
Data Fields
uint32_t fraction: 32 Fraction, in 1/2^32 units.
int32_t integer: 32 Integer part, rounded towards negative infinity.

Macro Definition Documentation

◆ Fixed_S16_3

#define Fixed_S16_3 (   raw)    ((Fixed_S16_3){.raw_value = (raw)})

Make a Fixed_S16_3 from its raw value.

Parameters
rawValue scaled by 8.

◆ FIXED_S16_3_FACTOR

#define FIXED_S16_3_FACTOR   (1 << FIXED_S16_3_PRECISION)

Scale factor of Fixed_S16_3.

◆ FIXED_S16_3_HALF

#define FIXED_S16_3_HALF   ((Fixed_S16_3){.raw_value = FIXED_S16_3_ONE.raw_value / 2})

Fixed_S16_3 one half.

◆ FIXED_S16_3_ONE

#define FIXED_S16_3_ONE   ((Fixed_S16_3){.integer = 1, .fraction = 0})

◆ FIXED_S16_3_PRECISION

#define FIXED_S16_3_PRECISION   3

Number of fraction bits of Fixed_S16_3.

◆ FIXED_S16_3_ZERO

#define FIXED_S16_3_ZERO   ((Fixed_S16_3){.integer = 0, .fraction = 0})

Fixed_S16_3 zero.

◆ Fixed_S32_16

#define Fixed_S32_16 (   raw)    ((Fixed_S32_16){.raw_value = (raw)})

Make a Fixed_S32_16 from its raw value.

Parameters
rawValue scaled by 65536.

◆ FIXED_S32_16_ONE

#define FIXED_S32_16_ONE   ((Fixed_S32_16){.integer = 1, .fraction = 0})

◆ FIXED_S32_16_PRECISION

#define FIXED_S32_16_PRECISION   16

Number of fraction bits of Fixed_S32_16.

◆ FIXED_S32_16_ZERO

#define FIXED_S32_16_ZERO   ((Fixed_S32_16){.integer = 0, .fraction = 0})

Fixed_S32_16 zero.

◆ FIXED_S64_32_FROM_INT

#define FIXED_S64_32_FROM_INT (   x)    ((Fixed_S64_32){.integer = x, .fraction = 0})

Make a Fixed_S64_32 from an integer.

Parameters
xInteger.

◆ FIXED_S64_32_FROM_RAW

#define FIXED_S64_32_FROM_RAW (   raw)    ((Fixed_S64_32){.raw_value = (raw)})

Make a Fixed_S64_32 from its raw value.

Parameters
rawValue scaled by 2^32.

◆ FIXED_S64_32_ONE

#define FIXED_S64_32_ONE   ((Fixed_S64_32){.integer = 1, .fraction = 0})

◆ FIXED_S64_32_PRECISION

#define FIXED_S64_32_PRECISION   32

Number of fraction bits of Fixed_S64_32.

◆ FIXED_S64_32_TO_INT

#define FIXED_S64_32_TO_INT (   x)    (x.integer)

Get the integer part of a Fixed_S64_32, rounded towards negative infinity.

Parameters
xValue.

◆ FIXED_S64_32_ZERO

#define FIXED_S64_32_ZERO   ((Fixed_S64_32){.integer = 0, .fraction = 0})

Fixed_S64_32 zero.

Typedef Documentation

◆ Fixed_S32_16Return

Alias of Fixed_S32_16 for function return types.

Avoids the function-like Fixed_S32_16() macro expanding in function pointer declarations.

Function Documentation

◆ Fixed_S16_3_add()

static __inline__ Fixed_S16_3 Fixed_S16_3_add ( Fixed_S16_3  a,
Fixed_S16_3  b 
)
static

Add two Fixed_S16_3 values.

Parameters
aFirst term.
bSecond term.
Returns
Sum.

References Fixed_S16_3, and Fixed_S16_3::raw_value.

◆ Fixed_S16_3_add3()

static __inline__ Fixed_S16_3 Fixed_S16_3_add3 ( Fixed_S16_3  a,
Fixed_S16_3  b,
Fixed_S16_3  c 
)
static

Add three Fixed_S16_3 values.

Parameters
aFirst term.
bSecond term.
cThird term.
Returns
Sum.

References Fixed_S16_3, and Fixed_S16_3::raw_value.

◆ Fixed_S16_3_equal()

static __inline__ bool Fixed_S16_3_equal ( Fixed_S16_3  a,
Fixed_S16_3  b 
)
static

Compare two Fixed_S16_3 values.

Parameters
aFirst value.
bSecond value.
Returns
true if equal.

References Fixed_S16_3::raw_value.

◆ Fixed_S16_3_mul()

static __inline__ Fixed_S16_3 Fixed_S16_3_mul ( Fixed_S16_3  a,
Fixed_S16_3  b 
)
static

Multiply two Fixed_S16_3 values.

Parameters
aFirst factor.
bSecond factor.
Returns
Product, truncated.

References Fixed_S16_3, FIXED_S16_3_PRECISION, and Fixed_S16_3::raw_value.

◆ Fixed_S16_3_rounded_int()

static __inline__ int16_t Fixed_S16_3_rounded_int ( Fixed_S16_3  a)
static

Round a Fixed_S16_3 value to the nearest integer, halves away from zero.

Parameters
aValue.
Returns
Rounded value.

References FIXED_S16_3_FACTOR, FIXED_S16_3_HALF, and Fixed_S16_3::raw_value.

◆ Fixed_S16_3_S32_16_mul()

static __inline__ Fixed_S16_3 Fixed_S16_3_S32_16_mul ( Fixed_S16_3  a,
Fixed_S32_16  b 
)
static

Multiply a Fixed_S16_3 by a Fixed_S32_16.

Parameters
aFirst factor.
bSecond factor.
Returns
Product, as a Fixed_S16_3.

References Fixed_S16_3, FIXED_S32_16_PRECISION, Fixed_S16_3::raw_value, and Fixed_S32_16::raw_value.

◆ Fixed_S16_3_sub()

static __inline__ Fixed_S16_3 Fixed_S16_3_sub ( Fixed_S16_3  a,
Fixed_S16_3  b 
)
static

Subtract two Fixed_S16_3 values.

Parameters
aMinuend.
bSubtrahend.
Returns
a - b.

References Fixed_S16_3, and Fixed_S16_3::raw_value.

◆ Fixed_S32_16_add()

static __inline__ Fixed_S32_16 Fixed_S32_16_add ( Fixed_S32_16  a,
Fixed_S32_16  b 
)
static

Add two Fixed_S32_16 values.

Parameters
aFirst term.
bSecond term.
Returns
Sum.

References Fixed_S32_16, and Fixed_S32_16::raw_value.

◆ Fixed_S32_16_add3()

static __inline__ Fixed_S32_16 Fixed_S32_16_add3 ( Fixed_S32_16  a,
Fixed_S32_16  b,
Fixed_S32_16  c 
)
static

Add three Fixed_S32_16 values.

Parameters
aFirst term.
bSecond term.
cThird term.
Returns
Sum.

References Fixed_S32_16, and Fixed_S32_16::raw_value.

◆ Fixed_S32_16_mul()

static __inline__ Fixed_S32_16 Fixed_S32_16_mul ( Fixed_S32_16  a,
Fixed_S32_16  b 
)
static

Multiply two Fixed_S32_16 values.

Parameters
aFirst factor.
bSecond factor.
Returns
Product, truncated.

References FIXED_S32_16_PRECISION, and Fixed_S32_16::raw_value.

◆ Fixed_S32_16_sub()

static __inline__ Fixed_S32_16 Fixed_S32_16_sub ( Fixed_S32_16  a,
Fixed_S32_16  b 
)
static

Subtract two Fixed_S32_16 values.

Parameters
aMinuend.
bSubtrahend.
Returns
a - b.

References Fixed_S32_16, and Fixed_S32_16::raw_value.

◆ Fixed_S64_32_add()

static __inline__ Fixed_S64_32 Fixed_S64_32_add ( Fixed_S64_32  a,
Fixed_S64_32  b 
)
static

Add two Fixed_S64_32 values.

Parameters
aFirst term.
bSecond term.
Returns
Sum.

References FIXED_S64_32_FROM_RAW, and Fixed_S64_32::raw_value.

◆ Fixed_S64_32_add3()

static __inline__ Fixed_S64_32 Fixed_S64_32_add3 ( Fixed_S64_32  a,
Fixed_S64_32  b,
Fixed_S64_32  c 
)
static

Add three Fixed_S64_32 values.

Parameters
aFirst term.
bSecond term.
cThird term.
Returns
Sum.

References FIXED_S64_32_FROM_RAW, and Fixed_S64_32::raw_value.

◆ Fixed_S64_32_mul()

static __inline__ Fixed_S64_32 Fixed_S64_32_mul ( Fixed_S64_32  a,
Fixed_S64_32  b 
)
static

Multiply two Fixed_S64_32 values.

Parameters
aFirst factor.
bSecond factor.
Returns
Product.

References Fixed_S64_32::raw_value.

◆ Fixed_S64_32_sub()

static __inline__ Fixed_S64_32 Fixed_S64_32_sub ( Fixed_S64_32  a,
Fixed_S64_32  b 
)
static

Subtract two Fixed_S64_32 values.

Parameters
aMinuend.
bSubtrahend.
Returns
a - b.

References FIXED_S64_32_FROM_RAW, and Fixed_S64_32::raw_value.

◆ math_fixed_recursive_filter()

Fixed_S64_32 math_fixed_recursive_filter ( Fixed_S64_32  x,
int  num_input_coefficients,
int  num_output_coefficients,
const Fixed_S64_32 *  cb,
const Fixed_S64_32 *  ca,
Fixed_S64_32 *  state_x,
Fixed_S64_32 *  state_y 
)

Run a value through an Nth order linear recursive (IIR) filter.

Computes y[n] = sum(cb[i] * x[n - i]) - sum(ca[i] * y[n - 1 - i]), a generalization of the digital biquad filter.

Parameters
xNext input value, x[n].
num_input_coefficientsNumber of input taps, at least 1.
num_output_coefficientsNumber of output taps.
cbInput coefficients, num_input_coefficients entries.
caOutput coefficients, num_output_coefficients entries.
[in,out]state_xHistory of x, num_input_coefficients entries, kept between calls.
[in,out]state_yHistory of y, num_output_coefficients entries, kept between calls.
Returns
Filtered value, y[n].