16 bit fixed-point multiply

- Hi Nathan...

We want z=x*y where x,y are 16 bits long. If x=x1x2 (not a multiplication!!) and y=y1y2, where x1,x2,y1,y2 are 8 bits long, it comes with 8 bit multiplication based operations:

z = y2*x2 + (x2*y1+x1*y2)<

for a 16 bits result:

z = x1*y1 + trunc(x2*y1+x1*y2+trunc(x2*y2))

where: trunc(x*y) = MSB (x*y)

It's not clear, I know...an example:

E03A * A7D9 = ????

here: x1 = e0, x2 = 3a, y1 = a7, y2 = d9

well: x2*y2=3a*d9=312a=MSB.LSB with MSB=31 LSB=2a

So we keep the MSB 31 and :

x1*y2+x2*y1+MSB=e0*d9+3a*a7+31

=bde0+25d6+31

=e3e7 (result can be greater than 0xffff!! care in your code...)

Forget the first 8 bits => we keep 0xe3

Finally:

z=x1*y1+0xe3=e0*a7+e3=0x9303

This is easy to implement in assembly, coz those operations are based only on 8 bit multiplications (moreover the ST7 core returns the MSB in the index register) and 16 bit additions.

By the way, if you want to implement signed operation, you should add before this a testing procedure on the bit dedicated to the sign and reset those bits before going through the multiplication...

Depending of the accuracy, you can also truncate x and y and use a single 8 bits multiplication, but I guess that's not what you want!!

Hope it can help!

Florent - Hello Nathan,

Please find attached 2 more routines that might help you. The attached 16mul.asm file is from the benchmark that you will find on our website in the Application Note section (it's the application note # AN 910). So this version should be very optimized.

In the attached math. zip file you will find another implementation possible.

Now, I would recommend you have a look at the ST7 forum discussion about "ST7 based SQRT algorithm". You will see that writting a routine in C can generate a faster code than writting in assembly.

Now if you decide to include some assembly code inside your C code, I am recommending you have a look at the examples given in the attached Cosmic C compiler User's manual rev4.4 pages 48,49 and 50 (CXST7_V4.4.zip).

I hope this will help,

Renaud

[ This message was edited by: Renaud on 11-04-2003 21:03 ]

Does anyone have benchmarks of the integer multiply of the ST7262?

I can see that the 8 bit multiplies take about 4-5 cycles.

I am trying to implement some 16 bit fixed-point filters on this 8 bit

processors. I am looking for a C-callable assembly or in-line assembly

that will do this math ...

z = (x*y)>>15

where x,y and z are integers. This is also called a fixed-point

multiply. The x*y operation will generate 32 bits however we are just interested in the top 16 bits of it that is why there is a right shift of 15 bits. The shift is only 15 bits because there are two sign bits.

Also I am looking for any information about how to create a C-callable

assembly routine. That would be very helpfull because I can probably figure out how to write the fixed-point multiply routine with some help from looking at the integer multiply that the compiler generates.

Thanks,

Nathan