C++でCPU機能を読み出す

CPUの機能を読み出し、それを表示してみましょう。

#include <iostream>
#include <bitset>
#include <intrin.h>

using namespace std;

enum _ECX       // CpuId(1, 0)
{
    SSE3 = 0,
    PCLMULQDQ = 1,
    DTES64 = 2,
    MONITOR = 3,
    DS_CPL = 4,
    VMX = 5,
    SMX = 6,
    EST = 7,
    TM2 = 8,
    SSSE3 = 9,
    CNXT_ID = 10,
    SDBG = 11,
    FMA = 12,
    CX16 = 13,
    XTPR = 14,
    PDCM = 15,
    PCID = 17,
    DCA = 18,
    SSE41 = 19,
    SSE42 = 20,
    X2APIC = 21,
    MOVBE = 22,
    POPCNT = 23,
    TSC_DEADLINE = 24,
    AES = 25,
    XSAVE = 26,
    OSXSAVE = 27,
    AVX = 28,
    F16C = 29,
    RDRND = 30,
    HYPERVISOR = 31
};

enum _EDX       // CpuId(1, 0)
{
    FPU = 0,
    VME = 1,
    DE = 2,
    PSE = 3,
    TSC = 4,
    MSR = 5,
    PAE = 6,
    MCE = 7,
    CX8 = 8,
    APIC = 9,
    SEP = 11,
    MTRR = 12,
    PGE = 13,
    MCA = 14,
    CMOV = 15,
    PAT = 16,
    PSE_36 = 17,
    PSN = 18,
    CLFSH = 19,
    DS = 21,
    ACPI = 22,
    MMX = 23,
    FXSR = 24,
    SSE = 25,
    SSE2 = 26,
    SS = 27,
    HTT = 28,
    TM = 29,
    IA64 = 30,
    PBE = 31
};

enum _EBX       // CpuId(7, 0)
{
    AVX2 = 5,
    AVX512F = 16
};

struct Flags
{
    string isa_feature;
    int    bit;
};

Flags edx[] = { { "FPU          ", _EDX::FPU },
                { "VME          ", _EDX::VME },
                { "DE           ", _EDX::DE },
                { "PSE          ", _EDX::PSE },
                { "TSC          ", _EDX::TSC },
                { "MSR          ", _EDX::MSR },
                { "PAE          ", _EDX::PAE },
                { "MCE          ", _EDX::MCE },
                { "CX8          ", _EDX::CX8 },
                { "APIC         ", _EDX::APIC },
                { "SEP          ", _EDX::SEP },
                { "MTRR         ", _EDX::MTRR },
                { "PGE          ", _EDX::PGE },
                { "MCA          ", _EDX::MCA },
                { "CMOV         ", _EDX::CMOV },
                { "PAT          ", _EDX::PAT },
                { "PSE-36       ", _EDX::PSE_36 },
                { "PSN          ", _EDX::PSN },
                { "CLFSH        ", _EDX::CLFSH },
                { "DS           ", _EDX::DS },
                { "ACPI         ", _EDX::ACPI },
                { "MMX          ", _EDX::MMX },
                { "FXSR         ", _EDX::FXSR },
                { "SSE          ", _EDX::SSE },
                { "SSE2         ", _EDX::SSE2 },
                { "SS           ", _EDX::SS },
                { "HTT          ", _EDX::HTT },
                { "TM           ", _EDX::TM },
                { "IA64         ", _EDX::IA64 },
                { "PBE          ", _EDX::PBE }
};

