Speeduino
Loading...
Searching...
No Matches
Macros | Functions
maths.h File Reference
#include <stdint.h>
#include "globals.h"
#include "bit_shifts.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DIV_ROUND_DOWN   -1
 Rounding behavior: always round down.
 
#define DIV_ROUND_UP   1
 Rounding behavior: always round up.
 
#define DIV_ROUND_NEAREST   0
 Rounding behavior: round to nearest.
 
#define DIV_ROUND_BEHAVIOR   DIV_ROUND_NEAREST
 Integer division rounding behavior.
 
#define DIV_ROUND_CORRECT(d, t)   ((t)(((d)>>1U)+(t)DIV_ROUND_BEHAVIOR))
 Computes the denominator correction for rounding division based on our rounding behavior.
 
#define DIV_ROUND_CLOSEST(n, d, t)
 Rounded integer division.
 
#define UDIV_ROUND_CLOSEST(n, d, t)   ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))
 Rounded unsigned integer division.
 
#define IS_INTEGER(d)   ((d) == (int32_t)(d))
 Test whether the parameter is an integer or not.
 

Functions

uint8_t random1to100 (void)
 
static uint32_t div360 (uint32_t n)
 Optimised integer division by 360.
 
static uint32_t percentage (uint8_t percent, uint32_t value)
 Integer based percentage calculation.
 
static uint16_t halfPercentage (uint8_t percent, uint16_t value)
 Integer based half-percentage calculation.
 
static int16_t nudge (int16_t min, int16_t max, int16_t value, int16_t nudgeAmount)
 Make one pass at correcting the value into the range [min, max)
 
static uint16_t udiv_32_16 (uint32_t dividend, uint16_t divisor)
 Optimised division: uint32_t/uint16_t => uint16_t.
 
static uint16_t udiv_32_16_closest (uint32_t dividend, uint16_t divisor)
 Same as udiv_32_16(), except this will round to nearest integer instead of truncating.
 
template<class T >
constexpr const Tclamp (const T &v, const T &lo, const T &hi)
 clamps a given value between the minimum and maximum thresholds.
 
static uint16_t LOW_PASS_FILTER (uint16_t input, uint8_t alpha, uint16_t prior)
 Simple low pass IIR filter 16-bit values.
 
static int16_t LOW_PASS_FILTER (int16_t input, uint8_t alpha, int16_t prior)
 Simple low pass IIR filter for S16 values.
 
static uint16_t div100 (uint16_t n)
 Performance optimised integer division by 100. I.e. same as n/100.
 
static int16_t div100 (int16_t n)
 Performance optimised integer division by 100. I.e. same as n/100.
 
static uint32_t div100 (uint32_t n)
 Performance optimised integer division by 100. I.e. same as n/100.
 
static int32_t div100 (int32_t n)
 Performance optimised integer division by 100. I.e. same as n/100.
 

Macro Definition Documentation

◆ IS_INTEGER

#define IS_INTEGER (   d)    ((d) == (int32_t)(d))

Test whether the parameter is an integer or not.

Function Documentation

◆ clamp()

template<class T >
constexpr const T & clamp ( const T v,
const T lo,
const T hi 
)
constexpr

clamps a given value between the minimum and maximum thresholds.

Uses operator< to compare the values.

Template Parameters
TAny type that supports operator<
Parameters
vThe value to clamp
loThe minimum threshold
hiThe maximum threshold
Returns
if v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise returns v.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ div100() [1/4]

static int16_t div100 ( int16_t  n)
inlinestatic

Performance optimised integer division by 100. I.e. same as n/100.

Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR

Parameters
nDividend to divide by 100
Returns
n/100, with rounding behavior applied
Here is the call graph for this function:

◆ div100() [2/4]

static int32_t div100 ( int32_t  n)
inlinestatic

Performance optimised integer division by 100. I.e. same as n/100.

Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR

Parameters
nDividend to divide by 100
Returns
n/100, with rounding behavior applied
Here is the call graph for this function:

◆ div100() [3/4]

static uint16_t div100 ( uint16_t  n)
inlinestatic

Performance optimised integer division by 100. I.e. same as n/100.

Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR

Parameters
nDividend to divide by 100
Returns
n/100, with rounding behavior applied
Here is the call graph for this function:
Here is the caller graph for this function:

◆ div100() [4/4]

static uint32_t div100 ( uint32_t  n)
inlinestatic

Performance optimised integer division by 100. I.e. same as n/100.

Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR

Parameters
nDividend to divide by 100
Returns
n/100, with rounding behavior applied
Here is the call graph for this function:

◆ div360()

static uint32_t div360 ( uint32_t  n)
inlinestatic

Optimised integer division by 360.

Parameters
nThe numerator (dividee) (an integer)
Returns
uint32_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ halfPercentage()

static uint16_t halfPercentage ( uint8_t  percent,
uint16_t  value 
)
inlinestatic

Integer based half-percentage calculation.

Parameters
percentThe percent to calculate ([0, 100])
valueThe value to operate on
Returns
uint16_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LOW_PASS_FILTER() [1/2]

static int16_t LOW_PASS_FILTER ( int16_t  input,
uint8_t  alpha,
int16_t  prior 
)
inlinestatic

Simple low pass IIR filter for S16 values.

Here is the call graph for this function:

◆ LOW_PASS_FILTER() [2/2]

static uint16_t LOW_PASS_FILTER ( uint16_t  input,
uint8_t  alpha,
uint16_t  prior 
)
inlinestatic

Simple low pass IIR filter 16-bit values.

This is effectively implementing the smooth filter from playground.arduino.cc/Main/Smooth But removes the use of floats and uses 8 bits of fixed precision.

Parameters
inputincoming unfiltered value
alphafilter factor. 0=off, 255=full smoothing (0.00 to 0.99 in float, 0-99%)
priorprevious filtered value.
Returns
uint16_t The filtered input
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nudge()

static int16_t nudge ( int16_t  min,
int16_t  max,
int16_t  value,
int16_t  nudgeAmount 
)
inlinestatic

Make one pass at correcting the value into the range [min, max)

Parameters
minMinimum value (inclusive)
maxMaximum value (exclusive)
valueValue to nudge
nudgeAmountAmount to change value by
Returns
int16_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ percentage()

static uint32_t percentage ( uint8_t  percent,
uint32_t  value 
)
inlinestatic

Integer based percentage calculation.

Parameters
percentThe percent to calculate ([0, 100])
valueThe value to operate on
Returns
uint32_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ random1to100()

uint8_t random1to100 ( void  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udiv_32_16()

static uint16_t udiv_32_16 ( uint32_t  dividend,
uint16_t  divisor 
)
inlinestatic

Optimised division: uint32_t/uint16_t => uint16_t.

Optimised division of unsigned 32-bit by unsigned 16-bit when it is known that the result fits into unsigned 16-bit.

~60% quicker than raw 32/32 => 32 division on ATMega

Note
Bad things will likely happen if the result doesn't fit into 16-bits.
Copied from https://stackoverflow.com/a/66593564
Parameters
dividendThe dividend (numerator)
divisorThe divisor (denominator)
Returns
uint16_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udiv_32_16_closest()

static uint16_t udiv_32_16_closest ( uint32_t  dividend,
uint16_t  divisor 
)
inlinestatic

Same as udiv_32_16(), except this will round to nearest integer instead of truncating.

Minor performance drop compared to non-rounding version.

Here is the call graph for this function:
Here is the caller graph for this function: