diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/ADT/APInt.h | 8 | ||||
-rw-r--r-- | llvm/unittests/ADT/APIntTest.cpp | 38 |
2 files changed, 42 insertions, 4 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index de56167320b..e0a6bbd8617 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1070,7 +1070,7 @@ public: /// 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 !ugt(RHS); } /// \brief Signed less or equal comparison /// @@ -1086,7 +1086,7 @@ public: /// 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(uint64_t RHS) const { return !sgt(RHS); } /// \brief Unsigned greather than comparison /// @@ -1134,7 +1134,7 @@ public: /// 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 +1150,7 @@ public: /// 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(uint64_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. diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index e4398f0f669..098140c50c6 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -215,6 +215,44 @@ TEST(APIntTest, i1) { } } +TEST(APIntTest, compare) { + std::array<APInt, 5> testVals{{ + APInt{16, 2}, + APInt{16, 1}, + APInt{16, 0}, + APInt{16, (uint64_t)-1, true}, + APInt{16, (uint64_t)-2, true}, + }}; + + for (auto &arg1 : testVals) + for (auto &arg2 : testVals) { + auto uv1 = arg1.getZExtValue(); + auto uv2 = arg2.getZExtValue(); + auto sv1 = arg1.getSExtValue(); + auto sv2 = arg2.getSExtValue(); + + EXPECT_EQ(uv1 < uv2, arg1.ult(arg2)); + EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2)); + EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2)); + EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2)); + + EXPECT_EQ(sv1 < sv2, arg1.slt(arg2)); + EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2)); + EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2)); + EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2)); + + EXPECT_EQ(uv1 < uv2, arg1.ult(uv2)); + EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2)); + EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2)); + EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2)); + + EXPECT_EQ(sv1 < sv2, arg1.slt(sv2)); + EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2)); + EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2)); + EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2)); + } +} + // Tests different div/rem varaints using scheme (a * b + c) / a void testDiv(APInt a, APInt b, APInt c) { |