Asm and CTFE

bearophile bearophileHUGS at lycos.com
Sat Nov 12 04:33:28 PST 2011


This code doesn't compile with the latest DMD 2.057head:

import std.math: poly;
enum p = poly(2.0, [-2, 3, 5]);
void main() {}


The error:
...\dmd2\src\phobos\std\math.d(3752): Error: asm statements cannot be interpreted at compile time
test.d(2):        called from here: poly(2L,[-2L,3L,5L])


The code of poly is something like this:


real poly(real x, const real[] A) pure {
    version (D_InlineAsm_X86) {
        version (Windows) {
            asm { // assembler by W. Bright
                mov     ECX,A[EBP];
                dec     ECX;
                lea     EDX,[ECX][ECX*8];
                add     EDX,ECX;
                add     EDX,A+4[EBP];
                fld     real ptr [EDX];
                jecxz   return_ST;
                fld     x[EBP];
                fxch    ST(1);
                align   4;
        L2:     fmul    ST,ST(1);
                fld     real ptr -10[EDX];
                sub     EDX,10;
                faddp   ST(1),ST;
                dec     ECX;
                jne     L2;
                fxch    ST(1);
                fstp    ST(0);
                align   4;
        return_ST:;
            }
        } else
            static assert(0);
    } else {
        sizediff_t i = A.length - 1;
        real r = A[i];
        while (--i >= 0) {
            r *= x;
            r += A[i];
        }
        return r;
    }
}


So there is code able to run at compile time, but D_InlineAsm_X86 is set at compile time too, despite CTFE is not able to run assembly code. In Phobos there are other similar cases where not-assembly fallback code is available, but it can't be used by CTFE.

Do you know how to improve this situation, avoiding messy code too?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list