tags:

views:

499

answers:

4

How do I check if a computer supports SSE2 in C++, I need to do that prior installing a software that needs the support for it. Any idea? Thank you.

Edit

from what I understand, I came up with this :

bool TestSSE2(char * szErrorMsg)
{
    __try 
    {
        __asm 
        {
              xorpd xmm0, xmm0        // executing SSE2 instruction
        }
    }
        #pragma warning (suppress: 6320)
        __except (EXCEPTION_EXECUTE_HANDLER) 
        {
            if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION) 
            {
                _tcscpy_s(szErrorMsg,MSGSIZE, _T("Streaming SIMD Extensions 2(SSE2) is not supported by the CPU.\r\n Unable to launch APP"));
                return false;

            }
        _tcscpy_s(szErrorMsg,MSGSIZE, _T("Streaming SIMD Extensions 2(SSE2) is not supported by the CPU.\r\n Unable to launch APP"));
        return false;
        }   
    return true;
}

Would this work? I'm not really sure how to test, since my cpu supports it, so I don't get false from the function call.

+10  A: 

The most basic way to check for SSE2 support is by using the CPUID instruction (on platforms where it is available). Either using inline assembly or using compiler intrinsics.

Alexander Gessler
+6  A: 

You can use the _cpuid function. All is explained in the MSDN.

Patrice Bernassola
+4  A: 

Call CPUID with eax = 1 to load the feature flags in to edx. Bit 26 is set if SSE2 is available. Some code for demonstration purposes, using MSVC++ inline assembly (only for x86 and not portable!):

inline unsigned int get_cpu_feature_flags()
{
    unsigned int features;

    __asm
    {
        // Save registers
        push    eax
        push    ebx
        push    ecx
        push    edx

        // Get the feature flags (eax=1) from edx
        mov     eax, 1
        cpuid
        mov     features, edx

        // Restore registers
        pop     edx
        pop     ecx
        pop     ebx
        pop     eax
    }

    return features;
}

// Bit 26 for SSE2 support
static const bool cpu_supports_sse2 = (cpu_feature_flags & 0x04000000)!=0;
AshleysBrain
You should better use the __cpuid intrinsic, because inline assembly is no longer supported by the Microsoft AMD64 compiler.
Axel Gneiting
A: 

I found this one by accident in the MSDN:

BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );

Windows-only, but if you are not interested in anything cross-platform, very simple.

Timbo