TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
biginteger.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_BIGINTEGER_H_
16 #define RAPIDJSON_BIGINTEGER_H_
17 
18 #include "../rapidjson.h"
19 
20 #if defined(_MSC_VER) && defined(_M_AMD64)
21 #include <intrin.h> // for _umul128
22 #endif
23 
25 namespace internal {
26 
27 class BigInteger {
28 public:
29  typedef uint64_t Type;
30 
31  BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
32  std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
33  }
34 
35  explicit BigInteger(uint64_t u) : count_(1) {
36  digits_[0] = u;
37  }
38 
39  BigInteger(const char* decimals, size_t length) : count_(1) {
40  RAPIDJSON_ASSERT(length > 0);
41  digits_[0] = 0;
42  size_t i = 0;
43  const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
44  while (length >= kMaxDigitPerIteration) {
45  AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
46  length -= kMaxDigitPerIteration;
47  i += kMaxDigitPerIteration;
48  }
49 
50  if (length > 0)
51  AppendDecimal64(decimals + i, decimals + i + length);
52  }
53 
55  digits_[0] = u;
56  count_ = 1;
57  return *this;
58  }
59 
61  Type backup = digits_[0];
62  digits_[0] += u;
63  for (size_t i = 0; i < count_ - 1; i++) {
64  if (digits_[i] >= backup)
65  return *this; // no carry
66  backup = digits_[i + 1];
67  digits_[i + 1] += 1;
68  }
69 
70  // Last carry
71  if (digits_[count_ - 1] < backup)
72  PushBack(1);
73 
74  return *this;
75  }
76 
78  if (u == 0) return *this = 0;
79  if (u == 1) return *this;
80  if (*this == 1) return *this = u;
81 
82  uint64_t k = 0;
83  for (size_t i = 0; i < count_; i++) {
84  uint64_t hi;
85  digits_[i] = MulAdd64(digits_[i], u, k, &hi);
86  k = hi;
87  }
88 
89  if (k > 0)
90  PushBack(k);
91 
92  return *this;
93  }
94 
96  if (u == 0) return *this = 0;
97  if (u == 1) return *this;
98  if (*this == 1) return *this = u;
99 
100  uint64_t k = 0;
101  for (size_t i = 0; i < count_; i++) {
102  const uint64_t c = digits_[i] >> 32;
103  const uint64_t d = digits_[i] & 0xFFFFFFFF;
104  const uint64_t uc = u * c;
105  const uint64_t ud = u * d;
106  const uint64_t p0 = ud + k;
107  const uint64_t p1 = uc + (p0 >> 32);
108  digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
109  k = p1 >> 32;
110  }
111 
112  if (k > 0)
113  PushBack(k);
114 
115  return *this;
116  }
117 
118  BigInteger& operator<<=(size_t shift) {
119  if (IsZero() || shift == 0) return *this;
120 
121  size_t offset = shift / kTypeBit;
122  size_t interShift = shift % kTypeBit;
123  RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
124 
125  if (interShift == 0) {
126  std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
127  count_ += offset;
128  }
129  else {
130  digits_[count_] = 0;
131  for (size_t i = count_; i > 0; i--)
132  digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
133  digits_[offset] = digits_[0] << interShift;
134  count_ += offset;
135  if (digits_[count_])
136  count_++;
137  }
138 
139  std::memset(digits_, 0, offset * sizeof(Type));
140 
141  return *this;
142  }
143 
144  bool operator==(const BigInteger& rhs) const {
145  return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
146  }
147 
148  bool operator==(const Type rhs) const {
149  return count_ == 1 && digits_[0] == rhs;
150  }
151 
153  static const uint32_t kPow5[12] = {
154  5,
155  5 * 5,
156  5 * 5 * 5,
157  5 * 5 * 5 * 5,
158  5 * 5 * 5 * 5 * 5,
159  5 * 5 * 5 * 5 * 5 * 5,
160  5 * 5 * 5 * 5 * 5 * 5 * 5,
161  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
162  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
163  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
164  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
165  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
166  };
167  if (exp == 0) return *this;
168  for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
169  for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
170  if (exp > 0) *this *= kPow5[exp - 1];
171  return *this;
172  }
173 
174  // Compute absolute difference of this and rhs.
175  // Assume this != rhs
176  bool Difference(const BigInteger& rhs, BigInteger* out) const {
177  int cmp = Compare(rhs);
178  RAPIDJSON_ASSERT(cmp != 0);
179  const BigInteger *a, *b; // Makes a > b
180  bool ret;
181  if (cmp < 0) { a = &rhs; b = this; ret = true; }
182  else { a = this; b = &rhs; ret = false; }
183 
184  Type borrow = 0;
185  for (size_t i = 0; i < a->count_; i++) {
186  Type d = a->digits_[i] - borrow;
187  if (i < b->count_)
188  d -= b->digits_[i];
189  borrow = (d > a->digits_[i]) ? 1 : 0;
190  out->digits_[i] = d;
191  if (d != 0)
192  out->count_ = i + 1;
193  }
194 
195  return ret;
196  }
197 
198  int Compare(const BigInteger& rhs) const {
199  if (count_ != rhs.count_)
200  return count_ < rhs.count_ ? -1 : 1;
201 
202  for (size_t i = count_; i-- > 0;)
203  if (digits_[i] != rhs.digits_[i])
204  return digits_[i] < rhs.digits_[i] ? -1 : 1;
205 
206  return 0;
207  }
208 
209  size_t GetCount() const { return count_; }
210  Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
211  bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
212 
213 private:
214  void AppendDecimal64(const char* begin, const char* end) {
215  uint64_t u = ParseUint64(begin, end);
216  if (IsZero())
217  *this = u;
218  else {
219  unsigned exp = static_cast<unsigned>(end - begin);
220  (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
221  }
222  }
223 
224  void PushBack(Type digit) {
226  digits_[count_++] = digit;
227  }
228 
229  static uint64_t ParseUint64(const char* begin, const char* end) {
230  uint64_t r = 0;
231  for (const char* p = begin; p != end; ++p) {
232  RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
233  r = r * 10 + (*p - '0');
234  }
235  return r;
236  }
237 
238  // Assume a * b + k < 2^128
239  static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
240 #if defined(_MSC_VER) && defined(_M_AMD64)
241  uint64_t low = _umul128(a, b, outHigh) + k;
242  if (low < k)
243  (*outHigh)++;
244  return low;
245 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
246  __extension__ typedef unsigned __int128 uint128;
247  uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
248  p += k;
249  *outHigh = static_cast<uint64_t>(p >> 64);
250  return static_cast<uint64_t>(p);
251 #else
252  const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
253  uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
254  x1 += (x0 >> 32); // can't give carry
255  x1 += x2;
256  if (x1 < x2)
257  x3 += (static_cast<uint64_t>(1) << 32);
258  uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
259  uint64_t hi = x3 + (x1 >> 32);
260 
261  lo += k;
262  if (lo < k)
263  hi++;
264  *outHigh = hi;
265  return lo;
266 #endif
267  }
268 
269  static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
270  static const size_t kCapacity = kBitCount / sizeof(Type);
271  static const size_t kTypeBit = sizeof(Type) * 8;
272 
274  size_t count_;
275 };
276 
277 } // namespace internal
279 
280 #endif // RAPIDJSON_BIGINTEGER_H_
static const size_t kCapacity
Definition: biginteger.h:270
BigInteger & operator<<=(size_t shift)
Definition: biginteger.h:118
Quat exp(const Quat &q)
Definition: Quat.h:729
BigInteger & operator+=(uint64_t u)
Definition: biginteger.h:60
bool IsZero() const
Definition: biginteger.h:211
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
Type GetDigit(size_t index) const
Definition: biginteger.h:210
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
void AppendDecimal64(const char *begin, const char *end)
Definition: biginteger.h:214
uint64_t Type
Definition: biginteger.h:29
BigInteger & operator=(uint64_t u)
Definition: biginteger.h:54
BigInteger & operator*=(uint32_t u)
Definition: biginteger.h:95
size_t GetCount() const
Definition: biginteger.h:209
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:198
void PushBack(Type digit)
Definition: biginteger.h:224
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
BigInteger(const BigInteger &rhs)
Definition: biginteger.h:31
unsigned int uint32_t
Definition: stdint.h:80
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
Definition: biginteger.h:239
BigInteger(const char *decimals, size_t length)
Definition: biginteger.h:39
static uint64_t ParseUint64(const char *begin, const char *end)
Definition: biginteger.h:229
size_t count_
Definition: biginteger.h:274
BigInteger(uint64_t u)
Definition: biginteger.h:35
unsigned __int64 uint64_t
Definition: stdint.h:90
BigInteger & operator*=(uint64_t u)
Definition: biginteger.h:77
Type digits_[kCapacity]
Definition: biginteger.h:273
bool operator==(const Type rhs) const
Definition: biginteger.h:148
float length(float v)
Definition: vectorMath.h:208
Definition: document.h:390
static const size_t kTypeBit
Definition: biginteger.h:271
bool operator==(const BigInteger &rhs) const
Definition: biginteger.h:144
Definition: biginteger.h:27
static const size_t kBitCount
Definition: biginteger.h:269
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:152
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:176