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

◆ avg15()

LIB8STATIC_ALWAYS_INLINE int16_t avg15 ( int16_t i,
int16_t j )

Calculate an integer average of two signed 15-bit integers (int16_t).

If the first argument is even, result is rounded down. If the first argument is odd, result is rounded up.

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

Definition at line 328 of file math8.h.

328 {
329#if AVG15_C == 1
330 return (i >> 1) + (j >> 1) + (i & 0x1);
331#elif AVG15_AVRASM == 1
332 asm volatile(
333 /* first divide j by 2, throwing away lowest bit */
334 "asr %B[j] \n\t"
335 "ror %A[j] \n\t"
336 /* now divide i by 2, with lowest bit going into C */
337 "asr %B[i] \n\t"
338 "ror %A[i] \n\t"
339 /* add j + C to i */
340 "adc %A[i], %A[j] \n\t"
341 "adc %B[i], %B[j] \n\t"
342 : [i] "+r"(i)
343 : [j] "r"(j));
344 return i;
345#else
346#error "No implementation for avg15 available."
347#endif
348}

References LIB8STATIC_ALWAYS_INLINE.