llvm.org GIT mirror llvm / c098b6e
Change APInt comparison with uint64_t. Summary: This patch changes the way APInt is compared with a value of type uint64_t. Before the uint64_t value was truncated to the size of APInt before comparison. Now the comparison takes into account full 64-bit precision. Test Plan: Unit tests added. No regressions. Self-hosted check-all done as well. Reviewers: chandlerc, dexonsmith Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D10655 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241204 91177308-0d34-0410-b5e6-96231b3b80d8 Pawel Bylica 5 years ago
2 changed file(s) with 140 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
10371037 /// the validity of the less-than relationship.
10381038 ///
10391039 /// \returns true if *this < RHS when considered unsigned.
1040 bool ult(uint64_t RHS) const { return ult(APInt(getBitWidth(), RHS)); }
1040 bool ult(uint64_t RHS) const {
1041 return getActiveBits() > 64 ? false : getZExtValue() < RHS;
1042 }
10411043
10421044 /// \brief Signed less than comparison
10431045 ///
10531055 /// the validity of the less-than relationship.
10541056 ///
10551057 /// \returns true if *this < RHS when considered signed.
1056 bool slt(uint64_t RHS) const { return slt(APInt(getBitWidth(), RHS)); }
1058 bool slt(int64_t RHS) const {
1059 return getMinSignedBits() > 64 ? isNegative() : getSExtValue() < RHS;
1060 }
10571061
10581062 /// \brief Unsigned less or equal comparison
10591063 ///
11011105 /// the validity of the greater-than relationship.
11021106 ///
11031107 /// \returns true if *this > RHS when considered unsigned.
1104 bool ugt(uint64_t RHS) const { return ugt(APInt(getBitWidth(), RHS)); }
1108 bool ugt(uint64_t RHS) const {
1109 return getActiveBits() > 64 ? true : getZExtValue() > RHS;
1110 }
11051111
11061112 /// \brief Signed greather than comparison
11071113 ///
11171123 /// the validity of the greater-than relationship.
11181124 ///
11191125 /// \returns true if *this > RHS when considered signed.
1120 bool sgt(uint64_t RHS) const { return sgt(APInt(getBitWidth(), RHS)); }
1126 bool sgt(int64_t RHS) const {
1127 return getMinSignedBits() > 64 ? !isNegative() : getSExtValue() > RHS;
1128 }
11211129
11221130 /// \brief Unsigned greater or equal comparison
11231131 ///
11491157 /// the validity of the greater-or-equal relationship.
11501158 ///
11511159 /// \returns true if *this >= RHS when considered signed.
1152 bool sge(uint64_t RHS) const { return !slt(RHS); }
1160 bool sge(int64_t RHS) const { return !slt(RHS); }
11531161
11541162 /// This operation tests if there are any pairs of corresponding bits
11551163 /// between this APInt and RHS that are both set.
251251 EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2));
252252 EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2));
253253 }
254 }
255
256 TEST(APIntTest, compareWithRawIntegers) {
257 EXPECT_TRUE(!APInt(8, 1).uge(256));
258 EXPECT_TRUE(!APInt(8, 1).ugt(256));
259 EXPECT_TRUE( APInt(8, 1).ule(256));
260 EXPECT_TRUE( APInt(8, 1).ult(256));
261 EXPECT_TRUE(!APInt(8, 1).sge(256));
262 EXPECT_TRUE(!APInt(8, 1).sgt(256));
263 EXPECT_TRUE( APInt(8, 1).sle(256));
264 EXPECT_TRUE( APInt(8, 1).slt(256));
265 EXPECT_TRUE(!(APInt(8, 0) == 256));
266 EXPECT_TRUE( APInt(8, 0) != 256);
267 EXPECT_TRUE(!(APInt(8, 1) == 256));
268 EXPECT_TRUE( APInt(8, 1) != 256);
269
270 auto uint64max = UINT64_MAX;
271 auto int64max = INT64_MAX;
272 auto int64min = INT64_MIN;
273
274 auto u64 = APInt{128, uint64max};
275 auto s64 = APInt{128, static_cast(int64max), true};
276 auto big = u64 + 1;
277
278 EXPECT_TRUE( u64.uge(uint64max));
279 EXPECT_TRUE(!u64.ugt(uint64max));
280 EXPECT_TRUE( u64.ule(uint64max));
281 EXPECT_TRUE(!u64.ult(uint64max));
282 EXPECT_TRUE( u64.sge(int64max));
283 EXPECT_TRUE( u64.sgt(int64max));
284 EXPECT_TRUE(!u64.sle(int64max));
285 EXPECT_TRUE(!u64.slt(int64max));
286 EXPECT_TRUE( u64.sge(int64min));
287 EXPECT_TRUE( u64.sgt(int64min));
288 EXPECT_TRUE(!u64.sle(int64min));
289 EXPECT_TRUE(!u64.slt(int64min));
290
291 EXPECT_TRUE(u64 == uint64max);
292 EXPECT_TRUE(u64 != int64max);
293 EXPECT_TRUE(u64 != int64min);
294
295 EXPECT_TRUE(!s64.uge(uint64max));
296 EXPECT_TRUE(!s64.ugt(uint64max));
297 EXPECT_TRUE( s64.ule(uint64max));
298 EXPECT_TRUE( s64.ult(uint64max));
299 EXPECT_TRUE( s64.sge(int64max));
300 EXPECT_TRUE(!s64.sgt(int64max));
301 EXPECT_TRUE( s64.sle(int64max));
302 EXPECT_TRUE(!s64.slt(int64max));
303 EXPECT_TRUE( s64.sge(int64min));
304 EXPECT_TRUE( s64.sgt(int64min));
305 EXPECT_TRUE(!s64.sle(int64min));
306 EXPECT_TRUE(!s64.slt(int64min));
307
308 EXPECT_TRUE(s64 != uint64max);
309 EXPECT_TRUE(s64 == int64max);
310 EXPECT_TRUE(s64 != int64min);
311
312 EXPECT_TRUE( big.uge(uint64max));
313 EXPECT_TRUE( big.ugt(uint64max));
314 EXPECT_TRUE(!big.ule(uint64max));
315 EXPECT_TRUE(!big.ult(uint64max));
316 EXPECT_TRUE( big.sge(int64max));
317 EXPECT_TRUE( big.sgt(int64max));
318 EXPECT_TRUE(!big.sle(int64max));
319 EXPECT_TRUE(!big.slt(int64max));
320 EXPECT_TRUE( big.sge(int64min));
321 EXPECT_TRUE( big.sgt(int64min));
322 EXPECT_TRUE(!big.sle(int64min));
323 EXPECT_TRUE(!big.slt(int64min));
324
325 EXPECT_TRUE(big != uint64max);
326 EXPECT_TRUE(big != int64max);
327 EXPECT_TRUE(big != int64min);
328 }
329
330 TEST(APIntTest, compareWithInt64Min) {
331 int64_t edge = INT64_MIN;
332 int64_t edgeP1 = edge + 1;
333 int64_t edgeM1 = INT64_MAX;
334 auto a = APInt{64, static_cast(edge), true};
335
336 EXPECT_TRUE(!a.slt(edge));
337 EXPECT_TRUE( a.sle(edge));
338 EXPECT_TRUE(!a.sgt(edge));
339 EXPECT_TRUE( a.sge(edge));
340 EXPECT_TRUE( a.slt(edgeP1));
341 EXPECT_TRUE( a.sle(edgeP1));
342 EXPECT_TRUE(!a.sgt(edgeP1));
343 EXPECT_TRUE(!a.sge(edgeP1));
344 EXPECT_TRUE( a.slt(edgeM1));
345 EXPECT_TRUE( a.sle(edgeM1));
346 EXPECT_TRUE(!a.sgt(edgeM1));
347 EXPECT_TRUE(!a.sge(edgeM1));
348 }
349
350 TEST(APIntTest, compareWithHalfInt64Max) {
351 uint64_t edge = 0x4000000000000000;
352 uint64_t edgeP1 = edge + 1;
353 uint64_t edgeM1 = edge - 1;
354 auto a = APInt{64, edge};
355
356 EXPECT_TRUE(!a.ult(edge));
357 EXPECT_TRUE( a.ule(edge));
358 EXPECT_TRUE(!a.ugt(edge));
359 EXPECT_TRUE( a.uge(edge));
360 EXPECT_TRUE( a.ult(edgeP1));
361 EXPECT_TRUE( a.ule(edgeP1));
362 EXPECT_TRUE(!a.ugt(edgeP1));
363 EXPECT_TRUE(!a.uge(edgeP1));
364 EXPECT_TRUE(!a.ult(edgeM1));
365 EXPECT_TRUE(!a.ule(edgeM1));
366 EXPECT_TRUE( a.ugt(edgeM1));
367 EXPECT_TRUE( a.uge(edgeM1));
368
369 EXPECT_TRUE(!a.slt(edge));
370 EXPECT_TRUE( a.sle(edge));
371 EXPECT_TRUE(!a.sgt(edge));
372 EXPECT_TRUE( a.sge(edge));
373 EXPECT_TRUE( a.slt(edgeP1));
374 EXPECT_TRUE( a.sle(edgeP1));
375 EXPECT_TRUE(!a.sgt(edgeP1));
376 EXPECT_TRUE(!a.sge(edgeP1));
377 EXPECT_TRUE(!a.slt(edgeM1));
378 EXPECT_TRUE(!a.sle(edgeM1));
379 EXPECT_TRUE( a.sgt(edgeM1));
380 EXPECT_TRUE( a.sge(edgeM1));
254381 }
255382
256383