确定SSE2的处理器支持?

Dog*_*Dog 14 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

Tim*_*mbo 17

我在MSDN中偶然发现了这个:

BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
Run Code Online (Sandbox Code Playgroud)

仅限Windows,但如果您对任何跨平台不感兴趣,非常简单.


Ash*_*ain 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;
Run Code Online (Sandbox Code Playgroud)

  • 您最好使用__cpuid内在函数,因为Microsoft AMD64编译器不再支持内联汇编. (5认同)

Ale*_*ler 10

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


Pat*_*ola 8

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