aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2023-05-26 19:08:38 +0000
committerFlorin Coras <florin.coras@gmail.com>2023-05-27 17:44:23 +0000
commit1000125395e3bf4be5a9026650074e8fedbeb24b (patch)
tree72095d8c3d3597d92b85df61183bab7f7c3a129b /src
parent7e58d9b6258cc1c45af8aa4aa844489f05bf6af4 (diff)
vppinfra: add bit_extract_u32 and bit_extract_u64
Type: improvement Change-Id: Icfaa856aa4b50ad5c6828f1690ce3fb6ba08ec00 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src')
-rw-r--r--src/vppinfra/bitops.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/vppinfra/bitops.h b/src/vppinfra/bitops.h
index d19046e1d24..3d17f0d5ace 100644
--- a/src/vppinfra/bitops.h
+++ b/src/vppinfra/bitops.h
@@ -273,6 +273,48 @@ uword_bitmap_find_first_set (uword *bmp)
return (b - bmp) * uword_bits + get_lowest_set_bit_index (b[0]);
}
+static_always_inline u32
+bit_extract_u32 (u32 v, u32 mask)
+{
+#ifdef __BMI2__
+ return _pext_u32 (v, mask);
+#else
+ u32 rv = 0;
+ u32 bit = 1;
+
+ while (mask)
+ {
+ u32 lowest_mask_bit = get_lowest_set_bit (mask);
+ mask ^= lowest_mask_bit;
+ rv |= (v & lowest_mask_bit) ? bit : 0;
+ bit <<= 1;
+ }
+
+ return rv;
+#endif
+}
+
+static_always_inline u64
+bit_extract_u64 (u64 v, u64 mask)
+{
+#ifdef __BMI2__
+ return _pext_u64 (v, mask);
+#else
+ u64 rv = 0;
+ u64 bit = 1;
+
+ while (mask)
+ {
+ u64 lowest_mask_bit = get_lowest_set_bit (mask);
+ mask ^= lowest_mask_bit;
+ rv |= (v & lowest_mask_bit) ? bit : 0;
+ bit <<= 1;
+ }
+
+ return rv;
+#endif
+}
+
#else
#warning "already included"
#endif /* included_clib_bitops_h */