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")
39 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
44 template <
typename Ch>
45 BigInteger(
const Ch* decimals,
size_t length) : count_(1)
50 const size_t kMaxDigitPerIteration = 19;
51 while(length >= kMaxDigitPerIteration)
53 AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
54 length -= kMaxDigitPerIteration;
55 i += kMaxDigitPerIteration;
59 AppendDecimal64(decimals + i, decimals + i + length);
67 std::memcpy(digits_, rhs.digits_, count_ *
sizeof(
Type));
81 Type backup = digits_[0];
83 for(
size_t i = 0; i < count_ - 1; i++)
85 if(digits_[i] >= backup)
87 backup = digits_[i + 1];
92 if(digits_[count_ - 1] < backup)
108 for(
size_t i = 0; i < count_; i++)
111 digits_[i] = MulAdd64(digits_[i], u, k, &hi);
131 for(
size_t i = 0; i < count_; i++)
133 const uint64_t c = digits_[i] >> 32;
134 const uint64_t d = digits_[i] & 0xFFFFFFFF;
138 const uint64_t p1 = uc + (p0 >> 32);
139 digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
151 if(
IsZero() || shift == 0)
154 size_t offset = shift / kTypeBit;
155 size_t interShift = shift % kTypeBit;
160 std::memmove(digits_ + offset, digits_, count_ *
sizeof(
Type));
166 for(
size_t i = count_; i > 0; i--)
167 digits_[i + offset] =
168 (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
169 digits_[offset] = digits_[0] << interShift;
175 std::memset(digits_, 0, offset *
sizeof(
Type));
182 return count_ == rhs.count_ &&
183 std::memcmp(digits_, rhs.digits_, count_ *
sizeof(
Type)) == 0;
186 bool operator==(
const Type rhs)
const {
return count_ == 1 && digits_[0] == rhs; }
190 static const uint32_t kPow5[12] = {5,
195 5 * 5 * 5 * 5 * 5 * 5,
196 5 * 5 * 5 * 5 * 5 * 5 * 5,
197 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
198 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
199 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
200 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
201 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
204 for(;
exp >= 27;
exp -= 27)
206 for(;
exp >= 13;
exp -= 13)
207 *
this *=
static_cast<uint32_t>(1220703125u);
209 *
this *= kPow5[
exp - 1];
235 for(
size_t i = 0; i <
a->count_; i++)
237 Type d =
a->digits_[i] - borrow;
240 borrow = (d >
a->digits_[i]) ? 1 : 0;
251 if(count_ != rhs.count_)
252 return count_ < rhs.count_ ? -1 : 1;
254 for(
size_t i = count_; i-- > 0;)
255 if(digits_[i] != rhs.digits_[i])
256 return digits_[i] < rhs.digits_[i] ? -1 : 1;
265 return digits_[index];
267 bool IsZero()
const {
return count_ == 1 && digits_[0] == 0; }
270 template <
typename Ch>
271 void AppendDecimal64(
const Ch* begin,
const Ch* end)
273 uint64_t u = ParseUint64(begin, end);
278 unsigned exp =
static_cast<unsigned>(end - begin);
283 void PushBack(
Type digit)
286 digits_[count_++] = digit;
289 template <
typename Ch>
290 static uint64_t ParseUint64(
const Ch* begin,
const Ch* end)
293 for(
const Ch* p = begin; p != end; ++p)
296 r = r * 10u +
static_cast<unsigned>(*p - Ch(
'0'));
304 #if defined(_MSC_VER) && defined(_M_AMD64)
305 uint64_t low = _umul128(
a, b, outHigh) + k;
309 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && \
311 __extension__
typedef unsigned __int128 uint128;
312 uint128 p =
static_cast<uint128
>(
a) *
static_cast<uint128
>(b);
314 *outHigh =
static_cast<uint64_t>(p >> 64);
317 const uint64_t a0 =
a & 0xFFFFFFFF, a1 =
a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
318 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
322 x3 += (
static_cast<uint64_t>(1) << 32);
323 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
334 static const size_t kBitCount = 3328;
335 static const size_t kCapacity = kBitCount /
sizeof(
Type);
336 static const size_t kTypeBit =
sizeof(
Type) * 8;
338 Type digits_[kCapacity];
Definition: biginteger.h:33
BigInteger & operator+=(uint64_t u)
Definition: biginteger.h:79
uint64_t Type
Definition: biginteger.h:35
BigInteger & operator=(uint64_t u)
Definition: biginteger.h:72
BigInteger & operator<<=(size_t shift)
Definition: biginteger.h:149
BigInteger(const Ch *decimals, size_t length)
Definition: biginteger.h:45
bool operator==(const BigInteger &rhs) const
Definition: biginteger.h:180
Type GetDigit(size_t index) const
Definition: biginteger.h:262
BigInteger & operator*=(uint64_t u)
Definition: biginteger.h:98
BigInteger & operator*=(uint32_t u)
Definition: biginteger.h:121
bool operator==(const Type rhs) const
Definition: biginteger.h:186
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:188
size_t GetCount() const
Definition: biginteger.h:261
BigInteger(const BigInteger &rhs)
Definition: biginteger.h:37
BigInteger(uint64_t u)
Definition: biginteger.h:42
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:215
bool IsZero() const
Definition: biginteger.h:267
BigInteger & operator=(const BigInteger &rhs)
Definition: biginteger.h:62
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:249
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
__host__ T exp(T x)
Definition: math_v2.hpp:391
Definition: allocators.h:459
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1517
Type
Type of JSON value.
Definition: rapidjson.h:760
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:326
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136