-
Notifications
You must be signed in to change notification settings - Fork 1
/
hardwaresetup.cpp
117 lines (90 loc) · 2.66 KB
/
hardwaresetup.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "hardwaresetup.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void HardWareSetup::cpuID(unsigned i, unsigned regs[4])
{
#ifdef _WINDOWS_
__cpuid((int *)regs, (int)i);
#else
asm volatile
("cpuid": "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (i), "c" (0));
#endif
}
unsigned int HardWareSetup::getCPUNum()
{
unsigned regs[4] = {0};
/// Get vendor
char vendor[12];
cpuID(0, regs);
((unsigned *)vendor)[0] = regs[1]; // EBX
((unsigned *)vendor)[1] = regs[3]; // EDX
((unsigned *)vendor)[2] = regs[2]; // ECX
string cpuVendor = string(vendor, 12);
/// Get CPU features
cpuID(1, regs);
unsigned cpuFeatures = regs[3]; // EDX
/// Logical core count per CPU
cpuID(1, regs);
unsigned logical = (regs[1] >> 16) & 0xff; // EBX[23:16]
///cout <<" logical cpus: " << logical << endl;
unsigned cores = logical;
if(cpuVendor == "GenuineIntel")
{
cpuID(4, regs);
cores = ((regs[0] >> 26) & 0x3f) + 1; // EAX[31:26] + 1
}
else if (cpuVendor == "AuthenticAMD")
{
cpuID(0x80000008, regs);
cores = ((unsigned)(regs[2] & 0xff)) + 1; // ECX[7:0] + 1
}
///cout <<" cpu cores: " << cores << endl;
bool hyperThreads = cpuFeatures & (1 << 28) && cores < logical;
//cout << " hyper-threads: " << (hyperThreads ? "true" : "false") << endl;
return cores;
}
unsigned int HardWareSetup::getCoreNum()
{
unsigned regs[4] = {0};
/// Get vendor
char vendor[12];
cpuID(0, regs);
((unsigned *)vendor)[0] = regs[1]; /// EBX
((unsigned *)vendor)[1] = regs[3]; /// EDX
((unsigned *)vendor)[2] = regs[2]; /// ECX
string cpuVendor = string(vendor, 12);
/// Get CPU features
cpuID(1, regs);
unsigned cpuFeatures = regs[3]; /// EDX
/// Logical core count per CPU
cpuID(1, regs);
unsigned logical = (regs[1] >> 16) & 0xff; /// EBX[23:16]
unsigned cores = logical;
if(cpuVendor == "GenuineIntel")
{
cpuID(4, regs);
cores = ((regs[0] >> 26) & 0x3f) + 1; /// EAX[31:26] + 1
}
else if (cpuVendor == "AuthenticAMD")
{
cpuID(0x80000008, regs);
cores = ((unsigned)(regs[2] & 0xff)) + 1; /// ECX[7:0] + 1
}
bool hyperThreads = cpuFeatures & (1 << 28) && cores < logical;
///cout << " hyper-threads: " << (hyperThreads ? "true" : "false") << endl;
return logical;
}
void print(int i, int j, int &k, int &l)
{
cout<<i<<"\t"<<j<<"\t"<<k<<"\t"<<l<<endl;
k = k+1;
l = l+2;
}
void HardWareSetup::test()
{
///unsigned int ncpu = HardWareSetup::getCPUNum();
///cout<<ncpu<<endl;
}