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
-
i | first value to average |
j | second 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
222 "add %A[i], %A[j] \n\t"
223
224 "adc %B[i], %B[j] \n\t"
225
226
227
228 "ror %B[i] \n\t"
229
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.