Fixed point multiplication in a known range

I am trying to propagate A*Bat a 16-bit fixed point, while maintaining the highest possible accuracy. A- 16-bit unsigned integer, Bdivisible by 1000 and always between 0.001and 9.999. It has been a while since I dealt with such problems, therefore:

  • I know what I can just do A*B/1000after switching to 32-bit variables and then cast back to 16 bits
  • I would like to do it faster than that
  • I would like to do all the operations without switching to 32-bit (since I only have 16-bit multiplication)

Is there an easy way to do this?

Edit: Awill be in the range from 0 to 4000, so all possible results are also in the 16-bit range.

Edit: Bcomes from the user, sets the number by the icon in the mask X.XXX, so the operation /1000.

+3
source share
1 answer

No, you need to go up to 32 bits. In general, a product of two 16-bit numbers will always give you a 32-bit result.

You should check the instruction set of the processor of the processor you are running on, because most of the multiplied instructions on 16-bit machines have the ability to return the result as a 32-bit integer.

This will help you because:

short testfunction (short a, short b)
{
  int A32 = a;
  int B32 = b;

  return A32*B32/1000
}

32- * 32- . 16- .

.

Texas Instruments C64x + DSP, :

short test (short a, short b) 
{
  int product = _mpy (a,b); // calculates product, returns 32 bit integer
  return product / 1000;
}

: 1000. ? . 1024 . :

  return (a*b)/1024 

? , 10 . , .

+3

Source: https://habr.com/ru/post/1755211/


All Articles