在安装软件之前,我需要确定处理器对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支持它,所以我不会从函数调用中得到false。
如何确定处理器对SSE2的支持?
发布于 2010-03-09 07:09:01
使用eax =1调用CPUID,将功能标志加载到edx中。如果SSE2可用,则位26置1。一些用于演示目的的代码,使用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-17 23:45:55
我在MSDN里偶然发现了这个
BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );仅适用于Windows,但如果您对任何跨平台的东西不感兴趣,则非常简单。
发布于 2010-03-09 02:25:55
检查SSE2支持的最基本方法是使用CPUID指令(在可用的平台上)。使用内联程序集或使用编译器内部函数。
https://stackoverflow.com/questions/2403660
复制相似问题