## Roll-Over Arithmetic in DSP TI

DSP Topics
SukhdeepMankoo
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

### Roll-Over Arithmetic in DSP TI

Hi,
http://en.wikibooks.org/wiki/Digital_Si ... rogramming
It has written that if we declare a variable suppose
unsigned short value=65500;
value+=100;

on the execution of above sequence of lines.
compiler should give output.
In Case of microcontroller:
value=44;

In Case of DSP:
value=65535.
it should not roll over.

But i have seen that running the above code into TI Code Composer Studio for TMS320C6713.
Output comes out to be value=44;

it is behaving as in microcontroller compilers.
Is this link is telling like or is it processor oriented?
Can u tell me, what is it?

Thanks to ROBOT.LK Team and specially to Neo.
Neo
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

### Re: Roll-Over Arithmetic in DSP TI

Hi Sukhdeep..

Yes, there are some instructions that supports saturation. These are common to almost every DSP as this is usually required in signal processing.

Consider following ASM instructions that are commonly used.

SSUB - Substract with saturation
SMPY - Multiply with saturation
SSHL - Shift left with saturation
...
...
...
SPACK

I'll take SADD to explain you a bit on these very useful instructions.

A1 - 0x4367 71F2
A2 - 0x5A2E 51A3

One cycle after,
A3 - 0x7FFF FFFF (output is saturated)

Two cycles after,
CSR - 0x0001 0300 (CPU updates the saturation bit in CSR register to notify the user that the output is saturated).
Notice the beautiful thing that it only takes single cycle to saturate the value.

Generally for SADD (.unit) src1, src2, dst,
src1 is added to src2 and saturated if an overflow occurs according to the following rules:

IF… THEN:
1) dst is an int and src1 + src2 > (2^31) – 1 result is (2^31) – 1
2) dst is an int and src1 + src2 < –2^31 result is –2^31
3) dst is a long and src1 + src2 > (2^39) – 1 result is (2^39) – 1
4) dst is a long and src1 + src2 < –2^39 result is –2^39

SukhdeepMankoo
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

### Re: Roll-Over Arithmetic in DSP TI

Thanks Neo,
May i know, how bring the result of two variables in saturation using C Language.
Neo
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

### Re: Roll-Over Arithmetic in DSP TI

I have only used that with assembly.

In C, I do something like below. So you just need to adjust the maxima and minima.
myVal = (myVal < 0) ? 0 : ( (myVal > 255) ? 255 : myVal );

However may try inline assembly using asm keyword on your variables.
SukhdeepMankoo
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Thanks Neo.