In C#, how to check if current CPU and OS support AVX instruction set?
I need to choose which native DLL to load, SSE2 or AVX.
Best way is to pinvoke GetEnabledXStateFeatures(), it ensures that both the processor and the OS support AVX:
public static bool HasAvxSupport() {
    try {
        return (GetEnabledXStateFeatures() & 4) != 0;
    }
    catch {
        return false;
    }
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern long GetEnabledXStateFeatures();
No decent way to distinguish between AVX and AVX2 btw, luckily you didn't ask for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With