Overte C++ Documentation
CPUIdent.h
1 //
2 // CPUIdent.h
3 //
4 // Adapted from Microsoft's example for using the cpuid intrinsic,
5 // found at https://msdn.microsoft.com/en-us/library/hskdteyh.aspx
6 //
7 // Provides acccess to information provided by the CPUID opcode
8 //
9 // TODO: Generalize to work outside of Windows.
10 //
11 // Created by Ryan Huffman on 3/25/16.
12 // Copyright 2016 High Fidelity, Inc.
13 //
14 // Distributed under the Apache License, Version 2.0.
15 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
16 //
17 
18 #ifndef hifi_CPUIdent_h
19 #define hifi_CPUIdent_h
20 
21 #include <vector>
22 #include <bitset>
23 #include <array>
24 #include <string>
25 #include <cstdint>
26 
27 class CPUIdent
28 {
29  // forward declarations
30  class CPUIdent_Internal;
31 
32 public:
33  struct Feature {
34  std::string name;
35  bool supported;
36  };
37 
38  static std::vector<Feature> getAllFeatures();
39 
40  static std::string Vendor(void) { return CPU_Rep.vendor_; }
41  static std::string Brand(void) { return CPU_Rep.brand_; }
42 
43  static bool SSE3(void) { return CPU_Rep.f_1_ECX_[0]; }
44  static bool PCLMULQDQ(void) { return CPU_Rep.f_1_ECX_[1]; }
45  static bool MONITOR(void) { return CPU_Rep.f_1_ECX_[3]; }
46  static bool SSSE3(void) { return CPU_Rep.f_1_ECX_[9]; }
47  static bool FMA(void) { return CPU_Rep.f_1_ECX_[12]; }
48  static bool CMPXCHG16B(void) { return CPU_Rep.f_1_ECX_[13]; }
49  static bool SSE41(void) { return CPU_Rep.f_1_ECX_[19]; }
50  static bool SSE42(void) { return CPU_Rep.f_1_ECX_[20]; }
51  static bool MOVBE(void) { return CPU_Rep.f_1_ECX_[22]; }
52  static bool POPCNT(void) { return CPU_Rep.f_1_ECX_[23]; }
53  static bool AES(void) { return CPU_Rep.f_1_ECX_[25]; }
54  static bool XSAVE(void) { return CPU_Rep.f_1_ECX_[26]; }
55  static bool OSXSAVE(void) { return CPU_Rep.f_1_ECX_[27]; }
56  static bool AVX(void) { return CPU_Rep.f_1_ECX_[28]; }
57  static bool F16C(void) { return CPU_Rep.f_1_ECX_[29]; }
58  static bool RDRAND(void) { return CPU_Rep.f_1_ECX_[30]; }
59 
60  static bool MSR(void) { return CPU_Rep.f_1_EDX_[5]; }
61  static bool CX8(void) { return CPU_Rep.f_1_EDX_[8]; }
62  static bool SEP(void) { return CPU_Rep.f_1_EDX_[11]; }
63  static bool CMOV(void) { return CPU_Rep.f_1_EDX_[15]; }
64  static bool CLFSH(void) { return CPU_Rep.f_1_EDX_[19]; }
65  static bool MMX(void) { return CPU_Rep.f_1_EDX_[23]; }
66  static bool FXSR(void) { return CPU_Rep.f_1_EDX_[24]; }
67  static bool SSE(void) { return CPU_Rep.f_1_EDX_[25]; }
68  static bool SSE2(void) { return CPU_Rep.f_1_EDX_[26]; }
69 
70  static bool FSGSBASE(void) { return CPU_Rep.f_7_EBX_[0]; }
71  static bool BMI1(void) { return CPU_Rep.f_7_EBX_[3]; }
72  static bool HLE(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[4]; }
73  static bool AVX2(void) { return CPU_Rep.f_7_EBX_[5]; }
74  static bool BMI2(void) { return CPU_Rep.f_7_EBX_[8]; }
75  static bool ERMS(void) { return CPU_Rep.f_7_EBX_[9]; }
76  static bool INVPCID(void) { return CPU_Rep.f_7_EBX_[10]; }
77  static bool RTM(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[11]; }
78  static bool AVX512F(void) { return CPU_Rep.f_7_EBX_[16]; }
79  static bool RDSEED(void) { return CPU_Rep.f_7_EBX_[18]; }
80  static bool ADX(void) { return CPU_Rep.f_7_EBX_[19]; }
81  static bool AVX512PF(void) { return CPU_Rep.f_7_EBX_[26]; }
82  static bool AVX512ER(void) { return CPU_Rep.f_7_EBX_[27]; }
83  static bool AVX512CD(void) { return CPU_Rep.f_7_EBX_[28]; }
84  static bool SHA(void) { return CPU_Rep.f_7_EBX_[29]; }
85 
86  static bool PREFETCHWT1(void) { return CPU_Rep.f_7_ECX_[0]; }
87 
88  static bool LAHF(void) { return CPU_Rep.f_81_ECX_[0]; }
89  static bool LZCNT(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_ECX_[5]; }
90  static bool ABM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[5]; }
91  static bool SSE4a(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[6]; }
92  static bool XOP(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[11]; }
93  static bool TBM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[21]; }
94 
95  static bool SYSCALL(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[11]; }
96  static bool MMXEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[22]; }
97  static bool RDTSCP(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[27]; }
98  static bool _3DNOWEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[30]; }
99  static bool _3DNOW(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[31]; }
100 
101 private:
102  static const CPUIdent_Internal CPU_Rep;
103 
104  class CPUIdent_Internal {
105  public:
106  CPUIdent_Internal();
107 
108  uint32_t nIds_{ 0 };
109  uint32_t nExIds_{ 0 };
110  std::string vendor_;
111  std::string brand_;
112  bool isIntel_{ false };
113  bool isAMD_{ false };
114  std::bitset<32> f_1_ECX_{ 0 };
115  std::bitset<32> f_1_EDX_{ 0 };
116  std::bitset<32> f_7_EBX_{ 0 };
117  std::bitset<32> f_7_ECX_{ 0 };
118  std::bitset<32> f_81_ECX_{ 0 };
119  std::bitset<32> f_81_EDX_{ 0 };
120  };
121 };
122 
123 #endif // hifi_CPUIdent_h