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 283 of file math8.h.
283 {
284#if AVG16R_C == 1
285
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
293 "add %A[i], %A[j] \n\t"
294
295 "adc %B[i], %B[j] \n\t"
296
297
298
299 "ror %B[i] \n\t"
300
301
302
303 "ror %A[i] \n\t"
304
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.