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

◆ mod8()

LIB8STATIC_ALWAYS_INLINE uint8_t mod8 ( uint8_t a,
uint8_t m )

Calculate the remainder of one unsigned 8-bit value divided by anoter, aka A % M.

Implemented by repeated subtraction, which is very compact, and very fast if A is "probably" less than M. If A is a large multiple of M, the loop has to execute multiple times. However, even in that case, the loop is only two instructions long on AVR, i.e., quick.

Parameters
adividend byte
mdivisor byte
Returns
remainder of a / m (i.e. a % m)

Definition at line 377 of file math8.h.

377 {
378#if defined(__AVR__)
379 asm volatile("L_%=: sub %[a],%[m] \n\t"
380 " brcc L_%= \n\t"
381 " add %[a],%[m] \n\t"
382 : [a] "+r"(a)
383 : [m] "r"(m));
384#else
385 while (a >= m)
386 a -= m;
387#endif
388 return a;
389}

References LIB8STATIC_ALWAYS_INLINE.