8

How can I check using Delphi 2007 that a box is AVX capable.

My question is only restricted to querying the support in the CPU (Assumption is made that the OS is OK / Windows 7 with SP1).

The PDF document entitled Introduction to Intel® Advanced Vector Extensions by Chris Lomont explains how to do it and provides an example code implementation but in c++.

It's also available at this page.

PhiS
  • 4,540
  • 25
  • 35
menjaraz
  • 7,551
  • 4
  • 41
  • 81

1 Answers1

13

Here's a translation of the assembler code given on an Intel blog:

function isAvxSupported: Boolean;
asm
{$IFDEF CPUX86}
    push ebx
{$ENDIF}
{$IFDEF CPUX64}
    mov r10, rbx
{$ENDIF}
    xor eax, eax
    cpuid
    cmp eax, 1
    jb @not_supported
    mov eax, 1
    cpuid
    and ecx, 018000000h
    cmp ecx, 018000000h
    jne @not_supported
    xor ecx, ecx
    db 0Fh, 01h, 0D0h //XGETBV
    and eax, 110b
    cmp eax, 110b
    jne @not_supported
    mov eax, 1
    jmp @done
@not_supported:
    xor eax, eax
@done:
{$IFDEF CPUX86}
    pop ebx
{$ENDIF}
{$IFDEF CPUX64}
    mov rbx, r10
{$ENDIF}
end;

This code will work in both 32 and 64 bit versions of Delphi.

Update: Register saving code added thanks to @PhiS.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you David ! My understanding of the piece of code is that the `XGETBV` instruction is not supported by Delphi, is that so ? – menjaraz Mar 30 '12 at 11:55
  • That's right. I found the magic db here: http://qc.embarcadero.com/wc/qcmain.aspx?d=80128 – David Heffernan Mar 30 '12 at 11:56
  • Good lucking porting this to X64 alone. :-) – Warren P Mar 30 '12 at 13:21
  • I think it's better with the X64 version. If someone asks the same question for X64 I'd rather have your upvotes all in one place for the same topic. – Warren P Mar 30 '12 at 13:27
  • @Warren Actually the x64 version is identical since x86 register calling convention returns values in EAX, and x64 calling convention returns values in RAX. And we are only using EAX/ECX registers which are both volatile on both ABIs. – David Heffernan Mar 30 '12 at 14:04
  • 3
    Brilliant! Wish I could +1 2x. – Warren P Mar 30 '12 at 14:13
  • 1
    David, this code will only _almost_ work on x64 and x86. The thing is CPUID also sets EBX, so you need to save/retore EBX (since Delphi requires this). Now if you do that by PUSH/POP EBX, that will not compile on Win64, because it expects PUSH + reg64 ... – PhiS Apr 03 '12 at 07:13
  • ... so you'll probably have to IFDEF that. I'd recommend to save RBX in an unused register on X64, e.g. in R10 – PhiS Apr 03 '12 at 07:20
  • I run the code using D2007, it crashes on machine that does not support AVX. However, if the code is compiled at XE8-32bit, it works fine – justyy Nov 07 '15 at 01:16
  • i found out the the compiler directive CPUX86, CPUX64 is added after XE2 http://docwiki.embarcadero.com/RADStudio/Seattle/en/Conditional_compilation_(Delphi) – justyy Nov 07 '15 at 01:18