Roll-Over Arithmetic in DSP TI

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

Roll-Over Arithmetic in DSP TI

Post by SukhdeepMankoo » Thu Jun 10, 2010 3:24 pm

Hi,
I have read the dsp programming on the following link:
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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Roll-Over Arithmetic in DSP TI

Post by Neo » Fri Jun 11, 2010 5:02 am

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.

SADD - Add with saturation
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.

SADD A1,A2,A3

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

If you use ADD instruction instead of SADD, the output will overflow as expected.
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: Roll-Over Arithmetic in DSP TI

Post by SukhdeepMankoo » Fri Jun 11, 2010 11:22 am

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

Re: Roll-Over Arithmetic in DSP TI

Post by Neo » Sat Jun 12, 2010 6:31 pm

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
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: Roll-Over Arithmetic in DSP TI

Post by SukhdeepMankoo » Mon Jun 14, 2010 12:09 pm

Thanks Neo.
Post Reply

Return to “Digital Signal Processors”