Inline assembly and CTFE

Walter Bright newshound2 at digitalmars.com
Thu Oct 3 15:40:13 PDT 2013


On 10/3/2013 3:25 PM, Joseph Cassman wrote:
> As of DMD 2.063.2 it looks to me like inline assembly language blocks are not
> usable in compile-time code. For example, the following code
>
>    void main() {
>      enum a = abc();
>    }
>    ulong abc() {
>      asm { mov RAX,1; }
>      return 1;
>    }
>
> will produce the error "Error: asm statements cannot be interpreted at compile
> time".
>
> Are there plans to eventually support the use of asm blocks in CTFE?

No. Simulating a CPU is way, way beyond its charter.


> If it were possible then I could use such a function in a static if test. What I
> am looking to do is use a function that calls CPUID (etc.) in a static if
> statement to determine which code to generate, somewhat like the following.
>
>    bool isAVXPossible() { asm { ... use CPUID, etc. ... } }
>    void foo() {
>      static if(isAVXPossible()) {
>        asm { ... generate AVX version ... }
>      else {
>        asm { ... generate non AVX version ... }
>      }
>    }
>
> I cannot use version(D_SIMD) because I need some more specificity in the
> versioning. Similarly, version(X86) and version(X86_64) are convenient but I
> would like to be more exact. Also, I tried the functionality in core.cpuid but
> it also seems only able to execute at run-time. When trying to use it in a
> static if statement I get the error "Error: static variable xfeatures cannot be
> read at compile time".
>
> Any ideas on how to implement isAVXPossible() and its related functionality
> without using a runtime test are appreciated.


But you don't want a compile time test for that! You want a runtime test, as it 
should depend on what machine the program is running under, which may not at all 
be the same one it compiled on.



More information about the Digitalmars-d mailing list