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

◆ avg16r()

LIB8STATIC_ALWAYS_INLINE uint16_t avg16r ( uint16_t i,
uint16_t j )

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

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

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

Definition at line 271 of file math8.h.

271 {
272#if AVG16R_C == 1
273 return (uint32_t)((uint32_t)(i) + (uint32_t)(j) + 1) >> 1;
274#elif AVG16R_AVRASM == 1
275 asm volatile(
276 /* First, add jLo (heh) to iLo, 9th bit overflows into C flag */
277 "add %A[i], %A[j] \n\t"
278 /* Now, add C + jHi to iHi, 17th bit overflows into C flag */
279 "adc %B[i], %B[j] \n\t"
280 /* Divide iHi by two, moving C flag into high 16th bit, old 9th bit now
281 in
282 C */
283 "ror %B[i] \n\t"
284 /* Divide iLo by two, moving C flag into high 8th bit, old 1st bit now
285 in
286 C */
287 "ror %A[i] \n\t"
288 /* Add C flag */
289 "adc %A[i], __zero_reg__\n\t"
290 "adc %B[i], __zero_reg__\n\t"
291 : [i] "+r"(i)
292 : [j] "r"(j));
293 return i;
294#else
295#error "No implementation for avg16r available."
296#endif
297}

References LIB8STATIC_ALWAYS_INLINE.