DSL
os_version.h
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #ifndef __DSL_OS_VERSION__
12 #define __DSL_OS_VERSION__
13 
14 #include <bitset>
15 #include <array>
16 
17 #ifdef DSL_HAVE_CPUID
18 #ifdef WIN32
19 #include <intrin.h>
20 #define __real_cpuid __cpuid
21 #else
22 #include <cpuid.h>
23 DSL_API void DSL_CC linux_cpuid(int cpuInfo[4], int function_id);
24 #define __real_cpuid linux_cpuid
25 #endif
26 #endif
27 
36 DSL_API const char * DSL_CC GetOSVersion();
37 DSL_API bool DSL_CC Is64Bit();
38 
39 #if defined(DSL_HAVE_CPUID) || defined(DOXYGEN_SKIP)
40 /* This class from MSDN */
41 class DSL_API_CLASS InstructionSet
42 {
43  // forward declarations
44  class InstructionSet_Internal;
45 
46 public:
47  // getters
48  static std::string Vendor(void) { return CPU_Rep.vendor_; }
49  static std::string Brand(void) { return CPU_Rep.brand_; }
50 
51  static bool SSE3(void) { return CPU_Rep.f_1_ECX_[0]; }
52  static bool PCLMULQDQ(void) { return CPU_Rep.f_1_ECX_[1]; }
53  static bool MONITOR(void) { return CPU_Rep.f_1_ECX_[3]; }
54  static bool SSSE3(void) { return CPU_Rep.f_1_ECX_[9]; }
55  static bool FMA(void) { return CPU_Rep.f_1_ECX_[12]; }
56  static bool CMPXCHG16B(void) { return CPU_Rep.f_1_ECX_[13]; }
57  static bool SSE41(void) { return CPU_Rep.f_1_ECX_[19]; }
58  static bool SSE42(void) { return CPU_Rep.f_1_ECX_[20]; }
59  static bool MOVBE(void) { return CPU_Rep.f_1_ECX_[22]; }
60  static bool POPCNT(void) { return CPU_Rep.f_1_ECX_[23]; }
61  static bool AES(void) { return CPU_Rep.f_1_ECX_[25]; }
62  static bool XSAVE(void) { return CPU_Rep.f_1_ECX_[26]; }
63  static bool OSXSAVE(void) { return CPU_Rep.f_1_ECX_[27]; }
64  static bool AVX(void) { return CPU_Rep.f_1_ECX_[28]; }
65  static bool F16C(void) { return CPU_Rep.f_1_ECX_[29]; }
66  static bool RDRAND(void) { return CPU_Rep.f_1_ECX_[30]; }
67 
68  static bool MSR(void) { return CPU_Rep.f_1_EDX_[5]; }
69  static bool CX8(void) { return CPU_Rep.f_1_EDX_[8]; }
70  static bool SEP(void) { return CPU_Rep.f_1_EDX_[11]; }
71  static bool CMOV(void) { return CPU_Rep.f_1_EDX_[15]; }
72  static bool CLFSH(void) { return CPU_Rep.f_1_EDX_[19]; }
73  static bool MMX(void) { return CPU_Rep.f_1_EDX_[23]; }
74  static bool FXSR(void) { return CPU_Rep.f_1_EDX_[24]; }
75  static bool SSE(void) { return CPU_Rep.f_1_EDX_[25]; }
76  static bool SSE2(void) { return CPU_Rep.f_1_EDX_[26]; }
77 
78  static bool FSGSBASE(void) { return CPU_Rep.f_7_EBX_[0]; }
79  static bool BMI1(void) { return CPU_Rep.f_7_EBX_[3]; }
80  static bool HLE(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[4]; }
81  static bool AVX2(void) { return CPU_Rep.f_7_EBX_[5]; }
82  static bool BMI2(void) { return CPU_Rep.f_7_EBX_[8]; }
83  static bool ERMS(void) { return CPU_Rep.f_7_EBX_[9]; }
84  static bool INVPCID(void) { return CPU_Rep.f_7_EBX_[10]; }
85  static bool RTM(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[11]; }
86  static bool AVX512F(void) { return CPU_Rep.f_7_EBX_[16]; }
87  static bool RDSEED(void) { return CPU_Rep.f_7_EBX_[18]; }
88  static bool ADX(void) { return CPU_Rep.f_7_EBX_[19]; }
89  static bool AVX512PF(void) { return CPU_Rep.f_7_EBX_[26]; }
90  static bool AVX512ER(void) { return CPU_Rep.f_7_EBX_[27]; }
91  static bool AVX512CD(void) { return CPU_Rep.f_7_EBX_[28]; }
92  static bool SHA(void) { return CPU_Rep.f_7_EBX_[29]; }
93 
94  static bool PREFETCHWT1(void) { return CPU_Rep.f_7_ECX_[0]; }
95 
96  static bool LAHF(void) { return CPU_Rep.f_81_ECX_[0]; }
97  static bool LZCNT(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_ECX_[5]; }
98  static bool ABM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[5]; }
99  static bool SSE4a(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[6]; }
100  static bool XOP(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[11]; }
101  static bool TBM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[21]; }
102 
103  static bool SYSCALL(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[11]; }
104  static bool MMXEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[22]; }
105  static bool RDTSCP(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[27]; }
106  static bool _3DNOWEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[30]; }
107  static bool _3DNOW(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[31]; }
108 
109 private:
110  static const InstructionSet_Internal CPU_Rep;
111 
112  class InstructionSet_Internal
113  {
114  public:
115  InstructionSet_Internal()
116  : nIds_{ 0 },
117  nExIds_{ 0 },
118  isIntel_{ false },
119  isAMD_{ false },
120  f_1_ECX_{ 0 },
121  f_1_EDX_{ 0 },
122  f_7_EBX_{ 0 },
123  f_7_ECX_{ 0 },
124  f_81_ECX_{ 0 },
125  f_81_EDX_{ 0 },
126  data_{},
127  extdata_{}
128  {
129  //int cpuInfo[4] = {-1};
130  std::array<int, 4> cpui;
131 
132  // Calling __cpuid with 0x0 as the function_id argument
133  // gets the number of the highest valid function ID.
134  __real_cpuid(cpui.data(), 0);
135  nIds_ = cpui[0];
136 
137  for (int i = 0; i <= nIds_; ++i)
138  {
139  __real_cpuid(cpui.data(), i);
140  data_.push_back(cpui);
141  }
142 
143  // Capture vendor string
144  char vendor[0x20];
145  memset(vendor, 0, sizeof(vendor));
146  *reinterpret_cast<int*>(vendor) = data_[0][1];
147  *reinterpret_cast<int*>(vendor + 4) = data_[0][3];
148  *reinterpret_cast<int*>(vendor + 8) = data_[0][2];
149  vendor_ = vendor;
150  if (vendor_ == "GenuineIntel")
151  {
152  isIntel_ = true;
153  } else if (vendor_ == "AuthenticAMD")
154  {
155  isAMD_ = true;
156  }
157 
158  // load bitset with flags for function 0x00000001
159  if (nIds_ >= 1)
160  {
161  f_1_ECX_ = data_[1][2];
162  f_1_EDX_ = data_[1][3];
163  }
164 
165  // load bitset with flags for function 0x00000007
166  if (nIds_ >= 7)
167  {
168  f_7_EBX_ = data_[7][1];
169  f_7_ECX_ = data_[7][2];
170  }
171 
172  // Calling __cpuid with 0x80000000 as the function_id argument
173  // gets the number of the highest valid extended ID.
174  __real_cpuid(cpui.data(), 0x80000000);
175  nExIds_ = cpui[0];
176 
177  char brand[0x40];
178  memset(brand, 0, sizeof(brand));
179 
180  for (int i = 0x80000000; i <= nExIds_; ++i)
181  {
182  __real_cpuid(cpui.data(), i);
183  extdata_.push_back(cpui);
184  }
185 
186  // load bitset with flags for function 0x80000001
187  if (nExIds_ >= 0x80000001)
188  {
189  f_81_ECX_ = extdata_[1][2];
190  f_81_EDX_ = extdata_[1][3];
191  }
192 
193  // Interpret CPU brand string if reported
194  if (nExIds_ >= 0x80000004)
195  {
196  memcpy(brand, extdata_[2].data(), sizeof(cpui));
197  memcpy(brand + 16, extdata_[3].data(), sizeof(cpui));
198  memcpy(brand + 32, extdata_[4].data(), sizeof(cpui));
199  brand_ = brand;
200  }
201  };
202 
203  int nIds_;
204  int nExIds_;
205  std::string vendor_;
206  std::string brand_;
207  bool isIntel_;
208  bool isAMD_;
209  std::bitset<32> f_1_ECX_;
210  std::bitset<32> f_1_EDX_;
211  std::bitset<32> f_7_EBX_;
212  std::bitset<32> f_7_ECX_;
213  std::bitset<32> f_81_ECX_;
214  std::bitset<32> f_81_EDX_;
215  std::vector<std::array<int, 4>> data_;
216  std::vector<std::array<int, 4>> extdata_;
217  };
218 };
219 #endif // DSL_HAVE_CPUID
220 
223 #endif // __DSL_OS_VERSION__
224