FastLED 3.9.15
Loading...
Searching...
No Matches

◆ submod8()

LIB8STATIC uint8_t submod8 ( uint8_t a,
uint8_t b,
uint8_t m )

Subtract two numbers, and calculate the modulo of the difference and a third number, M.

In other words, it returns (A-B) % M. It is designed as a compact mechanism for decrementing a "mode" switch and wrapping around back to "mode 0" when the switch goes past the start of the available range. e.g. if you have seven modes, this switches to the previous one and wraps around if needed:

mode = submod8( mode, 1, 7);
LIB8STATIC uint8_t submod8(uint8_t a, uint8_t b, uint8_t m)
Subtract two numbers, and calculate the modulo of the difference and a third number,...
Definition math8.h:441
Parameters
adividend byte
bvalue to subtract from the dividend
mdivisor byte
Returns
remainder of (a - b) / m
See also
mod8() for notes on performance.

Definition at line 441 of file math8.h.

441 {
442#if defined(__AVR__)
443 asm volatile(" sub %[a],%[b] \n\t"
444 "L_%=: sub %[a],%[m] \n\t"
445 " brcc L_%= \n\t"
446 " add %[a],%[m] \n\t"
447 : [a] "+r"(a)
448 : [b] "r"(b), [m] "r"(m));
449#else
450 a -= b;
451 while (a >= m)
452 a -= m;
453#endif
454 return a;
455}

References LIB8STATIC.