34 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
41 BigInteger(
const char* decimals,
size_t length) : count_(1) {
45 const size_t kMaxDigitPerIteration = 19;
46 while (length >= kMaxDigitPerIteration) {
47 AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
48 length -= kMaxDigitPerIteration;
49 i += kMaxDigitPerIteration;
53 AppendDecimal64(decimals + i, decimals + i + length);
60 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
72 Type backup = digits_[0];
74 for (
size_t i = 0; i < count_ - 1; i++) {
75 if (digits_[i] >= backup)
77 backup = digits_[i + 1];
82 if (digits_[count_ - 1] < backup)
89 if (u == 0)
return *
this = 0;
90 if (u == 1)
return *
this;
91 if (*
this == 1)
return *
this = u;
94 for (
size_t i = 0; i < count_; i++) {
96 digits_[i] = MulAdd64(digits_[i], u, k, &hi);
107 if (u == 0)
return *
this = 0;
108 if (u == 1)
return *
this;
109 if (*
this == 1)
return *
this = u;
112 for (
size_t i = 0; i < count_; i++) {
113 const uint64_t c = digits_[i] >> 32;
114 const uint64_t d = digits_[i] & 0xFFFFFFFF;
118 const uint64_t p1 = uc + (p0 >> 32);
119 digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
130 if (IsZero() || shift == 0)
return *
this;
132 size_t offset = shift / kTypeBit;
133 size_t interShift = shift % kTypeBit;
136 if (interShift == 0) {
137 std::memmove(digits_ + offset, digits_, count_ *
sizeof(
Type));
142 for (
size_t i = count_; i > 0; i--)
143 digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
144 digits_[offset] = digits_[0] << interShift;
150 std::memset(digits_, 0, offset *
sizeof(
Type));
155 bool operator==(
const BigInteger& rhs)
const {
156 return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ *
sizeof(
Type)) == 0;
159 bool operator==(
const Type rhs)
const {
160 return count_ == 1 && digits_[0] == rhs;
170 5 * 5 * 5 * 5 * 5 * 5,
171 5 * 5 * 5 * 5 * 5 * 5 * 5,
172 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
173 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
174 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
175 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
176 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
178 if (exp == 0)
return *
this;
180 for (; exp >= 13; exp -= 13) *
this *=
static_cast<uint32_t>(1220703125u);
181 if (exp > 0) *
this *= kPow5[exp - 1];
188 int cmp = Compare(rhs);
192 if (cmp < 0) { a = &rhs; b =
this; ret =
true; }
193 else { a =
this; b = &rhs; ret =
false; }
196 for (
size_t i = 0; i < a->count_; i++) {
197 Type d = a->digits_[i] - borrow;
200 borrow = (d > a->digits_[i]) ? 1 : 0;
210 if (count_ != rhs.count_)
211 return count_ < rhs.count_ ? -1 : 1;
213 for (
size_t i = count_; i-- > 0;)
214 if (digits_[i] != rhs.digits_[i])
215 return digits_[i] < rhs.digits_[i] ? -1 : 1;
220 size_t GetCount()
const {
return count_; }
222 bool IsZero()
const {
return count_ == 1 && digits_[0] == 0; }
225 void AppendDecimal64(
const char* begin,
const char* end) {
226 uint64_t u = ParseUint64(begin, end);
230 unsigned exp =
static_cast<unsigned>(end - begin);
231 (MultiplyPow5(exp) <<= exp) += u;
235 void PushBack(
Type digit) {
237 digits_[count_++] = digit;
240 static uint64_t ParseUint64(
const char* begin,
const char* end) {
242 for (
const char* p = begin; p != end; ++p) {
244 r = r * 10u +
static_cast<unsigned>(*p -
'0');
251#if defined(_MSC_VER) && defined(_M_AMD64)
252 uint64_t low = _umul128(a, b, outHigh) + k;
256#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) && !defined(__NVCOMPILER)
257 __extension__
typedef unsigned __int128 uint128;
258 uint128 p =
static_cast<uint128
>(a) *
static_cast<uint128
>(b);
260 *outHigh =
static_cast<uint64_t>(p >> 64);
263 const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
264 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
268 x3 += (
static_cast<uint64_t>(1) << 32);
269 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
280 static const size_t kBitCount = 3328;
281 static const size_t kCapacity = kBitCount /
sizeof(
Type);
282 static const size_t kTypeBit =
sizeof(
Type) * 8;
284 Type digits_[kCapacity];