5#include <avr-fast-shift.h>
6#include <avr-fast-div.h>
35#define DIV_ROUND_DOWN -1
45#define DIV_ROUND_NEAREST 0
48#define DIV_ROUND_BEHAVIOR DIV_ROUND_NEAREST
58#define DIV_ROUND_CORRECT(d, t) ((t)(((d)>>1U)+(t)DIV_ROUND_BEHAVIOR))
75#define DIV_ROUND_CLOSEST(n, d, t) ( \
76 (((n) < (t)(0)) ^ ((d) < (t)(0))) ? \
77 ((t)((n) - DIV_ROUND_CORRECT(d, t))/(t)(d)) : \
78 ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d)))
92#define UDIV_ROUND_CLOSEST(n, d, t) ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))
101template <u
int16_t divisor>
112#define IS_INTEGER(d) ((d) == (int32_t)(d))
128 constexpr libdivide::libdivide_u16_t
libdiv_u16_100 = { .magic = 18351, .more = 70 };
143 constexpr libdivide::libdivide_s16_t
libdiv_s16_100 = { .magic = 20972, .more = 5 };
155 constexpr libdivide::libdivide_u32_t
libdiv_u32_100 = { .magic = 2748779070, .more = 6 };
167 constexpr libdivide::libdivide_s32_t
libdiv_s32_100 = { .magic = 1374389535, .more = 5 };
183 constexpr libdivide::libdivide_u32_t
libdiv_u32_360 = { .magic = 1813430637, .more = 72 };
213template <u
int8_t bitsPrecision>
247 #define TEST_AND_APPLY(precision) \
248 if (percent<(UINT16_C(1)<<(UINT16_C(16)-(precision)))) { \
249 return _percentageApprox<(precision)>(percent, value); \
257 #undef TEST_AND_APPLY
295 constexpr libdivide::libdivide_u32_t
libdiv_u32_200 = { .magic = 2748779070, .more = 7 };
324template <
typename TDiv
idend,
typename TDivisor>
347template <
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:58
static constexpr uint16_t div_round_closest_u16(uint16_t n)
Rounded unsigned integer division optimized for compile time constants.
Definition maths.h:102
#define DIV_ROUND_CLOSEST(n, d, t)
Rounded integer division.
Definition maths.h:75
#define UDIV_ROUND_CLOSEST(n, d, t)
Rounded unsigned integer division.
Definition maths.h:92
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:173
static uint8_t a
Definition maths.cpp:7
uint8_t random1to100(void)
Definition maths.cpp:8
#define TEST_AND_APPLY(precision)
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:372
static uint32_t percentage(uint16_t percent, uint32_t value)
Integer based percentage calculation.
Definition maths.h:279
static uint32_t div360(uint32_t n)
Optimised integer division by 360.
Definition maths.h:181
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:392
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:410
static uint32_t percentageApprox(uint16_t percent, uint32_t value)
Integer based percentage calculation: faster, but less accurate, than percentage()
Definition maths.h:240
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:341
static uint32_t rshift_round(uint32_t a)
Rounded arithmetic right shift.
Definition maths.h:203
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:311
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:325
static uint16_t halfPercentage(uint8_t percent, uint16_t value)
Integer based half-percentage calculation.
Definition maths.h:292
static uint16_t div100(uint16_t n)
Performance optimised integer division by 100. I.e. same as n/100.
Definition maths.h:123