Branchless Saturating Integer Arithmetic
This project is maintained by paulhuggett
To show how useful a saturating addition function can be, we can look at a simple DSP signal mixer. This takes two input signals and combines them to produce a single output. It’s common for DSP systems to use fixed-point integers to represent amplitudes. The choices made in the formatting of these values imposes a strict limit on the range of number that can be represented. The hypothetical system described here can reproduce values in the range [-1.0, 1.0].
The first two charts below show two input sinusoids. You can experiment with changing the frequency, amplitude, and phase of each. The next three charts show the result of mixing the two input signals.
The first (titled “Desired Output”) shows the mixer’s mathematically correct output. Where there is constructive interference between the two input waveforms, values may lie outside the [-1.0,1.0] range — these are shown in red — and a real-world implementation will distort this part of the output waveform.
The second chart (“Actual Output: Saturating Addition”) shows the result of using saturating addition. Where the output would exceed the maximum or minimum amplitude values, the result is clamped to that range.
The third chart (“Actual Output: Modulo Addition”) shows the use of conventional modulo arithmetic. Here calculations that overflow result in dramatic discontinuities in the output waveform as the values “wrap around” when they overflow.
It should be clear that neither saturating nor modulo addition will be able to produce the desired output — there will be distortion of some kind regardless of our choice — but that using saturating arithmetic yields a waveform that is much closer to the ideal.
Compare the results when using saturating addition or modulo addition to the desired output. Saturating addition has produced output which is much closer to the ideal output.