9

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.

Jan Stolarek
  • 1,409
  • 1
  • 11
  • 21
  • `_mm_permute_pd` (`vpermilpd`) is not faster than `shufpd dest,same,same`, unless it allows a memory source operand to fold into the instruction. See http://agner.org/optimize/ for instruction tables, etc. – Peter Cordes Mar 23 '16 at 03:02

3 Answers3

7

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

Community
  • 1
  • 1
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • If you want GCC / clang to define feature-macros like `__AVX__` *according to whether your computer has them or not*, you have to compile with `-march=native` – Peter Cordes Apr 26 '21 at 04:51
5

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.

vharavy
  • 4,881
  • 23
  • 30
  • GCC supports `-mtune=native` to tune the instruction set depending on the current CPU, maybe there's something similar for ICC? – peppe Jun 17 '14 at 11:10
  • Yes, there is `-xHost` for MacOS and Linux and `/QxHost` for Windows. There are also various options like `-march`, `-arch` etc. – vharavy Jun 17 '14 at 11:25
0

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.

Roy Longbottom
  • 1,192
  • 1
  • 6
  • 8