[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to
From: |
Peter Maydell |
Subject: |
[Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields |
Date: |
Mon, 25 Jun 2012 20:38:55 +0100 |
Add field32() and field64() functions which extract a particular
bit field from a word and return it. Based on an idea by Jia Liu.
Suggested-by: Jia Liu <address@hidden>
Signed-off-by: Peter Maydell <address@hidden>
---
Jia Liu had a function like this in the OpenRISC support patchset;
this implementation borrows the API but has a different implementation,
because I wanted to handle the length == wordsize case.
I've also provided a 64 bit version as well as a 32 bit one (alas, gcc
is not smart enough to notice that it only needs to do 32 bit arithmetic
if you pass in a uint32_t to the 64 bit function).
Based on previous experience with a different codebase I think that this
will result in much more comprehensible code than manual shift-and-mask
which is what we tend to do today.
No users yet, but I wanted to throw this out for review anyway. If
people really don't want it until it gets a first user I can throw it
into my next random patchset that does bit ops...
bitops.h | 28 ++++++++++++++++++++++++++++
1 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/bitops.h b/bitops.h
index 07d1a06..36e4c78 100644
--- a/bitops.h
+++ b/bitops.h
@@ -269,4 +269,32 @@ static inline unsigned long hweight_long(unsigned long w)
return count;
}
+/**
+ * field64 - 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 field64(uint64_t value, int start, int length)
+{
+ assert(start >= 0 && start <= 63 && length > 0 && start + length <= 64);
+ return (value >> start) & (~0ULL >> (64 - length));
+}
+
+/**
+ * field32 - 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 field32(uint32_t value, int start, int length)
+{
+ assert(start >= 0 && start <= 31 && length > 0 && start + length <= 32);
+ return (value >> start) & ~0U >> (32 - length);
+}
+
#endif
--
1.7.1
- [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields,
Peter Maydell <=
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, malc, 2012/06/25
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Blue Swirl, 2012/06/26
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Peter Maydell, 2012/06/26
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Blue Swirl, 2012/06/26
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Peter Maydell, 2012/06/26
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Blue Swirl, 2012/06/26
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Markus Armbruster, 2012/06/27
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Blue Swirl, 2012/06/27
- Re: [Qemu-devel] [PATCH] bitops.h: Add field32() and field64() functions to extract bitfields, Markus Armbruster, 2012/06/28