2010-03-08 41 views
14

在安裝軟件之前,我需要確定處理器對SSE2的支持。據我所知,我想出了這個:確定處理器對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的支持?

+0

任何特定的操作系統? –

+0

Windows xp或更高版本。 – DogDog

+0

就我所知,您的代碼應該正常工作。我更喜歡CPUID方式,因爲它更靈活一些,並且還可以訪問大量其他CPU能力標誌。 –

回答

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; 
+5

您應該更好地使用__cpuid內在函數,因爲Microsoft AMD64編譯器不再支持內聯彙編。 –

+0

是否有編譯時需要檢查的內容,以瞭解SSE指令設置處理器的可用性? SSE3,SSE4,SSE4.1等... – Alex

10

檢查SSE2支持的最基本方法是使用CPUID指令(在可用的平臺上)。使用內聯彙編或使用編譯器內在函數。

8

您可以使用_cpuid函數。全部在MSDN中解釋。

16

我發現這一個意外的MSDN

BOOL sse2supported = ::IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE); 

只有Windows,但如果你對任何跨平臺都不感興趣,很簡單。

+0

真棒!感謝分享。 – cruppstahl