diff --git a/include/sys/util.h b/include/sys/util.h index 08739ff9ec6..c9db6a654f4 100644 --- a/include/sys/util.h +++ b/include/sys/util.h @@ -58,6 +58,22 @@ extern "C" { #define GENMASK(h, l) \ (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) +/** @brief Extract the Least Significant Bit from @p value. */ +#define LSB_GET(value) ((value) & -(value)) + +/** + * @brief Extract a bitfield element from @p value corresponding to + * the field mask @p mask. + */ +#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask)) + +/** + * @brief Prepare a bitfield element using @p value with @p mask representing + * its field position and width. The result should be combined + * with other fields using a logical OR. + */ +#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask)) + /** @brief 0 if @p cond is true-ish; causes a compile error otherwise. */ #define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)