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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Mar 28 11:32:34 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:

While [we default to C++17](https://github.com/llvm/llvm-project/blob/d61ec513c42005bb071eb15386deb5de585ff267/libc/CMakeLists.txt#L29-L30), we do have a few violations (grep our cmake for `CXX_STANDARD`):
- https://github.com/llvm/llvm-project/blob/d61ec513c42005bb071eb15386deb5de585ff267/libc/src/stdlib/CMakeLists.txt#L414-L415
- https://github.com/llvm/llvm-project/blob/main/libc/test/src/network/CMakeLists.txt
- https://github.com/llvm/llvm-project/blob/main/libc/test/src/time/CMakeLists.txt

I don't think we document this version anywhere (is it supposed to implicitly match the rest of LLVM)?

Contrast this with libc++, which [REQUIRES](https://github.com/llvm/llvm-project/blob/d61ec513c42005bb071eb15386deb5de585ff267/libcxx/CMakeLists.txt#L522-L523) C++23 (though I couldn't find public docs stating that).

I'll note that `-std=gnu++20` [is recognized](https://godbolt.org/z/cnah4MTa6) by [the minimum versions of the compilers we support](https://libc.llvm.org/compiler_support.html).

Orthogonally, we should also raise our minimum supported compiler versions to match what's being tested in CI.

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


More information about the libc-commits mailing list