确定SSE2的处理器支持? [英] Determine processor support for SSE2?

查看:1117
本文介绍了确定SSE2的处理器支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不太确定如何测试,因为我的CPU支持它,所以我不会从函数调用假。

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.

如何确定处理器支持SSE2?

How do I determine processor support for SSE2?

推荐答案

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

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;

这篇关于确定SSE2的处理器支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