[fpc-devel] The "magic div" algorithm

J. Gareth Moreton gareth at moreton-family.com
Mon Aug 30 21:00:21 CEST 2021


Well, you'll be pleased to know that I am now back from my camping and 
hiking trip in the Lake District and have actually purchased "Hacker's 
Delight", and I have to say it's extremely insightful. I would love to 
play around and implement some of the suggested optimisations.

Some of them will require some clever insight and manipulation at the 
node level though (e.g. converting "(x mod constant) = 0" into something 
that produces fast code unique to checking to see if the division is 
exact)... the main problem is seeing through all the typecasts and the 
different loads.

Gareth aka. Kit

On 24/08/2021 20:14, Marģers . via fpc-devel wrote:
> I came up with even shorter variant of div
> example
> function teDWordDivBy7_v4( divided : dword):dword; assembler; 
> nostackframe;
> asm
>      mov ecx,divided
>      mov rax,2635249153693862181
>      mul rcx
>      mov eax,edx
> end;
>
> current version for comparison
>
> function teDWordDivBy7_v0( divided : dword):dword; assembler; 
> nostackframe;
> asm
>      mov ecx,divided
>      mov eax,613566757
>      mul ecx
>      add edx,ecx
>      rcr edx,1
>      shr edx,2
>      mov eax,edx
> end;
>
>             	
>
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20210830/07e07ad6/attachment.htm>


More information about the fpc-devel mailing list