performance - How to do saturating addition in C? -
what best (cleanest, efficient) way write saturating addition in c?
the function or macro should add 2 unsigned inputs (need both 16- , 32-bit versions) , return all-bits-one (0xffff or 0xffffffff) if sum overflows.
target x86 , arm using gcc (4.1.2) , visual studio (for simulation only, fallback implementation ok there).
you want portable c code here, compiler turn proper arm assembly. arm has conditional moves, , these can conditional on overflow. algorithm becomes add, , conditionally set destination unsigned(-1) if overflow detected.
uint16_t add16(uint16_t a, uint16_t b) { uint16_t c = + b; if (c<a) /* can happen due overflow */ c = -1; return c; }
note differs other algorithms in corrects overflow, instead of relying on calculation detect overflow.
x86-64 clang 3.7 -o3 output adds32: better other answer:
add edi, esi mov eax, -1 cmovae eax, edi ret
armv7: gcc 4.8 -o3 -mcpu=cortex-a15 -fverbose-asm
output adds32:
adds r0, r0, r1 @ c, a, b cs movcs r0, #-1 @ conditional-move bx lr
16bit: still doesn't use arm's unsigned-saturating add instruction (uadd16
)
add r1, r1, r0 @ tmp114, movw r3, #65535 @ tmp116, uxth r1, r1 @ c, tmp114 cmp r0, r1 @ a, c ite ls @ movls r0, r1 @,, c movhi r0, r3 @,, tmp116 bx lr @
Comments
Post a Comment