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

◆ avg16()

LIB8STATIC_ALWAYS_INLINE uint16_t avg16 ( uint16_t i,
uint16_t j )

Calculate an integer average of two unsigned 16-bit integer values (uint16_t), rounded down.

Fractional results are rounded down, e.g. avg16(20,41) = 30

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

Definition at line 216 of file math8.h.

216 {
217#if AVG16_C == 1
218 return (uint32_t)((uint32_t)(i) + (uint32_t)(j)) >> 1;
219#elif AVG16_AVRASM == 1
220 asm volatile(
221 /* First, add jLo (heh) to iLo, 9th bit overflows into C flag */
222 "add %A[i], %A[j] \n\t"
223 /* Now, add C + jHi to iHi, 17th bit overflows into C flag */
224 "adc %B[i], %B[j] \n\t"
225 /* Divide iHi by two, moving C flag into high 16th bit, old 9th bit now
226 in
227 C */
228 "ror %B[i] \n\t"
229 /* Divide iLo by two, moving C flag into high 8th bit */
230 "ror %A[i] \n\t"
231 : [i] "+r"(i)
232 : [j] "r"(j));
233 return i;
234#else
235#error "No implementation for avg16 available."
236#endif
237}

References LIB8STATIC_ALWAYS_INLINE.