Index: include/llvm/ADT/APInt.h =================================================================== --- include/llvm/ADT/APInt.h +++ include/llvm/ADT/APInt.h @@ -1038,7 +1038,9 @@ /// the validity of the less-than relationship. /// /// \returns true if *this < RHS when considered unsigned. - bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); } + bool ult(uint64_t RHS) const { + return getActiveBits() > 64 ? false : getZExtValue() < RHS; + } /// \brief Signed less than comparison /// @@ -1054,7 +1056,9 @@ /// the validity of the less-than relationship. /// /// \returns true if *this < RHS when considered signed. - bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); } + bool slt(int64_t RHS) const { + return getMinSignedBits() >= 64 ? isNegative() : getSExtValue() < RHS; + } /// \brief Unsigned less or equal comparison /// @@ -1070,7 +1074,7 @@ /// the validity of the less-or-equal relationship. /// /// \returns true if *this <= RHS when considered unsigned. - bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); } + bool ule(uint64_t RHS) const { return ult(RHS) || *this == RHS; } /// \brief Signed less or equal comparison /// @@ -1086,7 +1090,7 @@ /// validity of the less-or-equal relationship. /// /// \returns true if *this <= RHS when considered signed. - bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); } + bool sle(int64_t RHS) const { return slt(RHS) || *this == RHS; } /// \brief Unsigned greather than comparison /// @@ -1102,7 +1106,7 @@ /// the validity of the greater-than relationship. /// /// \returns true if *this > RHS when considered unsigned. - bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); } + bool ugt(uint64_t RHS) const { return !ult(RHS) && *this != RHS; } /// \brief Signed greather than comparison /// @@ -1118,7 +1122,7 @@ /// the validity of the greater-than relationship. /// /// \returns true if *this > RHS when considered signed. - bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); } + bool sgt(int64_t RHS) const { return !slt(RHS) && *this != RHS; } /// \brief Unsigned greater or equal comparison /// @@ -1134,7 +1138,7 @@ /// the validity of the greater-or-equal relationship. /// /// \returns true if *this >= RHS when considered unsigned. - bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); } + bool uge(uint64_t RHS) const { return !ult(RHS); } /// \brief Signed greather or equal comparison /// @@ -1150,7 +1154,7 @@ /// the validity of the greater-or-equal relationship. /// /// \returns true if *this >= RHS when considered signed. - bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); } + bool sge(int64_t RHS) const { return !slt(RHS); } /// This operation tests if there are any pairs of corresponding bits /// between this APInt and RHS that are both set. Index: unittests/ADT/APIntTest.cpp =================================================================== --- unittests/ADT/APIntTest.cpp +++ unittests/ADT/APIntTest.cpp @@ -215,6 +215,79 @@ } } +TEST(APIntTest, compare) { + EXPECT_TRUE(!APInt(8, 1).uge(256)); + EXPECT_TRUE(!APInt(8, 1).ugt(256)); + EXPECT_TRUE( APInt(8, 1).ule(256)); + EXPECT_TRUE( APInt(8, 1).ult(256)); + EXPECT_TRUE(!APInt(8, 1).sge(256)); + EXPECT_TRUE(!APInt(8, 1).sgt(256)); + EXPECT_TRUE( APInt(8, 1).sle(256)); + EXPECT_TRUE( APInt(8, 1).slt(256)); + EXPECT_TRUE(!(APInt(8, 0) == 256)); + EXPECT_TRUE( APInt(8, 0) != 256); + EXPECT_TRUE(!(APInt(8, 1) == 256)); + EXPECT_TRUE( APInt(8, 1) != 256); + + auto uint64max = std::numeric_limits::max(); + auto int64max = std::numeric_limits::max(); + auto int64min = std::numeric_limits::min(); + + auto u64 = APInt{128, uint64max}; + auto s64 = APInt{128, static_cast(int64max), true}; + auto big = u64 + 1; + + EXPECT_TRUE( u64.uge(uint64max)); + EXPECT_TRUE(!u64.ugt(uint64max)); + EXPECT_TRUE( u64.ule(uint64max)); + EXPECT_TRUE(!u64.ult(uint64max)); + EXPECT_TRUE( u64.sge(int64max)); + EXPECT_TRUE( u64.sgt(int64max)); + EXPECT_TRUE(!u64.sle(int64max)); + EXPECT_TRUE(!u64.slt(int64max)); + EXPECT_TRUE( u64.sge(int64min)); + EXPECT_TRUE( u64.sgt(int64min)); + EXPECT_TRUE(!u64.sle(int64min)); + EXPECT_TRUE(!u64.slt(int64min)); + + EXPECT_TRUE(u64 == uint64max); + EXPECT_TRUE(u64 != int64max); + EXPECT_TRUE(u64 != int64min); + + EXPECT_TRUE(!s64.uge(uint64max)); + EXPECT_TRUE(!s64.ugt(uint64max)); + EXPECT_TRUE( s64.ule(uint64max)); + EXPECT_TRUE( s64.ult(uint64max)); + EXPECT_TRUE( s64.sge(int64max)); + EXPECT_TRUE(!s64.sgt(int64max)); + EXPECT_TRUE( s64.sle(int64max)); + EXPECT_TRUE(!s64.slt(int64max)); + EXPECT_TRUE( s64.sge(int64min)); + EXPECT_TRUE( s64.sgt(int64min)); + EXPECT_TRUE(!s64.sle(int64min)); + EXPECT_TRUE(!s64.slt(int64min)); + + EXPECT_TRUE(s64 != uint64max); + EXPECT_TRUE(s64 == int64max); + EXPECT_TRUE(s64 != int64min); + + EXPECT_TRUE( big.uge(uint64max)); + EXPECT_TRUE( big.ugt(uint64max)); + EXPECT_TRUE(!big.ule(uint64max)); + EXPECT_TRUE(!big.ult(uint64max)); + EXPECT_TRUE( big.sge(int64max)); + EXPECT_TRUE( big.sgt(int64max)); + EXPECT_TRUE(!big.sle(int64max)); + EXPECT_TRUE(!big.slt(int64max)); + EXPECT_TRUE( big.sge(int64min)); + EXPECT_TRUE( big.sgt(int64min)); + EXPECT_TRUE(!big.sle(int64min)); + EXPECT_TRUE(!big.slt(int64min)); + + EXPECT_TRUE(big != uint64max); + EXPECT_TRUE(big != int64max); + EXPECT_TRUE(big != int64min); +} // Tests different div/rem varaints using scheme (a * b + c) / a void testDiv(APInt a, APInt b, APInt c) {