[fpc-devel] The "magic div" algorithm

J. Gareth Moreton gareth at moreton-family.com
Tue Nov 9 22:12:49 CET 2021

This one for Marģers specifically,

You'll be pleased to know that your insight has been partially implemented!


This only expands 32-bit divisions to 64-bit, since the smaller sizes 
requires more work at the node level, but is certainly possible and will 
happily continue to experiment with seeing if it can be implemented.  
This was fun to do!

The test "bench/bdiv.pp" (also "tests/test/cg/tmoddiv6.pp" which just 
includes the given file) is my benchmark test for division and modulus 
operations if anyone wants to see if they can make the fundamental 
routine faster on any platform.

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20211109/554c605d/attachment.htm>

More information about the fpc-devel mailing list