[libc] Fix bug in UInt comparison operators.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D128303
This commit is contained in:
Siva Chandra Reddy 2022-06-21 20:46:59 +00:00
parent 77ad77c071
commit 5aa9efbab5
2 changed files with 45 additions and 6 deletions

View File

@ -247,33 +247,53 @@ public:
constexpr bool operator>(const UInt<Bits> &other) const {
for (size_t i = WordCount; i > 0; --i) {
if (val[i - 1] <= other.val[i - 1])
uint64_t word = val[i - 1];
uint64_t other_word = other.val[i - 1];
if (word > other_word)
return true;
else if (word < other_word)
return false;
}
return true;
// Equal
return false;
}
constexpr bool operator>=(const UInt<Bits> &other) const {
for (size_t i = WordCount; i > 0; --i) {
if (val[i - 1] < other.val[i - 1])
uint64_t word = val[i - 1];
uint64_t other_word = other.val[i - 1];
if (word > other_word)
return true;
else if (word < other_word)
return false;
}
// Equal
return true;
}
constexpr bool operator<(const UInt<Bits> &other) const {
for (size_t i = WordCount; i > 0; --i) {
if (val[i - 1] >= other.val[i - 1])
uint64_t word = val[i - 1];
uint64_t other_word = other.val[i - 1];
if (word > other_word)
return false;
else if (word < other_word)
return true;
}
return true;
// Equal
return false;
}
constexpr bool operator<=(const UInt<Bits> &other) const {
for (size_t i = WordCount; i > 0; --i) {
if (val[i - 1] > other.val[i - 1])
uint64_t word = val[i - 1];
uint64_t other_word = other.val[i - 1];
if (word > other_word)
return false;
else if (word < other_word)
return true;
}
// Equal
return true;
}

View File

@ -161,3 +161,22 @@ TEST(LlvmLibcUInt128ClassTest, EqualsTests) {
ASSERT_FALSE(a1 == a_upper);
ASSERT_TRUE(a_lower != a_upper);
}
TEST(LlvmLibcUInt128ClassTest, ComparisonTests) {
UInt128 a({0xffffffff00000000, 0xffff00000000ffff});
UInt128 b({0xff00ff0000ff00ff, 0xf0f0f0f00f0f0f0f});
EXPECT_GT(a, b);
EXPECT_GE(a, b);
EXPECT_LT(b, a);
EXPECT_LE(b, a);
UInt128 x(0xffffffff00000000);
UInt128 y(0x00000000ffffffff);
EXPECT_GT(x, y);
EXPECT_GE(x, y);
EXPECT_LT(y, x);
EXPECT_LE(y, x);
EXPECT_LE(a, a);
EXPECT_GE(a, a);
}