[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1c1492558c1a72b64bb26f7a44c4e69fff0e6b44.1679149543.git.william.gray@linaro.org>
Date: Sat, 18 Mar 2023 10:59:49 -0400
From: William Breathitt Gray <william.gray@...aro.org>
To: linux-iio@...r.kernel.org
Cc: Johannes Berg <johannes.berg@...el.com>,
Jonathan Cameron <jic23@...nel.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org,
William Breathitt Gray <william.gray@...aro.org>
Subject: [PATCH 2/4] bitfield: Introduce the FIELD_MODIFY() macro
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@...aro.org>
---
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
Powered by blists - more mailing lists