Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the availability of SSE/SSE2 instruction set in Visual Studio

How can I check in code whether SSE/SSE2 is enabled or not by the Visual Studio compiler?

I have tried #ifdef __SSE__ but it didn't work.

like image 307
user2202420 Avatar asked Sep 01 '13 23:09

user2202420


3 Answers

Some additional information on _M_IX86_FP.

_M_IX86_FP is only defined for 32-bit code. 64-bit x86 code has at least SSE2. You can use _M_AMD64 or _M_X64 to determine if the code is 64-bit.

#ifdef __AVX2__
//AVX2
#elif defined ( __AVX__ )
//AVX
#elif (defined(_M_AMD64) || defined(_M_X64))
//SSE2 x64
#elif _M_IX86_FP == 2
//SSE2 x32
#elif _M_IX86_FP == 1
//SSE x32
#else
//nothing
#endif
like image 129
Z boson Avatar answered Oct 17 '22 23:10

Z boson


From the documentation:

_M_IX86_FP

Expands to a value indicating which /arch compiler option was used:

  • 0 if /arch:IA32 was used.
  • 1 if /arch:SSE was used.
  • 2 if /arch:SSE2 was used. This value is the default if /arch was not specified.

I don't see any mention of _SSE_.

like image 26
Carl Norum Avatar answered Oct 17 '22 22:10

Carl Norum


The relevant preprocessor macros have two underscores at each end:

#ifdef __SSE__

#ifdef __SSE2__

#ifdef __SSE3__

#ifdef __SSE4_1__

#ifdef __AVX__

...etc...

UPDATE: apparently the above macros are not automatically pre-defined for you when using Visual Studio (even though they are in every other x86 compiler that I have ever used), so you may need to define them yourself if you want portability with gcc, clang, ICC, et al...

like image 4
Paul R Avatar answered Oct 18 '22 00:10

Paul R