Dectecting support for processor functions (e.g. sse2, fma4, etc.)

I have code that depends on CPU and OS support for various CPU functions.

In particular, I need to check SIMD instruction set support. Namely sse2 , avx , avx2 , fma4 and neon . ( neon is an ARM SIMD function. I'm less interested in this, given the fewer ARM end users.)

Now I am doing the following:

 function cpu_flags() if is_linux() cpuinfo = readstring(`cat /proc/cpuinfo`); cpu_flag_string = match(r"flags\t\t: (.*)", cpuinfo).captures[1] elseif is_apple() sysinfo = readstring(`sysctl -a`); cpu_flag_string = match(r"machdep.cpu.features: (.*)", cpuinfo).captures[1] else @assert is_windows() warn("CPU Feature detection does not work on windows.") cpu_flag_string = "" end split(lowercase(cpu_flag_string)) end 

This has two drawbacks:

  • It does not work on Windows
  • I'm just not sure if this is correct; This is true? Or will it go bad if, for example, the OS is disabled, but physically the processor supports it?

So my questions are:

  • How can I do this work on windows.
  • Is this the right way, or even an OK way to get this information?

This is part of the build script (with BinDeps.jl ); so I need a solution that is not related to opening a GUI. And ideally, this is one that does not add third-party addiction. Extracting information from GCC will work in some way, since I already require that GCC compile some shared libraries. (selection of those libraries that this code is to detect a set of commands for)

+6
source share
1 answer

I'm just not sure if this is correct; This is true? Or will it go bad if, for example, the OS is disabled, but physically the processor supports it?

I do not think that the OS has the right to talk about disabling vector instructions; I saw how the BIOS can disable material (in particular, virtualization extensions), but in this case you will not even find them in /proc/cpuinfo - such is its point :-).

Extracting information from GCC will somehow work, as I already require GCC to collect some shared libraries

If you always have gcc (MinGW on Windows), you can use __builtin_cpu_supports :

 #include <stdio.h> int main() { if (__builtin_cpu_supports("mmx")) { printf("\nI got MMX !\n"); } else printf("\nWhat ? MMX ? What is that ?\n"); return (0); } 

and apparently these built-in functions work with mingw-w64 too.

AFAIK uses the CPUID instruction to extract the relevant information (therefore, it should well reflect the environment in which your code will work).

(from fooobar.com/questions/407681 / ... )

+2
source

All Articles