15 #ifndef RAPIDJSON_BIGINTEGER_H_
16 #define RAPIDJSON_BIGINTEGER_H_
18 #include "../rapidjson.h"
20 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && defined(_M_AMD64)
22 #if !defined(_ARM64EC_)
23 #pragma intrinsic(_umul128)
25 #pragma comment(lib,"softintrin")
37 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
45 BigInteger(
const Ch* decimals,
size_t length) : count_(1) {
49 const size_t kMaxDigitPerIteration = 19;
50 while (length >= kMaxDigitPerIteration) {
51 AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
52 length -= kMaxDigitPerIteration;
53 i += kMaxDigitPerIteration;
57 AppendDecimal64(decimals + i, decimals + i + length);
64 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
76 Type backup = digits_[0];
78 for (
size_t i = 0; i < count_ - 1; i++) {
79 if (digits_[i] >= backup)
81 backup = digits_[i + 1];
86 if (digits_[count_ - 1] < backup)
93 if (u == 0)
return *
this = 0;
94 if (u == 1)
return *
this;
95 if (*
this == 1)
return *
this = u;
98 for (
size_t i = 0; i < count_; i++) {
100 digits_[i] = MulAdd64(digits_[i], u, k, &hi);
111 if (u == 0)
return *
this = 0;
112 if (u == 1)
return *
this;
113 if (*
this == 1)
return *
this = u;
116 for (
size_t i = 0; i < count_; i++) {
117 const uint64_t c = digits_[i] >> 32;
118 const uint64_t d = digits_[i] & 0xFFFFFFFF;
122 const uint64_t p1 = uc + (p0 >> 32);
123 digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
134 if (
IsZero() || shift == 0)
return *
this;
136 size_t offset = shift / kTypeBit;
137 size_t interShift = shift % kTypeBit;
140 if (interShift == 0) {
141 std::memmove(digits_ + offset, digits_, count_ *
sizeof(
Type));
146 for (
size_t i = count_; i > 0; i--)
147 digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
148 digits_[offset] = digits_[0] << interShift;
154 std::memset(digits_, 0, offset *
sizeof(
Type));
160 return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ *
sizeof(
Type)) == 0;
164 return count_ == 1 && digits_[0] == rhs;
174 5 * 5 * 5 * 5 * 5 * 5,
175 5 * 5 * 5 * 5 * 5 * 5 * 5,
176 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
177 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
178 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
179 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
180 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
182 if (exp == 0)
return *
this;
184 for (; exp >= 13; exp -= 13) *
this *=
static_cast<uint32_t>(1220703125u);
185 if (exp > 0) *
this *= kPow5[exp - 1];
196 if (cmp < 0) {
a = &rhs; b =
this; ret =
true; }
197 else {
a =
this; b = &rhs; ret =
false; }
200 for (
size_t i = 0; i <
a->count_; i++) {
201 Type d =
a->digits_[i] - borrow;
204 borrow = (d >
a->digits_[i]) ? 1 : 0;
214 if (count_ != rhs.count_)
215 return count_ < rhs.count_ ? -1 : 1;
217 for (
size_t i = count_; i-- > 0;)
218 if (digits_[i] != rhs.digits_[i])
219 return digits_[i] < rhs.digits_[i] ? -1 : 1;
226 bool IsZero()
const {
return count_ == 1 && digits_[0] == 0; }
229 template<
typename Ch>
230 void AppendDecimal64(
const Ch* begin,
const Ch* end) {
231 uint64_t u = ParseUint64(begin, end);
235 unsigned exp =
static_cast<unsigned>(end - begin);
240 void PushBack(
Type digit) {
242 digits_[count_++] = digit;
245 template<
typename Ch>
246 static uint64_t ParseUint64(
const Ch* begin,
const Ch* end) {
248 for (
const Ch* p = begin; p != end; ++p) {
250 r = r * 10u +
static_cast<unsigned>(*p - Ch(
'0'));
257 #if defined(_MSC_VER) && defined(_M_AMD64)
258 uint64_t low = _umul128(
a, b, outHigh) + k;
262 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
263 __extension__
typedef unsigned __int128 uint128;
264 uint128 p =
static_cast<uint128
>(
a) *
static_cast<uint128
>(b);
266 *outHigh =
static_cast<uint64_t>(p >> 64);
269 const uint64_t a0 =
a & 0xFFFFFFFF, a1 =
a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
270 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
274 x3 += (
static_cast<uint64_t>(1) << 32);
275 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
286 static const size_t kBitCount = 3328;
287 static const size_t kCapacity = kBitCount /
sizeof(
Type);
288 static const size_t kTypeBit =
sizeof(
Type) * 8;
290 Type digits_[kCapacity];
297 #endif // RAPIDJSON_BIGINTEGER_H_