确定SSE2的处理器支持?

时间:2010-03-08 18:22:23

标签: c++ windows windows-xp sse2

我需要在安装软件之前确定SSE2的处理器支持。根据我的理解,我想出了这个:

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;
}

这会有用吗?我不太确定如何测试,因为我的CPU支持它,所以我不会从函数调用中得到错误。

如何确定SSE2的处理器支持?

4 个答案:

答案 0 :(得分:17)

我在MSDN

中偶然发现了这个
BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );

仅限Windows,但如果您对跨平台的任何内容不感兴趣,那么非常简单。

答案 1 :(得分:11)

使用eax = 1调用CPUID以将功能标志加载到edx中。如果SSE2可用,则设置位26。一些用于演示目的的代码,使用MSVC ++内联汇编(仅适用于x86而不是可移植的!):

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;

答案 2 :(得分:10)

检查SSE2支持的最基本方法是使用CPUID指令(在可用的平台上)。使用内联汇编或使用编译器内在函数。

答案 3 :(得分:8)

您可以使用_cpuid函数。所有内容都在MSDN中解释。