windows 确定处理器对 SSE2 的支持?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2403660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:05:47  来源:igfitidea点击:

Determine processor support for SSE2?

c++windowswindows-xpsse2

提问by DogDog

I need to do determine processor support for SSE2 prior installing a software. From what I understand, I came up with this:

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

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.

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

How do I determine processor support for SSE2?

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

采纳答案by AshleysBrain

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!):

使用 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;

回答by Timbo

I found this one by accident in the MSDN:

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

BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );

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

Windows-only,但如果你对任何跨平台的东西都不感兴趣,很简单。

回答by Alexander Gessler

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

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

回答by Patrice Bernassola

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

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