It is a common code pattern to modify a bitfield by masking the field and performing a bitwise OR with the respective FIELD_PREP. Wrap such a task into a macro by introducing FIELD_MODIFY() which modifies the field specified by a mask from a bitfield by putting a val in the field. Signed-off-by: William Breathitt Gray <william.gray@xxxxxxxxxx> --- include/linux/bitfield.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index ebfa12f69501..b06a98f0a87f 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -38,8 +38,7 @@ * FIELD_PREP(REG_FIELD_D, 0x40); * * Modify: - * reg &= ~REG_FIELD_C; - * reg |= FIELD_PREP(REG_FIELD_C, c); + * reg = FIELD_MODIFY(REG_FIELD_C, reg, c); */ #define __bf_shf(x) (__builtin_ffsll(x) - 1) @@ -155,6 +154,21 @@ (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ }) +/** + * FIELD_MODIFY() - modify a bitfield element + * @_mask: shifted mask defining the field's length and position + * @_reg: value of entire bitfield + * @_val: value to put in the field + * + * FIELD_MODIFY() modifies the field specified by @_mask from the + * bitfield passed in as @_reg by putting @val in the field. + */ +#define FIELD_MODIFY(_mask, _reg, _val) \ + ({ \ + __BF_FIELD_CHECK(_mask, _reg, _val, "FIELD_MODIFY: "); \ + (typeof(_mask))(((_reg) & ~(_mask)) | FIELD_PREP(_mask, _val)); \ + }) + extern void __compiletime_error("value doesn't fit into mask") __field_overflow(void); extern void __compiletime_error("bad bitfield mask") -- 2.39.2