How can I disable auto-vectorization with AVX and FMA instructions? I would still prefer the compiler to employ SSE and SSE2 automatically, but not FMA and AVX.
My c
What you want to do is compile different object files for each instruction set you are targeting. Then create a cpu dispatcher which asks CPUID for the available instruction set and then jumps to the appropriate version of the function.
I already described this in several different questions and answers
disable-avx2-functions-on-non-haswell-processors
do-i-need-to-make-multiple-executables-for-targetting-different-instruction-set
how-to-check-with-intel-intrinsics-if-avx-extensions-is-supported-by-the-cpu
cpu-dispatcher-for-visual-studio-for-avx-and-sse
create-separate-object-files-from-the-same-source-code-and-link-to-an-executable
You will need to separate the code that uses AVX into a separate compile unit (in other words, a separate .cpp file), and compile only that with -mfma
or whatever options you want. Normally, gcc
will use -march=native
, so it will compile for "your processor", and if you want generic code, you will need to use -march=x86_64
or -march=core2
, or something like that.