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

8x8 Multiply with 16-bit Result

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
BadBrainBoy



Joined: 28 Apr 2005
Posts: 4

View user's profile Send private message

8x8 Multiply with 16-bit Result
PostPosted: Fri May 13, 2005 4:41 pm     Reply with quote

Hi All,

The default when doing int8 * int8 with a hardware multiplier PIC is to keep only the LSB of the result (PRODL). This is C standard, I suppose, but not very useful.

If you want the full 16-bit result from int8 * int8, you can use this (written for the 18F2220):
=========================================
#byte PRODH = 0xff4 // HW multiply result MSB
#byte PRODL = 0xff3 // HW multiply result LSB
// MULT macro
// Performs 8x8 hardware multiply of x and y and stores the 16-bit
// result in xy
#define MULT(x,y,xy) \
#asm \
movf x,w \
mulwf y \
#endasm \
xy = MAKE16(PRODH,PRODL);
=========================================
Now just write MULT(arg1,arg2,result) when you want to multiply two int8 variables named arg1 and arg2 and store the product in int16 result.

This is very efficient, it compiles to only 4 machine instructions with 6 instruction cycles. It is set up for straight unsigned multiplication; you may fiddle with it for signed.

Have fun!
sd211



Joined: 15 Nov 2005
Posts: 4
Location: NC, USA

View user's profile Send private message Visit poster's website

Re: 8x8 Multiply with 16-bit Result
PostPosted: Sun Jan 01, 2006 7:46 pm     Reply with quote

BadBrainBoy wrote:
Hi All,

The default when doing int8 * int8 with a hardware multiplier PIC is to keep only the LSB of the result (PRODL). This is C standard, I suppose, but not very useful.

If you want the full 16-bit result from int8 * int8, you can use this (written for the 18F2220):
=========================================
#byte PRODH = 0xff4 // HW multiply result MSB
#byte PRODL = 0xff3 // HW multiply result LSB
// MULT macro
// Performs 8x8 hardware multiply of x and y and stores the 16-bit
// result in xy
#define MULT(x,y,xy) \
#asm \
movf x,w \
mulwf y \
#endasm \
xy = MAKE16(PRODH,PRODL);
=========================================
Now just write MULT(arg1,arg2,result) when you want to multiply two int8 variables named arg1 and arg2 and store the product in int16 result.

This is very efficient, it compiles to only 4 machine instructions with 6 instruction cycles. It is set up for straight unsigned multiplication; you may fiddle with it for signed.

Have fun!


There is function _mul(x,y), which will give you very similar code, maybe faster by 1 cycle.
Sergey
Jimmy



Joined: 21 Aug 2006
Posts: 1

View user's profile Send private message

PostPosted: Mon Aug 21, 2006 3:03 am     Reply with quote

I tried c = _mul(arg1,arg2);. It's not working properly. I use PIC16F917 and simulate with MPLAB. The main function is like this.

void main()
{
int8 varA=50,varB=100;
long int c;

c = _mul(varA,varB);
while(TRUE);
}

in the watch window show c = 34835 insteal to show c = 5000.

I try many value, but always wrong result.
Is there anyone can help on this? thanks.
Mircea



Joined: 12 Jan 2007
Posts: 7
Location: Europe

View user's profile Send private message

PostPosted: Tue May 06, 2008 3:10 pm     Reply with quote

Code:
Not all processors have mul statement.
Here is an efficent algorithm to multiply two
integers.

Consider two numbers, example a=122 and b=57.
We want to get the result of their multiplication.
The expected result is  122*57 = 6954.

We are using the "generic" formula:


              2*a        b
   a * b  =  -----  *  -----
               1         2


Multiplying a by 2 and dividing b by 2 (integer divison),
we get this table:
-------------------------------
     1      2     3
-------------------------------
   122     57    ()   
   244     28
   488     14
   976      7    ()
  1952      3    ()
  3904      1    ()   stop algorithm
-------------------------------

The marker ()  in the third column means that the corresponding
value in column 2 is NOT divisible by 2.

Now we add the values in column 1 on marked rows:

     122+
     976
    1952
    3904
---------------
    6954    (which is the good result).
   
One can observe:
---------------
1. The operations involved:
- multiplication by 2
- division by 2
- test bit 0 to see if it is odd
 are easy to perform by controllers.

2. It is not necessary to store the partial
values in columns 1 and 2, everything cand be done
in one single loop.

3. The algorithm is very efficient.
Some peolpe said that the above algorithm is
used to create mul or imul elementary statements
inside the controllers or processors that have a such statement.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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