Preventing GCC from automatically using AVX and FMA instructions when compiled with -mavx and -mfma

Violet Giraffe picture Violet Giraffe · Sep 18, 2013 · Viewed 9.8k times · Source

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 code that uses AVX checks for its availability, but GCC doesn't do it when auto-vectorizing. So if I compile with -mfma and run the code on any CPU prior to Haswell I get SIGILL. How to solve this issue?

Answer

Z boson picture Z boson · Sep 18, 2014

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