How to do multiplication without using math operation ???

Thread Starter

ecaits

Joined Jan 6, 2014
56
Dear All,

How to do multiplication operation without using math library. I am using float datatype.

I am working on PIC16F877 using hi-tech C compiler.
I have read that multiplication in math operation consuming more program memory so I want to optimize my program code, if any other logic is available for multiplication....

Thank you in advance,

Nirav
 
Last edited:

Sensacell

Joined Jun 19, 2012
3,432
If you just need to multiply by binary factors (2, 4, 8 etc) just bit shift the registers left, each shift multiplies by 2. Shift right to divide by 2.

Multiply by a fixed number? combine shift and add functions.

Lookup tables also work for small ranges of numbers- fast, but eats up lots of memory.

Other than that, its just a big fat slow multiply routine.
 

ErnieM

Joined Apr 24, 2011
8,377
How to do multiplication operation without using math library.
You don't. The best you do is use a better library.

I am using float datatype.
That reminds me of an old joke:

Old Joke said:
Patient: Doctor, it hurts when I do this.

Doctor: Then don't do that.
Post your problem and let us see if you really do need floats. It is very rare that you really need them.
 

THE_RB

Joined Feb 11, 2008
5,438
You don't. The best you do is use a better library.
...
I've done it by successive addition;
Rich (BB code):
// result = X * Y
result = 0;
while(X)
{
  result += Y;
  X--;  
}
Obviously it can be a lot slower but is very space efficient because it uses very little ROM compared to something like a 16*16 or 32*16 math routine.

And yes it's probably completely useless if someoone is using floats.
 

ErnieM

Joined Apr 24, 2011
8,377
I've done it by successive addition;
Yep, and I've done division by similar means when I had no room for the C function code to do division.

However, the OP is using floats which needs lots of support just to add or subtract. Not sure if that could fit inside a PIC16F877 with a measly 14KB for program code.

ecaits: post the problem and let us show you some of the old guy tricks.
 

joeyd999

Joined Jun 6, 2011
5,237
...the OP is using floats which needs lots of support just to add or subtract. Not sure if that could fit inside a PIC16F877 with a measly 14KB for program code.
I hope you are being sarcastic, Ernie. 14K is huge! Consider that the Level II TRS-80 BASIC resided in 12K of memory, and included both single and double precision floats, integer math, and full string support.

OTH, IMHO, I've not yet seen a good, tight C library for float support for small processors.
 

ErnieM

Joined Apr 24, 2011
8,377
OK, just checked using the XC8 and to divide one float by and int and store into an int uses about 500 locations*. So it could fit.

And if 14K wasn't measly you would not be so impressed with the TRASH-80. :p

(* That is on a PIC18F4550... for some reason my XC for PIC16 series thinks it's the HI-TECH compiler and lacks the license to run).
 
Top