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.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
This commit is contained in:
Gabe Black
2021-03-05 23:04:49 -08:00
parent 6f5668be86
commit a18241a6c1
2 changed files with 47 additions and 5 deletions

View File

@@ -135,7 +135,6 @@ mulSigned(std::make_signed_t<T> &high, std::make_signed_t<T> &low,
};
/**
* @ingroup api_base_utils
* Multiply two values with place value p.
*
* (A * p + a) * (B * p + b) =
@@ -150,8 +149,8 @@ mulSigned(std::make_signed_t<T> &high, std::make_signed_t<T> &low,
*/
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 @@ mulUnsigned(std::make_unsigned_t<T> &high, std::make_unsigned_t<T> &low,
*/
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);
@@ -193,6 +206,23 @@ mulSigned(std::make_signed_t<T> &high, std::make_signed_t<T> &low,
low = u_low;
}
/**
* @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.
*

View File

@@ -214,6 +214,12 @@ TEST(IntmathTest, mulUnsignedWide)
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 @@ TEST(IntmathTest, mulSignedWide)
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);