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:425
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 425 of file math8.h.

425 {
426#if defined(__AVR__)
427 asm volatile(" sub %[a],%[b] \n\t"
428 "L_%=: sub %[a],%[m] \n\t"
429 " brcc L_%= \n\t"
430 " add %[a],%[m] \n\t"
431 : [a] "+r"(a)
432 : [b] "r"(b), [m] "r"(m));
433#else
434 a -= b;
435 while (a >= m)
436 a -= m;
437#endif
438 return a;
439}

References LIB8STATIC.