Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check AVX instruction set support

Tags:

c#

windows

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.

like image 539
Soonts Avatar asked Oct 28 '25 09:10

Soonts


1 Answers

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.

like image 158
Hans Passant Avatar answered Oct 31 '25 05:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!