diff --git a/libc/src/__support/CPP/UInt.h b/libc/src/__support/CPP/UInt.h --- a/libc/src/__support/CPP/UInt.h +++ b/libc/src/__support/CPP/UInt.h @@ -247,33 +247,53 @@ constexpr bool operator>(const UInt &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 &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 &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 &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; } diff --git a/libc/test/src/__support/uint128_test.cpp b/libc/test/src/__support/uint128_test.cpp --- a/libc/test/src/__support/uint128_test.cpp +++ b/libc/test/src/__support/uint128_test.cpp @@ -161,3 +161,22 @@ 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); +}