Optimizing Dividing by 2,4,16 etc ?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Optimizing Dividing by 2,4,16 etc ?

Post by Franky »

Hi, I just played a little bit around with PB and found out, that PB optimizes

Code: Select all

  a=a*16
to

Code: Select all

   SAL dword[v_a],4

But it doesn´t do this with division:

Code: Select all

  a=a/16
Is translated to

Code: Select all

  MOV    ebx,dword [v_a]
  MOV    eax,ebx
  MOV    ebx,16
  CDQ
  IDIV   ebx
  MOV    ebx, eax
  MOV    dword [v_a],ebx

So,
1.) why first move the value of a t ebx and then move it from ebx to eax, why not directly to eax?
2.)Why not just use SAR instead of IDIV?
Give Up everything but trying!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

a = (-4) / 2
Debug a
a = (-4) >> 2
Debug a
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »


a = (-4) / 2
Debug a
a = (-4) >> 1
Debug a
;)
Give Up everything but trying!
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Trond wrote:

Code: Select all

a = (-4) / 2
Debug a
a = (-4) >> 2
Debug a

Code: Select all

a = (-4) / 2
Debug a
a = (-4) >> 1
Debug a
:wink:
Dare2 cut down to size
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

Debug -3 / 2 
Debug -3 >> 1
:twisted: :twisted:
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

:D
Dare2 cut down to size
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »

Noone makes me give up 8)

So, this is the new version, rounding the same way, as PureBasic does:


My Test brought an optimization from 25Seconds to 11 Seconds:

Code: Select all

#Compare=0

Global a,b,c.l,*adr1.long,*adr2.long,adr1.l,adr2.l
adr1=AllocateMemory(80000)
adr2=AllocateMemory(80000)

;Rechnung: c=a/8

Zeit=GetTickCount_()
   For b=1 To 99999
    CompilerIf #compare
     *adr1=adr1
    CompilerEndIf
     For a=-9999 To 9999
 !        MOV    eax,dword [v_a]
; !        MOV    ebx,dword [v_a]
 ;!        MOV    eax,ebx
 !        MOV    ebx,8
 !        CDQ
 !        IDIV   ebx
 ;!        MOV    ebx, eax
 ;!        MOV    dword [v_c],ebx
 !        MOV    dword [v_c],eax
     CompilerIf #Compare
       *adr1\l=c
       *adr1+4
     CompilerEndIf
   Next
 Next
zeit2=GetTickCount_()
   For b=1 To 99999
     CompilerIf #compare
      *adr2=adr2
     CompilerEndIf
;    CallDebugger
    For a=-9999 To 9999
!        MOV    ebx,dword [v_a]
!         SAR    ebx,3
!        MOV    dword [v_c],ebx
!         CMP    ebx,0
!         JNL l___div_end
!            SAL ebx,3
!           CMP dword[v_a],ebx
!            JE l___div_end
!               INC dword[v_c]
         __div_end:
     CompilerIf #compare
         *adr2\l=c
         *adr2+4
     CompilerEndIf
  Next
 Next
zeit3=GetTickCount_()
 
CompilerIf #compare
*adr1=adr1
*adr2=adr2
dif=0
For a=0 To 19998
     If *adr1\l<>*adr2\l
            INC dif
           Debug Str(a-9999)+"/8="+Str(*adr1\l)+"   SAR a,3="+Str(*adr2\l)+"  %8="+Str(a%8)
     EndIf 
     *adr1+4
     *adr2+4
Next 
CompilerEndIf 
MessageRequester("Ergebnis:","Freds Method: "+Str(zeit2-zeit)+"ms"+Chr(13)+"My Method: "+Str(zeit3-zeit2)+"ms"+Chr(13)+"Menge der Unterschiede: "+Str(dif))
Give Up everything but trying!
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@Franky:
respect! *thumbs up*
i would like to see such speed optimisations directly done by the compiler... your code is surely a nice contribution to the pb team
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

...and what makes you think that SAR or SAL is more optimized instruction than IDIV, or DIV or IMUL or MUL :?:
Don't be so sure that it is faster in modern processors :!:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Post Reply