Home > Backend Development > C++ > Is the Provided Code Sufficient for Checking SSE3 Support on CPUs?

Is the Provided Code Sufficient for Checking SSE3 Support on CPUs?

Susan Sarandon
Release: 2024-11-16 16:53:03
Original
410 people have browsed it

Is the Provided Code Sufficient for Checking SSE3 Support on CPUs?

Checking SSE3 Support on CPUs

The Question

Is the following code sufficient to verify if a CPU supports the SSE3 instruction set?

bool CheckSSE3()
{
    int CPUInfo[4] = {-1};

    //-- Get number of valid info ids
    __cpuid(CPUInfo, 0);
    int nIds = CPUInfo[0];

    //-- Get info for id "1"
    if (nIds >= 1)
    {
        __cpuid(CPUInfo, 1);
        bool bSSE3NewInstructions = (CPUInfo[2] & 0x1) || false;
        return bSSE3NewInstructions;     
    }

    return false;      
}
Copy after login

The Answer

Unfortunately, the provided code snippet has a limitation on Windows XP. To overcome this and ensure accurate detection of SSE3 support across a wider range of systems, a more comprehensive solution is recommended.

Below is an approach that provides reliable SSE3 detection on both Windows XP and other operating systems:

// Access CPUID instruction
#ifdef _WIN32
#define cpuid(info, x)    __cpuidex(info, x, 0)
#else
#include <cpuid.h>
void cpuid(int info[4], int InfoType){
    __cpuid_count(InfoType, 0, info[0], info[1], info[2], info[3]);
}
#endif

// Detect SSE3 support
bool HW_SSE3;
int info[4];
cpuid(info, 0);
int nIds = info[0];
if (nIds >= 0x00000001){
    cpuid(info,0x00000001);
    HW_SSE3   = (info[2] &amp; ((int)1 <<  0)) != 0;
}
Copy after login

The above is the detailed content of Is the Provided Code Sufficient for Checking SSE3 Support on CPUs?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template