19 #ifndef RAPIDJSON_BIGINTEGER_H_ 20 #define RAPIDJSON_BIGINTEGER_H_ 22 #include "../rapidjson.h" 24 #if defined(_MSC_VER) && !__INTEL_COMPILER && defined(_M_AMD64) 26 #pragma intrinsic(_umul128) 46 const size_t kMaxDigitPerIteration =
48 while (length >= kMaxDigitPerIteration) {
50 length -= kMaxDigitPerIteration;
51 i += kMaxDigitPerIteration;
74 for (
size_t i = 0; i <
count_ - 1; i++) {
75 if (
digits_[i] >= backup)
return *
this;
87 if (u == 0)
return *
this = 0;
88 if (u == 1)
return *
this;
89 if (*
this == 1)
return *
this = u;
92 for (
size_t i = 0; i <
count_; i++) {
104 if (u == 0)
return *
this = 0;
105 if (u == 1)
return *
this;
106 if (*
this == 1)
return *
this = u;
109 for (
size_t i = 0; i <
count_; i++) {
115 const uint64_t p1 = uc + (p0 >> 32);
116 digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
126 if (
IsZero() || shift == 0)
return *
this;
129 size_t interShift = shift %
kTypeBit;
132 if (interShift == 0) {
137 for (
size_t i =
count_; i > 0; i--)
145 std::memset(
digits_, 0, offset *
sizeof(Type));
166 5 * 5 * 5 * 5 * 5 * 5,
167 5 * 5 * 5 * 5 * 5 * 5 * 5,
168 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
169 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
170 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
171 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
172 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
173 if (exp == 0)
return *
this;
174 for (; exp >= 27; exp -= 27)
176 for (; exp >= 13; exp -= 13)
177 *
this *= static_cast<uint32_t>(1220703125u);
178 if (exp > 0) *
this *= kPow5[exp - 1];
200 for (
size_t i = 0; i < a->
count_; i++) {
203 borrow = (d > a->
digits_[i]) ? 1 : 0;
205 if (d != 0) out->
count_ = i + 1;
214 for (
size_t i =
count_; i-- > 0;)
234 unsigned exp =
static_cast<unsigned>(end - begin);
246 for (
const char *p = begin; p != end; ++p) {
248 r = r * 10u +
static_cast<unsigned>(*p -
'0');
256 #if defined(_MSC_VER) && defined(_M_AMD64) 257 uint64_t low = _umul128(a, b, outHigh) + k;
258 if (low < k) (*outHigh)++;
260 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && \ 262 __extension__
typedef unsigned __int128 uint128;
263 uint128 p =
static_cast<uint128
>(
a) * static_cast<uint128>(b);
265 *outHigh =
static_cast<uint64_t>(p >> 64);
268 const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF,
270 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
273 if (x1 < x2) x3 += (
static_cast<uint64_t>(1) << 32);
274 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
295 #endif // RAPIDJSON_BIGINTEGER_H_ static const size_t kCapacity
BigInteger & operator<<=(size_t shift)
BigInteger & operator+=(uint64_t u)
#define RAPIDJSON_ASSERT(x)
Assertion.
Type GetDigit(size_t index) const
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
void AppendDecimal64(const char *begin, const char *end)
BigInteger & operator=(uint64_t u)
BigInteger & operator*=(uint32_t u)
int Compare(const BigInteger &rhs) const
void PushBack(Type digit)
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
BigInteger(const BigInteger &rhs)
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
BigInteger(const char *decimals, size_t length)
static uint64_t ParseUint64(const char *begin, const char *end)
unsigned __int64 uint64_t
BigInteger & operator=(const BigInteger &rhs)
BigInteger & operator*=(uint64_t u)
bool operator==(const Type rhs) const
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
static const size_t kTypeBit
bool operator==(const BigInteger &rhs) const
static const size_t kBitCount
BigInteger & MultiplyPow5(unsigned exp)
bool Difference(const BigInteger &rhs, BigInteger *out) const