From 780a0c9266692ca159eae149babf229a641725ab Mon Sep 17 00:00:00 2001 From: Pawel Bylica Date: Thu, 25 Jun 2015 10:23:52 +0000 Subject: [PATCH] Express APInt::{s,u}{l,g}e(uint64_t) in terms of APInt::{s,u}{l,g}t(uint64_t). NFC. This is preparation for http://reviews.llvm.org/D10655: Change APInt comparison with uint64_t. Some unit tests added also. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240626 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/APInt.h | 8 ++++---- unittests/ADT/APIntTest.cpp | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index de56167320b..e0a6bbd8617 100644 --- a/include/llvm/ADT/APInt.h +++ b/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/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index e4398f0f669..098140c50c6 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -215,6 +215,44 @@ TEST(APIntTest, i1) { } } +TEST(APIntTest, compare) { + std::array 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) { -- 2.34.1