How to check with Intel intrinsics if AVX extensions is supported by the CPU?

Jan Stolarek picture Jan Stolarek · Jun 17, 2014 · Viewed 9k times · Source

I'm writing a program using Intel intrinsics. I want to use _mm_permute_pd intrinsic, which is only available on CPUs with AVX. For CPUs without AVX I can use _mm_shuffle_pd but according to the specs it is much slower than _mm_permute_pd. Do the header files for Intel intrinsics define constants that allow me to distinguish whether AVX is supported so that I can write sth like this:

#ifdef __IS_AVX_SUPPORTED__  // is there sth like this defined?
// use _mm_permute_pd
# else
// use _mm_shuffle_pd
#endif

? I have found this tutorial, which shows how to perform a runtime check but I need to do a static, compile-time check for the current machine.

Answer

Z boson picture Z boson · Jun 17, 2014

GCC, ICC, MSVC, and Clang all define a macro __AVX__ which you can check. In fact it's the only SIMD constant defined by all those compilers (MSVC is the one that breaks the mold). This only tells you if your code was compiled with AVX support (e.g. -mavx with GCC or /arch:AVX with MSVC) it does not tell you if your CPU supports AVX. If you want to know if the CPU supports AVX you need to check CPUID. Here, asm-in-c-error, is an example to read CPUID from all those compilers.

To do this properly I suggest you make a CPU dispatcher.

Edit: In case anyone wants to know how to use the values from CPUID to find out if AVX is available see https://github.com/Mysticial/FeatureDetector