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
Determine processor support for SSE2?
提问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
回答by Alexander Gessler
The most basic way to check for SSE2 support is by using the CPUID
instruction (on platforms where it is available). Either using inline assembly or using compiler intrinsics.
检查 SSE2 支持的最基本方法是使用CPUID
指令(在可用的平台上)。使用内联汇编或使用编译器内在函数。