DSPRelated.com
Forums

C code simulation of DSP fixed-point operations with overflow and saturation

Started by jbrower 1 year ago9 replieslatest reply 1 year ago279 views

All-

Does anyone have a link to old Texas Inst C code that simulated what their fixed-point DSPs did with overflow and saturation with arithmetic operations (add, sub, shl, shr) ?

I seem to remember an app note with C code in an appendix, but I may be confused.

Thanks, Jeff

[ - ]
Reply by Robert WolfeApril 15, 2023

Hello,

Sorry, I don't remember an app note (but very well could have existed).  But I had done exactly what you mentioned, with C55 processors - bit accurate C simulations of fixed-point recursive filters, incorporating all facets of the processor operation, including the overflow and saturation, etc.  It was a bit painstaking, but very possible - referencing and replicating the technical notes for the detail description of each operation.  In the end, I could essentially run a C program, and know the exact response on the processor itself.  Made for rapid debug and changes.

15 year old code, but I've attached examples of accumulator round, truncation and saturation operations.

Regards,

Robert


c55.simul.c



[ - ]
Reply by jbrowerApril 15, 2023

Hi Robert-

Thanks, that looks promising. Do you also have code for add, sub, mul, and shift that detects overflow and calls sat() in c55.simul.c ?

-Jeff

[ - ]
Reply by Robert WolfeApril 15, 2023

Hi,

Here's an example from a filter simulation, with 16 bit coefficients, where the round and saturation check is done after each biquad.

Robert

iir_16.c

[ - ]
Reply by jbrowerApril 15, 2023

Hi Robert-

Thanks. I think I see now how your code works - processes all biquads first, then checks for overflow.

I need a shift left function that checks for overflow after shifting and saturates if needed. Maybe I can use part of sat() for this.

-Jeff

[ - ]
Reply by Robert WolfeApril 15, 2023

Welcome.  In this case, I did (process all biquads first).  But in other cases, I did the check for saturation/overflow after each of the biquad MAC operation ... shouldn't really matter, you can make that check at any point.  Good luck.


Robert

[ - ]
Reply by dgshaw6April 15, 2023
There is a fairly complete C source library from ITU that has lots of basic functions that deal with saturation and overflow protection.
  1. G.191
[ - ]
Reply by jbrowerApril 15, 2023

Hi DGShaw6, thanks !

Yes I know of the ITU STL but it has a "General Public License" that disallows modifications, and it needs mods because it has global variables and error handling not suitable for a Linux library (i.e. calls to abort()

I did find the GNU overflow built-ins https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflo..., which look ok for add, sub, mul, but they seem to have overlooked left-shift.

-Jeff

[ - ]
Reply by rbjApril 15, 2023

It looks like you're reviving a TI application.  But if you're doing filtering in fixed-point with a general-purpose CPU (that doesn't do floating point), I have some generic C code in this Stack Exchange answer: https://dsp.stackexchange.com/questions/21792/best...

It also demonstrates a better way to do quantization, using first-order noise shaping with a zero at z=1.

[ - ]
Reply by jbrowerApril 15, 2023

Hi Robert-

Thanks for the link to your fixed-point filtering C code, it does look super efficient ! As would be expected from any RBJ authored code.

What I've been looking for is overflow-safe individual operations (add, sub, mul, shift). I need these for commercial codecs and speech recognition stuff we build where certain customers worry about 3GPP and ITU licenses embedded in the source. Codec authors will swear up and down such cases are never a problem, but some customers will do anything to minimize their possible legal entanglements. Even if nothing has happened to anyone on the planet since the 1990s they don't want to be the first, hehe.

Recently I found the SEI CERT C standard, which appears to be unencumbered (written as a set of guidelines and recommendations). For example part way down this page is an overflow-safe left shift:

https://wiki.sei.cmu.edu/confluence/display/c/INT3...

Hopefully these will provide an acceptable method for our use-case, we'll see !

-Jeff