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

Benjamin Berg benjamin at sipsolutions.net
Tue Oct 27 06:16:48 PDT 2026


From: Benjamin Berg <benjamin.berg at 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 at intel.com>
Reviewed-by: Andrei Otcheretianski <andrei.otcheretianski at intel.com>
---
 src/utils/common.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

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 */
-- 
2.56.0




More information about the Hostap mailing list