News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Optimisation pitfall?

Started by raymond, January 22, 2005, 05:23:12 AM

Previous topic - Next topic

Mark_Larson

Ok.  I macroed it.  Won't help you for non-runtime known values.  But it runs in 53 cycles on my P3 at work.  There are still lots of things I can do to tweak it.  This is just a first pass of playing with the code.


SQRT_MAC macro uInt:req
mov edx,uInt
xor eax,eax
mov ebx,40000000h

EBX_REG = 40000000h
EAX_REG = 0
EDX_REG = uInt
REPEAT 16
add eax,ebx ;first run we can skip the add, and do a mov eax,ebx, since eax is ZERO
EAX_REG = EAX_REG + EBX_REG
; cmp eax,edx
; ja @F
if EAX_REG LE EDX_REG
sub edx,eax
add eax,ebx
EDX_REG = EDX_REG - EAX_REG
EAX_REG = EAX_REG + EBX_REG
jmp $+4 ;sub eax,ebx should be 2 bytes
endif ; if EAX_REG LE EDX_REG
;@@:
sub eax,ebx
EAX_REG = EAX_REG - EBX_REG
;sqr30:
shr eax,1
shr ebx,2
EAX_REG = EAX_REG / 2
EBX_REG = EBX_REG / 4
endm

cmp eax,edx
jae @F
inc eax
@@:
; ret
endm
BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm

Mark_Larson

I made a slight modificatoin to the code.  It now runs in 50 cycles on my P3.


SQRT_MAC macro uInt:req
mov edx,uInt
xor eax,eax
mov ebx,40000000h

EBX_REG = 40000000h
EAX_REG = 0
EDX_REG = uInt
REPEAT 16

if EAX_REG EQ 0
mov eax,ebx ;first run we can skip the add, and do a mov eax,ebx, since eax is ZERO
else
add eax,ebx ;first run we can skip the add, and do a mov eax,ebx, since eax is ZERO
endif ; if EAX_REG EQ 0

EAX_REG = EAX_REG + EBX_REG
; cmp eax,edx
; ja @F
if EAX_REG LE EDX_REG
sub edx,eax
add eax,ebx
EDX_REG = EDX_REG - EAX_REG
EAX_REG = EAX_REG + EBX_REG
jmp $+4 ;sub eax,ebx should be 2 bytes
endif ; if EAX_REG LE EDX_REG
;@@:
sub eax,ebx
EAX_REG = EAX_REG - EBX_REG
;sqr30:
shr eax,1
shr ebx,2
EAX_REG = EAX_REG / 2
EBX_REG = EBX_REG / 4
endm

cmp eax,edx
jae @F
inc eax
@@:
; ret
endm

BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm