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

multiply problem

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



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

multiply problem
PostPosted: Mon Mar 06, 2006 11:06 am     Reply with quote

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
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 11:20 am     Reply with quote

-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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 12:57 pm     Reply with quote

Quote:
mun32_A=(int32_t)(mun16_A*B);

Before I look at your program, where's the typedef statement
for that special data type ?
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 1:23 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 1:48 pm     Reply with quote

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);
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 4:21 pm     Reply with quote

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
Ttelmah
Guest







PostPosted: Mon Mar 06, 2006 4:28 pm     Reply with quote

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
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