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.
 
#define fastMap1023toX(x, out_max)   ( rshift<10>((uint32_t)(x) * (out_max)) )
 
#define fastMap10Bit(x, out_min, out_max)   ( rshift<10>( (uint32_t)(x) * ((out_max)-(out_min)) ) + (out_min))
 

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.
 
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

◆ fastMap1023toX

#define fastMap1023toX (   x,
  out_max 
)    ( rshift<10>((uint32_t)(x) * (out_max)) )

◆ fastMap10Bit

#define fastMap10Bit (   x,
  out_min,
  out_max 
)    ( rshift<10>( (uint32_t)(x) * ((out_max)-(out_min)) ) + (out_min))

◆ IS_INTEGER

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

Test whether the parameter is an integer or not.

Function Documentation

◆ 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:

◆ 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  )
extern
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: