Determine processor support for SSE2?
Asked Answered
N

4

14

I need to do determine processor support for SSE2 prior installing a software. 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.

How do I determine processor support for SSE2?

Nordine answered 8/3, 2010 at 18:22 Comment(4)
Any particular operating system ?Booklover
Your code should work fine as far as I can tell. I'd prefer the CPUID way because it's a bit more flexible and gives you access to a whole bunch of other CPU capability flags as well.Sopping
You could run a virtualized OS, and have it emulate a non-SSE2 CPU. It might not be fast, but it should be accurate, no?Sardius
Processor support and OS support for SSE2 are two different things. The processor may support SSE2 so the proper bit is set after CPUID. However, the OS may not have enabled it (I think it needs to enable MMX extensions). For CPU support, Ashley provided the answer. For OS support under Windows, Timbo provided the answer. I have not seen the answer for OS support under Linux. I think these are GCC tests for SSE2 OS support: sse-6.c source file.Afrikaans
S
11

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;
Slotter answered 8/3, 2010 at 23:9 Comment(2)
You should better use the __cpuid intrinsic, because inline assembly is no longer supported by the Microsoft AMD64 compiler.Buryat
Is there a guide on what to check at compilation time to know what SSE instruction sets a processor has available? SSE3, SSE4, SSE4.1 etc...Barham
D
17

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.

Diandiana answered 17/3, 2010 at 15:45 Comment(0)
S
11

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;
Slotter answered 8/3, 2010 at 23:9 Comment(2)
You should better use the __cpuid intrinsic, because inline assembly is no longer supported by the Microsoft AMD64 compiler.Buryat
Is there a guide on what to check at compilation time to know what SSE instruction sets a processor has available? SSE3, SSE4, SSE4.1 etc...Barham
S
10

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.

Sopping answered 8/3, 2010 at 18:25 Comment(0)
G
8

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

Garcon answered 8/3, 2010 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.