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

◆ avg8r()

LIB8STATIC_ALWAYS_INLINE uint8_t avg8r ( uint8_t i,
uint8_t j )

Calculate an integer average of two unsigned 8-bit integer values (uint8_t), rounded up.

Fractional results are rounded up, e.g. avg8r(20,41) = 31

Parameters
ifirst value to average
jsecond value to average
Returns
mean average of i and j, rounded up

Definition at line 245 of file math8.h.

245 {
246#if AVG8R_C == 1
247 return (i + j + 1) >> 1;
248#elif AVG8R_AVRASM == 1
249 asm volatile(
250 /* First, add j to i, 9th bit overflows into C flag */
251 "add %0, %1 \n\t"
252 /* Divide by two, moving C flag into high 8th bit, old 1st bit now in C
253 */
254 "ror %0 \n\t"
255 /* Add C flag */
256 "adc %0, __zero_reg__\n\t"
257 : "+r"(i)
258 : "r"(j));
259 return i;
260#else
261#error "No implementation for avg8r available."
262#endif
263}

References LIB8STATIC_ALWAYS_INLINE.