diff mbox series

[02/21] common: Add *_get_bits inline functions

Message ID 20261027141645.ddfa34254050.If0c1b8004b8edd1fa48dd57b5b862cc64ed5bdfd@changeid
State New
Headers show
Series Control Integrity Protocol (CIP) support | expand

Commit Message

Benjamin Berg Oct. 27, 2026, 1:16 p.m. UTC
From: Benjamin Berg <benjamin.berg@intel.com>

Similar to what the kernel already has. Passing a value and field mask
returns the value of the given field (correctly masked and shifted).

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
Reviewed-by: Andrei Otcheretianski <andrei.otcheretianski@intel.com>
---
 src/utils/common.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff mbox series

Patch

diff --git a/src/utils/common.h b/src/utils/common.h
index 34f58daf64..eb434ebb5c 100644
--- a/src/utils/common.h
+++ b/src/utils/common.h
@@ -666,4 +666,24 @@  void * __hide_aliasing_typecast(void *foo);
 #define offsetofend(type, member) (offsetof(type, member) + sizeof(((type *) 0)->member))
 #endif
 
+
+static inline u64 field_multiplier(u64 field)
+{
+	/* keep the lowest bit in field */
+	return field & (~field + 1);
+}
+
+#define DEFINE_GET_BITS(type)						\
+static inline type							\
+type ## _get_bits(type val, type field)					\
+{									\
+	return (val & field) / field_multiplier(field);			\
+}
+
+DEFINE_GET_BITS(u64)
+DEFINE_GET_BITS(u32)
+DEFINE_GET_BITS(u16)
+DEFINE_GET_BITS(u8)
+#undef DEFINE_GET_BITS
+
 #endif /* COMMON_H */