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

    Last edited: Jun 10, 2014
  2. Sensacell

    Senior Member

    Jun 19, 2012
    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


    Oct 2, 2009
    If you want to optimize your code don't use float.
  4. micropad


    Dec 24, 2011
    Insert asm commands]


    Your code

  5. kubeek

    AAC Fanatic!

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