diff --git a/src/base/intmath.hh b/src/base/intmath.hh index 7acb8893cb..4be4a3b0e9 100644 --- a/src/base/intmath.hh +++ b/src/base/intmath.hh @@ -44,6 +44,7 @@ #include #include #include +#include #include "base/bitfield.hh" @@ -223,6 +224,24 @@ mulSigned(std::make_signed_t &high, std::make_signed_t &low, #endif } +template +static constexpr std::pair, std::make_unsigned_t> +mulUnsigned(std::make_unsigned_t val_a, std::make_unsigned_t val_b) +{ + std::make_unsigned_t hi, low; + mulUnsigned(hi, low, val_a, val_b); + return {hi, low}; +}; + +template +static constexpr std::pair, std::make_signed_t> +mulSigned(std::make_signed_t val_a, std::make_signed_t val_b) +{ + std::make_signed_t hi, low; + mulSigned(hi, low, val_a, val_b); + return {hi, low}; +}; + /** * This function is used to align addresses in memory. * diff --git a/src/base/intmath.test.cc b/src/base/intmath.test.cc index b7fb34aebc..e42a9a8bcc 100644 --- a/src/base/intmath.test.cc +++ b/src/base/intmath.test.cc @@ -38,6 +38,7 @@ */ #include +#include #include "base/intmath.hh" @@ -220,6 +221,12 @@ TEST(IntmathTest, mulUnsignedWide) EXPECT_EQ(hi, 0x1); EXPECT_EQ(low, 0xfffffffffffffffe); + hi = 0; + low = 0; + std::tie(hi, low) = mulUnsigned(a, b); + EXPECT_EQ(hi, 0x1); + EXPECT_EQ(low, 0xfffffffffffffffe); + a = 0; b = 0x5555555555555555; mulUnsigned(hi, low, a, b); @@ -243,6 +250,12 @@ TEST(IntmathTest, mulSignedWide) EXPECT_EQ(hi, 0x3fffffffffffffff); EXPECT_EQ(low, -0x8000000000000000); + hi = 0; + low = 0; + std::tie(hi, low) = mulSigned(a, b); + EXPECT_EQ(hi, 0x3fffffffffffffff); + EXPECT_EQ(low, -0x8000000000000000); + a = 0; b = -0x5555555555555555; mulSigned(hi, low, a, b);