Flags ecx[] = { { "SSE3         ",  _ECX::SSE3 },
                { "PCLMULQDQ    ",  _ECX::PCLMULQDQ },
                { "DTES64       ",  _ECX::DTES64 },
                { "MONITOR      ",  _ECX::MONITOR },
                { "DS_CPL       ",  _ECX::DS_CPL },
                { "VMX          ",  _ECX::VMX },
                { "SMX          ",  _ECX::SMX },
                { "EST          ",  _ECX::EST },
                { "TM2          ",  _ECX::TM2 },
                { "SSSE3        ",  _ECX::SSSE3 },
                { "CNXT_ID      ",  _ECX::CNXT_ID },
                { "SDBG         ",  _ECX::SDBG },
                { "FMA          ",  _ECX::FMA },
                { "CX16         ",  _ECX::CX16 },
                { "XTPR         ",  _ECX::XTPR },
                { "PDCM         ",  _ECX::PDCM },
                { "PCID         ",  _ECX::PCID },
                { "DCA          ",  _ECX::DCA },
                { "SSE41        ",  _ECX::SSE41 },
                { "SSE42        ",  _ECX::SSE42 },
                { "X2APIC       ",  _ECX::X2APIC },
                { "MOVBE        ",  _ECX::MOVBE },
                { "POPCNT       ",  _ECX::POPCNT },
                { "TSC_DEADLINE ",  _ECX::TSC_DEADLINE },
                { "AES          ",  _ECX::AES },
                { "XSAVE        ",  _ECX::XSAVE },
                { "OSXSAVE      ",  _ECX::OSXSAVE },
                { "AVX          ",  _ECX::AVX },
                { "F16C         ",  _ECX::F16C },
                { "RDRND        ",  _ECX::RDRND },
                { "HYPERVISOR   ",  _ECX::HYPERVISOR }
};

Flags ebx[] = { { "AVX2         ",  _EBX::AVX2 },
                { "AVX512F      ",  _EBX::AVX512F }
};

int main()
{
    int regs[4];

    __cpuid(regs, 0);
    int nMax = regs[0];
    if (nMax >= 1)
    {
        __cpuid(regs, 1);
        bitset<32> REG(regs[3]);
        for (const auto& arr : edx)
        {
            cout << arr.isa_feature << (REG[arr.bit] ?
                " supported." : " not supported!") << std::endl;
        }
        REG = regs[2];
        for (const auto& arr : ecx)
        {
            cout << arr.isa_feature << (REG[arr.bit] ?
                " supported." : " not supported!") << std::endl;
        }
    }
    if (nMax >= 7)
    {
        __cpuid(regs, 7);
        bitset<32> REG(regs[1]);
        for (const auto& arr : ebx)
        {
            cout << arr.isa_feature << (REG[arr.bit] ?
                " supported." : " not supported!") << std::endl;
        }
    }
}

CPU brand string: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHzで実行した例:

FPU           supported.
VME           supported.
DE            supported.
PSE           supported.
TSC           supported.
MSR           supported.
PAE           supported.
MCE           supported.
CX8           supported.
APIC          supported.
SEP           supported.
MTRR          supported.
PGE           supported.
MCA           supported.
CMOV          supported.
PAT           supported.
PSE-36        supported.
PSN           not supported!
CLFSH         supported.
DS            supported.
ACPI          supported.
MMX           supported.
FXSR          supported.
SSE           supported.
SSE2          supported.
SS            supported.
HTT           supported.
TM            supported.
IA64          not supported!
PBE           supported.
SSE3          supported.
PCLMULQDQ     supported.
DTES64        supported.
MONITOR       supported.
DS_CPL        supported.
VMX           supported.
SMX           not supported!
EST           supported.
TM2           supported.
SSSE3         supported.
CNXT_ID       not supported!
SDBG          supported.
FMA           supported.
CX16          supported.
XTPR          supported.
PDCM          supported.
PCID          supported.
DCA           not supported!
SSE41         supported.
SSE42         supported.
X2APIC        supported.
MOVBE         supported.
POPCNT        supported.
TSC_DEADLINE  supported.
AES           supported.
XSAVE         supported.
OSXSAVE       supported.
AVX           supported.
F16C          supported.
RDRND         supported.
HYPERVISOR    not supported!
AVX2          supported.
AVX512F       supported.