@ 260927.1

230609.1.2 · 29ee9e7b87b0c4984a69e15d3b8f309d7f820f8b

Check if cpuid support is available

reading declarations

Changes to src/cpu_features.cpp. All files

src/cpu_features.cpp

@@ -1,11 +1,25 @@ #include <cpuid.h> #include <hypergrep/cpu_features.hpp> -bool has_avx2_support() {+bool has_cpuid() {   unsigned int eax, ebx, ecx, edx;-  if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {-    if (ecx & bit_AVX2) {-      return true; // AVX2 is supported+  __cpuid(0, eax, ebx, ecx, edx);+  return ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e;+}++bool has_cpuid_extension() {+  unsigned int eax, ebx, ecx, edx;+  __cpuid(1, eax, ebx, ecx, edx);+  return ecx & bit_SSE4_2;+}++bool has_avx2_support() {+  if (has_cpuid() && has_cpuid_extension()) {+    unsigned int eax, ebx, ecx, edx;+    if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {+      if (ecx & bit_AVX2) {+        return true; // AVX2 is supported+      }     }   }   return false; // AVX2 is not supported@@ -13,10 +27,12 @@ bool has_avx2_support() {  // Function to check if the CPU supports AVX-512 bool has_avx512_support() {-  unsigned int eax, ebx, ecx, edx;-  if (__get_cpuid(0x00000007, &eax, &ebx, &ecx, &edx)) {-    if (ebx & bit_AVX512F && ebx & bit_AVX512DQ) {-      return true; // AVX-512 is supported+  if (has_cpuid() && has_cpuid_extension()) {+    unsigned int eax, ebx, ecx, edx;+    if (__get_cpuid(0x00000007, &eax, &ebx, &ecx, &edx)) {+      if (ebx & bit_AVX512F && ebx & bit_AVX512DQ) {+        return true; // AVX-512 is supported+      }     }   }   return false; // AVX-512 is not supported@@ -24,11 +40,13 @@ bool has_avx512_support() {  // Function to check if the CPU supports AVX-512VBMI bool has_avx512vbmi_support() {-  unsigned int eax, ebx, ecx, edx;-  if (__get_cpuid(0x00000007, &eax, &ebx, &ecx, &edx)) {-    if (ebx & bit_AVX512VBMI) {-      return 1; // AVX-512VBMI is supported+  if (has_cpuid() && has_cpuid_extension()) {+    unsigned int eax, ebx, ecx, edx;+    if (__get_cpuid(0x00000007, &eax, &ebx, &ecx, &edx)) {+      if (ebx & bit_AVX512VBMI) {+        return true; // AVX-512VBMI is supported+      }     }   }-  return 0; // AVX-512VBMI is not supported+  return false; // AVX-512VBMI is not supported }\ No newline at end of file