vppinfra: add universal array mask_compare and compress funcs

Type: improvement
Change-Id: I6d812339f626ea630ad9354632d2f9506122d379
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/buffer_funcs.c b/src/vlib/buffer_funcs.c
index fcef2d8..9ed1bac 100644
--- a/src/vlib/buffer_funcs.c
+++ b/src/vlib/buffer_funcs.c
@@ -6,123 +6,12 @@
 #include <vlib/vlib.h>
 #include <vppinfra/vector_funcs.h>
 
-typedef struct
-{
-  uword used_elts[VLIB_FRAME_SIZE / 64];
-  u32 uword_offset;
-} extract_data_t;
-
-static_always_inline u32 *
-extract_unused_elts_x64 (u32 *elts, u16 *indices, u16 index, int n_left,
-			 u64 *bmp, u32 *dst)
-{
-  u64 mask = 0;
-#if defined(CLIB_HAVE_VEC128)
-  mask = clib_compare_u16_x64 (index, indices);
-  if (n_left == 64)
-    {
-      if (mask == ~0ULL)
-	{
-	  clib_memcpy_u32 (dst, elts, 64);
-	  *bmp = ~0ULL;
-	  return dst + 64;
-	}
-    }
-  else
-    mask &= pow2_mask (n_left);
-
-  *bmp |= mask;
-
-#if defined(CLIB_HAVE_VEC512_COMPRESS)
-  u32x16u *ev = (u32x16u *) elts;
-  for (int i = 0; i < 4; i++)
-    {
-      int cnt = _popcnt32 ((u16) mask);
-      u32x16_compress_store (ev[i], mask, dst);
-      dst += cnt;
-      mask >>= 16;
-    }
-
-#elif defined(CLIB_HAVE_VEC256_COMPRESS)
-  u32x8u *ev = (u32x8u *) elts;
-  for (int i = 0; i < 8; i++)
-    {
-      int cnt = _popcnt32 ((u8) mask);
-      u32x8_compress_store (ev[i], mask, dst);
-      dst += cnt;
-      mask >>= 8;
-    }
-#elif defined(CLIB_HAVE_VEC256)
-  while (mask)
-    {
-      u16 bit = count_trailing_zeros (mask);
-      mask = clear_lowest_set_bit (mask);
-      dst++[0] = elts[bit];
-    }
-#else
-  while (mask)
-    {
-      u16 bit = count_trailing_zeros (mask);
-      mask ^= 1ULL << bit;
-      dst++[0] = elts[bit];
-    }
-#endif
-#else
-  for (int i = 0; i < n_left; i++)
-    {
-      if (indices[i] == index)
-	{
-	  dst++[0] = elts[i];
-	  mask |= 1ULL << i;
-	}
-    }
-  *bmp |= mask;
-#endif
-  return dst;
-}
-
 static_always_inline u32
