inline functions

bearophile bearophileHUGS at lycos.com
Sat Mar 26 05:37:10 PDT 2011


This little test program:


struct Vector(T) {
    T[3] data;

    T dot(const ref Vector o) {
        return data[0] * o.data[0] +
               data[1] * o.data[1] +
               data[2] * o.data[2];
    }

    T lengthSquaredSlow() {
        return dot(this);
    }

    T lengthSquaredFast() {
        return data[0] * data[0] +
               data[1] * data[1] +
               data[2] * data[2];
    }
}

Vector!double v;
void main() {}

The assembly, DMD 2.052, -O -release -inline:


dot (T=double):
        push    EBX
        mov EDX,EAX
        mov EBX,8[ESP]
        fld qword ptr 010h[EDX]
        fld qword ptr [EDX]
        fxch    ST1
        fmul    qword ptr 010h[EBX]
        fxch    ST1
        fld qword ptr 8[EDX]
        fxch    ST1
        fmul    qword ptr [EBX]
        fxch    ST1
        fmul    qword ptr 8[EBX]
        faddp   ST(1),ST
        faddp   ST(1),ST
        pop EBX
        ret 4

lengthSquaredSlow (T=double):
        mov ECX,EAX
        fld qword ptr 010h[EAX]
        fld qword ptr [ECX]
        fxch    ST1
        fmul    qword ptr 010h[ECX]
        fxch    ST1
        fld qword ptr 8[ECX]
        fxch    ST1
        fmul    qword ptr [ECX]
        fxch    ST1
        fmul    qword ptr 8[ECX]
        faddp   ST(1),ST
        faddp   ST(1),ST
        ret

lengthSquaredFast (T=double):
        mov ECX,EAX
        fld qword ptr 010h[EAX]
        fld qword ptr [ECX]
        fxch    ST1
        fmul    qword ptr 010h[ECX]
        fxch    ST1
        fld qword ptr 8[ECX]
        fxch    ST1
        fmul    qword ptr [ECX]
        fxch    ST1
        fmul    qword ptr 8[ECX]
        faddp   ST(1),ST
        faddp   ST(1),ST
        ret

The fast and slow versions seem to be compiled to the same code. So please, show a D code example where there is some difference.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list