View previous topic :: View next topic |
Author |
Message |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Unsigned division with constant |
Posted: Thu Jul 12, 2007 5:12 am |
|
|
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
|
|
Posted: Thu Jul 12, 2007 7:28 am |
|
|
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
|
|
Posted: Sat Jul 14, 2007 3:44 pm |
|
|
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
|
|
Posted: Sat Jul 14, 2007 4:43 pm |
|
|
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). |
|
|
|