[gem5-dev] Change in gem5/gem5[develop]: base: Teach gem5 how to use 128 bit types for multiplication.

2021-04-22 Thread Gabe Black (Gerrit) via gem5-dev
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 
Tested-by: kokoro 
Reviewed-by: Daniel Carvalho 
---
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 
 static constexpr std::enable_if_t
-mulUnsigned(std::make_unsigned_t , std::make_unsigned_t ,
-std::make_unsigned_t val_a, std::make_unsigned_t val_b)
+mulUnsignedManual(std::make_unsigned_t , std::make_unsigned_t  
,
+  std::make_unsigned_t val_a, std::make_unsigned_t  
val_b)

 {
 low = val_a * val_b;

@@ -178,8 +177,22 @@
  */
 template 
 static constexpr std::enable_if_t
-mulSigned(std::make_signed_t , std::make_signed_t ,
-  std::make_signed_t val_a, std::make_signed_t val_b)
+mulUnsigned(std::make_unsigned_t , std::make_unsigned_t ,
+std::make_unsigned_t val_a, std::make_unsigned_t val_b)
+{
+#ifdef __SIZEOF_INT128__
+__uint128_t val = (__uint128_t)val_a * (__uint128_t)val_b;
+low = val;
+high = (val >> 64);
+#else
+mulUnsignedManual(high, low, val_a, val_b);
+#endif
+}
+
+template 
+static constexpr std::enable_if_t
+mulSignedManual(std::make_signed_t , std::make_signed_t ,
+std::make_signed_t val_a, std::make_signed_t val_b)
 {
 uint64_t u_high = 0, u_low = 0;
 mulUnsigned(u_high, u_low, val_a, val_b);
@@ -194,6 +207,23 @@
 }

 /**
+ * @ingroup api_base_utils
+ */
+template 
+static constexpr std::enable_if_t
+mulSigned(std::make_signed_t , std::make_signed_t ,
+  std::make_signed_t val_a, std::make_signed_t val_b)
+{
+#ifdef __SIZEOF_INT128__
+__int128_t val = (__int128_t)val_a * (__int128_t)val_b;
+low = val;
+high = (val >> 64);
+#else
+mulSignedManual(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, 0xfffe);

+hi = 0;
+low = 0;
+mulUnsignedManual(hi, low, a, b);
+EXPECT_EQ(hi, 0x1);
+EXPECT_EQ(low, 0xfffe);
+
 a = 0;
 b = 0x;
 mulUnsigned(hi, low, a, b);
@@ -231,6 +237,12 @@
 EXPECT_EQ(hi, 0x3fff);
 EXPECT_EQ(low, -0x8000);

+hi = 0;
+low = 0;
+mulSignedManual(hi, low, a, b);
+EXPECT_EQ(hi, 0x3fff);
+EXPECT_EQ(low, -0x8000);
+
 a = 0;
 b = -0x;
 mulSigned(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 
Gerrit-Reviewer: Bobby R. Bruce 
Gerrit-Reviewer: Daniel Carvalho 
Gerrit-Reviewer: Gabe Black 
Gerrit-Reviewer: kokoro 
Gerrit-MessageType: merged
___
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org

[gem5-dev] Change in gem5/gem5[develop]: base: Teach gem5 how to use 128 bit types for multiplication.

2021-03-06 Thread Gabe Black (Gerrit) via gem5-dev
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 
 static constexpr std::enable_if_t
-mulUnsigned(std::make_unsigned_t , std::make_unsigned_t ,
-std::make_unsigned_t val_a, std::make_unsigned_t val_b)
+mulUnsignedManual(std::make_unsigned_t , std::make_unsigned_t  
,
+  std::make_unsigned_t val_a, std::make_unsigned_t  
val_b)

 {
 low = val_a * val_b;

@@ -170,8 +170,22 @@

 template 
 static constexpr std::enable_if_t
-mulSigned(std::make_signed_t , std::make_signed_t ,
-  std::make_signed_t val_a, std::make_signed_t val_b)
+mulUnsigned(std::make_unsigned_t , std::make_unsigned_t ,
+std::make_unsigned_t val_a, std::make_unsigned_t val_b)
+{
+#ifdef __SIZEOF_INT128__
+__uint128_t val = (__uint128_t)val_a * (__uint128_t)val_b;
+low = val;
+hi = (val >> 64);
+#else
+mulUnsignedManual(hi, low, val_a, val_b);
+#endif
+}
+
+template 
+static constexpr std::enable_if_t
+mulSignedManual(std::make_signed_t , std::make_signed_t ,
+std::make_signed_t val_a, std::make_signed_t val_b)
 {
 uint64_t u_hi, u_low;
 mulUnsigned(u_hi, u_low, val_a, val_b);
@@ -185,6 +199,20 @@
 low = u_low;
 }

+template 
+static constexpr std::enable_if_t
+mulSigned(std::make_signed_t , std::make_signed_t ,
+  std::make_signed_t val_a, std::make_signed_t val_b)
+{
+#ifdef __SIZEOF_INT128__
+__int128_t val = (__int128_t)val_a * (__int128_t)val_b;
+low = val;
+hi = (val >> 64);
+#else
+mulSignedManual(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(hi, low, a, b);
 EXPECT_EQ(hi, 0x1);
 EXPECT_EQ(low, 0xfffe);
+
+hi = 0;
+low = 0;
+mulUnsignedManual(hi, low, a, b);
+EXPECT_EQ(hi, 0x1);
+EXPECT_EQ(low, 0xfffe);
 }

 TEST(IntMathTest, mulSignedWide)
@@ -150,6 +156,12 @@
 mulSigned(hi, low, a, b);
 EXPECT_EQ(hi, 0x3fff);
 EXPECT_EQ(low, -0x8000);
+
+hi = 0;
+low = 0;
+mulSignedManual(hi, low, a, b);
+EXPECT_EQ(hi, 0x3fff);
+EXPECT_EQ(low, -0x8000);
 }

 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 
Gerrit-MessageType: newchange
___
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