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
-
i | first value to average |
j | second 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
277 "add %A[i], %A[j] \n\t"
278
279 "adc %B[i], %B[j] \n\t"
280
281
282
283 "ror %B[i] \n\t"
284
285
286
287 "ror %A[i] \n\t"
288
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.