Marc Paquette

CodeWarrior 8- & 16-bit tools: Looking for 16 X 16 bits FAST multiplication trick

Discussion created by Marc Paquette Employee on Jan 27, 2006
To help you find solutions to problems that have already been solved, we have posted this message. It contains an entire topic ported from a separate forum. The original message and all replies are in this single message.
Posted: Jul 13, 2004 - 04:16 AM   
Dear all,
In my application I need to perform 16x16 bits, result on 32 bits multiplications but very fast.
When coding this :
long a;
int c,d;
a = c * d;
the compiler uses _IMUL because it requires to be casted and of course the result is false but this is performed very fast.
When coding :
a = (long) c * d;
The result is correct and the compiler uses _LMUL library function. The _LMUL routine calls other routines :
This is very long to perform all these routines and I guess it could be improved

Has anybody got trick on how to code a fast 16x16 multiplication ?
Best regards
   Posted: Jul 15, 2004 - 03:05 PM   
You might see a slight speed improvement if you specified that the compiler should  optimize the code for time (-Ot) instead of size, which will place all the routines as inline code rather than separate functions.
Other than that, it really matters if on what processor you are using and a lot of other things. You'd have to open an service request and even then I cna't assure you that there will be a quick solution.
 Posted: Jul 16, 2004 - 02:46 AM   
In fact I've coded something less heavy than the library multiplication.
For instance on a HC08QL4 at clk=3.2MHz, , the 16x16 takes 200 us. When coding myslef a 16x16 it takes 60us. Better isn't it ?
But of course my function is less easy to use than the classic multiplication. The C multiplication manages all cases whereas I need to know exactly what I'm going to do when using my multiplication.
Now my problem is how to pass 2 16-bits paramters and how to read a 32-bit returned value
I'm investigating....

Thanks for your answer.
Yes our engineers said the best thing would be for you to create your own function and use that instead of the multiplaction operator. Did you try long 16bitMul(short short) to pass 16's and return a 32 bit? (note that HC tools are no where near my area, and that is just a random thought from a C point of view)
Ron Liechty
Ombudsman for Metrowerks