![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest
|
Is it me, but this wont work? |
Posted: Wed Dec 16, 2009 5:11 am |
|
|
Is it me, but this wont work?
Res is all time = 0 why????
Code: | void Test1(){
int16 Res;
int8 Tal,RP2;
Tal=230;
RP2=80;
Res=Tal*RP2*4/284;
fprintf(com1,"R:%Lu\r\n",Res);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Dec 16, 2009 5:38 am |
|
|
284 is a 16-bit value. The multiplications are all 8-bit values.
8bit / 16-bit = 0.xxxx ==> 0
Upgrading (casting) one of the 8-bit values to a larger type should fix the problem.
Res=(int32)Tal*RP2*4/284;
you need an int32 because Tal*RP2*4 = 8-bit * 8-bit * 2-bit = 18 bit result
You can optimize the code size a lot by reducing the int32 to an int16 when you rewrite the calculation as:
Res=(int16)Tal*RP2/71; |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Dec 16, 2009 5:44 am |
|
|
It won't.
This is down to numeric typing. Tal, and RP2, are _both_ 8bit integer values. So are '4', and 284. So the arithmetic performed _will_ use 8bit values. It'll generate 230*80 = 0xE0 (8bit), *4 = 0x80 (8bit). 128/230 = 0.
You need to force any one of the values to be converted to 16bit, _before_ the arithmetic, to make the compiler use 16bit arithmetic.
Code: |
void Test1(){
int16 Res;
int8 Tal,RP2;
Tal=230;
RP2=80;
Res=(int16)Tal*RP2*4/284;
fprintf(com1,"R:%Lu\r\n",Res);
}
|
This is basically standard C, but happens 'earlier' here in CCS, than is common. Most compilers will actually use 16bit aritmetic as their 'minimum' type, and even when they don't, will 'realise' that multiplying two 8bit values, potentially needs a 16bit result. With CCS, to save space, this is not donw, but it means _you_ have to be explicit, and force the conversion, even when it might not seem to be necessary...
There is one fractionally more efficient way of doing this:
Code: |
void Test1(){
int16 Res;
int8 Tal,RP2;
Tal=230;
RP2=80;
Res=_mul(Tal,RP2)*4/284;
fprintf(com1,"R:%Lu\r\n",Res);
}
|
The _mul operator, behaves like the multiply operator on most normal C's, generating the 16bit result from multiplying two 8bit values. This saves the time/space,of converting the 8bit value before the operation, but since it generates the 16bit result, it again results in 16bit arithmetic being used for the rest of the line.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Dec 16, 2009 7:52 am |
|
|
Hi
Thanks both,
But Code: | Res=_mul(Tal,RP2)*4/284; | wont work.
Code: | Res=(_mul(Tal,RP2)/284)*4; | Work
I think we get overrun int32*4 and there after make the division. Therefore it making the division first and then the mul it work.
But I think it's complicated. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|