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

225 {
226#if AVG16_C == 1
227 // return (uint32_t)((uint32_t)(i) + (uint32_t)(j)) >> 1;
228 uint32_t tmp = i;
229 tmp += j;
230 return static_cast<uint16_t>(tmp >> 1);
231#elif AVG16_AVRASM == 1
232 asm volatile(
233 /* First, add jLo (heh) to iLo, 9th bit overflows into C flag */
234 "add %A[i], %A[j] \n\t"
235 /* Now, add C + jHi to iHi, 17th bit overflows into C flag */
236 "adc %B[i], %B[j] \n\t"
237 /* Divide iHi by two, moving C flag into high 16th bit, old 9th bit now
238 in
239 C */
240 "ror %B[i] \n\t"
241 /* Divide iLo by two, moving C flag into high 8th bit */
242 "ror %A[i] \n\t"
243 : [i] "+r"(i)
244 : [j] "r"(j));
245 return i;
246#else
247#error "No implementation for avg16 available."
248#endif
249}

References LIB8STATIC_ALWAYS_INLINE.