[libc-commits] [libc] 5aa9efb - [libc] Fix bug in UInt comparison operators.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Tue Jun 21 22:41:27 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-06-22T05:41:07Z
New Revision: 5aa9efbab548f8ae4241c615a603addebbf0e15b

URL: https://github.com/llvm/llvm-project/commit/5aa9efbab548f8ae4241c615a603addebbf0e15b
DIFF: https://github.com/llvm/llvm-project/commit/5aa9efbab548f8ae4241c615a603addebbf0e15b.diff

LOG: [libc] Fix bug in UInt comparison operators.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D128303

Added: 
    

Modified: 
    libc/src/__support/CPP/UInt.h
    libc/test/src/__support/uint128_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/UInt.h b/libc/src/__support/CPP/UInt.h
index 87defdd4f98b9..8e002bdae0de0 100644
--- a/libc/src/__support/CPP/UInt.h
+++ b/libc/src/__support/CPP/UInt.h
@@ -247,33 +247,53 @@ template <size_t Bits> class UInt {
 
   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;
   }
 

diff  --git a/libc/test/src/__support/uint128_test.cpp b/libc/test/src/__support/uint128_test.cpp
index 4ef104cf2cfc4..c13b2be2d24f4 100644
--- a/libc/test/src/__support/uint128_test.cpp
+++ b/libc/test/src/__support/uint128_test.cpp
@@ -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);
+}


        


More information about the libc-commits mailing list