]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: optimize sig_cnt setting
authorVictor Julien <vjulien@oisf.net>
Tue, 5 Mar 2024 07:36:08 +0000 (13:06 +0530)
committerVictor Julien <victor@inliniac.net>
Fri, 24 May 2024 17:11:03 +0000 (19:11 +0200)
Utilize _popcnt64 where available.

(cherry picked from commit c4ac6cd)

configure.ac
src/detect-engine-siggroup.c
src/suricata.c

index 72629f422d38b3c33e92ed6bb242521e8e6fe5a1..04305ecd95cdd587e6ffbd7b863c61384d0ed0ed 100644 (file)
         [], [
             #include <sys/random.h> 
             ])
+    AC_CHECK_DECL([_popcnt64],
+        AC_DEFINE([HAVE_POPCNT64], [1], [Use _popcnt64]),
+        [], [
+            #include <x86intrin.h> 
+            ])
 
     OCFLAGS=$CFLAGS
     CFLAGS=""
index a87084916ff09ecf711961a4d1691ea8bd52e230..f47b19234f210314acfc17753dac116252547501 100644 (file)
@@ -440,6 +440,24 @@ error:
     return -1;
 }
 
+#ifdef HAVE_POPCNT64
+#include <x86intrin.h>
+static uint32_t Popcnt(const uint8_t *array, const uint32_t size)
+{
+    /* input needs to be a multiple of 8 for u64 casts to work */
+    DEBUG_VALIDATE_BUG_ON(size < 8);
+    DEBUG_VALIDATE_BUG_ON(size % 8);
+
+    uint32_t cnt = 0;
+    uint64_t *ptr = (uint64_t *)array;
+    for (uint64_t idx = 0; idx < size; idx += 8) {
+        cnt += _popcnt64(*ptr);
+        ptr++;
+    }
+    return cnt;
+}
+#endif
+
 /**
  * \brief Updates the SigGroupHead->sig_cnt with the total count of all the
  *        Signatures present in this SigGroupHead.
@@ -450,14 +468,16 @@ error:
  */
 void SigGroupHeadSetSigCnt(SigGroupHead *sgh, uint32_t max_idx)
 {
-    uint32_t sig;
-
-    sgh->init->sig_cnt = 0;
-    for (sig = 0; sig < max_idx + 1; sig++) {
+#ifdef HAVE_POPCNT64
+    sgh->init->sig_cnt = Popcnt(sgh->init->sig_array, sgh->init->sig_size);
+#else
+    uint32_t cnt = 0;
+    for (uint32_t sig = 0; sig < max_idx + 1; sig++) {
         if (sgh->init->sig_array[sig / 8] & (1 << (sig % 8)))
-            sgh->init->sig_cnt++;
+            cnt++;
     }
-
+    sgh->init->sig_cnt = cnt;
+#endif
     return;
 }
 
index 72aa927e476f3306af83f78951b945f9d1cb5cfd..6057b82e94365e2a5613f6d5a6c9e5f172040d71 100644 (file)
@@ -767,6 +767,9 @@ static void PrintBuildInfo(void)
     strlcat(features, "RUST ", sizeof(features));
 #if defined(SC_ADDRESS_SANITIZER)
     strlcat(features, "ASAN ", sizeof(features));
+#endif
+#if defined(HAVE_POPCNT64)
+    strlcat(features, "POPCNT64 ", sizeof(features));
 #endif
     if (strlen(features) == 0) {
         strlcat(features, "none", sizeof(features));