Gabe Black has submitted this change. ( 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
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/42359
Maintainer: Gabe Black <gabe.bl...@gmail.com>
Tested-by: kokoro <noreply+kok...@google.com>
Reviewed-by: Daniel Carvalho <oda...@yahoo.com.br>
---
M src/base/intmath.hh
M src/base/intmath.test.cc
2 files changed, 47 insertions(+), 5 deletions(-)

Approvals:
  Daniel Carvalho: Looks good to me, approved
  Gabe Black: Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/base/intmath.hh b/src/base/intmath.hh
index 308c33f..7acb889 100644
--- a/src/base/intmath.hh
+++ b/src/base/intmath.hh
@@ -135,7 +135,6 @@
 };

 /**
- * @ingroup api_base_utils
  * Multiply two values with place value p.
  *
  *  (A * p + a) * (B * p + b) =
@@ -150,8 +149,8 @@
  */
 template <typename T>
 static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
-mulUnsigned(std::make_unsigned_t<T> &high, 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> &high, 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;

@@ -178,8 +177,22 @@
  */
 template <typename T>
 static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
-mulSigned(std::make_signed_t<T> &high, 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> &high, 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;
+    high = (val >> 64);
+#else
+    mulUnsignedManual<T>(high, 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> &high, std::make_signed_t<T> &low,
+                std::make_signed_t<T> val_a, std::make_signed_t<T> val_b)
 {
     uint64_t u_high = 0, u_low = 0;
     mulUnsigned<T>(u_high, u_low, val_a, val_b);
@@ -194,6 +207,23 @@
 }

 /**
+ * @ingroup api_base_utils
+ */
+template <typename T>
+static constexpr std::enable_if_t<sizeof(T) == sizeof(uint64_t)>
+mulSigned(std::make_signed_t<T> &high, 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;
+    high = (val >> 64);
+#else
+    mulSignedManual<T>(high, low, val_a, val_b);
+#endif
+}
+
+/**
  * This function is used to align addresses in memory.
  *
  * @param val is the address to be aligned.
diff --git a/src/base/intmath.test.cc b/src/base/intmath.test.cc
index 4eb8aaf..b7fb34a 100644
--- a/src/base/intmath.test.cc
+++ b/src/base/intmath.test.cc
@@ -214,6 +214,12 @@
     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);
+
     a = 0;
     b = 0x5555555555555555;
     mulUnsigned<uint64_t>(hi, low, a, b);
@@ -231,6 +237,12 @@
     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);
+
     a = 0;
     b = -0x5555555555555555;
     mulSigned<int64_t>(hi, low, a, b);



7 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted one.
--
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: 9
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to