qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] include: bitops: Add mask extract64/deposit64


From: Alistair Francis
Subject: [PATCH 1/2] include: bitops: Add mask extract64/deposit64
Date: Mon, 5 Aug 2024 14:33:35 +1000

Based on the RISC-V get_field() and set_field() macros add
mask_extract64() and mask_deposit64() bitop functions. These can extrac
and deposit values into fields using a bit field mask directly instead
of a length and shift.

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 include/qemu/bitops.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
index 2c0a2fe751..dd26f4a6b5 100644
--- a/include/qemu/bitops.h
+++ b/include/qemu/bitops.h
@@ -409,6 +409,22 @@ static inline uint64_t extract64(uint64_t value, int 
start, int length)
     return (value >> start) & (~0ULL >> (64 - length));
 }
 
+/**
+ * mask_extract64:
+ * @value: the value to extract the bit field from
+ * @mask: the mask bit field to extract
+ *
+ * Extract from the 64 bit input @value the bit mask specified by the
+ * @mask parameter, and return it. The value returned is shifted
+ * so that only the bit field is returned.
+ *
+ * Returns: the value of the bit field extracted from the input value.
+ */
+static inline uint64_t mask_extract64(uint64_t value, uint64_t mask)
+{
+    return (value & mask) / (mask & ~(mask << 1));
+}
+
 /**
  * sextract32:
  * @value: the value to extract the bit field from
@@ -511,6 +527,25 @@ static inline uint64_t deposit64(uint64_t value, int 
start, int length,
     return (value & ~mask) | ((fieldval << start) & mask);
 }
 
+/**
+ * mask_deposit64:
+ * @value: initial value to insert bit field into
+ * @mask: the mask bit field
+ * @fieldval: the value to insert into the bit field
+ *
+ * Deposit @fieldval into the 64 bit @value at the bit field specified
+ * by the @mask parameter, and return the modified
+ * @value. Bits of @value outside the bit field are not modified.
+ * Bits of @fieldval above @mask bits are ignored.
+ *
+ * Returns: the modified @value.
+ */
+static inline uint64_t mask_deposit64(uint64_t value, uint64_t mask,
+                                      uint64_t fieldval)
+{
+    return (value & ~mask) | ((fieldval * (mask & ~(mask << 1))) & mask);
+}
+
 /**
  * half_shuffle32:
  * @x: 32-bit value (of which only the bottom 16 bits are of interest)
-- 
2.45.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]