# How to do multiplication without using math operation ???

Discussion in 'Embedded Systems and Microcontrollers' started by ecaits, Jun 10, 2014.

1. ### ecaits Thread Starter Member

Jan 6, 2014
52
0
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....

Nirav

Last edited: Jun 10, 2014
2. ### Sensacell Well-Known Member

Jun 19, 2012
1,183
276
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.

3. ### MrChips Moderator

Oct 2, 2009
12,624
3,451
If you want to optimize your code don't use float.

Dec 24, 2011
38
0
Insert asm commands]

#asm

#endasm

5. ### kubeek AAC Fanatic!

Sep 20, 2005
4,687
805
What he said. Use fixed decimal point instead of floating and you will save about a thousand instructions for each multiplication and a bunch of program memory.

6. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
You don't. The best you do is use a better library.

That reminds me of an old joke:

Post your problem and let us see if you really do need floats. It is very rare that you really need them.

7. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
I've done it by successive addition;
Code ( (Unknown Language)):
1.
2. // result = X * Y
3. result = 0;
4. while(X)
5. {
6.   result += Y;
7.   X--;
8. }
9.
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.

8. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

9. ### joeyd999 AAC Fanatic!

Jun 6, 2011
2,756
2,922
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.

10. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

(* 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).