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

自闭症网瘾萝莉.ら 提交于 2019-12-22 05:13:34

问题


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.


回答1:


I assume you are using Intel C++ Compiler. In this case - yes, there are such macros: Intel C++ Compiler Reference Guide: __AVX__, __AVX2__.

P.S. Be aware that if you compile you application with AVX instruction set enabled it will fail on CPUs not supporting AVX. If you are going to distribute your software as source code package and compile on target machine - this is may be a viable solution. Otherwise you should check for AVX dynamically.

P.P.S. There are several options for ICC. Take a look at the following compiler options and also references from it to other.




回答2:


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




回答3:


It seems to me that the only way is to compile and run a program that identifies whether AVX is available. Then manually or automatically compile separate code with or without AVX functions. For VS 2013, I would used my code in commomAVX folder in the following to identify hasAVX (or not) and use this to execute one of two different BAT files to compile and link the appropriate program.

http://www.roylongbottom.org.uk/gigaflops-benchmarks.zip

My question was to help to identify a solution regarding the use of suitable compile options such as /arch:AVX.



来源:https://stackoverflow.com/questions/24260490/how-to-check-with-intel-intrinsics-if-avx-extensions-is-supported-by-the-cpu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!