llvm.org GIT mirror llvm / 780a0c9
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 Pawel Bylica 5 years ago
2 changed file(s) with 42 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
10691069 /// the validity of the less-or-equal relationship.
10701070 ///
10711071 /// \returns true if *this <= RHS when considered unsigned.
1072 bool ule(uint64_t RHS) const { return ule(APInt(getBitWidth(), RHS)); }
1072 bool ule(uint64_t RHS) const { return !ugt(RHS); }
10731073
10741074 /// \brief Signed less or equal comparison
10751075 ///
10851085 /// validity of the less-or-equal relationship.
10861086 ///
10871087 /// \returns true if *this <= RHS when considered signed.
1088 bool sle(uint64_t RHS) const { return sle(APInt(getBitWidth(), RHS)); }
1088 bool sle(uint64_t RHS) const { return !sgt(RHS); }
10891089
10901090 /// \brief Unsigned greather than comparison
10911091 ///
11331133 /// the validity of the greater-or-equal relationship.
11341134 ///
11351135 /// \returns true if *this >= RHS when considered unsigned.
1136 bool uge(uint64_t RHS) const { return uge(APInt(getBitWidth(), RHS)); }
1136 bool uge(uint64_t RHS) const { return !ult(RHS); }
11371137
11381138 /// \brief Signed greather or equal comparison
11391139 ///
11491149 /// the validity of the greater-or-equal relationship.
11501150 ///
11511151 /// \returns true if *this >= RHS when considered signed.
1152 bool sge(uint64_t RHS) const { return sge(APInt(getBitWidth(), RHS)); }
1152 bool sge(uint64_t RHS) const { return !slt(RHS); }
11531153
11541154 /// This operation tests if there are any pairs of corresponding bits
11551155 /// between this APInt and RHS that are both set.
212212 EXPECT_EQ(four, q);
213213 EXPECT_EQ(-one, r);
214214 }
215 }
216
217 TEST(APIntTest, compare) {
218 std::array testVals{{
219 APInt{16, 2},
220 APInt{16, 1},
221 APInt{16, 0},
222 APInt{16, (uint64_t)-1, true},
223 APInt{16, (uint64_t)-2, true},
224 }};
225
226 for (auto &arg1 : testVals)
227 for (auto &arg2 : testVals) {
228 auto uv1 = arg1.getZExtValue();
229 auto uv2 = arg2.getZExtValue();
230 auto sv1 = arg1.getSExtValue();
231 auto sv2 = arg2.getSExtValue();
232
233 EXPECT_EQ(uv1 < uv2, arg1.ult(arg2));
234 EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2));
235 EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2));
236 EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2));
237
238 EXPECT_EQ(sv1 < sv2, arg1.slt(arg2));
239 EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2));
240 EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2));
241 EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2));
242
243 EXPECT_EQ(uv1 < uv2, arg1.ult(uv2));
244 EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2));
245 EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2));
246 EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2));
247
248 EXPECT_EQ(sv1 < sv2, arg1.slt(sv2));
249 EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2));
250 EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2));
251 EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2));
252 }
215253 }
216254
217255