cancel
Showing results for 
Search instead for 
Did you mean: 

32 sum with saturation

stanzanim
Associate III
Posted on January 08, 2016 at 18:25

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 web 

uint32_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 implementation

thanks
2 REPLIES 2
Posted on January 08, 2016 at 19:32

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

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
stanzanim
Associate III
Posted on January 12, 2016 at 11:27

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