5#include <avr-fast-shift.h>
6#include <avr-fast-div.h>
50#define DIV_ROUND_DOWN -1
60#define DIV_ROUND_NEAREST 0
63#define DIV_ROUND_BEHAVIOR DIV_ROUND_NEAREST
73#define DIV_ROUND_CORRECT(d, t) ((t)(((d)>>1U)+(t)DIV_ROUND_BEHAVIOR))
90#define DIV_ROUND_CLOSEST(n, d, t) ( \
91 (((n) < (t)(0)) ^ ((d) < (t)(0))) ? \
92 ((t)((n) - DIV_ROUND_CORRECT(d, t))/(t)(d)) : \
93 ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d)))
107#define UDIV_ROUND_CLOSEST(n, d, t) ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))
116template <u
int16_t divisor>
126#define UDIV_ROUND_UP(n, d, t) ((t)((n) + (t)((d)+1U))/(t)(d))
131#define IS_INTEGER(d) ((d) == (int32_t)(d))
244template <u
int8_t bitsPrecision>
278 #define TEST_AND_APPLY(precision) \
279 if (percent<(UINT16_C(1)<<(UINT16_C(16)-(precision)))) { \
280 return _percentageApprox<(precision)>(percent, value); \
288 #undef TEST_AND_APPLY
364template <
typename TDiv
idend,
typename TDivisor>
389template <
typename T,
typename TPrime>
#define DIV_ROUND_CORRECT(d, t)
Computes the denominator correction for rounding division based on our rounding behavior.
Definition maths.h:73
TESTABLE_STATIC_CONSTEXPR uint16_t div_round_closest_u16(uint16_t n)
Rounded unsigned integer division optimized for compile time constants.
Definition maths.h:117
#define DIV_ROUND_CLOSEST(n, d, t)
Rounded integer division.
Definition maths.h:90
#define UDIV_ROUND_CLOSEST(n, d, t)
Rounded unsigned integer division.
Definition maths.h:107
static uint8_t a
Definition maths.cpp:8
uint8_t random1to100(void) noexcept
Definition maths.cpp:9
static constexpr uint32_t MICROS_PER_MIN
Self-explanatory.
Definition maths.h:22
static constexpr uint32_t MICROS_PER_SEC
Self-explanatory.
Definition maths.h:19
#define TEST_AND_APPLY(precision)
static constexpr uint32_t MILLI_PER_SEC
Self-explanatory.
Definition maths.h:28
static uint16_t LOW_PASS_FILTER(uint16_t input, uint8_t alpha, uint16_t prior)
Simple low pass IIR filter 16-bit values.
Definition maths.h:414
TESTABLE_STATIC_CONSTEXPR TDividend fast_div_closest(TDividend dividend, TDivisor divisor)
Same as fast_div(), except this will round to nearest integer instead of truncating.
Definition maths.h:365
static constexpr uint32_t MICROS_PER_HOUR
Self-explanatory.
Definition maths.h:25
uint16_t pwmFreqToTicks(uint16_t frequency)
Convert a frequency in Hz to the equivalent number of PWM timer ticks.
Definition maths.cpp:32
static uint32_t percentage(uint16_t percent, uint32_t value)
Integer based percentage calculation.
Definition maths.h:317
static uint32_t div360(uint32_t n)
Optimised integer division by 360.
Definition maths.h:208
static uint8_t scale(const uint8_t from, const uint8_t fromRange, const uint8_t toRange)
Scale a value from one range to another.
Definition maths.h:434
static uint8_t fast_map(const uint8_t from, const uint8_t fromLow, const uint8_t fromHigh, const uint8_t toLow, const uint8_t toHigh)
Specialist version of map(long, long, long, long, long) for performance.
Definition maths.h:452
static uint32_t percentageApprox(uint16_t percent, uint32_t value)
Integer based percentage calculation: faster, but less accurate, than percentage()
Definition maths.h:271
static constexpr uint8_t ONE_HUNDRED_PCT
This is only here to eliminate magic numbers.
Definition maths.h:309
static uint32_t rshift_round(uint32_t a)
Rounded arithmetic right shift.
Definition maths.h:232
TESTABLE_STATIC_CONSTEXPR const T & clamp(const T &v, const T &lo, const T &hi)
clamps a given value between the minimum and maximum thresholds.
Definition maths.h:382
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)
Definition maths.h:351
static uint16_t halfPercentage(uint8_t percent, uint16_t value)
Integer based half-percentage calculation.
Definition maths.h:330
static uint16_t div100(uint16_t n)
Performance optimised integer division by 100. I.e. same as n/100.
Definition maths.h:142
constexpr array()=default
Unit testability support.
#define TESTABLE_STATIC_CONSTEXPR
Mark a function with constexpr, unless a unit test is in progress - then the entity is inlined.
Definition unit_testing.h:56