Skip to content

Commit 05420a1

Browse files
authored
Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74887)
This reverts the following commits: - a539a09 - 31316b3 Rationale for revert: #74258 (comment)
1 parent d86a937 commit 05420a1

File tree

5 files changed

+22
-81
lines changed

5 files changed

+22
-81
lines changed

libc/src/__support/UInt.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525

2626
namespace LIBC_NAMESPACE::cpp {
2727

28-
// BigInt has the same semantics as the 'standard integer types' and can be
29-
// safely 'bit_cast'ed to compatible types.
3028
template <size_t Bits, bool Signed> struct BigInt {
29+
3130
static_assert(Bits > 0 && Bits % 64 == 0,
3231
"Number of bits in BigInt should be a multiple of 64.");
3332
LIBC_INLINE_VAR static constexpr size_t WORDCOUNT = Bits / 64;
34-
uint64_t val[WORDCOUNT];
33+
uint64_t val[WORDCOUNT]{};
3534

3635
LIBC_INLINE_VAR static constexpr uint64_t MASK32 = 0xFFFFFFFFu;
3736

@@ -159,7 +158,7 @@ template <size_t Bits, bool Signed> struct BigInt {
159158

160159
LIBC_INLINE constexpr BigInt<Bits, Signed>
161160
operator+(const BigInt<Bits, Signed> &other) const {
162-
BigInt<Bits, Signed> result(0);
161+
BigInt<Bits, Signed> result;
163162
SumCarry<uint64_t> s{0, 0};
164163
for (size_t i = 0; i < WORDCOUNT; ++i) {
165164
s = add_with_carry(val[i], other.val[i], s.carry);
@@ -172,7 +171,7 @@ template <size_t Bits, bool Signed> struct BigInt {
172171
// it will always use the constexpr version of add_with_carry.
173172
LIBC_INLINE constexpr BigInt<Bits, Signed>
174173
operator+(BigInt<Bits, Signed> &&other) const {
175-
BigInt<Bits, Signed> result(0);
174+
BigInt<Bits, Signed> result;
176175
SumCarry<uint64_t> s{0, 0};
177176
for (size_t i = 0; i < WORDCOUNT; ++i) {
178177
s = add_with_carry_const(val[i], other.val[i], s.carry);
@@ -200,7 +199,7 @@ template <size_t Bits, bool Signed> struct BigInt {
200199

201200
LIBC_INLINE BigInt<Bits, Signed>
202201
operator-(const BigInt<Bits, Signed> &other) const {
203-
BigInt<Bits, Signed> result(0);
202+
BigInt<Bits, Signed> result;
204203
DiffBorrow<uint64_t> d{0, 0};
205204
for (size_t i = 0; i < WORDCOUNT; ++i) {
206205
d = sub_with_borrow(val[i], other.val[i], d.borrow);
@@ -211,7 +210,7 @@ template <size_t Bits, bool Signed> struct BigInt {
211210

212211
LIBC_INLINE constexpr BigInt<Bits, Signed>
213212
operator-(BigInt<Bits, Signed> &&other) const {
214-
BigInt<Bits, Signed> result(0);
213+
BigInt<Bits, Signed> result;
215214
DiffBorrow<uint64_t> d{0, 0};
216215
for (size_t i = 0; i < WORDCOUNT; ++i) {
217216
d = sub_with_borrow_const(val[i], other.val[i], d.borrow);
@@ -691,7 +690,7 @@ template <size_t Bits, bool Signed> struct BigInt {
691690

692691
LIBC_INLINE constexpr BigInt<Bits, Signed>
693692
operator&(const BigInt<Bits, Signed> &other) const {
694-
BigInt<Bits, Signed> result(0);
693+
BigInt<Bits, Signed> result;
695694
for (size_t i = 0; i < WORDCOUNT; ++i)
696695
result.val[i] = val[i] & other.val[i];
697696
return result;
@@ -706,7 +705,7 @@ template <size_t Bits, bool Signed> struct BigInt {
706705

707706
LIBC_INLINE constexpr BigInt<Bits, Signed>
708707
operator|(const BigInt<Bits, Signed> &other) const {
709-
BigInt<Bits, Signed> result(0);
708+
BigInt<Bits, Signed> result;
710709
for (size_t i = 0; i < WORDCOUNT; ++i)
711710
result.val[i] = val[i] | other.val[i];
712711
return result;
@@ -721,7 +720,7 @@ template <size_t Bits, bool Signed> struct BigInt {
721720

722721
LIBC_INLINE constexpr BigInt<Bits, Signed>
723722
operator^(const BigInt<Bits, Signed> &other) const {
724-
BigInt<Bits, Signed> result(0);
723+
BigInt<Bits, Signed> result;
725724
for (size_t i = 0; i < WORDCOUNT; ++i)
726725
result.val[i] = val[i] ^ other.val[i];
727726
return result;
@@ -735,7 +734,7 @@ template <size_t Bits, bool Signed> struct BigInt {
735734
}
736735

737736
LIBC_INLINE constexpr BigInt<Bits, Signed> operator~() const {
738-
BigInt<Bits, Signed> result(0);
737+
BigInt<Bits, Signed> result;
739738
for (size_t i = 0; i < WORDCOUNT; ++i)
740739
result.val[i] = ~val[i];
741740
return result;

libc/src/__support/float_to_string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ FloatToString<long double>::get_positive_block(int block_index) {
653653

654654
// shift_amount = -(c0 - exponent) = c_0 + 16 * ceil(exponent/16) - exponent
655655

656-
cpp::UInt<MID_INT_SIZE> val(0);
656+
cpp::UInt<MID_INT_SIZE> val;
657657
#ifdef LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE
658658
// ------------------------------ TABLE MODE -------------------------------
659659
const int32_t SHIFT_CONST = TABLE_SHIFT_CONST;
@@ -704,7 +704,7 @@ FloatToString<long double>::get_negative_block(int block_index) {
704704
if (exponent < 0) {
705705
const int32_t idx = -exponent / IDX_SIZE;
706706

707-
cpp::UInt<MID_INT_SIZE> val(0);
707+
cpp::UInt<MID_INT_SIZE> val;
708708
#ifdef LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE
709709
// ------------------------------ TABLE MODE -------------------------------
710710
const int32_t SHIFT_CONST = TABLE_SHIFT_CONST;

libc/test/src/__support/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ add_libc_test(
8989
SRCS
9090
uint_test.cpp
9191
DEPENDS
92-
libc.src.__support.CPP.bit
93-
libc.src.__support.CPP.optional
94-
libc.src.__support.CPP.type_traits
95-
libc.src.__support.macros.properties.float
9692
libc.src.__support.uint
93+
libc.src.__support.CPP.optional
9794
)
9895

9996
add_libc_test(

libc/test/src/__support/uint_test.cpp

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,23 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/CPP/bit.h" // bit_cast
109
#include "src/__support/CPP/optional.h"
11-
#include "src/__support/CPP/type_traits.h" // is_trivially_constructible
1210
#include "src/__support/UInt.h"
13-
#include "src/__support/macros/properties/float.h" // LIBC_COMPILER_HAS_FLOAT128
1411

1512
#include "test/UnitTest/Test.h"
1613

17-
#include <math.h> // HUGE_VALF, HUGE_VALF
18-
19-
namespace LIBC_NAMESPACE {
20-
21-
using LL_UInt64 = cpp::UInt<64>;
22-
// We want to test cpp::UInt<128> explicitly. So, for
14+
// We want to test LIBC_NAMESPACE::cpp::UInt<128> explicitly. So, for
2315
// convenience, we use a sugar which does not conflict with the UInt128 type
2416
// which can resolve to __uint128_t if the platform has it.
25-
using LL_UInt128 = cpp::UInt<128>;
26-
using LL_UInt192 = cpp::UInt<192>;
27-
using LL_UInt256 = cpp::UInt<256>;
28-
using LL_UInt320 = cpp::UInt<320>;
29-
using LL_UInt512 = cpp::UInt<512>;
30-
using LL_UInt1024 = cpp::UInt<1024>;
31-
32-
using LL_Int128 = cpp::Int<128>;
33-
using LL_Int192 = cpp::Int<192>;
34-
35-
TEST(LlvmLibcUIntClassTest, BitCastToFromDouble) {
36-
static_assert(cpp::is_trivially_constructible<LL_UInt64>::value);
37-
static_assert(cpp::is_trivially_copyable<LL_UInt64>::value);
38-
static_assert(sizeof(LL_UInt64) == sizeof(double));
39-
const double inf = HUGE_VAL;
40-
const double max = DBL_MAX;
41-
const double array[] = {0.0, 0.1, 1.0, max, inf};
42-
for (double value : array) {
43-
LL_UInt64 back = cpp::bit_cast<LL_UInt64>(value);
44-
double forth = cpp::bit_cast<double>(back);
45-
EXPECT_TRUE(value == forth);
46-
}
47-
}
17+
using LL_UInt128 = LIBC_NAMESPACE::cpp::UInt<128>;
18+
using LL_UInt192 = LIBC_NAMESPACE::cpp::UInt<192>;
19+
using LL_UInt256 = LIBC_NAMESPACE::cpp::UInt<256>;
20+
using LL_UInt320 = LIBC_NAMESPACE::cpp::UInt<320>;
21+
using LL_UInt512 = LIBC_NAMESPACE::cpp::UInt<512>;
22+
using LL_UInt1024 = LIBC_NAMESPACE::cpp::UInt<1024>;
4823

49-
#ifdef __SIZEOF_INT128__
50-
TEST(LlvmLibcUIntClassTest, BitCastToFromNativeUint128) {
51-
static_assert(cpp::is_trivially_constructible<LL_UInt128>::value);
52-
static_assert(cpp::is_trivially_copyable<LL_UInt128>::value);
53-
static_assert(sizeof(LL_UInt128) == sizeof(__uint128_t));
54-
const __uint128_t array[] = {0, 1, ~__uint128_t(0)};
55-
for (__uint128_t value : array) {
56-
LL_UInt128 back = cpp::bit_cast<LL_UInt128>(value);
57-
__uint128_t forth = cpp::bit_cast<__uint128_t>(back);
58-
EXPECT_TRUE(value == forth);
59-
}
60-
}
61-
#endif
62-
63-
#ifdef LIBC_COMPILER_HAS_FLOAT128
64-
TEST(LlvmLibcUIntClassTest, BitCastToFromNativeFloat128) {
65-
static_assert(cpp::is_trivially_constructible<LL_UInt128>::value);
66-
static_assert(cpp::is_trivially_copyable<LL_UInt128>::value);
67-
static_assert(sizeof(LL_UInt128) == sizeof(float128));
68-
const float128 array[] = {0, 0.1, 1};
69-
for (float128 value : array) {
70-
LL_UInt128 back = cpp::bit_cast<LL_UInt128>(value);
71-
float128 forth = cpp::bit_cast<float128>(back);
72-
EXPECT_TRUE(value == forth);
73-
}
74-
}
75-
#endif
24+
using LL_Int128 = LIBC_NAMESPACE::cpp::Int<128>;
25+
using LL_Int192 = LIBC_NAMESPACE::cpp::Int<192>;
7626

7727
TEST(LlvmLibcUIntClassTest, BasicInit) {
7828
LL_UInt128 half_val(12345);
@@ -684,5 +634,3 @@ TEST(LlvmLibcUIntClassTest, ConstructorFromUInt128Tests) {
684634
}
685635

686636
#endif // __SIZEOF_INT128__
687-
688-
} // namespace LIBC_NAMESPACE

utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ libc_test(
7474
name = "uint_test",
7575
srcs = ["uint_test.cpp"],
7676
deps = [
77-
"//libc:__support_cpp_bit",
7877
"//libc:__support_cpp_optional",
79-
"//libc:__support_cpp_type_traits",
80-
"//libc:__support_macros_properties_float",
8178
"//libc:__support_uint",
8279
],
8380
)

0 commit comments

Comments
 (0)