/
processor_support.h
46 lines (46 loc) · 1.05 KB
/
processor_support.h
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
#ifndef PROCESSOR_SUPPORT_H_
#define PROCESSOR_SUPPORT_H_
#if defined(__INTEL_COMPILER)
#define USING_INTEL_COMPILER
#elif defined(__GNUC__)
#define USING_GCC_COMPILER
#include <cpuid.h>
#elif defined(_MSC_VER)
#define USING MSC_COMPILER
#endif
struct regs_t
{
unsigned int EAX, EBX, ECX, EDX;
};
#define BIT(n) ((1 << n))
class ProcessorSupport
{
#ifdef POPCNT_CAPABILITY
public:
ProcessorSupport() {}
bool POPCNTenabled()
{
regs_t regs;
try
{
#if (defined(USING_INTEL_COMPILER) || defined(USING_MSC_COMPILER))
__cpuid((int *)®s, 0);
__cpuid((int *)®s, 0x1);
#elif defined(USING_GCC_COMPILER)
__get_cpuid(0x1, ®s.EAX, ®s.EBX, ®s.ECX, ®s.EDX);
#else
std::cerr << "ERROR: please define __cpuid() for this build.\n";
assert(0);
#endif
if (!((regs.ECX & BIT(20)) && (regs.ECX & BIT(23))))
return false;
}
catch (int e)
{
return false;
}
return true;
}
#endif
};
#endif