-
Notifications
You must be signed in to change notification settings - Fork 13
/
cpuid.cpp
58 lines (52 loc) · 1.39 KB
/
cpuid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#if defined(__i386__) || defined(__x86_64__)
#include "cpuid.h"
#include <string.h>
void CpuId::cpuid(int *data, int leaf) {
__asm__ ("cpuid"
: "=a" (data[0]), "=b" (data[1]), "=c" (data[2]), "=d" (data[3])
: "a" (leaf));
}
CpuId::CpuId() {
int data[4];
cpuid(data, 0);
hiLeaf = (unsigned int) data[0];
memcpy(&manufacturerId[0], &data[1], 4);
memcpy(&manufacturerId[4], &data[3], 4);
memcpy(&manufacturerId[8], &data[2], 4);
manufacturerId[12] = 0;
cpuid(data, 0x80000000);
hiExtLeaf = (unsigned int) data[0];
}
const char* CpuId::getBrandString() {
if (hasBrandString)
return brandString;
if (hiExtLeaf < 0x80000004)
return nullptr;
int data[4];
for (int i = 0; i < 3; i++) {
cpuid(data, 0x80000002 + i);
memcpy(&brandString[i * 16], data, 16);
}
brandString[48] = 0;
return brandString;
}
void CpuId::queryFeatureFlags() {
if (hasFeatureFlags)
return;
if (hiLeaf < 1)
return;
hasFeatureFlags = true;
int data[4];
cpuid(data, 1);
featureFlagsC = data[2];
featureFlagsD = data[3];
}
bool CpuId::queryFeatureFlag(CpuId::FeatureFlag flag) {
queryFeatureFlags();
auto flagi = (unsigned char) flag;
if (flagi & 128)
return (featureFlagsD & (1 << (flagi & 0x7f))) != 0;
else
return (featureFlagsC & (1 << (flagi & 0x7f))) != 0;
}
#endif