Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/42359 )
Change subject: base: Teach gem5 how to use 128 bit types for
multiplication.
......................................................................
base: Teach gem5 how to use 128 bit types for multiplication.
gcc provides __uint128_t and __int128_t types which represent 128 bit
wide unsigned and signed integers, respectively. We can detect that
extension and use it to perform wide multiplication which takes
advantage of the built in single multiply instruction on x86 hardware
without having to compute the value manually with 64 bit variables.
Since both gcc and clang should support this extension and the manual
version may not be exercised normally, this change also extends the
gtest for intmath so that it will explicitly run the manual versions of
these functions. On systems with the extension both versions will be
tested, and on other systems the manual version will be harmlessly
tested twice.
Change-Id: I32640679396584cd43bc91a3f7e649c6e6f94afa
---
M src/base/intmath.hh
M src/base/intmath.test.cc
2 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/src/base/intmath.hh b/src/base/intmath.hh
index 8c88ae7..b88e41c 100644
--- a/src/base/intmath.hh
+++ b/src/base/intmath.hh
@@ -145,8 +145,8 @@
template <typename T>
static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
-mulUnsigned(std::make_unsigned_t<T> &hi, std::make_unsigned_t<T> &low,
- std::make_unsigned_t<T> val_a, std::make_unsigned_t<T> val_b)
+mulUnsignedManual(std::make_unsigned_t<T> &hi, std::make_unsigned_t<T>
&low,
+ std::make_unsigned_t<T> val_a, std::make_unsigned_t<T>
val_b)
{
low = val_a * val_b;
@@ -170,8 +170,22 @@
template <typename T>
static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
-mulSigned(std::make_signed_t<T> &hi, std::make_signed_t<T> &low,
- std::make_signed_t<T> val_a, std::make_signed_t<T> val_b)
+mulUnsigned(std::make_unsigned_t<T> &hi, std::make_unsigned_t<T> &low,
+ std::make_unsigned_t<T> val_a, std::make_unsigned_t<T> val_b)
+{
+#ifdef __SIZEOF_INT128__
+ __uint128_t val = (__uint128_t)val_a * (__uint128_t)val_b;
+ low = val;
+ hi = (val >> 64);
+#else
+ mulUnsignedManual<T>(hi, low, val_a, val_b);
+#endif
+}
+
+template <typename T>
+static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
+mulSignedManual(std::make_signed_t<T> &hi, std::make_signed_t<T> &low,
+ std::make_signed_t<T> val_a, std::make_signed_t<T> val_b)
{
uint64_t u_hi, u_low;
mulUnsigned<T>(u_hi, u_low, val_a, val_b);
@@ -185,6 +199,20 @@
low = u_low;
}
+template <typename T>
+static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
+mulSigned(std::make_signed_t<T> &hi, std::make_signed_t<T> &low,
+ std::make_signed_t<T> val_a, std::make_signed_t<T> val_b)
+{
+#ifdef __SIZEOF_INT128__
+ __int128_t val = (__int128_t)val_a * (__int128_t)val_b;
+ low = val;
+ hi = (val >> 64);
+#else
+ mulSignedManual<T>(hi, low, val_a, val_b);
+#endif
+}
+
/**
* @ingroup api_base_utils
*/
diff --git a/src/base/intmath.test.cc b/src/base/intmath.test.cc
index 55d3e71..707d5cb 100644
--- a/src/base/intmath.test.cc
+++ b/src/base/intmath.test.cc
@@ -139,6 +139,12 @@
mulUnsigned<uint64_t>(hi, low, a, b);
EXPECT_EQ(hi, 0x1);
EXPECT_EQ(low, 0xfffffffffffffffe);
+
+ hi = 0;
+ low = 0;
+ mulUnsignedManual<uint64_t>(hi, low, a, b);
+ EXPECT_EQ(hi, 0x1);
+ EXPECT_EQ(low, 0xfffffffffffffffe);
}
TEST(IntMathTest, mulSignedWide)
@@ -150,6 +156,12 @@
mulSigned<int64_t>(hi, low, a, b);
EXPECT_EQ(hi, 0x3fffffffffffffff);
EXPECT_EQ(low, -0x8000000000000000);
+
+ hi = 0;
+ low = 0;
+ mulSignedManual<int64_t>(hi, low, a, b);
+ EXPECT_EQ(hi, 0x3fffffffffffffff);
+ EXPECT_EQ(low, -0x8000000000000000);
}
TEST(IntmathTest, roundUp)
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42359
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: I32640679396584cd43bc91a3f7e649c6e6f94afa
Gerrit-Change-Number: 42359
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