Which is faster bit shifts or multiplication/division

Discussion in 'Discussion and Q&A Archive' started by Sonic master, Jan 25, 2012.

Thread Status:
Not open for further replies.
  1. Sonic master

    Sonic master Well-Known Member Member

    Joined:
    Mar 27, 2010
    Messages:
    303
    I am working on some asm code and there are some parts were I can get the same resaults using bit shifts so I was just wondering which one is faster.



    Code:
    
    lsl.w	#3,d4	;times d4 by 8
    
    
    or



    Code:
    
    mulul.w	#8,d4	;times d4 by 8
    
    
    And for division



    Code:
    
    	lsr.w	#2,d0;divide by 4
    
    
    or



    Code:
    
    	DIVU.w	#4,d0;divide by 4
    
    
     
    Last edited by a moderator: Jan 25, 2012
  2. MarkeyJester

    MarkeyJester ♡ ! Member

    Joined:
    Jun 27, 2009
    Messages:
    2,867
    This is a little complexe but, if multiplying by 2, then adding the register to itself is the quickest. If multiplying by 4, then if it is byte or word, then adding the register to itself twice is the quickest, if it is long-word then shift left is the quickest, if multiplying by 8 and it is byte or word, then adding and shifting left result in the same speed, but shifting will result in smaller binary data when assembled, long-word, shifting is quicker. For 16, 32, shift left, for 64, use the swap instruction and then the clr.w instruction on the register. For anything that isn't in the pattern of 2, 4, 8, 16, 32, etc, you'll have to use MUL or somehow create a table of all possible results and read them accordingly (that may be quicker than MUL).


    I don't know about division as much, but I can tell you that shifting will be quicker than DIV, I'm about to go off to work, so I cannot check up on division for now.
     
  3. Sonic master

    Sonic master Well-Known Member Member

    Joined:
    Mar 27, 2010
    Messages:
    303
    Thank you MarkeyJester I wish I would have thought about adding to register to its self twice to get the same resualts as multiplying by four. And by the way I forgot to mention what this is for I am working on a software scrolling code that as of now takes about 9 frames to scroll 896 tiles my test is a 32x28 image although I will more than likly not use an image for something that big once I realy use it for something other than a test and anyways after appying your suggestions it is going faster I understand that there are limitions on how fast vram data can be transferd mabey after I get this done I will store the whole thing in the ram and dma it on vblank. Also one more thing are there any good m69k dosc that you could recomend me I know there are more instructions that I never heard of such as the swap instruction and I am more of a beginner to m68k assembly and I would like to learn more.
     
  4. theocas

    theocas #! Member

    Joined:
    Apr 10, 2010
    Messages:
    375
    It depends very largely on the occasion - sometimes addition is faster, sometimes bit shifting is.

    Again, shifting is much faster here. The shift you propose will take 16 cycles (10+2n.) Multiplication will take you 74 cycles in this case. (It always takes 74 cycles - it's fixed.)




    Again, your shifting is much faster here. It is 14 cycles (10+2n again) whereas your division takes 144 cycles! That's about the length of half a HBlank, if I recall correctly.



    On top of using addition for things like multiplication by two, you can use something like this for a multiplication by two:





    Code:
    
    add.w d0, d0
    
    

    That takes you 8 cycles if you do use it on registers on the chip, which is faster than performing an additional bus cycle (4 more cycles when you access external memory pointed to by an address register, and another 4 if you just reference a memory address for effective address calculation)


    Hopefully that helps.
     
Thread Status:
Not open for further replies.