![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
multiply problem |
Posted: Mon Mar 06, 2006 11:06 am |
|
|
I am using PCWH 3.245
I tried using a simple multiply and found the answer was wrong.
1b9*4e2=6a52, should be 0x86952
I then discovered _mul(a,b). That worked fine.
Here is my demo code.
Why doesn't the simple multiply work right?
Did I do something wrong?
I tried searching the group first but I didn't see this.
Thanks, Pete
Code: |
#define A 0x1b9
#define B 0x4e2
unsigned int16 mun16_A, mun16_B;
unsigned int32 mun32_A;
mun16_A=A;
printf("A=0x%lx B=0x%lx C=0x%lx \n\r",A,B,C);
printf("\n\r");
printf("mun16_A=0x%lx mun16_B=0x%lx mun16_C=0x%lx \n\r",mun16_A,mun16_B,mun16_C);
printf("mun32_A=0x%lx mun32_B=0x%lx mun32_C=0x%lx \n\r",mun32_A,mun32_B,mun32_C);
printf("\n\r");
printf("\n\r");
mun32_A=(int32_t)(mun16_A*B);
printf("should be 0x00086952, mun16_A*B=0x%lx\n\r",mun32_A);
mun16_B=_mul(mun16_A, B);
printf("should be 0x00086952, _mul(mun16_A,B)=0x%lx\n\r",mun16_B);
|
_________________ -Pete |
|
![](templates/subSilver/images/spacer.gif) |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Mon Mar 06, 2006 11:20 am |
|
|
-oops
I took out some extra code an put in a mistake, here is the right stuff
Code: |
#define A 0x1b9
#define B 1250
#define C 0x3ff
#define D 0x400
unsigned int16 mun16_A, mun16_B;
unsigned int32 mun32_A;
mun16_A=A;
printf("A=0x%lx B=0x%lx C=0x%lx \n\r",A,B,C);
printf("\n\r");
printf("mun16_A=0x%lx mun16_B=0x%lx\n\r",mun16_A,mun16_B);
printf("mun32_A=0x%lx\n\r",mun32_A);
printf("\n\r");
printf("\n\r");
mun32_A=(int32_t)(mun16_A*B);
printf("should be 0x00086952, mun16_A*B=0x%lx\n\r",mun32_A);
mun32_A=_mul(mun16_A, B);
printf("should be 0x00086952, _mul(mun16_A,B)=0x%lx\n\r",mun32_A);
|
_________________ -Pete |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 06, 2006 12:57 pm |
|
|
Quote: | mun32_A=(int32_t)(mun16_A*B); |
Before I look at your program, where's the typedef statement
for that special data type ? |
|
![](templates/subSilver/images/spacer.gif) |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Mon Mar 06, 2006 1:23 pm |
|
|
Sorry, just change that to int32.
I have a stdint.h file that I use to make int8_t, int16_t and int32_t available cross platform. At one time I wrote c code that ran on a Rabbit processor and a PIC. It avoid the issue of an int being 8 bits on a PIC and 16 bits on a rabbit. _________________ -Pete |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 06, 2006 1:48 pm |
|
|
Fix it by casting one of the operands to 32-bits. See the changes
shown in bold below.
Quote: | mun32_A = (int32)mun16_A * B;
printf("should be 0x00086952, mun16_A * B = %lx\n\r", mun32_A);
mun32_A = _mul((int32)mun16_A, B);
printf("should be 0x00086952, _mul(mun16_A, B) = %lx\n\r", mun32_A); |
|
|
![](templates/subSilver/images/spacer.gif) |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Mon Mar 06, 2006 4:21 pm |
|
|
Yup, that did it. Didn't need to cast in the _mul() though.
I thought just casting the whole operation would do it.
One of those things I don't do very often, so I never learned that lesson till now.
Thank you. _________________ -Pete |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Mar 06, 2006 4:28 pm |
|
|
This is the whole 'point' of using _mul. It takes two int_16 values, and multiplies them to produce an int32 result. Quicker and smaller than casting. When you casted the whole command in brackets, the effect was to multiply two int16 values, which therefore used int16 arithmetic, and then convert the result to int32 (which would be the default behaviour since the result was being transferred to an int32).
Best Wishes |
|
![](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
|