Last active
December 22, 2022 13:39
-
-
Save kassane/5402cadc99219b853d1da49087732a16 to your computer and use it in GitHub Desktop.
Check architecture version to x86_64 CPU - written on C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: https://unix.stackexchange.com/questions/631217/how-do-i-check-if-my-cpu-supports-x86-64-v2/631226#631226 | |
// by: Matheus Catarino França | |
// License: CC0 | |
#include <fstream> | |
#include <iostream> | |
#include <string> | |
int main() { | |
std::ifstream cpuinfo("/proc/cpuinfo"); | |
std::string line; | |
int level = 0; | |
while (std::getline(cpuinfo, line)) { | |
if (line.find("flags") != std::string::npos) { | |
while (std::getline(cpuinfo, line)) { | |
if (line.find("lm") != std::string::npos && | |
line.find("cmov") != std::string::npos && | |
line.find("cx8") != std::string::npos && | |
line.find("fpu") != std::string::npos && | |
line.find("fxsr") != std::string::npos && | |
line.find("mmx") != std::string::npos && | |
line.find("syscall") != std::string::npos && | |
line.find("sse2") != std::string::npos) { | |
level = 1; | |
} | |
if (level == 1 && line.find("cx16") != std::string::npos && | |
line.find("lahf") != std::string::npos && | |
line.find("popcnt") != std::string::npos && | |
line.find("sse4_1") != std::string::npos && | |
line.find("sse4_2") != std::string::npos && | |
line.find("ssse3") != std::string::npos) { | |
level = 2; | |
} | |
if (level == 2 && line.find("avx") != std::string::npos && | |
line.find("avx2") != std::string::npos && | |
line.find("bmi1") != std::string::npos && | |
line.find("bmi2") != std::string::npos && | |
line.find("f16c") != std::string::npos && | |
line.find("fma") != std::string::npos && | |
line.find("abm") != std::string::npos && | |
line.find("movbe") != std::string::npos && | |
line.find("xsave") != std::string::npos) { | |
level = 3; | |
} | |
if (level == 3 && line.find("avx512f") != std::string::npos && | |
line.find("avx512bw") != std::string::npos && | |
line.find("avx512cd") != std::string::npos && | |
line.find("avx512dq") != std::string::npos && | |
line.find("avx512vl") != std::string::npos) { | |
level = 4; | |
} | |
} | |
} | |
} | |
if (level > 0) { | |
std::cout << "CPU supports x86-64-v" << level << std::endl; | |
} else { | |
std::cout << "CPU does not support required features" << std::endl; | |
return 1; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
int main() { | |
// Declare variables to store the result of the CPUID instruction | |
unsigned int eax, ebx, ecx, edx; | |
// Use inline assembly to execute the CPUID instruction | |
__asm__ ( | |
"cpuid\n\t" | |
: "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) | |
: "a" (0) | |
); | |
// Check the value of the ebx, ecx, and edx registers to determine the CPU architecture | |
if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) { | |
std::cout << "This CPU is an Intel processor." << std::endl; | |
} else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) { | |
std::cout << "This CPU is an AMD processor." << std::endl; | |
} else { | |
std::cout << "Unable to determine CPU architecture." << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment