[llvm] r240626 - Express APInt::{s, u}{l, g}e(uint64_t) in terms of APInt::{s, u}{l, g}t(uint64_t). NFC.
Pawel Bylica
chfast at gmail.com
Thu Jun 25 03:23:52 PDT 2015
Author: chfast
Date: Thu Jun 25 05:23:52 2015
New Revision: 240626
URL: http://llvm.org/viewvc/llvm-project?rev=240626&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/ADT/APInt.h
llvm/trunk/unittests/ADT/APIntTest.cpp
Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=240626&r1=240625&r2=240626&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Thu Jun 25 05:23:52 2015
@@ -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.
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=240626&r1=240625&r2=240626&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Thu Jun 25 05:23:52 2015
@@ -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) {
More information about the llvm-commits
mailing list