Checking if SSE is supported at runtime [duplicate

2019-03-16 19:39发布

问题:

This question already has an answer here:

  • How to check if a CPU supports the SSE3 instruction set? 5 answers
  • cpu dispatcher for visual studio for AVX and SSE 3 answers

I would like to check if SSE4 or AVX is supported at runtime, so that my program may take advantage of processor specific instructions without creating a binary for each processor.

If I could determine it at runtime, I could use an interface and switch between different instruction sets.

回答1:

GCC has a way of doing this that starts by calling __builtin_cpu_init then calling __builtin_cpu_is and __builtin_cpu_supports to check features. https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/X86-Built-in-Functions.html

On x86, when using the C++ frontend, GCC supports "function multiversioning", which allows you to write multiple versions of the function, specify the target it should be used on, and let GCC take care of making sure it is called. https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Multiversioning.html



回答2:

On MSVC, extern int __isa_available has information about the CPU support on a MSVC build.

It is used by the vectorizer in MSVC 2013 to pick what assembly to run.



标签: c++ c sse simd avx