25
25
26
26
namespace LIBC_NAMESPACE ::cpp {
27
27
28
+ // BigInt has the same semantics as the 'standard integer types' and can be
29
+ // safely 'bit_cast'ed to compatible types.
28
30
template <size_t Bits, bool Signed> struct BigInt {
29
-
30
31
static_assert (Bits > 0 && Bits % 64 == 0 ,
31
32
" Number of bits in BigInt should be a multiple of 64." );
32
33
LIBC_INLINE_VAR static constexpr size_t WORDCOUNT = Bits / 64 ;
33
- uint64_t val[WORDCOUNT]{} ;
34
+ uint64_t val[WORDCOUNT];
34
35
35
36
LIBC_INLINE_VAR static constexpr uint64_t MASK32 = 0xFFFFFFFFu ;
36
37
@@ -158,7 +159,7 @@ template <size_t Bits, bool Signed> struct BigInt {
158
159
159
160
LIBC_INLINE constexpr BigInt<Bits, Signed>
160
161
operator +(const BigInt<Bits, Signed> &other) const {
161
- BigInt<Bits, Signed> result;
162
+ BigInt<Bits, Signed> result ( 0 ) ;
162
163
SumCarry<uint64_t > s{0 , 0 };
163
164
for (size_t i = 0 ; i < WORDCOUNT; ++i) {
164
165
s = add_with_carry (val[i], other.val [i], s.carry );
@@ -171,7 +172,7 @@ template <size_t Bits, bool Signed> struct BigInt {
171
172
// it will always use the constexpr version of add_with_carry.
172
173
LIBC_INLINE constexpr BigInt<Bits, Signed>
173
174
operator +(BigInt<Bits, Signed> &&other) const {
174
- BigInt<Bits, Signed> result;
175
+ BigInt<Bits, Signed> result ( 0 ) ;
175
176
SumCarry<uint64_t > s{0 , 0 };
176
177
for (size_t i = 0 ; i < WORDCOUNT; ++i) {
177
178
s = add_with_carry_const (val[i], other.val [i], s.carry );
@@ -199,7 +200,7 @@ template <size_t Bits, bool Signed> struct BigInt {
199
200
200
201
LIBC_INLINE BigInt<Bits, Signed>
201
202
operator -(const BigInt<Bits, Signed> &other) const {
202
- BigInt<Bits, Signed> result;
203
+ BigInt<Bits, Signed> result ( 0 ) ;
203
204
DiffBorrow<uint64_t > d{0 , 0 };
204
205
for (size_t i = 0 ; i < WORDCOUNT; ++i) {
205
206
d = sub_with_borrow (val[i], other.val [i], d.borrow );
@@ -210,7 +211,7 @@ template <size_t Bits, bool Signed> struct BigInt {
210
211
211
212
LIBC_INLINE constexpr BigInt<Bits, Signed>
212
213
operator -(BigInt<Bits, Signed> &&other) const {
213
- BigInt<Bits, Signed> result;
214
+ BigInt<Bits, Signed> result ( 0 ) ;
214
215
DiffBorrow<uint64_t > d{0 , 0 };
215
216
for (size_t i = 0 ; i < WORDCOUNT; ++i) {
216
217
d = sub_with_borrow_const (val[i], other.val [i], d.borrow );
@@ -690,7 +691,7 @@ template <size_t Bits, bool Signed> struct BigInt {
690
691
691
692
LIBC_INLINE constexpr BigInt<Bits, Signed>
692
693
operator &(const BigInt<Bits, Signed> &other) const {
693
- BigInt<Bits, Signed> result;
694
+ BigInt<Bits, Signed> result ( 0 ) ;
694
695
for (size_t i = 0 ; i < WORDCOUNT; ++i)
695
696
result.val [i] = val[i] & other.val [i];
696
697
return result;
@@ -705,7 +706,7 @@ template <size_t Bits, bool Signed> struct BigInt {
705
706
706
707
LIBC_INLINE constexpr BigInt<Bits, Signed>
707
708
operator |(const BigInt<Bits, Signed> &other) const {
708
- BigInt<Bits, Signed> result;
709
+ BigInt<Bits, Signed> result ( 0 ) ;
709
710
for (size_t i = 0 ; i < WORDCOUNT; ++i)
710
711
result.val [i] = val[i] | other.val [i];
711
712
return result;
@@ -720,7 +721,7 @@ template <size_t Bits, bool Signed> struct BigInt {
720
721
721
722
LIBC_INLINE constexpr BigInt<Bits, Signed>
722
723
operator ^(const BigInt<Bits, Signed> &other) const {
723
- BigInt<Bits, Signed> result;
724
+ BigInt<Bits, Signed> result ( 0 ) ;
724
725
for (size_t i = 0 ; i < WORDCOUNT; ++i)
725
726
result.val [i] = val[i] ^ other.val [i];
726
727
return result;
@@ -734,7 +735,7 @@ template <size_t Bits, bool Signed> struct BigInt {
734
735
}
735
736
736
737
LIBC_INLINE constexpr BigInt<Bits, Signed> operator ~() const {
737
- BigInt<Bits, Signed> result;
738
+ BigInt<Bits, Signed> result ( 0 ) ;
738
739
for (size_t i = 0 ; i < WORDCOUNT; ++i)
739
740
result.val [i] = ~val[i];
740
741
return result;
0 commit comments