-extract_unused_elts_by_index (extract_data_t *d, u32 *elts, u16 *indices,
-			      u16 index, int n_left, u32 *dst)
-{
-  u32 *dst0 = dst;
-  u64 *bmp = d->used_elts;
-  while (n_left >= 64)
-    {
-      dst = extract_unused_elts_x64 (elts, indices, index, 64, bmp, dst);
-
-      /* next */
-      indices += 64;
-      elts += 64;
-      bmp++;
-      n_left -= 64;
-    }
-
-  if (n_left)
-    dst = extract_unused_elts_x64 (elts, indices, index, n_left, bmp, dst);
-
-  return dst - dst0;
-}
-
-static_always_inline u32
-find_first_unused_elt (extract_data_t *d)
-{
-  u64 *ue = d->used_elts + d->uword_offset;
-
-  while (PREDICT_FALSE (ue[0] == ~0))
-    {
-      ue++;
-      d->uword_offset++;
-    }
-
-  return d->uword_offset * 64 + count_trailing_zeros (~ue[0]);
-}
-
-static_always_inline u32
-enqueue_one (vlib_main_t *vm, vlib_node_runtime_t *node, extract_data_t *d,
+enqueue_one (vlib_main_t *vm, vlib_node_runtime_t *node, u64 *used_elt_bmp,
 	     u16 next_index, u32 *buffers, u16 *nexts, u32 n_buffers,
 	     u32 n_left, u32 *tmp)
 {
+  u64 match_bmp[VLIB_FRAME_SIZE / 64];
   vlib_frame_t *f;
   u32 n_extracted, n_free;
   u32 *to;
@@ -138,8 +27,12 @@
   else
     to = tmp;
 
-  n_extracted = extract_unused_elts_by_index (d, buffers, nexts, next_index,
-					      n_buffers, to);
+  clib_mask_compare_u16 (next_index, nexts, match_bmp, n_buffers);
+
+  n_extracted = clib_compress_u32 (to, buffers, match_bmp, n_buffers);
+
+  for (int i = 0; i < ARRAY_LEN (match_bmp); i++)
+    used_elt_bmp[i] |= match_bmp[i];
 
   if (to != tmp)
     {
@@ -183,18 +76,23 @@
 
   while (count >= VLIB_FRAME_SIZE)
     {
-      extract_data_t d = {};
+      u64 used_elt_bmp[VLIB_FRAME_SIZE / 64] = {};
       n_left = VLIB_FRAME_SIZE;
+      u32 off = 0;
 
       next_index = nexts[0];
-      n_left = enqueue_one (vm, node, &d, next_index, buffers, nexts,
+      n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
 			    VLIB_FRAME_SIZE, n_left, tmp);
 
       while (n_left)
 	{
-	  next_index = nexts[find_first_unused_elt (&d)];
-	  n_left = enqueue_one (vm, node, &d, next_index, buffers, nexts,
-				VLIB_FRAME_SIZE, n_left, tmp);
+	  while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
+	    off++;
+
+	  next_index =
+	    nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
+	  n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
+				nexts, VLIB_FRAME_SIZE, n_left, tmp);
 	}
 
       buffers += VLIB_FRAME_SIZE;
@@ -204,18 +102,23 @@
 
   if (count)
     {
-      extract_data_t d = {};
+      u64 used_elt_bmp[VLIB_FRAME_SIZE / 64] = {};
       next_index = nexts[0];
       n_left = count;
+      u32 off = 0;
 
-      n_left = enqueue_one (vm, node, &d, next_index, buffers, nexts, count,
-			    n_left, tmp);
+      n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
+			    count, n_left, tmp);
 
       while (n_left)
 	{
-	  next_index = nexts[find_first_unused_elt (&d)];
-	  n_left = enqueue_one (vm, node, &d, next_index, buffers, nexts,
-				count, n_left, tmp);
+	  while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
+	    off++;
+
+	  next_index =
+	    nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
+	  n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
+				nexts, count, n_left, tmp);
 	}
     }
 }
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index dd3690b..6da1fa2 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -261,4 +261,22 @@
       LINK_LIBRARIES vppinfra Threads::Threads
       )
   endforeach()
+
+set(test_files
+  test_vector_funcs_compress.c
+  test_vector_funcs_mask_compare.c
+)
+
+add_vpp_executable(test_vector_funcs
+  SOURCES
+  test_vector_funcs.c
+  ${test_files}
+  LINK_LIBRARIES vppinfra
+)
+
+vpp_library_set_multiarch_sources(test_vector_funcs
+  SOURCES
+  ${test_files}
+)
+
 endif(VPP_BUILD_VPPINFRA_TESTS)
diff --git a/src/vppinfra/cpu.c b/src/vppinfra/cpu.c
index d2edc61..045d1f7 100644
--- a/src/vppinfra/cpu.c
+++ b/src/vppinfra/cpu.c
@@ -238,6 +238,18 @@
   return node;
 }
 
