diff mbox

[U-Boot,v3,05/16] include/bitfield: Add new bitfield operations

Message ID 1437746136-14379-2-git-send-email-codrin.ciubotariu@freescale.com
State Accepted
Delegated to: York Sun
Headers show

Commit Message

Codrin Ciubotariu July 24, 2015, 1:55 p.m. UTC
These new operations allow manipulation of bitfields
within a word by using a mask instead of width and
shift values to extract/replace the bitfields.

Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@freescale.com>
---

Changes for v3:
	- none; new patch;

 include/bitfield.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Joe Hershberger Aug. 7, 2015, 8:17 p.m. UTC | #1
Hi Codrin,

On Fri, Jul 24, 2015 at 8:55 AM, Codrin Ciubotariu
<codrin.ciubotariu@freescale.com> wrote:
> These new operations allow manipulation of bitfields
> within a word by using a mask instead of width and
> shift values to extract/replace the bitfields.
>
> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@freescale.com>
> ---
>
> Changes for v3:
>         - none; new patch;
>
>  include/bitfield.h | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
diff mbox

Patch

diff --git a/include/bitfield.h b/include/bitfield.h
index ec4815c..cffaf7a 100644
--- a/include/bitfield.h
+++ b/include/bitfield.h
@@ -27,6 +27,12 @@ 
  * old = bitfield_extract(old_reg_val, 10, 5);
  * new_reg_val = bitfield_replace(old_reg_val, 10, 5, new);
  *
+ * or
+ *
+ * mask = bitfield_mask(10, 5);
+ * old = bitfield_extract_by_mask(old_reg_val, mask);
+ * new_reg_val = bitfield_replace_by_mask(old_reg_val, mask, new);
+ *
  * The numbers 10 and 5 could for example come from data
  * tables which describe all bitfields in all registers.
  */
@@ -56,3 +62,29 @@  static inline uint bitfield_replace(uint reg_val, uint shift, uint width,
 
 	return (reg_val & ~mask) | (bitfield_val << shift);
 }
+
+/* Produces a shift of the bitfield given a mask */
+static inline uint bitfield_shift(uint mask)
+{
+	return mask ? ffs(mask) - 1 : 0;
+}
+
+/* Extract the value of a bitfield found within a given register value */
+static inline uint bitfield_extract_by_mask(uint reg_val, uint mask)
+{
+	uint shift = bitfield_shift(mask);
+
+	return (reg_val & mask) >> shift;
+}
+
+/*
+ * Replace the value of a bitfield found within a given register value
+ * Returns the newly modified uint value with the replaced field.
+ */
+static inline uint bitfield_replace_by_mask(uint reg_val, uint mask,
+					    uint bitfield_val)
+{
+	uint shift = bitfield_shift(mask);
+
+	return (reg_val & ~mask) | ((bitfield_val << shift) & mask);
+}