in c++/src/CpuInfoUtil.cc [139:209]
void OsRetrieveCpuInfo(int64_t* hardwareFlags, CpuInfo::Vendor* vendor,
std::string* modelName) {
int register_EAX_id = 1;
int highest_valid_id = 0;
int highest_extended_valid_id = 0;
std::bitset<32> features_ECX;
std::array<int, 4> cpuInfo;
// Get highest valid id
__cpuid(cpuInfo.data(), 0);
highest_valid_id = cpuInfo[0];
// HEX of "GenuineIntel": 47656E75 696E6549 6E74656C
// HEX of "AuthenticAMD": 41757468 656E7469 63414D44
if (cpuInfo[1] == 0x756e6547 && cpuInfo[3] == 0x49656e69 && cpuInfo[2] == 0x6c65746e) {
*vendor = CpuInfo::Vendor::Intel;
} else if (cpuInfo[1] == 0x68747541 && cpuInfo[3] == 0x69746e65 && cpuInfo[2] == 0x444d4163) {
*vendor = CpuInfo::Vendor::AMD;
}
if (highest_valid_id <= register_EAX_id) {
return;
}
// EAX=1: Processor Info and Feature Bits
__cpuidex(cpuInfo.data(), register_EAX_id, 0);
features_ECX = cpuInfo[2];
// Get highest extended id
__cpuid(cpuInfo.data(), 0x80000000);
highest_extended_valid_id = cpuInfo[0];
// Retrieve CPU model name
if (highest_extended_valid_id >= static_cast<int>(0x80000004)) {
modelName->clear();
for (int i = 0x80000002; i <= static_cast<int>(0x80000004); ++i) {
__cpuidex(cpuInfo.data(), i, 0);
*modelName += std::string(reinterpret_cast<char*>(cpuInfo.data()), sizeof(cpuInfo));
}
}
bool zmm_enabled = false;
if (features_ECX[27]) { // OSXSAVE
// Query if the OS supports saving ZMM registers when switching contexts
int64_t xcr0 = _xgetbv(0);
zmm_enabled = (xcr0 & 0xE0) == 0xE0;
}
if (features_ECX[9]) *hardwareFlags |= CpuInfo::SSSE3;
if (features_ECX[19]) *hardwareFlags |= CpuInfo::SSE4_1;
if (features_ECX[20]) *hardwareFlags |= CpuInfo::SSE4_2;
if (features_ECX[23]) *hardwareFlags |= CpuInfo::POPCNT;
if (features_ECX[28]) *hardwareFlags |= CpuInfo::AVX;
// cpuid with EAX=7, ECX=0: Extended Features
register_EAX_id = 7;
if (highest_valid_id > register_EAX_id) {
__cpuidex(cpuInfo.data(), register_EAX_id, 0);
std::bitset<32> features_EBX = cpuInfo[1];
if (features_EBX[3]) *hardwareFlags |= CpuInfo::BMI1;
if (features_EBX[5]) *hardwareFlags |= CpuInfo::AVX2;
if (features_EBX[8]) *hardwareFlags |= CpuInfo::BMI2;
if (zmm_enabled) {
if (features_EBX[16]) *hardwareFlags |= CpuInfo::AVX512F;
if (features_EBX[17]) *hardwareFlags |= CpuInfo::AVX512DQ;
if (features_EBX[28]) *hardwareFlags |= CpuInfo::AVX512CD;
if (features_EBX[30]) *hardwareFlags |= CpuInfo::AVX512BW;
if (features_EBX[31]) *hardwareFlags |= CpuInfo::AVX512VL;
}
}
}