[libc-commits] [libc] [libc] Make bigint trivially constructable (PR #206277)
via libc-commits
libc-commits at lists.llvm.org
Sun Jun 28 00:08: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 01/10] 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 02/10] 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 03/10] 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 04/10] 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 05/10] 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>
>From 6f2683603b1da33fe4973e08057ec231a04a5a15 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:27:09 +0530
Subject: [PATCH 06/10] integer_literals debug
---
.../src/__support/integer_literals_test.cpp | 310 +++++++++---------
1 file changed, 155 insertions(+), 155 deletions(-)
diff --git a/libc/test/src/__support/integer_literals_test.cpp b/libc/test/src/__support/integer_literals_test.cpp
index 5c626f43beb95..3e7ea59d86cda 100644
--- a/libc/test/src/__support/integer_literals_test.cpp
+++ b/libc/test/src/__support/integer_literals_test.cpp
@@ -1,155 +1,155 @@
-//===-- Unittests for user defined integer literals -----------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/__support/integer_literals.h"
-#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
-#include "test/UnitTest/Test.h"
-
-using LIBC_NAMESPACE::operator""_u8;
-using LIBC_NAMESPACE::operator""_u16;
-using LIBC_NAMESPACE::operator""_u32;
-using LIBC_NAMESPACE::operator""_u64;
-using LIBC_NAMESPACE::operator""_u128;
-using LIBC_NAMESPACE::operator""_u256;
-
-TEST(LlvmLibcIntegerLiteralTest, u8) {
- EXPECT_EQ(uint8_t(0), 0_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 255_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 0xFF_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 0b11111111_u8);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u16) {
- EXPECT_EQ(uint16_t(0), 0_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 255_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 0xFF_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 0b11111111_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 65535_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 0xFFFF_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 0b11111111'11111111_u16);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u32) {
- EXPECT_EQ(uint32_t(0), 0_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 255_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 0xFF_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 0b11111111_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 65535_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 0xFFFF_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 0b11111111'11111111_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 4294967295_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 0xFFFFFFFF_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u32);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u64) {
- EXPECT_EQ(uint64_t(0), 0_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 255_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 0xFF_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 0b11111111_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 65535_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 0xFFFF_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 0b11111111'11111111_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 4294967295_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 0xFFFFFFFF_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u64);
- EXPECT_EQ(uint64_t(UINT64_MAX), 18446744073709551615_u64);
- EXPECT_EQ(uint64_t(UINT64_MAX), 0xFFFFFFFF'FFFFFFFF_u64);
- EXPECT_EQ(
- uint64_t(UINT64_MAX),
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u128) {
-#ifdef LIBC_TYPES_HAS_INT128
- const __uint128_t ZERO = 0;
- const __uint128_t U8_MAX = UINT8_MAX;
- const __uint128_t U16_MAX = UINT16_MAX;
- const __uint128_t U32_MAX = UINT32_MAX;
- const __uint128_t U64_MAX = UINT64_MAX;
- const __uint128_t U128_MAX = (U64_MAX << 64) | U64_MAX;
-#else
- const UInt128 ZERO = 0;
- const UInt128 U8_MAX = UINT8_MAX;
- const UInt128 U16_MAX = UINT16_MAX;
- const UInt128 U32_MAX = UINT32_MAX;
- const UInt128 U64_MAX = UINT64_MAX;
- const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX;
-#endif // LIBC_TYPES_HAS_INT128
- EXPECT_EQ(ZERO, 0_u128);
- EXPECT_EQ(U8_MAX, 255_u128);
- EXPECT_EQ(U8_MAX, 0xFF_u128);
- EXPECT_EQ(U8_MAX, 0b11111111_u128);
- EXPECT_EQ(U16_MAX, 65535_u128);
- EXPECT_EQ(U16_MAX, 0xFFFF_u128);
- EXPECT_EQ(U16_MAX, 0b11111111'11111111_u128);
- EXPECT_EQ(U32_MAX, 4294967295_u128);
- EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u128);
- EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u128);
- EXPECT_EQ(U64_MAX, 18446744073709551615_u128);
- EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u128);
- EXPECT_EQ(
- U64_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
- EXPECT_EQ(U128_MAX, 340282366920938463463374607431768211455_u128);
- EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u128);
- EXPECT_EQ(
- U128_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u256) {
- using UInt256 = LIBC_NAMESPACE::UInt<256>;
- const UInt256 ZERO = 0;
- const UInt256 U8_MAX = UINT8_MAX;
- const UInt256 U16_MAX = UINT16_MAX;
- const UInt256 U32_MAX = UINT32_MAX;
- const UInt256 U64_MAX = UINT64_MAX;
- const UInt256 U128_MAX = (U64_MAX << 64) | U64_MAX;
- const UInt256 U256_MAX = (U128_MAX << 128) | U128_MAX;
- EXPECT_EQ(ZERO, 0_u256);
- EXPECT_EQ(U8_MAX, 255_u256);
- EXPECT_EQ(U8_MAX, 0xFF_u256);
- EXPECT_EQ(U8_MAX, 0b11111111_u256);
- EXPECT_EQ(U16_MAX, 65535_u256);
- EXPECT_EQ(U16_MAX, 0xFFFF_u256);
- EXPECT_EQ(U16_MAX, 0b11111111'11111111_u256);
- EXPECT_EQ(U32_MAX, 4294967295_u256);
- EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u256);
- EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u256);
- EXPECT_EQ(U64_MAX, 18446744073709551615_u256);
- EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u256);
- EXPECT_EQ(
- U64_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256);
- EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
- EXPECT_EQ(
- U256_MAX,
- 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, parse_bigint) {
- using T = LIBC_NAMESPACE::Int<128>;
- struct {
- const char *str;
- T expected;
- } constexpr TEST_CASES[] = {
- {"0", 0}, {"-1", -1}, {"+1", 1}, {"-0xFF", -255}, {"-0b11", -3},
- };
- for (auto tc : TEST_CASES) {
- T actual = LIBC_NAMESPACE::parse_bigint<T>(tc.str);
- EXPECT_EQ(actual, tc.expected);
- }
-}
-
-TEST(LlvmLibcIntegerLiteralTest, parse_bigint_invalid) {
- using T = LIBC_NAMESPACE::Int<128>;
- const T expected; // default construction
- EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(nullptr), expected);
- EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(""), expected);
-}
+//===-- Unittests for user defined integer literals -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::operator""_u8;
+using LIBC_NAMESPACE::operator""_u16;
+using LIBC_NAMESPACE::operator""_u32;
+using LIBC_NAMESPACE::operator""_u64;
+using LIBC_NAMESPACE::operator""_u128;
+using LIBC_NAMESPACE::operator""_u256;
+
+TEST(LlvmLibcIntegerLiteralTest, u8) {
+ EXPECT_EQ(uint8_t(0), 0_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 255_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 0xFF_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 0b11111111_u8);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u16) {
+ EXPECT_EQ(uint16_t(0), 0_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 255_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 0xFF_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 0b11111111_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 65535_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 0xFFFF_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 0b11111111'11111111_u16);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u32) {
+ EXPECT_EQ(uint32_t(0), 0_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 255_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 0xFF_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 0b11111111_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 65535_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 0xFFFF_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 0b11111111'11111111_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 4294967295_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 0xFFFFFFFF_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u32);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u64) {
+ EXPECT_EQ(uint64_t(0), 0_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 255_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 0xFF_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 0b11111111_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 65535_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 0xFFFF_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 0b11111111'11111111_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 4294967295_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 0xFFFFFFFF_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u64);
+ EXPECT_EQ(uint64_t(UINT64_MAX), 18446744073709551615_u64);
+ EXPECT_EQ(uint64_t(UINT64_MAX), 0xFFFFFFFF'FFFFFFFF_u64);
+ EXPECT_EQ(
+ uint64_t(UINT64_MAX),
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u128) {
+#ifdef LIBC_TYPES_HAS_INT128
+ const __uint128_t ZERO = 0;
+ const __uint128_t U8_MAX = UINT8_MAX;
+ const __uint128_t U16_MAX = UINT16_MAX;
+ const __uint128_t U32_MAX = UINT32_MAX;
+ const __uint128_t U64_MAX = UINT64_MAX;
+ const __uint128_t U128_MAX = (U64_MAX << 64) | U64_MAX;
+#else
+ const UInt128 ZERO = 0;
+ const UInt128 U8_MAX = UINT8_MAX;
+ const UInt128 U16_MAX = UINT16_MAX;
+ const UInt128 U32_MAX = UINT32_MAX;
+ const UInt128 U64_MAX = UINT64_MAX;
+ const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX;
+#endif // LIBC_TYPES_HAS_INT128
+ EXPECT_EQ(ZERO, 0_u128);
+ EXPECT_EQ(U8_MAX, 255_u128);
+ EXPECT_EQ(U8_MAX, 0xFF_u128);
+ EXPECT_EQ(U8_MAX, 0b11111111_u128);
+ EXPECT_EQ(U16_MAX, 65535_u128);
+ EXPECT_EQ(U16_MAX, 0xFFFF_u128);
+ EXPECT_EQ(U16_MAX, 0b11111111'11111111_u128);
+ EXPECT_EQ(U32_MAX, 4294967295_u128);
+ EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u128);
+ EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u128);
+ EXPECT_EQ(U64_MAX, 18446744073709551615_u128);
+ EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u128);
+ EXPECT_EQ(
+ U64_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
+ EXPECT_EQ(U128_MAX, 340282366920938463463374607431768211455_u128);
+ EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u128);
+ EXPECT_EQ(
+ U128_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u256) {
+ using UInt256 = LIBC_NAMESPACE::UInt<256>;
+ const UInt256 ZERO = 0;
+ const UInt256 U8_MAX = UINT8_MAX;
+ const UInt256 U16_MAX = UINT16_MAX;
+ const UInt256 U32_MAX = UINT32_MAX;
+ const UInt256 U64_MAX = UINT64_MAX;
+ const UInt256 U128_MAX = (U64_MAX << 64) | U64_MAX;
+ const UInt256 U256_MAX = (U128_MAX << 128) | U128_MAX;
+ EXPECT_EQ(ZERO, 0_u256);
+ EXPECT_EQ(U8_MAX, 255_u256);
+ EXPECT_EQ(U8_MAX, 0xFF_u256);
+ EXPECT_EQ(U8_MAX, 0b11111111_u256);
+ EXPECT_EQ(U16_MAX, 65535_u256);
+ EXPECT_EQ(U16_MAX, 0xFFFF_u256);
+ EXPECT_EQ(U16_MAX, 0b11111111'11111111_u256);
+ EXPECT_EQ(U32_MAX, 4294967295_u256);
+ EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u256);
+ EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u256);
+ EXPECT_EQ(U64_MAX, 18446744073709551615_u256);
+ EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u256);
+ EXPECT_EQ(
+ U64_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256);
+ EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
+ EXPECT_EQ(
+ U256_MAX,
+ 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, parse_bigint) {
+ using T = LIBC_NAMESPACE::Int<128>;
+ struct {
+ const char *str;
+ T expected;
+ } constexpr TEST_CASES[] = {
+ {"0", 0}, {"-1", -1}, {"+1", 1}, {"-0xFF", -255}, {"-0b11", -3},
+ };
+ for (auto tc : TEST_CASES) {
+ T actual = LIBC_NAMESPACE::parse_bigint<T>(tc.str);
+ EXPECT_EQ(actual, tc.expected);
+ }
+}
+
+TEST(LlvmLibcIntegerLiteralTest, parse_bigint_invalid) {
+ using T = LIBC_NAMESPACE::Int<128>;
+ const T expected{}; // default construction
+ EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(nullptr), expected);
+ EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(""), expected);
+}
>From 4cc8f63bc88bdb49093f251fdd95f4f92716ca93 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:40:37 +0530
Subject: [PATCH 07/10] debug big_int errs + init
---
libc/src/__support/big_int.h | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 1d7de31da1d1d..98bab6a7127ac 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -339,8 +339,8 @@ struct BigInt {
"WordType must be unsigned integer.");
struct Division {
- BigInt quotient;
- BigInt remainder;
+ BigInt quotient{};
+ BigInt remainder{};
};
public:
@@ -692,7 +692,7 @@ struct BigInt {
// And finally we perform some extra alignment steps for the remaining bits.
LIBC_INLINE constexpr cpp::optional<BigInt>
div_uint_half_times_pow_2(multiword::half_width_t<WordType> x, size_t e) {
- BigInt remainder;
+ BigInt remainder{};
if (x == 0)
return cpp::nullopt;
if (e >= Bits) {
@@ -700,7 +700,7 @@ struct BigInt {
*this = BigInt<Bits, false, WordType>();
return remainder;
}
- BigInt quotient;
+ BigInt quotient{};
WordType x_word = static_cast<WordType>(x);
constexpr size_t LOG2_WORD_SIZE =
static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1);
@@ -1010,7 +1010,7 @@ struct BigInt {
LIBC_INLINE constexpr static Division divide_unsigned(const BigInt ÷nd,
const BigInt ÷r) {
BigInt remainder = dividend;
- BigInt quotient;
+ BigInt quotient{};
if (remainder >= divider) {
BigInt subtractor = divider;
int cur_bit = multiword::countl_zero(subtractor.val) -
@@ -1392,6 +1392,11 @@ first_trailing_one(T value) {
return value == 0 ? 0 : cpp::countr_zero(value) + 1;
}
+static_assert(LIBC_NAMESPACE::cpp::is_trivially_constructible<
+ LIBC_NAMESPACE::BigInt<128, false>>::value);
+static_assert(LIBC_NAMESPACE::cpp::is_trivially_copyable<
+ LIBC_NAMESPACE::BigInt<128, false>>::value);
+
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_BIG_INT_H
>From 34ffde1e25c507127de52e924d8f52a468986089 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:43:12 +0530
Subject: [PATCH 08/10] crlf-> lf
---
.../src/__support/integer_literals_test.cpp | 310 +++++++++---------
1 file changed, 155 insertions(+), 155 deletions(-)
diff --git a/libc/test/src/__support/integer_literals_test.cpp b/libc/test/src/__support/integer_literals_test.cpp
index 3e7ea59d86cda..fe51cfb8e4f65 100644
--- a/libc/test/src/__support/integer_literals_test.cpp
+++ b/libc/test/src/__support/integer_literals_test.cpp
@@ -1,155 +1,155 @@
-//===-- Unittests for user defined integer literals -----------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/__support/integer_literals.h"
-#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
-#include "test/UnitTest/Test.h"
-
-using LIBC_NAMESPACE::operator""_u8;
-using LIBC_NAMESPACE::operator""_u16;
-using LIBC_NAMESPACE::operator""_u32;
-using LIBC_NAMESPACE::operator""_u64;
-using LIBC_NAMESPACE::operator""_u128;
-using LIBC_NAMESPACE::operator""_u256;
-
-TEST(LlvmLibcIntegerLiteralTest, u8) {
- EXPECT_EQ(uint8_t(0), 0_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 255_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 0xFF_u8);
- EXPECT_EQ(uint8_t(UINT8_MAX), 0b11111111_u8);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u16) {
- EXPECT_EQ(uint16_t(0), 0_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 255_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 0xFF_u16);
- EXPECT_EQ(uint16_t(UINT8_MAX), 0b11111111_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 65535_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 0xFFFF_u16);
- EXPECT_EQ(uint16_t(UINT16_MAX), 0b11111111'11111111_u16);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u32) {
- EXPECT_EQ(uint32_t(0), 0_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 255_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 0xFF_u32);
- EXPECT_EQ(uint32_t(UINT8_MAX), 0b11111111_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 65535_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 0xFFFF_u32);
- EXPECT_EQ(uint32_t(UINT16_MAX), 0b11111111'11111111_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 4294967295_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 0xFFFFFFFF_u32);
- EXPECT_EQ(uint32_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u32);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u64) {
- EXPECT_EQ(uint64_t(0), 0_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 255_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 0xFF_u64);
- EXPECT_EQ(uint64_t(UINT8_MAX), 0b11111111_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 65535_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 0xFFFF_u64);
- EXPECT_EQ(uint64_t(UINT16_MAX), 0b11111111'11111111_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 4294967295_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 0xFFFFFFFF_u64);
- EXPECT_EQ(uint64_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u64);
- EXPECT_EQ(uint64_t(UINT64_MAX), 18446744073709551615_u64);
- EXPECT_EQ(uint64_t(UINT64_MAX), 0xFFFFFFFF'FFFFFFFF_u64);
- EXPECT_EQ(
- uint64_t(UINT64_MAX),
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u128) {
-#ifdef LIBC_TYPES_HAS_INT128
- const __uint128_t ZERO = 0;
- const __uint128_t U8_MAX = UINT8_MAX;
- const __uint128_t U16_MAX = UINT16_MAX;
- const __uint128_t U32_MAX = UINT32_MAX;
- const __uint128_t U64_MAX = UINT64_MAX;
- const __uint128_t U128_MAX = (U64_MAX << 64) | U64_MAX;
-#else
- const UInt128 ZERO = 0;
- const UInt128 U8_MAX = UINT8_MAX;
- const UInt128 U16_MAX = UINT16_MAX;
- const UInt128 U32_MAX = UINT32_MAX;
- const UInt128 U64_MAX = UINT64_MAX;
- const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX;
-#endif // LIBC_TYPES_HAS_INT128
- EXPECT_EQ(ZERO, 0_u128);
- EXPECT_EQ(U8_MAX, 255_u128);
- EXPECT_EQ(U8_MAX, 0xFF_u128);
- EXPECT_EQ(U8_MAX, 0b11111111_u128);
- EXPECT_EQ(U16_MAX, 65535_u128);
- EXPECT_EQ(U16_MAX, 0xFFFF_u128);
- EXPECT_EQ(U16_MAX, 0b11111111'11111111_u128);
- EXPECT_EQ(U32_MAX, 4294967295_u128);
- EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u128);
- EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u128);
- EXPECT_EQ(U64_MAX, 18446744073709551615_u128);
- EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u128);
- EXPECT_EQ(
- U64_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
- EXPECT_EQ(U128_MAX, 340282366920938463463374607431768211455_u128);
- EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u128);
- EXPECT_EQ(
- U128_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, u256) {
- using UInt256 = LIBC_NAMESPACE::UInt<256>;
- const UInt256 ZERO = 0;
- const UInt256 U8_MAX = UINT8_MAX;
- const UInt256 U16_MAX = UINT16_MAX;
- const UInt256 U32_MAX = UINT32_MAX;
- const UInt256 U64_MAX = UINT64_MAX;
- const UInt256 U128_MAX = (U64_MAX << 64) | U64_MAX;
- const UInt256 U256_MAX = (U128_MAX << 128) | U128_MAX;
- EXPECT_EQ(ZERO, 0_u256);
- EXPECT_EQ(U8_MAX, 255_u256);
- EXPECT_EQ(U8_MAX, 0xFF_u256);
- EXPECT_EQ(U8_MAX, 0b11111111_u256);
- EXPECT_EQ(U16_MAX, 65535_u256);
- EXPECT_EQ(U16_MAX, 0xFFFF_u256);
- EXPECT_EQ(U16_MAX, 0b11111111'11111111_u256);
- EXPECT_EQ(U32_MAX, 4294967295_u256);
- EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u256);
- EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u256);
- EXPECT_EQ(U64_MAX, 18446744073709551615_u256);
- EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u256);
- EXPECT_EQ(
- U64_MAX,
- 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256);
- EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
- EXPECT_EQ(
- U256_MAX,
- 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
-}
-
-TEST(LlvmLibcIntegerLiteralTest, parse_bigint) {
- using T = LIBC_NAMESPACE::Int<128>;
- struct {
- const char *str;
- T expected;
- } constexpr TEST_CASES[] = {
- {"0", 0}, {"-1", -1}, {"+1", 1}, {"-0xFF", -255}, {"-0b11", -3},
- };
- for (auto tc : TEST_CASES) {
- T actual = LIBC_NAMESPACE::parse_bigint<T>(tc.str);
- EXPECT_EQ(actual, tc.expected);
- }
-}
-
-TEST(LlvmLibcIntegerLiteralTest, parse_bigint_invalid) {
- using T = LIBC_NAMESPACE::Int<128>;
- const T expected{}; // default construction
- EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(nullptr), expected);
- EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(""), expected);
-}
+//===-- Unittests for user defined integer literals -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::operator""_u8;
+using LIBC_NAMESPACE::operator""_u16;
+using LIBC_NAMESPACE::operator""_u32;
+using LIBC_NAMESPACE::operator""_u64;
+using LIBC_NAMESPACE::operator""_u128;
+using LIBC_NAMESPACE::operator""_u256;
+
+TEST(LlvmLibcIntegerLiteralTest, u8) {
+ EXPECT_EQ(uint8_t(0), 0_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 255_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 0xFF_u8);
+ EXPECT_EQ(uint8_t(UINT8_MAX), 0b11111111_u8);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u16) {
+ EXPECT_EQ(uint16_t(0), 0_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 255_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 0xFF_u16);
+ EXPECT_EQ(uint16_t(UINT8_MAX), 0b11111111_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 65535_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 0xFFFF_u16);
+ EXPECT_EQ(uint16_t(UINT16_MAX), 0b11111111'11111111_u16);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u32) {
+ EXPECT_EQ(uint32_t(0), 0_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 255_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 0xFF_u32);
+ EXPECT_EQ(uint32_t(UINT8_MAX), 0b11111111_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 65535_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 0xFFFF_u32);
+ EXPECT_EQ(uint32_t(UINT16_MAX), 0b11111111'11111111_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 4294967295_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 0xFFFFFFFF_u32);
+ EXPECT_EQ(uint32_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u32);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u64) {
+ EXPECT_EQ(uint64_t(0), 0_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 255_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 0xFF_u64);
+ EXPECT_EQ(uint64_t(UINT8_MAX), 0b11111111_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 65535_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 0xFFFF_u64);
+ EXPECT_EQ(uint64_t(UINT16_MAX), 0b11111111'11111111_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 4294967295_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 0xFFFFFFFF_u64);
+ EXPECT_EQ(uint64_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u64);
+ EXPECT_EQ(uint64_t(UINT64_MAX), 18446744073709551615_u64);
+ EXPECT_EQ(uint64_t(UINT64_MAX), 0xFFFFFFFF'FFFFFFFF_u64);
+ EXPECT_EQ(
+ uint64_t(UINT64_MAX),
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u128) {
+#ifdef LIBC_TYPES_HAS_INT128
+ const __uint128_t ZERO = 0;
+ const __uint128_t U8_MAX = UINT8_MAX;
+ const __uint128_t U16_MAX = UINT16_MAX;
+ const __uint128_t U32_MAX = UINT32_MAX;
+ const __uint128_t U64_MAX = UINT64_MAX;
+ const __uint128_t U128_MAX = (U64_MAX << 64) | U64_MAX;
+#else
+ const UInt128 ZERO = 0;
+ const UInt128 U8_MAX = UINT8_MAX;
+ const UInt128 U16_MAX = UINT16_MAX;
+ const UInt128 U32_MAX = UINT32_MAX;
+ const UInt128 U64_MAX = UINT64_MAX;
+ const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX;
+#endif // LIBC_TYPES_HAS_INT128
+ EXPECT_EQ(ZERO, 0_u128);
+ EXPECT_EQ(U8_MAX, 255_u128);
+ EXPECT_EQ(U8_MAX, 0xFF_u128);
+ EXPECT_EQ(U8_MAX, 0b11111111_u128);
+ EXPECT_EQ(U16_MAX, 65535_u128);
+ EXPECT_EQ(U16_MAX, 0xFFFF_u128);
+ EXPECT_EQ(U16_MAX, 0b11111111'11111111_u128);
+ EXPECT_EQ(U32_MAX, 4294967295_u128);
+ EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u128);
+ EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u128);
+ EXPECT_EQ(U64_MAX, 18446744073709551615_u128);
+ EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u128);
+ EXPECT_EQ(
+ U64_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
+ EXPECT_EQ(U128_MAX, 340282366920938463463374607431768211455_u128);
+ EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u128);
+ EXPECT_EQ(
+ U128_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, u256) {
+ using UInt256 = LIBC_NAMESPACE::UInt<256>;
+ const UInt256 ZERO = 0;
+ const UInt256 U8_MAX = UINT8_MAX;
+ const UInt256 U16_MAX = UINT16_MAX;
+ const UInt256 U32_MAX = UINT32_MAX;
+ const UInt256 U64_MAX = UINT64_MAX;
+ const UInt256 U128_MAX = (U64_MAX << 64) | U64_MAX;
+ const UInt256 U256_MAX = (U128_MAX << 128) | U128_MAX;
+ EXPECT_EQ(ZERO, 0_u256);
+ EXPECT_EQ(U8_MAX, 255_u256);
+ EXPECT_EQ(U8_MAX, 0xFF_u256);
+ EXPECT_EQ(U8_MAX, 0b11111111_u256);
+ EXPECT_EQ(U16_MAX, 65535_u256);
+ EXPECT_EQ(U16_MAX, 0xFFFF_u256);
+ EXPECT_EQ(U16_MAX, 0b11111111'11111111_u256);
+ EXPECT_EQ(U32_MAX, 4294967295_u256);
+ EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u256);
+ EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u256);
+ EXPECT_EQ(U64_MAX, 18446744073709551615_u256);
+ EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u256);
+ EXPECT_EQ(
+ U64_MAX,
+ 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256);
+ EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
+ EXPECT_EQ(
+ U256_MAX,
+ 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256);
+}
+
+TEST(LlvmLibcIntegerLiteralTest, parse_bigint) {
+ using T = LIBC_NAMESPACE::Int<128>;
+ struct {
+ const char *str;
+ T expected;
+ } constexpr TEST_CASES[] = {
+ {"0", 0}, {"-1", -1}, {"+1", 1}, {"-0xFF", -255}, {"-0b11", -3},
+ };
+ for (auto tc : TEST_CASES) {
+ T actual = LIBC_NAMESPACE::parse_bigint<T>(tc.str);
+ EXPECT_EQ(actual, tc.expected);
+ }
+}
+
+TEST(LlvmLibcIntegerLiteralTest, parse_bigint_invalid) {
+ using T = LIBC_NAMESPACE::Int<128>;
+ const T expected{}; // default construction
+ EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(nullptr), expected);
+ EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(""), expected);
+}
>From 25a51076667c5dc4b7d0d15d6691156d2758650c Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 01:54:22 +0530
Subject: [PATCH 09/10] big_int remaining
---
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 98bab6a7127ac..26af527dc2255 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -607,7 +607,7 @@ struct BigInt {
template <size_t OtherBits>
LIBC_INLINE constexpr auto
ful_mul(const BigInt<OtherBits, Signed, WordType> &other) const {
- BigInt<Bits + OtherBits, Signed, WordType> result;
+ BigInt<Bits + OtherBits, Signed, WordType> result{};
multiword::multiply_with_carry(result.val, val, other.val);
return result;
}
>From 639c4630b84107378432b26a5edffdb7a3c394f2 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 28 Jun 2026 12:38:20 +0530
Subject: [PATCH 10/10] bit_cast
---
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 26af527dc2255..a23386214e21a 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 {
-// 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::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