+__clib_export u8 *
+format_march_variant (u8 *s, va_list *args)
+{
+  clib_march_variant_type_t t = va_arg (*args, clib_march_variant_type_t);
+  char *variants[] = { [0] = "default",
+#define _(s, n) [CLIB_MARCH_VARIANT_TYPE_##s] = n,
+		       foreach_march_variant
+#undef _
+  };
+  return format (s, "%s", variants[t]);
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vppinfra/cpu.h b/src/vppinfra/cpu.h
index c770b9c..c1f2e9e 100644
--- a/src/vppinfra/cpu.h
+++ b/src/vppinfra/cpu.h
@@ -416,6 +416,7 @@
 format_function_t format_cpu_uarch;
 format_function_t format_cpu_model_name;
 format_function_t format_cpu_flags;
+format_function_t format_march_variant;
 
 /*
  * fd.io coding-style-patch-verification: ON
diff --git a/src/vppinfra/test_vector_funcs.c b/src/vppinfra/test_vector_funcs.c
new file mode 100644
index 0000000..85f159a
--- /dev/null
+++ b/src/vppinfra/test_vector_funcs.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/test_vector_funcs.h>
+
+test_registration_t *test_registrations[CLIB_MARCH_TYPE_N_VARIANTS] = {};
+
+int
+main (int argc, char *argv[])
+{
+  clib_mem_init (0, 64ULL << 20);
+
+  for (int i = 0; i < CLIB_MARCH_TYPE_N_VARIANTS; i++)
+    {
+      test_registration_t *r = test_registrations[i];
+
+      if (r == 0)
+	continue;
+
+      fformat (stdout, "\nMultiarch Variant: %U\n", format_march_variant, i);
+      fformat (stdout,
+	       "-------------------------------------------------------\n");
+      while (r)
+	{
+	  clib_error_t *err;
+	  err = (r->fn) (0);
+	  fformat (stdout, "%-50s %s\n", r->name, err ? "FAIL" : "PASS");
+	  if (err)
+	    {
+	      clib_error_report (err);
+	      fformat (stdout, "\n");
+	    }
+
+	  r = r->next;
+	}
+    }
+
+  fformat (stdout, "\n");
+  return 0;
+}
diff --git a/src/vppinfra/test_vector_funcs.h b/src/vppinfra/test_vector_funcs.h
new file mode 100644
index 0000000..bc499fb
--- /dev/null
+++ b/src/vppinfra/test_vector_funcs.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#ifndef included_test_test_h
+#define included_test_test_h
+
+#include <vppinfra/cpu.h>
+
+typedef clib_error_t *(test_fn_t) (clib_error_t *);
+
+typedef struct test_registration_
+{
+  char *name;
+  u8 multiarch : 1;
+  test_fn_t *fn;
+  struct test_registration_ *next;
+} test_registration_t;
+
+extern test_registration_t *test_registrations[CLIB_MARCH_TYPE_N_VARIANTS];
+
+#define __clib_test_fn static __clib_noinline __clib_section (".test_wrapper")
+
+#define REGISTER_TEST(x)                                                      \
+  test_registration_t CLIB_MARCH_SFX (__test_##x);                            \
+  static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) (   \
+    void)                                                                     \
+  {                                                                           \
+    test_registration_t *r = &CLIB_MARCH_SFX (__test_##x);                    \
+    r->next = test_registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)];   \
+    test_registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r;         \
+  }                                                                           \
+  test_registration_t CLIB_MARCH_SFX (__test_##x)
+
+#endif
diff --git a/src/vppinfra/test_vector_funcs_compress.c b/src/vppinfra/test_vector_funcs_compress.c
new file mode 100644
index 0000000..c081560
--- /dev/null
+++ b/src/vppinfra/test_vector_funcs_compress.c
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/vector_funcs.h>
+#include <vppinfra/test_vector_funcs.h>
+
+__clib_test_fn u32
+clib_compress_u32_wrapper (u32 *dst, u32 *src, u64 *mask, u32 n_elts)
+{
+  return clib_compress_u32 (dst, src, mask, n_elts);
+}
+
+typedef struct
+{
+  u64 mask[10];
+  u32 n_elts;
+} compress_test_t;
+
+static compress_test_t tests[] = {
+  { .mask = { 1 }, .n_elts = 1 },
+  { .mask = { 2 }, .n_elts = 2 },
+  { .mask = { 3 }, .n_elts = 2 },
+  { .mask = { 0, 1 }, .n_elts = 66 },
+  { .mask = { 0, 2 }, .n_elts = 69 },
+  { .mask = { 0, 3 }, .n_elts = 66 },
+  { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 62 },
+  { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 255 },
+  { .mask = { ~0ULL, 1, 1, ~0ULL }, .n_elts = 256 },
+};
+
+static clib_error_t *
+test_clib_compress_u32 (clib_error_t *err)
+{
+  u32 src[513];
+  u32 dst[513];
+  u32 i, j;
+
+  for (i = 0; i < ARRAY_LEN (src); i++)
+    src[i] = i;
+
+  for (i = 0; i < ARRAY_LEN (tests); i++)
+    {
+      compress_test_t *t = tests + i;
+      u32 *dp = dst;
+      u32 r;
+
+      for (j = 0; j < ARRAY_LEN (dst); j++)
+	dst[j] = 0xa5a5a5a5;
+
+      r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts);
+
+      for (j = 0; j < t->n_elts; j++)
+	{
+	  if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
+	    continue;
+
+	  if (dp[0] != src[j])
+	    return clib_error_return (err,
+				      "wrong data in testcase %u at "
+				      "(dst[%u] = 0x%x, src[%u] = 0x%x)",
+				      i, dp - dst, dp[0], j, src[j]);
+	  dp++;
+	}
+
+      if (dst[dp - dst + 1] != 0xa5a5a5a5)
+	return clib_error_return (err, "buffer overrun in testcase %u", i);
+
+      if (dp - dst != r)
+	return clib_error_return (err, "wrong number of elts in testcase %u",
+				  i);
+    }
+
+  return err;
+}
+
+REGISTER_TEST (clib_compress_u32) = {
+  .name = "clib_compress_u32",
+  .fn = test_clib_compress_u32,
+};
diff --git a/src/vppinfra/test_vector_funcs_mask_compare.c b/src/vppinfra/test_vector_funcs_mask_compare.c
new file mode 100644
index 0000000..009f877
--- /dev/null
+++ b/src/vppinfra/test_vector_funcs_mask_compare.c
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/vector_funcs.h>
+#include <vppinfra/test_vector_funcs.h>
+
+__clib_test_fn void
+clib_mask_compare_u16_wrapper (u16 v, u16 *a, u64 *mask, u32 n_elts)
+{
+  clib_mask_compare_u16 (v, a, mask, n_elts);
+}
+
+__clib_test_fn void
+clib_mask_compare_u32_wrapper (u32 v, u32 *a, u64 *mask, u32 n_elts)
+{
+  clib_mask_compare_u32 (v, a, mask, n_elts);
+}
+
+static clib_error_t *
+test_clib_mask_compare_u16 (clib_error_t *err)
+{
+  u16 array[513];
+  u64 mask[10];
+  u32 i, j;
+
+  for (i = 0; i < ARRAY_LEN (array); i++)
+    array[i] = i;
+
+  for (i = 0; i < ARRAY_LEN (array); i++)
+    {
+      for (j = 0; j < ARRAY_LEN (mask); j++)
+	mask[j] = 0xa5a5a5a5a5a5a5a5;
+
+      clib_mask_compare_u16_wrapper (i, array, mask, i + 1);
+
+      for (j = 0; j < (i >> 6); j++)
+	{
+	  if (mask[j])
+	    return clib_error_return (err, "mask at position %u not zero", j);
+	}
+      if (mask[j] != 1ULL << (i & 0x3f))
+	return clib_error_return (err,
+				  "mask at position %u is %lx, expected %lx",
+				  j, mask[j], 1ULL << (i % 64));
+
+      if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
+	return clib_error_return (err, "mask overrun at position %u", j + 1);
+    }
+  return err;
+}
+
+REGISTER_TEST (clib_mask_compare_u16) = {
+  .name = "clib_mask_compare_u16",
+  .fn = test_clib_mask_compare_u16,
+};
+
+static clib_error_t *
+test_clib_mask_compare_u32 (clib_error_t *err)
+{
+  u32 array[513];
+  u64 mask[10];
+  u32 i, j;
+
+  for (i = 0; i < ARRAY_LEN (array); i++)
+    array[i] = i;
+
+  for (i = 0; i < ARRAY_LEN (array); i++)
+    {
+      for (j = 0; j < ARRAY_LEN (mask); j++)
+	mask[j] = 0xa5a5a5a5a5a5a5a5;
+
+      clib_mask_compare_u32_wrapper (i, array, mask, i + 1);
+
+      for (j = 0; j < (i >> 6); j++)
+	{
+	  if (mask[j])
+	    return clib_error_return (err, "mask at position %u not zero", j);
+	}
+      if (mask[j] != 1ULL << (i & 0x3f))
+	return clib_error_return (err,
+				  "mask at position %u is %lx, expected %lx",
+				  j, mask[j], 1ULL << (i % 64));
+
+      if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
+	return clib_error_return (err, "mask overrun at position %u", j + 1);
+    }
+  return err;
+}
+
+REGISTER_TEST (clib_mask_compare_u32) = {
+  .name = "clib_mask_compare_u32",
+  .fn = test_clib_mask_compare_u32,
+};
diff --git a/src/vppinfra/vector_funcs.h b/src/vppinfra/vector_funcs.h
index 5c446a5..fed1b5c 100644
--- a/src/vppinfra/vector_funcs.h
+++ b/src/vppinfra/vector_funcs.h
@@ -5,16 +5,10 @@
 #ifndef included_vector_funcs_h
 #define included_vector_funcs_h
 #include <vppinfra/clib.h>
-
-/** \brief Compare 64 16-bit elemments with provied value and return bitmap
-
-    @param v value to compare elements with
-    @param a array of 64 u16 elements
-    @return u64 bitmap where each bit represents result of comparison
-*/
+#include <vppinfra/memcpy.h>
 
 static_always_inline u64
-clib_compare_u16_x64 (u16 v, u16 *a)
+clib_mask_compare_u16_x64 (u16 v, u16 *a, u32 n_elts)
 {
   u64 mask = 0;
 #if defined(CLIB_HAVE_VEC512)
@@ -42,14 +36,9 @@
      of that bit represend element index. Finally vaddvq_u16() gives us sum
      of all elements of the vector which will give us u8 bitmap. */
 
-  mask = ((u64) vaddvq_u16 ((av[0] == v8) & m) |
-	  (u64) vaddvq_u16 ((av[1] == v8) & m) << 8 |
-	  (u64) vaddvq_u16 ((av[2] == v8) & m) << 16 |
-	  (u64) vaddvq_u16 ((av[3] == v8) & m) << 24 |
-	  (u64) vaddvq_u16 ((av[4] == v8) & m) << 32 |
-	  (u64) vaddvq_u16 ((av[5] == v8) & m) << 40 |
-	  (u64) vaddvq_u16 ((av[6] == v8) & m) << 48 |
-	  (u64) vaddvq_u16 ((av[7] == v8) & m) << 56);
+  for (int i = 0; i < 8; i++)
+    mask |= (u64) vaddvq_u16 ((av[i] == v8) & m) << (i * 8);
+
 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
   u16x8 v8 = u16x8_splat (v);
   u16x8u *av = (u16x8u *) a;
@@ -58,11 +47,188 @@
 	  (u64) i8x16_msb_mask (i8x16_pack (v8 == av[4], v8 == av[5])) << 32 |
 	  (u64) i8x16_msb_mask (i8x16_pack (v8 == av[6], v8 == av[7])) << 48);
 #else
-  for (int i = 0; i < 64; i++)
+  for (int i = 0; i < n_elts; i++)
     if (a[i] == v)
       mask |= 1ULL << i;
 #endif
   return mask;
 }
 
