cpuid
是一個(gè) x86 和 x86-64 架構(gòu)上的 CPU 指令,用于獲取 CPU 的信息
__cpuid
內(nèi)部函數(shù)。__get_cpuid
內(nèi)部函數(shù)。cpuid
指令。以下是一個(gè)簡(jiǎn)單的示例,展示了如何在不同平臺(tái)上使用 C++ 和內(nèi)部函數(shù)調(diào)用 cpuid
指令:
#include<iostream>
#include <cstdint>
#ifdef _WIN32
#include <intrin.h>
#else
#include <cpuid.h>
#endif
void cpuid(uint32_t leaf, uint32_t subleaf, uint32_t& eax, uint32_t& ebx, uint32_t& ecx, uint32_t& edx) {
#ifdef _WIN32
int regs[4];
__cpuidex(regs, leaf, subleaf);
eax = regs[0];
ebx = regs[1];
ecx = regs[2];
edx = regs[3];
#else
unsigned int a, b, c, d;
__get_cpuid_count(leaf, subleaf, &a, &b, &c, &d);
eax = a;
ebx = b;
ecx = c;
edx = d;
#endif
}
int main() {
uint32_t eax, ebx, ecx, edx;
cpuid(0, 0, eax, ebx, ecx, edx);
std::cout << "Maximum supported CPUID leaf: " << eax<< std::endl;
return 0;
}
這個(gè)示例首先根據(jù)平臺(tái)選擇適當(dāng)?shù)念^文件和內(nèi)部函數(shù)。然后,定義一個(gè)名為 cpuid
的函數(shù),該函數(shù)接受 leaf
和 subleaf
參數(shù),并返回 eax
、ebx
、ecx
和 edx
寄存器的值。最后,main
函數(shù)調(diào)用 cpuid
函數(shù)并輸出最大支持的 CPUID 葉子。
請(qǐng)注意,這個(gè)示例僅適用于 x86 和 x86-64 架構(gòu)。在其他架構(gòu)上,可能需要使用不同的方法來(lái)獲取 CPU 信息。