How to enable SSSE3 intrinsics but disable their use in compiler optimization

3.9k views Asked by At

I have a code that uses SSSE3 intrinsic commands (note the triple S) and a runtime check whether to use it, therefore I assumed that the application should execute on CPUs without SSSE3 support. However, when using -mssse3 with -O1 optimization the compiler also inserts SSSE3 instructions which I didn't explicitly call, hence the program crashes.

Is there a way to enable SSSE3 code when I EXPLICITLY call the relevant intrinsic functions, but to stop the compiler from adding its own SSSE3 code?

Note that I cannot disable the -O1 optimization.

2

There are 2 answers

1
Mats Petersson On BEST ANSWER

The solution to this problem is to NOT compile ALL the program code with the -mssse3 option, and only compile the portion that actually uses these features with that option. In other words:

 // main.cpp
 ... 

     if (use_ssse3()) 
         do_something_ssse3();
     else
         do_something_traditional();

 // traditional.cpp:
 void do_something_traditional()
 {
     ... 
     code goes here ... 
 }

 // ssse3.cpp:
 void do_something_ssse3()
 {
     ... 
     code goes here ... 
 }

Only "ssse3.cpp" should be compiled with the -mssse3 flag.

1
Gunther Piez On

If you use gcc, you can just compile the code without using the -mssse3 switch, and pull in the SSSE3 intrinsics with

#define __SSSE3__ 1
#include <tmmintrin.h>

where you need them.