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

283 {
284#if AVG16R_C == 1
285 // return (uint32_t)((uint32_t)(i) + (uint32_t)(j) + 1) >> 1;
286 uint32_t tmp = i;
287 tmp += j;
288 tmp += 1;
289 return static_cast<uint16_t>(tmp >> 1);
290#elif AVG16R_AVRASM == 1
291 asm volatile(
292 /* First, add jLo (heh) to iLo, 9th bit overflows into C flag */
293 "add %A[i], %A[j] \n\t"
294 /* Now, add C + jHi to iHi, 17th bit overflows into C flag */
295 "adc %B[i], %B[j] \n\t"
296 /* Divide iHi by two, moving C flag into high 16th bit, old 9th bit now
297 in
298 C */
299 "ror %B[i] \n\t"
300 /* Divide iLo by two, moving C flag into high 8th bit, old 1st bit now
301 in
302 C */
303 "ror %A[i] \n\t"
304 /* Add C flag */
305 "adc %A[i], __zero_reg__\n\t"
306 "adc %B[i], __zero_reg__\n\t"
307 : [i] "+r"(i)
308 : [j] "r"(j));
309 return i;
310#else
311#error "No implementation for avg16r available."
312#endif
313}

References LIB8STATIC_ALWAYS_INLINE.