[libc-commits] [PATCH] D128303: [libc] Fix bug in UInt comparison operators.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Jun 21 13:49:26 PDT 2022


sivachandra created this revision.
sivachandra added a reviewer: lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
sivachandra requested review of this revision.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128303

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


Index: libc/test/src/__support/uint128_test.cpp
===================================================================
--- libc/test/src/__support/uint128_test.cpp
+++ 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);
+}
Index: libc/src/__support/CPP/UInt.h
===================================================================
--- libc/src/__support/CPP/UInt.h
+++ libc/src/__support/CPP/UInt.h
@@ -247,33 +247,53 @@
 
   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;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128303.438824.patch
Type: text/x-patch
Size: 2525 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220621/498f31df/attachment-0001.bin>


More information about the libc-commits mailing list