Skip to contentSkip to conversation

Provide sextract32() and sextract64()

PATCH Applied Peter Maydell 3 participants 7 messages Jun 28, 2013 – Jul 23, 2013

lore
Share & tools
lore
#0 lore raw

cover Provide sextract32() and sextract64()

A common operation in instruction decoding is to take a field from an instruction that represents a signed integer in some arbitrary number of bits, and sign extend it into a C signed integer type for manipulation. Provide new functions sextract32() and sextract64() which perform this operation; they are like the existing extract32() and extract64() except that the field is sign-extended into the returned result.

I agree with Markus that sign-extending versions of our extract functions are more flexible than the simple sext() I proposed in an earlier RFC, hence the switch in this patch. The other change is patch 2, which provides some simple test cases. (Will be handy if we ever decide to revamp to avoid the dependency on shift-left-signed behaviour.)

Peter Maydell (2):
bitops: Provide sextract32() and sextract64()
tests: Add test-bitops.c with some sextract tests

 include/qemu/bitops.h |   50 +++++++++++++++++++++++++++++++++
 tests/Makefile        |    2 ++
 tests/test-bitops.c   |   75 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tests/test-bitops.c
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/2 bitops: Provide sextract32() and sextract64()

A common operation in instruction decoding is to take a field from an instruction that represents a signed integer in some arbitrary number of bits, and sign extend it into a C signed integer type for manipulation. Provide new functions sextract32() and sextract64() which perform this operation; they are like the existing extract32() and extract64() except that the field is sign-extended into the returned result.

---
 include/qemu/bitops.h |   50 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
include/qemu/bitops.h +50 -0
index affcc96..06e2e6f 100644
@@ -222,6 +222,56 @@ static inline uint64_t extract64(uint64_t value, int start, int length)
 }
 
 /**
+ * sextract32:
+ * @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
+ *
+ * Extract from the 32 bit input @value the bit field specified by the
+ * @start and @length parameters, and return it, sign extended to
+ * an int32_t (ie with the most significant bit of the field propagated
+ * to all the upper bits of the return value). The bit field must lie
+ * entirely within the 32 bit word. It is valid to request that
+ * all 32 bits are returned (ie @length 32 and @start 0).
+ *
+ * Returns: the sign extended value of the bit field extracted from the
+ * input value.
+ */
+static inline int32_t sextract32(uint32_t value, int start, int length)
+{
+    assert(start >= 0 && length > 0 && length <= 32 - start);
+    /* Note that this implementation relies on right shift of signed
+     * integers being an arithmetic shift.
+     */
+    return ((int32_t)(value << (32 - length - start))) >> (32 - length);
+}
+
+/**
+ * sextract64:
+ * @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
+ *
+ * Extract from the 64 bit input @value the bit field specified by the
+ * @start and @length parameters, and return it, sign extended to
+ * an int64_t (ie with the most significant bit of the field propagated
+ * to all the upper bits of the return value). The bit field must lie
+ * entirely within the 64 bit word. It is valid to request that
+ * all 64 bits are returned (ie @length 64 and @start 0).
+ *
+ * Returns: the sign extended value of the bit field extracted from the
+ * input value.
+ */
+static inline uint64_t sextract64(uint64_t value, int start, int length)
+{
+    assert(start >= 0 && length > 0 && length <= 64 - start);
+    /* Note that this implementation relies on right shift of signed
+     * integers being an arithmetic shift.
+     */
+    return ((int64_t)(value << (64 - length - start))) >> (64 - length);
+}
+
+/**
  * deposit32:
  * @value: initial value to insert bit field into
  * @start: the lowest bit in the bit field (numbered from 0)
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/2 tests: Add test-bitops.c with some sextract tests

In reply to Peter Maydell

Add some simple test cases for the new sextract32
and sextract64 functions.

---
 tests/Makefile      |    2 ++
 tests/test-bitops.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 tests/test-bitops.c
tests/Makefile +2 -0
index c107489..73a3c81 100644
@@ -44,6 +44,7 @@ check-unit-y += tests/test-cutils$(EXESUF)
 gcov-files-test-cutils-y += util/cutils.c
 check-unit-y += tests/test-mul64$(EXESUF)
 gcov-files-test-mul64-y = util/host-utils.c
+check-unit-y += tests/test-bitops$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -118,6 +119,7 @@ tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marsh
 tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 
 tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
+tests/test-bitops$(EXESUF): tests/test-bitops.o libqemuutil.a
 
 libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o
 libqos-obj-y += tests/libqos/i2c.o
tests/test-bitops.c new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
1.7.9.5
Reviewed-by on the v1 series
#3 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Peter Maydell (2):
bitops: Provide sextract32() and sextract64()
tests: Add test-bitops.c with some sextract tests

 include/qemu/bitops.h |   50 +++++++++++++++++++++++++++++++++
 tests/Makefile        |    2 ++
 tests/test-bitops.c   |   75 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tests/test-bitops.c

r~

#4 lore raw
In reply to Peter Maydell

Ping!

thanks
-- PMM

Quoted 25 lines · Peter Maydell

A common operation in instruction decoding is to take a field from an instruction that represents a signed integer in some arbitrary number of bits, and sign extend it into a C signed integer type for manipulation. Provide new functions sextract32() and sextract64() which perform this operation; they are like the existing extract32() and extract64() except that the field is sign-extended into the returned result.

I agree with Markus that sign-extending versions of our extract functions are more flexible than the simple sext() I proposed in an earlier RFC, hence the switch in this patch. The other change is patch 2, which provides some simple test cases. (Will be handy if we ever decide to revamp to avoid the dependency on shift-left-signed behaviour.)

Peter Maydell (2):
bitops: Provide sextract32() and sextract64()
tests: Add test-bitops.c with some sextract tests

 include/qemu/bitops.h |   50 +++++++++++++++++++++++++++++++++
 tests/Makefile        |    2 ++
 tests/test-bitops.c   |   75 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tests/test-bitops.c
#5 lore raw

Ping^2 !

thanks
-- PMM

Quoted 31 lines · Peter Maydell

Ping!

thanks
-- PMM

Quoted 25 lines · Peter Maydell

A common operation in instruction decoding is to take a field from an instruction that represents a signed integer in some arbitrary number of bits, and sign extend it into a C signed integer type for manipulation. Provide new functions sextract32() and sextract64() which perform this operation; they are like the existing extract32() and extract64() except that the field is sign-extended into the returned result.

I agree with Markus that sign-extending versions of our extract functions are more flexible than the simple sext() I proposed in an earlier RFC, hence the switch in this patch. The other change is patch 2, which provides some simple test cases. (Will be handy if we ever decide to revamp to avoid the dependency on shift-left-signed behaviour.)

Peter Maydell (2):
bitops: Provide sextract32() and sextract64()
tests: Add test-bitops.c with some sextract tests

 include/qemu/bitops.h |   50 +++++++++++++++++++++++++++++++++
 tests/Makefile        |    2 ++
 tests/test-bitops.c   |   75 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tests/test-bitops.c