24

For the GCC CFLAGS options: -msse, -msse2, -mssse3, -msse4, -msse4.1, -msse4.2. Are they exclusive in their use or can they be used together?

My understanding is that the choosing which to set depends on whether the target arch, which the program will run on, supports it or not - is this correct?

If so, how could I know what sse my target arch supports? In Linux, I cat /proc/cpuinfo, but what if Mac or Windows?

Thanks!

mike65535
  • 483
  • 2
  • 10
  • 21
yaya
  • 351
  • 1
  • 2
  • 3
  • SSE instructions have been enhanced/modified over various generations of CPUs. compiling for a 4.2 target precludes using the generated code on olde generations. – Marc B May 21 '12 at 13:58
  • thank you for the 1st reply. so what should I choose? just the latest one(is it -msse4.2)? – yaya May 21 '12 at 14:08
  • 1
    Unless you have a specific SIMD requirement then just compile for lowest common denominator, i.e. leave out the -msse switches. – Paul R May 21 '12 at 14:13
  • @PaulR I agree for `-msse` options, even code with special support will only see small benefits. But for the closely related `-mavx` this may be different: Even very ordinary non vectorized floating point intensive code may benefit from the three operand syntex in AVX. – Gunther Piez May 21 '12 at 14:50

1 Answers1

38

The -m switched can be used in parallel, furthermore some of them are implied by the architecture or other switches. For instance, if you build code for x86_64, -msse -msse2 is always enabled.

For code intended to run on your system you should choose -march=native, which will select what is available on your processor. For instance, if you have a Sandy Bridge, this will enable -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -mavx.

If you want to specify in detail which instruction set to use, you should only use what is available, not always the "latest". The "latest" one is currently -mavx2, which I don't recommend: The first processor which will support it will be available in 2013.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • What's the point of `msse4`? As far as I can tell it is the same as `msse4.2`. Maybe it creates a macro `__SSE4__`? It seems like it just causes confusion. – Z boson Mar 29 '16 at 09:47
  • 1
    @Zboson I don't know what the point is. You should ask that on the gcc mailing list, there is indeed no difference at all (yet?). You can check that with something like `gcc -dM -E -msse4.2 - 1;gcc -dM -E -msse4 - 2;diff 1 2` – Gunther Piez Mar 30 '16 at 12:39
  • Do you have sources for the statement, that `-msse -msse2` is implied in x86_64? – steffen Sep 26 '18 at 14:32
  • 3
    @steffen "For the x86-32 compiler, you must use -march=cpu-type, -msse or -msse2 switches to enable SSE extensions and make this option effective. For the x86-64 compiler, these extensions are enabled by default." ([source](https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/x86-Options.html)) – user1494080 Aug 19 '19 at 11:19