15 #ifndef RAPIDJSON_BIGINTEGER_H_
16 #define RAPIDJSON_BIGINTEGER_H_
18 #include "../rapidjson.h"
20 #if defined(_MSC_VER) && defined(_M_AMD64)
22 #pragma intrinsic(_umul128)
33 std::memcpy(digits_, rhs.
digits_, count_ *
sizeof(
Type));
40 BigInteger(
const char* decimals,
size_t length) : count_(1) {
44 const size_t kMaxDigitPerIteration = 19;
45 while (length >= kMaxDigitPerIteration) {
46 AppendDecimal64(decimals +
i, decimals +
i + kMaxDigitPerIteration);
47 length -= kMaxDigitPerIteration;
48 i += kMaxDigitPerIteration;
52 AppendDecimal64(decimals +
i, decimals +
i + length);
59 std::memcpy(digits_, rhs.
digits_, count_ *
sizeof(
Type));
71 Type backup = digits_[0];
73 for (
size_t i = 0;
i < count_ - 1;
i++) {
74 if (digits_[
i] >= backup)
76 backup = digits_[
i + 1];
81 if (digits_[count_ - 1] < backup)
88 if (u == 0)
return *
this = 0;
89 if (u == 1)
return *
this;
90 if (*
this == 1)
return *
this = u;
93 for (
size_t i = 0;
i < count_;
i++) {
95 digits_[
i] = MulAdd64(digits_[
i], u, k, &hi);
106 if (u == 0)
return *
this = 0;
107 if (u == 1)
return *
this;
108 if (*
this == 1)
return *
this = u;
111 for (
size_t i = 0;
i < count_;
i++) {
113 const uint64_t d = digits_[
i] & 0xFFFFFFFF;
117 const uint64_t p1 = uc + (p0 >> 32);
118 digits_[
i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
129 if (IsZero() || shift == 0)
return *
this;
131 size_t offset = shift / kTypeBit;
132 size_t interShift = shift % kTypeBit;
135 if (interShift == 0) {
141 for (
size_t i = count_;
i > 0;
i--)
142 digits_[
i +
offset] = (digits_[
i] << interShift) | (digits_[
i - 1] >> (kTypeBit - interShift));
143 digits_[
offset] = digits_[0] << interShift;
149 std::memset(digits_, 0,
offset *
sizeof(
Type));
155 return count_ == rhs.
count_ && std::memcmp(digits_, rhs.
digits_, count_ *
sizeof(
Type)) == 0;
159 return count_ == 1 && digits_[0] == rhs;
169 5 * 5 * 5 * 5 * 5 * 5,
170 5 * 5 * 5 * 5 * 5 * 5 * 5,
171 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
172 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
173 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
174 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
175 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
177 if (exp == 0)
return *
this;
179 for (; exp >= 13; exp -= 13) *
this *=
static_cast<uint32_t>(1220703125u);
180 if (exp > 0) *
this *= kPow5[exp - 1];
191 if (
cmp < 0) {
a = &rhs;
b =
this; ret =
true; }
192 else {
a =
this;
b = &rhs; ret =
false; }
195 for (
size_t i = 0;
i <
a->count_;
i++) {
196 Type d =
a->digits_[
i] - borrow;
199 borrow = (d >
a->digits_[
i]) ? 1 : 0;
210 return count_ < rhs.
count_ ? -1 : 1;
212 for (
size_t i = count_;
i-- > 0;)
214 return digits_[
i] < rhs.
digits_[
i] ? -1 : 1;
221 bool IsZero()
const {
return count_ == 1 && digits_[0] == 0; }
225 uint64_t u = ParseUint64(begin, end);
229 unsigned exp =
static_cast<unsigned>(end - begin);
230 (MultiplyPow5(exp) <<= exp) += u;
236 digits_[count_++] = digit;
241 for (
const char* p = begin; p != end; ++p) {
243 r =
r * 10u +
static_cast<unsigned>(*p -
'0');
250 #if defined(_MSC_VER) && defined(_M_AMD64)
255 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
256 __extension__
typedef unsigned __int128
uint128;
259 *outHigh =
static_cast<uint64_t>(p >> 64);
262 const uint64_t a0 =
a & 0xFFFFFFFF, a1 =
a >> 32, b0 =
b & 0xFFFFFFFF, b1 =
b >> 32;
263 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
267 x3 += (
static_cast<uint64_t>(1) << 32);
268 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
279 static const size_t kBitCount = 3328;
280 static const size_t kCapacity = kBitCount /
sizeof(
Type);
281 static const size_t kTypeBit =
sizeof(
Type) * 8;
std::pair< uint64, uint64 > uint128
BigInteger & operator*=(uint32_t u)
BigInteger(const BigInteger &rhs)
bool Difference(const BigInteger &rhs, BigInteger *out) const
void PushBack(Type digit)
BigInteger & operator<<=(size_t shift)
int Compare(const BigInteger &rhs) const
BigInteger & operator=(const BigInteger &rhs)
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
BigInteger & MultiplyPow5(unsigned exp)
BigInteger(const char *decimals, size_t length)
BigInteger & operator+=(uint64_t u)
BigInteger & operator=(uint64_t u)
static uint64_t ParseUint64(const char *begin, const char *end)
void AppendDecimal64(const char *begin, const char *end)
BigInteger & operator*=(uint64_t u)
Type GetDigit(size_t index) const
bool operator==(const BigInteger &rhs) const
bool operator==(const Type rhs) const
std::ofstream out("events_result.xml")
main entry point for tests
sequence::ECompare Compare(const CSeq_loc &loc1, const CSeq_loc &loc2, CScope *scope)
Returns the sequence::ECompare containment relationship between CSeq_locs.
#define RAPIDJSON_ASSERT(x)
Assertion.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
static unsigned int ud(time_t one, time_t two)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
unsigned __int64 uint64_t