Essentially, before using AVX2, you need to check
- if the AVX2 instruction set is supported by checking CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]=1
- if use of YMM registers is enabled by the OS
Step 1 can be done e.g. using this function:
function IsAVX2supported: boolean;
asm
// Save EBX
{$IFDEF CPUx86}
push ebx
{$ELSE CPUx64}
mov r10, rbx
{$ENDIF}
//Check CPUID.0
xor eax, eax
cpuid //modifies EAX,EBX,ECX,EDX
cmp al, 7 // do we have a CPUID leaf 7 ?
jge @Leaf7
xor eax, eax
jmp @Exit
@Leaf7:
//Check CPUID.7
mov eax, 7h
xor ecx, ecx
cpuid
bt ebx, 5 //AVX2: CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]=1
setc al
@Exit:
// Restore EBX
{$IFDEF CPUx86}
pop ebx
{$ELSE CPUx64}
mov rbx, r10
{$ENDIF}
end;
Step 2 can be done e.g. using this function:
function OSEnabledXmmYmm: boolean;
// necessary to check before using AVX, FMA or AES instructions!
asm
{$IFDEF CPUx86}
push ebx
{$ELSE CPUx64}
mov r10, rbx
{$ENDIF}
mov eax,1
cpuid
bt ecx, 27 // CPUID.1:ECX.OSXSAVE[bit 27] = 1 (XGETBV enabled for application use; implies XGETBV is an available instruction also)
jnc @not_supported
xor ecx,ecx //Specify control register XCR0 = XFEATURE_ENABLED_MASK register
db 0Fh, 01h, 0D0h // xgetbv //Reads XCR (extended control register) -> EDX:EAX
{lgdt eax = db 0Fh, 01h = privileged instruction, so don't go here unless xgetbv is allowed}
//CHECK XFEATURE_ENABLED_MASK[2:1] = ‘11b’
and eax, 06h //06h= 00000000000000000000000000000110b
cmp eax, 06h//; check OS has enabled both XMM (bit 1) and YMM (bit 2) state management support
jne @not_supported
mov eax,1
jmp @out
@not_supported:
xor eax,eax
@out:
{$IFDEF CPUx86}
pop ebx
{$ELSE CPUx64}
mov rbx, r10
{$ENDIF}
end;
Of course, you can also use this to modify the function you posted to just have a single function to call.