14

在安装软件之前,我需要确定处理器对 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

4 回答 4

17

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

BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );

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

于 2010-03-17T15:45:55.147 回答
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;
于 2010-03-08T23:09:00.813 回答
10

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

于 2010-03-08T18:25:55.083 回答
8

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

于 2010-03-08T18:28:54.860 回答