12#include "src/libdivide/libdivide.h"
13#include "src/libdivide/constant_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))
156static inline int div100(
int n) {
260 #define INDEX_REG "r16"
265 " lsl %A0 ; shift\n\t"
266 " rol %B0 ; rem:quot\n\t"
267 " rol %C0 ; left\n\t"
268 " rol %D0 ; by 1\n\t"
269 " brcs 1f ; if carry out, rem > divisor\n\t"
270 " cp %C0, %A1 ; is rem less\n\t"
271 " cpc %D0, %B1 ; than divisor ?\n\t"
272 " brcs 2f ; yes, when carry out\n\t"
274 " sub %C0, %A1 ; compute\n\t"
275 " sbc %D0, %B1 ; rem -= divisor\n\t"
276 " ori %A0, 1 ; record quotient bit as 1\n\t"
279 " brne 0b ; until bits == 0"
329template <
typename T,
typename TPrime>
Optimized multi-byte bit shifting for AVR-GCC only. See Optimised bitwise shifts.
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:348
#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
uint8_t random1to100(void)
Definition maths.cpp:8
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:353
static uint32_t div360(uint32_t n)
Optimised integer division by 360.
Definition maths.h:179
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:373
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:391
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.
Definition maths.h:301
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:323
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:225
static uint16_t udiv_32_16(uint32_t dividend, uint16_t divisor)
Optimised division: uint32_t/uint16_t => uint16_t.
Definition maths.h:254
static uint32_t percentage(uint8_t percent, uint32_t value)
Integer based percentage calculation.
Definition maths.h:194
static uint16_t halfPercentage(uint8_t percent, uint16_t value)
Integer based half-percentage calculation.
Definition maths.h:207
static uint16_t div100(uint16_t n)
Performance optimised integer division by 100. I.e. same as n/100.
Definition maths.h:123