13#include "src/libdivide/libdivide.h"
14#include "src/libdivide/constant_fast_div.h"
36#define DIV_ROUND_DOWN -1
46#define DIV_ROUND_NEAREST 0
49#define DIV_ROUND_BEHAVIOR DIV_ROUND_NEAREST
59#define DIV_ROUND_CORRECT(d, t) ((t)(((d)>>1U)+(t)DIV_ROUND_BEHAVIOR))
76#define DIV_ROUND_CLOSEST(n, d, t) ( \
77 (((n) < (t)(0)) ^ ((d) < (t)(0))) ? \
78 ((t)((n) - DIV_ROUND_CORRECT(d, t))/(t)(d)) : \
79 ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d)))
93#define UDIV_ROUND_CLOSEST(n, d, t) ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))
98#define IS_INTEGER(d) ((d) == (int32_t)(d))
142static inline int div100(
int n) {
218#if defined(CORE_AVR) || defined(ARDUINO_ARCH_AVR)
242#if defined(CORE_AVR) || defined(ARDUINO_ARCH_AVR)
246 #define INDEX_REG "r16"
251 " lsl %A0 ; shift\n\t"
252 " rol %B0 ; rem:quot\n\t"
253 " rol %C0 ; left\n\t"
254 " rol %D0 ; by 1\n\t"
255 " brcs 1f ; if carry out, rem > divisor\n\t"
256 " cp %C0, %A1 ; is rem less\n\t"
257 " cpc %D0, %B1 ; than divisor ?\n\t"
258 " brcs 2f ; yes, when carry out\n\t"
260 " sub %C0, %A1 ; compute\n\t"
261 " sbc %D0, %B1 ; rem -= divisor\n\t"
262 " ori %A0, 1 ; record quotient bit as 1\n\t"
265 " brne 0b ; until bits == 0"
289#if defined(CORE_AVR) || defined(ARDUINO_ARCH_AVR)
315template <
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:349
#define DIV_ROUND_CORRECT(d, t)
Computes the denominator correction for rounding division based on our rounding behavior.
Definition maths.h:59
#define DIV_ROUND_CLOSEST(n, d, t)
Rounded integer division.
Definition maths.h:76
#define UDIV_ROUND_CLOSEST(n, d, t)
Rounded unsigned integer division.
Definition maths.h:93
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:339
static uint32_t div360(uint32_t n)
Optimised integer division by 360.
Definition maths.h:165
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:287
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:309
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:211
static uint16_t udiv_32_16(uint32_t dividend, uint16_t divisor)
Optimised division: uint32_t/uint16_t => uint16_t.
Definition maths.h:240
static uint32_t percentage(uint8_t percent, uint32_t value)
Integer based percentage calculation.
Definition maths.h:180
static uint16_t halfPercentage(uint8_t percent, uint16_t value)
Integer based half-percentage calculation.
Definition maths.h:193
static uint16_t div100(uint16_t n)
Performance optimised integer division by 100. I.e. same as n/100.
Definition maths.h:109