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

257 {
258#if AVG8R_C == 1
259 return (i + j + 1) >> 1;
260#elif AVG8R_AVRASM == 1
261 asm volatile(
262 /* First, add j to i, 9th bit overflows into C flag */
263 "add %0, %1 \n\t"
264 /* Divide by two, moving C flag into high 8th bit, old 1st bit now in C
265 */
266 "ror %0 \n\t"
267 /* Add C flag */
268 "adc %0, __zero_reg__\n\t"
269 : "+r"(i)
270 : "r"(j));
271 return i;
272#else
273#error "No implementation for avg8r available."
274#endif
275}

References LIB8STATIC_ALWAYS_INLINE.