[libc-commits] [libc] [llvm] [libc] Refactor `BigInt` (PR #86137)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Wed Mar 27 13:56:13 PDT 2024


================
@@ -930,9 +870,67 @@ struct BigInt {
   // Return the i-th word of the number.
   LIBC_INLINE constexpr WordType &operator[](size_t i) { return val[i]; }
 
-  LIBC_INLINE WordType *data() { return val; }
+private:
+  LIBC_INLINE friend constexpr int cmp(const BigInt &lhs, const BigInt &rhs) {
+    const auto compare = [](WordType a, WordType b) {
+      return a == b ? 0 : a > b ? 1 : -1;
+    };
+    if constexpr (Signed) {
+      const bool lhs_is_neg = lhs.is_neg();
+      const bool rhs_is_neg = rhs.is_neg();
+      if (lhs_is_neg != rhs_is_neg)
+        return rhs_is_neg ? 1 : -1;
+    }
+    for (size_t i = WORD_COUNT; i-- > 0;)
+      if (auto cmp = compare(lhs[i], rhs[i]); cmp != 0)
+        return cmp;
+    return 0;
+  }
----------------
gchatelet wrote:

I would **love** to use the spaceship operator but this is C++20.

https://github.com/llvm/llvm-project/pull/86137


More information about the libc-commits mailing list