[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 03/13] bitops.h: Add functions to extract and deposi
From: |
Peter Maydell |
Subject: |
[Qemu-devel] [PATCH 03/13] bitops.h: Add functions to extract and deposit bitfields |
Date: |
Thu, 28 Jun 2012 15:35:56 +0100 |
Add functions deposit32(), deposit64(), extract32() and extract64()
to extract and deposit bitfields in 32 and 64 bit words. Based on
ideas by Jia Liu and Avi Kivity.
Suggested-by: Jia Liu <address@hidden>
Suggested-by: Avi Kivity <address@hidden>
Signed-off-by: Peter Maydell <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
bitops.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 66 insertions(+), 0 deletions(-)
diff --git a/bitops.h b/bitops.h
index 07d1a06..5a44bb1 100644
--- a/bitops.h
+++ b/bitops.h
@@ -269,4 +269,70 @@ static inline unsigned long hweight_long(unsigned long w)
return count;
}
+/**
+ * extract32 - return a specified bit field from a uint32_t value
+ * @value: The value to extract the bit field from
+ * @start: The lowest bit in the bit field (numbered from 0)
+ * @length: The length of the bit field
+ *
+ * Returns the value of the bit field extracted from the input value.
+ */
+static inline uint32_t extract32(uint32_t value, int start, int length)
+{
+ assert(start >= 0 && length > 0 && length <= 32 - start);
+ return (value >> start) & (~0U >> (32 - length));
+}
+
+/**
+ * extract64 - return a specified bit field from a uint64_t value
+ * @value: The value to extract the bit field from
+ * @start: The lowest bit in the bit field (numbered from 0)
+ * @length: The length of the bit field
+ *
+ * Returns the value of the bit field extracted from the input value.
+ */
+static inline uint64_t extract64(uint64_t value, int start, int length)
+{
+ assert(start >= 0 && length > 0 && length <= 64 - start);
+ return (value >> start) & (~0ULL >> (64 - length));
+}
+
+/**
+ * deposit32 - Insert into a specified bit field in a uint32_t
+ * @value: Initial value to insert bit field into
+ * @start: The lowest bit in the bit field (numbered from 0)
+ * @length: The length of the bit field
+ * @fieldval: The value to insert into the bit field
+ *
+ * Returns the input value with the fieldval inserted
+ * into it at the specified location.
+ */
+static inline uint32_t deposit32(uint32_t value, int start, int length,
+ uint32_t fieldval)
+{
+ uint32_t mask;
+ assert(start >= 0 && length > 0 && length <= 32 - start);
+ mask = (~0U >> (32 - length)) << start;
+ return (value & ~mask) | ((fieldval << start) & mask);
+}
+
+/**
+ * deposit64 - Insert into a specified bit field in a uint64_t
+ * @value: Initial value to insert bit field into
+ * @start: The lowest bit in the bit field (numbered from 0)
+ * @length: The length of the bit field
+ * @fieldval: The value to insert into the bit field
+ *
+ * Returns the input value with the fieldval inserted
+ * into it at the specified location.
+ */
+static inline uint64_t deposit64(uint64_t value, int start, int length,
+ uint64_t fieldval)
+{
+ uint64_t mask;
+ assert(start >= 0 && length > 0 && length <= 64 - start);
+ mask = (~0ULL >> (64 - length)) << start;
+ return (value & ~mask) | ((fieldval << start) & mask);
+}
+
#endif
--
1.7.1
- [Qemu-devel] [PATCH 00/13] ARM: Add LPAE support, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 10/13] target-arm: Use target_phys_addr_t in get_phys_addr(), Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 02/13] target-arm: Fix typo that meant TTBR1 accesses went to TTBR0, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 09/13] target-arm: Add 64 bit PAR, TTBR0, TTBR1 for LPAE, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 07/13] target-arm: Add AMAIR0, AMAIR1 LPAE cp15 registers, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 04/13] ARM: Make target_phys_addr_t 64 bits and physaddrs 40 bits, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 08/13] target-arm: Add 64 bit variants of DBGDRAR and DBGDSAR for LPAE, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 12/13] target-arm: Implement TTBCR changes for LPAE, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 03/13] bitops.h: Add functions to extract and deposit bitfields,
Peter Maydell <=
- [Qemu-devel] [PATCH 05/13] target-arm: Implement privileged-execute-never (PXN), Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 11/13] target-arm: Implement long-descriptor PAR format, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 01/13] hw/cadence_gem: Make rx_desc_addr and tx_desc_addr uint32_t, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 13/13] target-arm: Add support for long format translation table walks, Peter Maydell, 2012/06/28
- [Qemu-devel] [PATCH 06/13] target-arm: Extend feature flags to 64 bits, Peter Maydell, 2012/06/28