CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

16x16 multiplication

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alexbilo



Joined: 01 Jun 2004
Posts: 39
Location: Trois-Rivières

View user's profile Send private message

16x16 multiplication
PostPosted: Mon Aug 30, 2004 6:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 30, 2004 6:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 30, 2004 7:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 30, 2004 10:03 am     Reply with quote

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. Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group