[fpc-devel] The "magic div" algorithm

Benito van der Zander benito at benibela.de
Tue Nov 16 18:52:01 CET 2021


Hi,

in my big decimal unit, I need to divide by 1000000000 all the time, 
e.g. 
https://github.com/benibela/bigdecimalmath/blob/master/bigdecimalmath.pas#L1324-L1325

would the magic div help there much?

Bye,
Benito
On 09.11.21 22:12, J. Gareth Moreton via fpc-devel wrote:
>
> This one for Marģers specifically,
>
> You'll be pleased to know that your insight has been partially 
> implemented!
>
> https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/51
>
> 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
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> 
> 	Virus-free. www.avast.com 
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> 
>
>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> _______________________________________________
> fpc-devel maillist  -fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20211116/1d2b552f/attachment.htm>


More information about the fpc-devel mailing list