230609.1.1 · 42d1c2c295ea7631102e7e243ffdf990561ca79c
Added functions for checking CPU features
reading declarations
@@ -28,6 +28,7 @@ message(STATUS ${HYPERSCAN_LIBRARY}) # ————————————————————————————— add_executable(hg src/compiler.cpp+ src/cpu_features.cpp src/is_binary.cpp src/directory_search.cpp src/file_filter.cpp
include/hypergrep/cpu_features.hpp
@@ -0,0 +1,10 @@+#pragma once++/// Returns true if AVX2 support is discovered+bool has_avx2_support();++/// Returns true if AVX512 support is discovered+bool has_avx512_support();++/// Return true if AVX512VBMI support is discovered+bool has_avx512vbmi_support();
@@ -1,6 +1,19 @@ #include <hypergrep/compiler.hpp>+#include <hypergrep/cpu_features.hpp> #include <hypergrep/search_options.hpp> +unsigned int get_cpu_features_flag() {+ if (has_avx512vbmi_support()) {+ return HS_CPU_FEATURES_AVX512VBMI;+ } else if (has_avx512_support()) {+ return HS_CPU_FEATURES_AVX512;+ } else if (has_avx2_support()) {+ return HS_CPU_FEATURES_AVX2;+ } else {+ return 0;+ }+}+ void compile_hs_database(hs_database **database, hs_scratch **scratch, search_options &options, const std::vector<std::string> &pattern_list) {@@ -8,6 +21,8 @@ void compile_hs_database(hs_database **database, hs_scratch **scratch, hs_error_t error_code; hs_compile_error_t *compile_error = NULL; + static const auto cpu_features_flag = get_cpu_features_flag();+ if (pattern_list.size() == 1) { const auto &pattern = pattern_list[0]; @@ -18,7 +33,8 @@ void compile_hs_database(hs_database **database, hs_scratch **scratch, (options.is_stdout || options.print_only_matching_parts || options.show_column_numbers || options.show_byte_offset ? HS_FLAG_SOM_LEFTMOST- : 0),+ : 0) |+ cpu_features_flag, pattern.size(), HS_MODE_BLOCK, NULL, database, &compile_error); } else { error_code = hs_compile(@@ -28,7 +44,8 @@ void compile_hs_database(hs_database **database, hs_scratch **scratch, (options.is_stdout || options.print_only_matching_parts || options.show_column_numbers || options.show_byte_offset ? HS_FLAG_SOM_LEFTMOST- : 0),+ : 0) |+ cpu_features_flag, HS_MODE_BLOCK, NULL, database, &compile_error); } } else {@@ -51,7 +68,8 @@ void compile_hs_database(hs_database **database, hs_scratch **scratch, (options.is_stdout || options.print_only_matching_parts || options.show_column_numbers || options.show_byte_offset ? HS_FLAG_SOM_LEFTMOST- : 0);+ : 0) |+ cpu_features_flag; std::vector<unsigned int> flags; flags.reserve(pattern_list.size()); for (std::size_t i = 0; i < pattern_list.size(); ++i) {@@ -70,12 +88,14 @@ void compile_hs_database(hs_database **database, hs_scratch **scratch, hs_compile_lit_multi(pattern_list_c.data(), flags.data(), NULL, // list of IDs - NULL means all zero lens.data(), pattern_list.size(), HS_MODE_BLOCK,- NULL, database, &compile_error);+ NULL, database, &compile_error) |+ cpu_features_flag; } else { error_code = hs_compile_multi(pattern_list_c.data(), flags.data(), NULL, // list of IDs - NULL means all zero pattern_list.size(), HS_MODE_BLOCK, NULL,- database, &compile_error);+ database, &compile_error) |+ cpu_features_flag; } }
@@ -0,0 +1,34 @@+#include <cpuid.h>+#include <hypergrep/cpu_features.hpp>++bool has_avx2_support() {+ 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+}++// 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+ }+ }+ return false; // AVX-512 is not supported+}++// 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+ }+ }+ return 0; // AVX-512VBMI is not supported+}