multiplication vs shifting


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429

    Default multiplication vs shifting

    ive got to multiply a variable by 25 and i was wondering which of these 2 would be quicker

    x = x*25

    or

    x = x<<4 + x<<3 + x

    I have a suspicion that the latter will be quicker, but take up more code space.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    ok, i just did some tests and i was half right and half wrong.

    x=x<<4+x<<3+x was quicker (taking 80% of the time that x=x*25 took to execute)

    However, what surprised me was that x=x*25 used MORE code space than x=x<<4+x<<3+x. Replacing x=x<<4+x<<3+x with x=x*25 in my program adds 338 bytes.

    It appears that x=x<<4+x<<3+x wins on both counts.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    PBP uses a subroutine for performing the multiplication. (For all 16F * calculations, and for 18F * calcs involving more than 8 bits.) If you perform the calculation multiple times, an individual line may be shorter than shifting. However, on just a one-time multiplication, the PBP subroutine will add significant code.

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default

    No other code, just Variable declaration and END,for a F877:

    For BYTE variable:

    Code:
    x=x*25            '40 bytes
    x=x<<4+x<<3+x     '43 bytes
    For WORD variable:

    Code:
    x=x*25             '43 bytes
    x=x<<4+x<<3+x      '49 bytes
    x=x<<25            '27 bytes!!!
    Ioannis

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    whats the point of the last one? shift a word variable 25 times either way and you will just get 0.

    i was multiplying a word var and saving into a long var
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default

    I know 25 shift will not give the result you want.

    Just tested to see how much less program space and how faster a shift will be. Useless for you but quite impressive!

    Which PIC are you using? Is there any chance to use one with hardware multiplier?

    Ioannis

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts