Determine processor support for SSE2?

后端 未结 4 1108
囚心锁ツ
囚心锁ツ 2020-12-15 23:10

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)
         


        
相关标签:
4条回答
  • 2020-12-15 23:39

    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.

    0 讨论(0)
  • 2020-12-15 23:41

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

    0 讨论(0)
  • 2020-12-15 23:49

    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;
    
    0 讨论(0)
  • 2020-12-15 23:53

    I found this one by accident in the MSDN:

    BOOL sse2supported = ::IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
    

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

    0 讨论(0)
提交回复
热议问题