Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/42384 )
Change subject: base: Add a szext function for true sign extension.
......................................................................
base: Add a szext function for true sign extension.
The existing sext function is a bit of a misnomer since it doesn't
actually sign extend its input, it just extends the sign if the sign bit
was zero.
This change adds a new szext function which truly sign extends the
value, although with a tiny amount of additional overhead.
Change-Id: I562ce479b771be8a3319934aeff55e797126a146
---
M src/base/bitfield.hh
M src/base/bitfield.test.cc
2 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh
index 9dc7722..b6e4edd 100644
--- a/src/base/bitfield.hh
+++ b/src/base/bitfield.hh
@@ -112,7 +112,8 @@
}
/**
- * Sign-extend an N-bit value to 64 bits.
+ * Sign-extend an N-bit value to 64 bits. Assumes all bits past the sign
are
+ * currently zero.
*
* @ingroup api_bitfield
*/
@@ -127,6 +128,24 @@
}
/**
+ * Sign-extend an N-bit value to 64 bits. Zero any bits past the sign if
+ * necessary.
+ *
+ * @ingroup api_bitfield
+ */
+template <int N>
+constexpr inline uint64_t
+szext(uint64_t val)
+{
+ bool sign_bit = bits(val, N - 1);
+ if (sign_bit)
+ val |= ~mask(N);
+ else
+ val &= mask(N);
+ return val;
+}
+
+/**
* Returns val with bits first to last set to the LSBs of bit_val
*
* E.g.:
diff --git a/src/base/bitfield.test.cc b/src/base/bitfield.test.cc
index 8097415..c2ef8d2 100644
--- a/src/base/bitfield.test.cc
+++ b/src/base/bitfield.test.cc
@@ -164,7 +164,8 @@
/*
* The following tests the "sext<N>(X)" function. sext carries out a sign
- * extention from N bits to 64 bits on value X.
+ * extention from N bits to 64 bits on value X. It does not zero bits past
the
+ * sign bit if it was zero.
*/
TEST(BitfieldTest, SignExtendPositiveInput)
{
@@ -191,6 +192,36 @@
uint64_t output = 0xF800000010000008;
EXPECT_EQ(output, sext<60>(val));
}
+/*
+ * The following tests the "szext<N>(X)" function. szext carries out a sign
+ * extention from N bits to 64 bits on value X. Will zero bits past the
sign
+ * bit if it was zero.
+ */
+TEST(BitfieldTest, SignZeroExtendPositiveInput)
+{
+ int8_t val = 14;
+ int64_t output = 14;
+ EXPECT_EQ(output, szext<8>(val));
+}
+
+TEST(BitfieldTest, SignZeroExtendNegativeInput)
+{
+ int8_t val = -14;
+ uint64_t output = -14;
+ EXPECT_EQ(output, szext<8>(val));
+}
+
+TEST(BitfieldTest, SignZeroExtendPositiveInputOutsideRange)
+{
+ EXPECT_EQ(0, szext<8>(1 << 10));
+}
+
+TEST(BitfieldTest, SignZeroExtendNegativeInputOutsideRange)
+{
+ uint64_t val = 0x4800000010000008;
+ uint64_t output = 0xF800000010000008;
+ EXPECT_EQ(output, szext<60>(val));
+}
/* The following tests "insertBits(A, B, C, D)". insertBits returns A
* with bits B to C set to D's (B - C) LSBs. "insertBits(A, B, D)"
overrides
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42384
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I562ce479b771be8a3319934aeff55e797126a146
Gerrit-Change-Number: 42384
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s