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 CCS Technical Support

Unsigned division with constant

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



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Unsigned division with constant
PostPosted: Thu Jul 12, 2007 5:12 am     Reply with quote

Any idea why the following code causes a compile error complaining about the range of the constant:

Code:

   unsigned int16 Freq10;
   unsigned int16 quot;
   quot = ((unsigned int32)0x98968000) / Freq10;


while the following code compiles OK:

Code:

   unsigned int32 dividend;
   dividend = 0x98968000;
   quot = dividend / Freq10;


I am using the PCM compiler, version 3.242.

Robert Scott
Ypsilanti, Michigan
Ttelmah
Guest







PostPosted: Thu Jul 12, 2007 7:28 am     Reply with quote

Quite an interesting bug.
Post it to CCS. What is happening, is that the conversion is assuming the value is 'signed', and can't then fit the number inside the available 31 bits.
You can use:
Code:

   unsigned int16 Freq10;
   unsigned int16 quot;
   Freq10=4;
   quot = -0x67698000 / (Unsigned int32)Freq10;

This 'cheats', and gives the signed equivalent value (the signed value that generates the same bit pattern), and then forces the arithmetic to treat this as unsigned, which should give the required result...

Best Wishes
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Sat Jul 14, 2007 3:44 pm     Reply with quote

Ttelmah wrote:
Quite an interesting bug.
Post it to CCS. ...


First, can anyone confirm this still is a bug in the current version of the compiler? I am using command-line PCM 3.242, so maybe it has been fixed by now.

Robert Scott
Ypsilanti, Michigan
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Jul 14, 2007 4:43 pm     Reply with quote

v3.249 gives the same compiler error.
v4.038 is ok.

Note that the example will overflow an int16 when Freq10 is smaller than 0x9897 (39,063).
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