multiplication vs shifting


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    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

  2. #2
    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

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    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

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    To me, the time it takes to execute is more important than the size of the code.
    And the fastest way will usually <strike>be the smallest too</strike> not be the smallest.

    On a 16F @ 4Mhz with WORD variables ...
    <table cellpadding=6 border=1><tr><td align=center>Formula</td><td align=center>Execution Time</td><td align=center>Words per<br>Instance</td><td align=center>Words added<br>to Library</td></tr><tr><td>x = x*25</td><td align=center>230 - 250uS<br>depends on X value</td><td align=center>12</td><td align=center>27</td></tr><tr><td>x = x<<4 + x<<3 + x</td><td align=center>110uS</td><td align=center>34</td><td align=center>13</td></tr><tr><td> T = X << 1
    T = T << 1
    T = T << 1
    X = T << 1 + T + X </td><td align=center>31uS</td></td><td align=center>30</td><td align=center>13<br>adds SHIFTL to library<br>but doesn't use it.</td></tr></table>

    I wonder if ASM will help
    Last edited by Darrel Taylor; - 8th April 2010 at 01:31. Reason: Updated table with code size
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Nice work DT.

    It's amusing that doing a single shift twice takes less time than a double shift.

    I have run into a problem though. Does anyone know why this gives me an incorrect result?

    Code:
    X VAR WORD
    T VAR LONG
    Y VAR LONG
    
    X=8100
    
    T = X << 1
    T = T << 1
    T = T << 1
    Y = T << 1 + T + X
    
    LCDOUT $FE,$80,"T=",DEC T
    LCDOUT $FE,$C0,"Y=",DEC Y
    T is 64800 (as expected), but Y is 2516719364 ?!

    I'm guessing that its something about long variables that I am missing.
    "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
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default That's a Bug



    Nice find Kamikaze!

    There's a small bug in the pbppi18l.mac file.
    The SHIFTL?NCN macro should be changed as follows. (3's in red)
    Code:
    SHIFTL?NCN macro Nin, Cin, Nout
        if ((Cin) == 1)
            bcf     STATUS, C
          if ((Nout) == (Nin))
            CHK?RP  Nout
            rlcf    Nout, F
            rlcf    (Nout) + 1, F
            rlcf    (Nout) + 2, F
            rlcf    (Nout) + 3, F
          else
            CHK?RP  Nin
            rlcf    Nin, W
            MOVE?AB Nout
            CHK?RP  Nin
            rlcf    (Nin) + 1, W
            MOVE?AB (Nout) + 1
            CHK?RP  Nin
            rlcf    (Nin) + 2, W
            MOVE?AB (Nout) + 2
            CHK?RP  Nin
            rlcf    (Nin) + 3, W     ; was 2
            MOVE?AB (Nout) + 3
          endif
        else
            MOVE?NN Nin, R0
            movlw   Cin
            L?CALL  SHIFTL
            MOVE?ANN R0, Nout
        endif
        endm
    SHIFTL_USED = 1
      endmod
    I'll let the guys know.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Yep, that fixed it. Thanks DT.

    I'm surprised that the bug hasn't been found before now.

    Out of curiosity, why did it only affect the shift on the last line, and not the first three?

    Code:
    T = X << 1
    T = T << 1
    T = T << 1
    Y = T << 1 + T + X
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

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