+/** \brief Compare 16-bit elemments with provied value and return bitmap
+
+    @param v value to compare elements with
+    @param a array of u16 elements
+    @param mask array of u64 where reuslting mask will be stored
+    @param n_elts number of elements in the array
+    @return none
+*/
+
+static_always_inline void
+clib_mask_compare_u16 (u16 v, u16 *a, u64 *mask, u32 n_elts)
+{
+  while (n_elts >= 64)
+    {
+      mask++[0] = clib_mask_compare_u16_x64 (v, a, 64);
+      n_elts -= 64;
+      a += 64;
+    }
+
+  if (PREDICT_TRUE (n_elts == 0))
+    return;
+
+  mask[0] = clib_mask_compare_u16_x64 (v, a, n_elts) & pow2_mask (n_elts);
+}
+
+static_always_inline u64
+clib_mask_compare_u32_x64 (u32 v, u32 *a, u32 n_elts)
+{
+  u64 mask = 0;
+#if defined(CLIB_HAVE_VEC512)
+  u32x16 v16 = u32x16_splat (v);
+  u32x16u *av = (u32x16u *) a;
+  mask = ((u64) u32x16_is_equal_mask (av[0], v16) |
+	  (u64) u32x16_is_equal_mask (av[1], v16) << 16 |
+	  (u64) u32x16_is_equal_mask (av[2], v16) << 32 |
+	  (u64) u32x16_is_equal_mask (av[3], v16) << 48);
+#elif defined(CLIB_HAVE_VEC256)
+  u32x8 v8 = u32x8_splat (v);
+  u32x8u *av = (u32x8u *) a;
+  u32x8 m = { 0, 4, 1, 5, 2, 6, 3, 7 };
+  i8x32 c;
+
+  c = i8x32_pack (i16x16_pack ((i32x8) (v8 == av[0]), (i32x8) (v8 == av[1])),
+		  i16x16_pack ((i32x8) (v8 == av[2]), (i32x8) (v8 == av[3])));
+  mask = i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m));
+
+  c = i8x32_pack (i16x16_pack ((i32x8) (v8 == av[4]), (i32x8) (v8 == av[5])),
+		  i16x16_pack ((i32x8) (v8 == av[6]), (i32x8) (v8 == av[7])));
+  mask |= (u64) i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m)) << 32;
+
+#elif defined(CLIB_HAVE_VEC128) && defined(__ARM_NEON)
+  u32x4 v4 = u32x4_splat (v);
+  u32x4 m = { 1, 2, 4, 8 };
+  u32x4u *av = (u32x4u *) a;
+
+  /* compare each u32 elemment with v4, result gives -1 in each element
+     of the resulting vector if comparison result is true.
+     Bitwise AND with m will give us one bit set for true result and offset
+     of that bit represend element index. Finally vaddvq_u32() gives us sum
+     of all elements of the vector which will give us u8 bitmap. */
+
+  for (int i = 0; i < 16; i++)
+    mask |= (u64) vaddvq_u32 ((av[i] == v4) & m) << (i * 4);
+
+#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
+  u32x4 v4 = u32x4_splat (v);
+  u32x4u *av = (u32x4u *) a;
+
+  for (int i = 0; i < 4; i++)
+    {
+      i16x8 p1 = i16x8_pack (v4 == av[0], v4 == av[1]);
+      i16x8 p2 = i16x8_pack (v4 == av[2], v4 == av[3]);
+      mask |= (u64) i8x16_msb_mask (i8x16_pack (p1, p2)) << (i * 16);
+      av += 4;
+    }
+
+#else
+  for (int i = 0; i < n_elts; i++)
+    if (a[i] == v)
+      mask |= 1ULL << i;
+#endif
+  return mask;
+}
+
+/** \brief Compare 32-bit elemments with provied value and return bitmap
+
+    @param v value to compare elements with
+    @param a array of u32 elements
+    @param mask array of u64 where reuslting mask will be stored
+    @param n_elts number of elements in the array
+    @return none
+*/
+
+static_always_inline void
+clib_mask_compare_u32 (u32 v, u32 *a, u64 *bitmap, u32 n_elts)
+{
+  while (n_elts >= 64)
+    {
+      bitmap++[0] = clib_mask_compare_u32_x64 (v, a, 64);
+      n_elts -= 64;
+      a += 64;
+    }
+
+  if (PREDICT_TRUE (n_elts == 0))
+    return;
+
+  bitmap[0] = clib_mask_compare_u32_x64 (v, a, n_elts) & pow2_mask (n_elts);
+}
+
+static_always_inline u32 *
+clib_compress_u32_x64 (u32 *dst, u32 *src, u64 mask)
+{
+#if defined(CLIB_HAVE_VEC512_COMPRESS)
+  u32x16u *sv = (u32x16u *) src;
+  for (int i = 0; i < 4; i++)
+    {
+      int cnt = _popcnt32 ((u16) mask);
+      u32x16_compress_store (sv[i], mask, dst);
+      dst += cnt;
+      mask >>= 16;
+    }
+
+#elif defined(CLIB_HAVE_VEC256_COMPRESS)
+  u32x8u *sv = (u32x8u *) src;
+  for (int i = 0; i < 8; i++)
+    {
+      int cnt = _popcnt32 ((u8) mask);
+      u32x8_compress_store (sv[i], mask, dst);
+      dst += cnt;
+      mask >>= 8;
+    }
+#else
+  while (mask)
+    {
+      u16 bit = count_trailing_zeros (mask);
+      mask = clear_lowest_set_bit (mask);
+      dst++[0] = src[bit];
+    }
+#endif
+  return dst;
+}
+
+/** \brief Compare array of 32-bit elemments into destination array based on
+ * mask
+
+    @param dst destination array of u32 elements
+    @param src source array of u32 elements
+    @param mask array of u64 values representing compress mask
+    @param n_elts number of elements in the source array
+    @return number of elements stored in destionation array
+*/
+
+static_always_inline u32
+clib_compress_u32 (u32 *dst, u32 *src, u64 *mask, u32 n_elts)
+{
+  u32 *dst0 = dst;
+  while (n_elts >= 64)
+    {
+      if (mask[0] == ~0ULL)
+	{
+	  clib_memcpy_u32 (dst, src, 64);
+	  dst += 64;
+	}
+      else
+	dst = clib_compress_u32_x64 (dst, src, mask[0]);
+
+      mask++;
+      src += 64;
+      n_elts -= 64;
+    }
+
+  if (PREDICT_TRUE (n_elts == 0))
+    return dst - dst0;
+
+  return clib_compress_u32_x64 (dst, src, mask[0] & pow2_mask (n_elts)) - dst0;
+}
+
 #endif