View previous topic :: View next topic |
Author |
Message |
alexbilo
Joined: 01 Jun 2004 Posts: 39 Location: Trois-Rivières
|
16x16 multiplication |
Posted: Mon Aug 30, 2004 6:23 am |
|
|
Hi,
I use a PIC18f452 with the v.3.187 compiler. I had to use a 16x16 multiplication in my code.
Code: |
int32 temp;
long voltage, current;
temp=voltage*current;
|
I thought that this code would obviously store the 16x16 multiplication result in the 32 bits variable, but I found out in the assembly listing that it effectively stores the result into 4 bytes, but it clears the two MSBs at the end of the multiplication. This results in a truncated value which is, of course, of no use.
Is this comportment normal?
If not, I'll post more information as needed.
Thanks, _________________ Alex |
|
|
mvaraujo
Joined: 20 Feb 2004 Posts: 59 Location: Brazil
|
|
Posted: Mon Aug 30, 2004 6:41 am |
|
|
Hi,
Yes, this is normal, I would do :
Code: |
int32 temp;
long voltage, current;
temp=((int32) voltage*current);
|
|
|
|
alexbilo
Joined: 01 Jun 2004 Posts: 39 Location: Trois-Rivières
|
|
Posted: Mon Aug 30, 2004 7:11 am |
|
|
Thanks for the answer...
Quote: |
int32 temp;
long voltage, current;
temp=((int32) voltage*current);
|
Just to know the logic behind this: as it seems, this operation will cast the variable "voltage" to a 32 bits int. and then it'll multiply it by current. My guess is that this will result in a 32 bits multiplication. For optimization sakes, I want to keep this multiplication 16x16 but I need the full result (32 bits).
Would this code be better?
Code: |
int32 temp;
long voltage, current;
temp=(int32)(voltage*current);
|
_________________ Alex |
|
|
bdavis
Joined: 31 May 2004 Posts: 86 Location: Colorado Springs, CO
|
|
Posted: Mon Aug 30, 2004 10:03 am |
|
|
I tried the typecast you mentioned, and it still clears the upper two bytes. Looks like you need to write your own fast multiply function if you don't want to have one of the numbers a 32 bit value. |
|
|
|