Object coding: SMMUL where to shift / SSAT when to saturate


#1

Hi,

I've read the various pages / posts about object coding & fixed point math. There are two things that remained unclear:

First, re ___SMUL:
In https://sebiik.github.io/community.axoloti.com.backup/t/coding-axoloti-objects/2606/11 it is shown that we can do e.g.
___SMUL(a << 3, b << 2) or
___SMUL(a << 5, b) or
___SMUL(a, b) << 5

What is the difference in the the three variants (in terms of numerical result), assuming that a and b are both within the 27 bit range?

Second , re __SSAT:
Are there an "best practices" when one should saturate after certain operations, and when it's not necessary?

Thanks!


#2

the ___SMMUL instruction uses the higher 15bits to do the multiply and disregards the lower bits. so if you shift up the a and b from the 27bit range before the multiplication you can use those higher bits to get a more precise result. if you shift after the multiply the 5 lowest bits will all be zero, so you loose resolution. better to use the <<3 and <<2.


#3

okay got it. thanks!


#4

E.g. when you add two Q27 numbers and want to clamp the result to Q27, you'd use x = __SSAT( x, 28 ) to make sure the result fits within the allotted 28 bits without overflowing. Or in other words, when a number may be bigger than you want, use __SSAT to clip it to the range that fits in the given number of bits.