Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/include/llvm/ADT/APInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,18 @@ class [[nodiscard]] APInt {
return I1.zext(I2.getBitWidth()) == I2;
}

/// Determine if two APInts have the same value, after sign-extending
/// one of them (if needed!) to ensure that the bit-widths match.
static bool isSameSignedValue(const APInt &I1, const APInt &I2) {
if (I1.getBitWidth() == I2.getBitWidth())
return I1 == I2;

if (I1.getBitWidth() > I2.getBitWidth())
return I1 == I2.sext(I1.getBitWidth());

return I1.sext(I2.getBitWidth()) == I2;
}

/// Overload to compute a hash_code for an APInt value.
LLVM_ABI friend hash_code hash_value(const APInt &Arg);

Expand Down
23 changes: 22 additions & 1 deletion llvm/unittests/ADT/APIntTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "llvm/Support/Alignment.h"
#include "gtest/gtest.h"
#include <array>
#include <climits>
#include <limits>
#include <optional>

Expand All @@ -29,6 +28,28 @@ TEST(APIntTest, ValueInit) {
EXPECT_TRUE(!Zero.sext(64));
}

TEST(APIntTest, IsSameValueAndIsSameSignedValue) {
APInt One8(8, 1, /*isSigned=*/false);
APInt Three4(4, 3, /*isSigned=*/false);
EXPECT_FALSE(APInt::isSameValue(One8, Three4));
EXPECT_FALSE(APInt::isSameSignedValue(One8, Three4));

APInt Two8(8, 2, /*isSigned=*/false);
APInt Two4(4, 2, /*isSigned=*/false);
EXPECT_TRUE(APInt::isSameValue(Two8, Two4));
EXPECT_TRUE(APInt::isSameSignedValue(Two8, Two4));

APInt Seven8(8, 7, /*isSigned=*/false);
APInt Seven3(3, 7, /*isSigned=*/false);
EXPECT_TRUE(APInt::isSameValue(Seven8, Seven3));
EXPECT_FALSE(APInt::isSameSignedValue(Seven8, Seven3));

APInt Ones8 = APInt::getAllOnes(8);
APInt Ones4 = APInt::getAllOnes(4);
EXPECT_FALSE(APInt::isSameValue(Ones8, Ones4));
EXPECT_TRUE(APInt::isSameSignedValue(Ones8, Ones4));
}

// Test that 0^5 == 0
TEST(APIntTest, PowZeroTo5) {
APInt Zero = APInt::getZero(32);
Expand Down