Michiel Helvensteijn wrote:
In any case, we will see exactly what kind of division and modulo D uses.

Given the code:

void foo()
{
    int x,y,z;
    x = y / z;
    x = y % z;

    uint ux,uy,uz;  // unsigned for C
    ux = uy / uz;
    ux = uy % ux;
}

dmd produces:

                enter   010h,0
                xor     EAX,EAX
                mov     -010h[EBP],EAX
                mov     -0Ch[EBP],EAX
                mov     EAX,-010h[EBP]
                cdq
                idiv    dword ptr -0Ch[EBP]
                mov     EAX,-010h[EBP]
                cdq
                idiv    dword ptr -0Ch[EBP]
                xor     ECX,ECX
                mov     -8[EBP],ECX
                mov     -4[EBP],ECX
                mov     EAX,-4[EBP]
                xor     EDX,EDX
                div     ECX
                mov     -8[EBP],EAX
                mov     EAX,-4[EBP]
                xor     EDX,EDX
                div     dword ptr -8[EBP]
                mov     -8[EBP],EDX
                leave
                ret

For comparison, gcc produces:

                push    EBP
                mov     EBP,ESP
                sub     ESP,038h
                mov     EAX,-8[EBP]
                mov     -034h[EBP],EAX
                mov     EDX,-034h[EBP]
                mov     EAX,EDX
                sar     EDX,01Fh
                idiv    dword ptr -0Ch[EBP]
                mov     -4[EBP],EAX
                mov     EDX,-8[EBP]
                mov     EAX,EDX
                sar     EDX,01Fh
                idiv    dword ptr -0Ch[EBP]
                mov     -4[EBP],EDX
                mov     EAX,-014h[EBP]
                mov     EDX,0
                div     dword ptr -018h[EBP]
                mov     -010h[EBP],EAX
                mov     EAX,-014h[EBP]
                mov     -038h[EBP],EAX
                mov     EAX,-038h[EBP]
                mov     EDX,0
                div     dword ptr -010h[EBP]
                mov     -010h[EBP],EDX
                leave
                ret

Producing the same results as the corresponding C compiler does is quite deliberate. I'd like to emphasize that the code generator has been in use for 25 years for several hundred thousand users, and it is incredibly unlikely that nobody would notice if it is getting basic integer arithmetic wrong. If you'd like to write up a formal definition that can be inserted into the D specification, that would be great.

Reply via email to