divmod assembly implementation
Sam Ludacis
samludacis at mail.com
Sat Nov 23 22:09:12 UTC 2024
On Saturday, 23 November 2024 at 21:36:23 UTC, Nick Treleaven
wrote:
> On Saturday, 23 November 2024 at 19:54:36 UTC, Sam Ludacis
> wrote:
>> G'day
>>
>> I'm trying to implement the Python divmod function in D w/
>> assembly. Should look something like this...
>>
>> '''
>> ulong divmod64(ulong a, ulong b, ulong quot, ulong rem)
>> {
>> asm {
>> xor RDX, RDX;
>> mov RAX, a;
>> div b;
>> mov quot, RAX;
>> mov rem, RDX;
>> }
>> }
>> '''
>>
>> Without doubt this routine will fail. But would anyone have a
>> way?
>
> Just to mention that if requiring compiler optimization for
> fast code is acceptable, you can just write it in D e.g.:
>
> ```d
> ulong[2] div(ulong a, ulong b)
> {
> return [a / b, a % b];
> }
> ```
> ```
> $ dmd -c divmod.d -O -vasm
> _D6divmod3divFmmZG2m:
> 0000: 48 83 EC 18 sub RSP,018h
> 0004: 48 8B C6 mov RAX,RSI
> 0007: 33 D2 xor EDX,EDX
> 0009: 48 F7 F7 div RDI
> 000c: 48 89 04 24 mov [RSP],RAX
> 0010: 48 89 54 24 08 mov 8[RSP],RDX
> 0015: 48 8B 54 24 08 mov RDX,8[RSP]
> 001a: 48 8B 04 24 mov RAX,[RSP]
> 001e: 48 83 C4 18 add RSP,018h
> 0022: C3 ret
> ```
>
> Based on an older request:
> https://forum.dlang.org/post/jd97nf$1jfj$1@digitalmars.com
Thank you
More information about the Digitalmars-d-learn
mailing list