2016-01-08 09:25 AM
hello forum
I am using a Cortex M0+ and I am looking for suggestions about handling saturation with 32 bit addition.I found this code in the webuint32_t sadd32(uint32_t a, uint32_t b)
{ return (a > (1<<32-1) - b) ? ((1<<32)-1) : a + b;}which can probably extended to int32_t (which puzzles me :) ). I am looking for possible suggestions about more efficient assembly implementationthanks2016-01-08 10:32 AM
PRESERVE8
THUMB
AREA |.text|, CODE, READONLY
; M0 version
AddSatU32 PROC
EXPORT AddSatU32
; Entry
; R0 = A (uint32_t)
; R1 = B (uint32_t)
; Exit
; R0 = A+B, or 0xFFFFFFFF
add r0, r0, r1 ; do the sum
sbcs r1, r1, r1 ; propagate carry 1->0x00000000, 0->0xFFFFFFFF
mvns r1, r1 ; not
orrs r0, r1 ; or (add's carry forces OR with 0xFFFFFFFF)
bx lr
ENDP ; AddSatU32 - sourcer32@gmail.com
END
2016-01-12 02:27 AM
thank much clive for your quick reply!
what about signed sum? it should be enough to check the V flag right? if you have a debugged example with signed operands please post it