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

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 28 16:29:18 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;
+  }
----------------
nickdesaulniers wrote:

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

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


More information about the llvm-commits mailing list