From eddea5535071c775ea2efc543a88adbc775318d0 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 12 Sep 2023 21:45:40 +0200
Subject: Check for CPU features before compiling.

---
 configure.ac                                    |  5 +++
 src/analysis/scan/patterns/backends/bitap-int.h | 14 ++++++++
 src/analysis/scan/patterns/backends/bitap.c     | 47 ++++++++++++++++---------
 3 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/configure.ac b/configure.ac
index f2b6c92..a2c00f0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -51,6 +51,11 @@ AC_PATH_PROG(YACC_INST, $YACC)
 AC_PATH_PROG(LEX_INST, $LEX)
 
 
+#--- Checks for CPU
+
+AX_EXT
+
+
 #--- Checks for libraries
 
 AC_CHECK_LIB([dl], [dlopen])
diff --git a/src/analysis/scan/patterns/backends/bitap-int.h b/src/analysis/scan/patterns/backends/bitap-int.h
index 83ecc17..ea739b4 100644
--- a/src/analysis/scan/patterns/backends/bitap-int.h
+++ b/src/analysis/scan/patterns/backends/bitap-int.h
@@ -33,12 +33,15 @@
 
 #include "../backend-int.h"
 #include "../../../../common/cpu.h"
+#include "../../../../../config.h"
 
 
 
 #define BITAP_ATOM_SIZE 7
 
 
+#ifdef HAVE_AVX2
+
 /* Suivi d'un groupe de chaînes */
 typedef struct _grouped_strings_avx2_t
 {
@@ -64,6 +67,9 @@ typedef struct _group_manager_avx2_t
 
 } group_manager_avx2_t;
 
+#endif
+
+#ifdef HAVE_AVX512_F
 
 /* Suivi d'un groupe de chaînes */
 typedef struct _grouped_strings_avx512_t
@@ -90,6 +96,8 @@ typedef struct _group_manager_avx512_t
 
 } group_manager_avx512_t;
 
+#endif
+
 
 /* Méthode de recherche basée sur l'algorithme Bitap (instance) */
 struct _GBitapBackend
@@ -98,11 +106,17 @@ struct _GBitapBackend
 
     CPUSMIDFeature optimization;            /* Mode de calculs             */
 
+#if defined HAVE_AVX2 || defined HAVE_AVX512_F
     union
     {
+#   ifdef HAVE_AVX2
         group_manager_avx2_t manager_avx2;  /* Gestionnaire pour AVX2      */
+#   endif
+#   ifdef HAVE_AVX512_F
         group_manager_avx512_t manager_avx512;/* Gestionnaire pour AVX-512 */
+#   endif
     };
+#endif
 
 };
 
diff --git a/src/analysis/scan/patterns/backends/bitap.c b/src/analysis/scan/patterns/backends/bitap.c
index adf66d6..99e16e5 100644
--- a/src/analysis/scan/patterns/backends/bitap.c
+++ b/src/analysis/scan/patterns/backends/bitap.c
@@ -73,6 +73,8 @@ static void g_bitap_backend_output_stats(const GBitapBackend *);
 /* ---------------------- OPTIMISATIONS POUR ARCHITECTURE AVX2 ---------------------- */
 
 
+#ifdef HAVE_AVX2
+
 /* Indique la valeur portée par une expression rationnelle. */
 static void extend_grouped_strings_avx2(grouped_strings_avx2_t ***, size_t *);
 
@@ -82,13 +84,15 @@ static patid_t enroll_plain_pattern_avx2(GBitapBackend *, GScanContext *, const
 /* Parcours un contenu binaire à la recherche de motifs. */
 static void run_scan_avx2(const GBitapBackend *, GScanContext *, const bin_t *, phys_t);
 
-
+#endif
 
 
 
 /* --------------------- OPTIMISATIONS POUR ARCHITECTURE AVX512 --------------------- */
 
 
+#ifdef HAVE_AVX512_F
+
 /* Indique la valeur portée par une expression rationnelle. */
 static void extend_grouped_strings_avx512(grouped_strings_avx512_t ***, size_t *);
 
@@ -98,7 +102,7 @@ static patid_t enroll_plain_pattern_avx512(GBitapBackend *, GScanContext *, cons
 /* Parcours un contenu binaire à la recherche de motifs. */
 static void run_scan_avx512(const GBitapBackend *, GScanContext *, const bin_t *, phys_t);
 
-
+#endif
 
 
 
@@ -270,21 +274,19 @@ static patid_t g_bitap_backend_enroll_plain_pattern(GBitapBackend *backend, GSca
 {
     patid_t result;                         /* Identifiant à retourner     */
 
-
-
-    result = INVALID_PATTERN_ID;
-
-
-
-
+#ifdef HAVE_AVX512_F
     if (0)
+        result = enroll_plain_pattern_avx512(backend, context, plain, len);
+    else
+#endif
 
+#ifdef HAVE_AVX2
+    if (0)
         result = enroll_plain_pattern_avx2(backend, context, plain, len);
-
     else
+#endif
 
-        result = enroll_plain_pattern_avx512(backend, context, plain, len);
-
+        result = INVALID_PATTERN_ID;
 
     return result;
 
@@ -351,13 +353,22 @@ static void g_bitap_backend_run_scan(const GBitapBackend *backend, GScanContext
 
     assert(data != NULL);
 
+
+
+
+#ifdef HAVE_AVX512_F
     if (0)
+        run_scan_avx512(backend, context, data, dlen);
+    else
+#endif
 
+#ifdef HAVE_AVX2
+    if (0)
         run_scan_avx2(backend, context, data, dlen);
-
     else
+#endif
 
-        run_scan_avx512(backend, context, data, dlen);
+        ;
 
     g_object_unref(G_OBJECT(content));
 
@@ -397,6 +408,7 @@ static void g_bitap_backend_output_stats(const GBitapBackend *backend)
  * Cf. https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#techs=AVX,AVX2
  */
 
+#ifdef HAVE_AVX2
 
 /******************************************************************************
 *                                                                             *
@@ -1481,6 +1493,8 @@ static void run_scan_avx2(const GBitapBackend *backend, GScanContext *context, G
 
 
 
+#endif
+
 
 
 /* ---------------------------------------------------------------------------------- */
@@ -1494,6 +1508,7 @@ static void run_scan_avx2(const GBitapBackend *backend, GScanContext *context, G
  *   - https://uops.info/table.html
  */
 
+#ifdef HAVE_AVX512_F
 
 /******************************************************************************
 *                                                                             *
@@ -1619,9 +1634,6 @@ static patid_t enroll_plain_pattern_avx512(GBitapBackend *backend, GScanContext
 }
 
 
-
-
-
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : backend = moteur de recherche à manipuler.                   *
@@ -2762,3 +2774,4 @@ static void run_scan_avx512__saved(const GBitapBackend *backend, GScanContext *c
 
 
 
+#endif
-- 
cgit v0.11.2-87-g4458