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

user2202420 picture user2202420 · Sep 2, 2013 · Viewed 9.2k times · Source

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.

Answer

Z boson picture Z boson · Sep 2, 2013

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