When will you implement cent and ucent?

Walter Bright newshound2 at digitalmars.com
Thu Mar 31 06:31:32 UTC 2022


On 3/28/2022 12:35 PM, deadalnix wrote:
> Now let's see what we can get with clang and __int128_t, same backend so the 
> comparison is fair:
> 
> ```cpp
> __uint128_t foobar(__uint128_t a, __uint128_t b) {
>      return a * b;
> }
> ```
> 
> codegen:
> ```asm
> foobar(unsigned __int128, unsigned __int128):        # @foobar(unsigned 
> __int128, unsigned __int128)
>          mov     r8, rdx
>          mov     rax, rdx
>          mul     rdi
>          imul    rsi, r8
>          add     rdx, rsi
>          imul    rcx, rdi
>          add     rdx, rcx
>          ret
> ```
> 
> Why do I even have to argue that case?

You see a similar thing when 32 bit compilation does 64 bit arithmetic:

ulong foobar(ulong x, ulong y)
{
     return x * y;
}

dmd -c test.d -vasm -m32 -O
_D4test6foobarFmmZm:
0000:   53                       push      EBX
0001:   8B 54 24 14              mov       EDX,014h[ESP]
0005:   8B 44 24 10              mov       EAX,010h[ESP]
0009:   8B 4C 24 0C              mov       ECX,0Ch[ESP]
000d:   8B 5C 24 08              mov       EBX,8[ESP]
0011:   0F AF C8                 imul      ECX,EAX
0014:   0F AF D3                 imul      EDX,EBX
0017:   03 CA                    add       ECX,EDX
0019:   F7 E3                    mul       EBX
001b:   03 D1                    add       EDX,ECX
001d:   5B                       pop       EBX
001e:   C2 10 00                 ret       010h

So, yeah, supporting 128 bit arithmetic in the codegen has significant 
advantages. 3 multiplies and 2 adds.

Having the compiler recognize the `mul` function as a builtin would enable this 
code gen for 128 bits.


More information about the Digitalmars-d mailing list