简体   繁体   中英

Check AVX instruction set support

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM