[libc-commits] [libc] [libc] Make bigint trivially constructable (PR #206277)
via libc-commits
libc-commits at lists.llvm.org
Sat Jun 27 12:47:46 PDT 2026
https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/206277
>From 1c07f07900f009398608b9d21ddd42aa26c2e304 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 00:16:39 +0530
Subject: [PATCH 1/5] test for constexpr failures
---
libc/src/__support/big_int.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 0e5c038ec356e..c1cf5b43fd1f5 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -358,7 +358,7 @@ struct BigInt {
LIBC_INLINE_VAR static constexpr size_t WORD_COUNT = Bits / WORD_SIZE;
- cpp::array<WordType, WORD_COUNT> val{}; // zero initialized.
+ cpp::array<WordType, WORD_COUNT> val; // zero initialized.
LIBC_INLINE constexpr BigInt() = default;
>From 7aabfa421ef39741ef5a8edeab6b294b7c1ca397 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 00:56:14 +0530
Subject: [PATCH 2/5] bigint changes
---
libc/src/__support/big_int.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index c1cf5b43fd1f5..c71dfacf51608 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -366,7 +366,8 @@ struct BigInt {
template <size_t OtherBits, bool OtherSigned, typename OtherWordType>
LIBC_INLINE constexpr BigInt(
- const BigInt<OtherBits, OtherSigned, OtherWordType> &other) {
+ const BigInt<OtherBits, OtherSigned, OtherWordType> &other)
+ : val{} {
using BigIntOther = BigInt<OtherBits, OtherSigned, OtherWordType>;
[[maybe_unused]] const bool should_sign_extend = Signed && other.is_neg();
@@ -491,7 +492,7 @@ struct BigInt {
LIBC_INLINE static constexpr BigInt one() { return BigInt(1); }
LIBC_INLINE static constexpr BigInt all_ones() { return ~zero(); }
LIBC_INLINE static constexpr BigInt min() {
- BigInt out;
+ BigInt out{};
if constexpr (SIGNED)
out.set_msb();
return out;
>From 266b0830202ad9c51621b8c6dd01c6021d43add8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:03:50 +0530
Subject: [PATCH 3/5] mroe changes in bigint
---
libc/src/__support/big_int.h | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index c71dfacf51608..019c932870f92 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -637,7 +637,7 @@ struct BigInt {
// 256 4 16 10 3
// 512 8 64 36 7
LIBC_INLINE constexpr BigInt quick_mul_hi(const BigInt &other) const {
- BigInt result;
+ BigInt result{};
multiword::quick_mul_hi(result.val, val, other.val);
return result;
}
@@ -850,7 +850,7 @@ struct BigInt {
#define DEFINE_BINOP(OP) \
LIBC_INLINE friend constexpr BigInt operator OP(const BigInt &lhs, \
const BigInt &rhs) { \
- BigInt result; \
+ BigInt result{}; \
for (size_t i = 0; i < WORD_COUNT; ++i) \
result[i] = lhs[i] OP rhs[i]; \
return result; \
@@ -868,7 +868,7 @@ struct BigInt {
#undef DEFINE_BINOP
LIBC_INLINE constexpr BigInt operator~() const {
- BigInt result;
+ BigInt result{};
for (size_t i = 0; i < WORD_COUNT; ++i)
result[i] = static_cast<WordType>(~val[i]);
return result;
@@ -1315,7 +1315,7 @@ mask_trailing_ones() {
return T::all_ones();
constexpr size_t QUOTIENT = count / T::WORD_SIZE;
constexpr size_t REMAINDER = count % T::WORD_SIZE;
- T out; // zero initialized
+ T out{}; // zero initialized
for (size_t i = 0; i <= QUOTIENT; ++i)
out[i] = i < QUOTIENT
? cpp::numeric_limits<typename T::word_type>::max()
@@ -1331,7 +1331,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() {
return T::all_ones();
constexpr size_t QUOTIENT = (T::BITS - count - 1U) / T::WORD_SIZE;
constexpr size_t REMAINDER = count % T::WORD_SIZE;
- T out; // zero initialized
+ T out{}; // zero initialized
for (size_t i = QUOTIENT; i < T::WORD_COUNT; ++i)
out[i] = i > QUOTIENT
? cpp::numeric_limits<typename T::word_type>::max()
>From fa370ee32c4b03e1e6e07c329476231303968df5 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:10:09 +0530
Subject: [PATCH 4/5] nits
---
libc/src/__support/big_int.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 019c932870f92..23677bd06fd68 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -457,20 +457,22 @@ struct BigInt {
}
// Construct a BigInt from a C array.
- template <size_t N> LIBC_INLINE constexpr BigInt(const WordType (&nums)[N]) {
+ template <size_t N>
+ LIBC_INLINE constexpr BigInt(const WordType (&nums)[N]) : val{} {
static_assert(N == WORD_COUNT);
for (size_t i = 0; i < WORD_COUNT; ++i)
val[i] = nums[i];
}
LIBC_INLINE constexpr explicit BigInt(
- const cpp::array<WordType, WORD_COUNT> &words) {
+ const cpp::array<WordType, WORD_COUNT> &words)
+ : val{} {
val = words;
}
// Initialize the first word to |v| and the rest to 0.
template <typename T, typename = cpp::enable_if_t<cpp::is_integral_v<T>>>
- LIBC_INLINE constexpr BigInt(T v) {
+ LIBC_INLINE constexpr BigInt(T v) : val{} {
constexpr size_t T_SIZE = sizeof(T) * CHAR_BIT;
const bool is_neg = v < 0;
for (size_t i = 0; i < WORD_COUNT; ++i) {
>From d4a92d25367033e13ec9e7d061e5f78f589523e2 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:17:23 +0530
Subject: [PATCH 5/5] temp remove bit_cast (bigint - TC )
---
libc/src/__support/big_int.h | 50 ++++++++++++++++++------------------
1 file changed, 25 insertions(+), 25 deletions(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 23677bd06fd68..1d7de31da1d1d 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -1186,31 +1186,31 @@ LIBC_INLINE_VAR constexpr bool is_unsigned_integral_or_big_int_v =
is_unsigned_integral_or_big_int<T>::value;
namespace cpp {
-
-// Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
-template <typename To, typename From>
-LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR cpp::enable_if_t<
- (sizeof(To) == sizeof(From)) && cpp::is_trivially_copyable<To>::value &&
- cpp::is_trivially_copyable<From>::value && is_big_int<To>::value,
- To>
-bit_cast(const From &from) {
- To out;
- using Storage = decltype(out.val);
- out.val = cpp::bit_cast<Storage>(from);
- return out;
-}
-
-// Specialization of cpp::bit_cast ('bit.h') from BigInt to T.
-template <typename To, size_t Bits>
-LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR
- cpp::enable_if_t<sizeof(To) == sizeof(UInt<Bits>) &&
- cpp::is_trivially_constructible<To>::value &&
- cpp::is_trivially_copyable<To>::value &&
- cpp::is_trivially_copyable<UInt<Bits>>::value,
- To>
- bit_cast(const UInt<Bits> &from) {
- return cpp::bit_cast<To>(from.val);
-}
+// Temporarily disabled
+// // Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
+// template <typename To, typename From>
+// LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR cpp::enable_if_t<
+// (sizeof(To) == sizeof(From)) && cpp::is_trivially_copyable<To>::value &&
+// cpp::is_trivially_copyable<From>::value && is_big_int<To>::value,
+// To>
+// bit_cast(const From &from) {
+// To out;
+// using Storage = decltype(out.val);
+// out.val = cpp::bit_cast<Storage>(from);
+// return out;
+// }
+
+// // Specialization of cpp::bit_cast ('bit.h') from BigInt to T.
+// template <typename To, size_t Bits>
+// LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR
+// cpp::enable_if_t<sizeof(To) == sizeof(UInt<Bits>) &&
+// cpp::is_trivially_constructible<To>::value &&
+// cpp::is_trivially_copyable<To>::value &&
+// cpp::is_trivially_copyable<UInt<Bits>>::value,
+// To>
+// bit_cast(const UInt<Bits> &from) {
+// return cpp::bit_cast<To>(from.val);
+// }
// Specialization of cpp::popcount ('bit.h') for BigInt.
template <typename T>
More information about the libc-commits
mailing list