Disable AVX2 functions on non-Haswell processors
avx2, c++, sse
Solution
If you just want to do this at compile-time then you can do this:
#ifdef __AVX2__
// AVX2 code
#elif __SSE__
// SSE code
#else
// scalar code
#endif
Note that when you compile with `gcc -mavx2 ...` then `__AVX2__` gets defined automatically. Similarly for `__SSE__`. (Note also that you can check what's pre-defined by your compiler for any given command line switching using the incantation `gcc -dM -E -mavx2 - < /dev/null`.)
If you want to do run-time dispatching though then that's a little more complicated.
Problem
I have written some AVX2 code to run on a Haswell i7 processor. The same codebase is also used on non-Haswell processors, where the same code should be replaced with their SSE equivalents. I was wondering is there a way for the compiler to ignore AVX2 instructions on non-Haswell processors. I need something like: ``` public void useSSEorAVX(...){ IF (compiler directive detected AVX2) AVX2 code (this part is ready) ELSE SSE code (this part is also ready) } } ``` Right now I am commenting out related code before compiling but there must be some more efficient way to do this. I am using Ubuntu and gcc. Thanks for your help.