全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
biginteger.h
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 #pragma intrinsic(_umul128)
23 #endif
24 
25 RAPIDJSON_NAMESPACE_BEGIN
26 namespace internal {
27 
28 class BigInteger {
29 public:
30  typedef uint64_t Type;
31 
32  BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
33  std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
34  }
35 
36  explicit BigInteger(uint64_t u) : count_(1) {
37  digits_[0] = u;
38  }
39 
40  BigInteger(const char* decimals, size_t length) : count_(1) {
41  RAPIDJSON_ASSERT(length > 0);
42  digits_[0] = 0;
43  size_t i = 0;
44  const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
45  while (length >= kMaxDigitPerIteration) {
46  AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
47  length -= kMaxDigitPerIteration;
48  i += kMaxDigitPerIteration;
49  }
50 
51  if (length > 0)
52  AppendDecimal64(decimals + i, decimals + i + length);
53  }
54 
55  BigInteger& operator=(uint64_t u) {
56  digits_[0] = u;
57  count_ = 1;
58  return *this;
59  }
60 
61  BigInteger& operator+=(uint64_t u) {
62  Type backup = digits_[0];
63  digits_[0] += u;
64  for (size_t i = 0; i < count_ - 1; i++) {
65  if (digits_[i] >= backup)
66  return *this; // no carry
67  backup = digits_[i + 1];
68  digits_[i + 1] += 1;
69  }
70 
71  // Last carry
72  if (digits_[count_ - 1] < backup)
73  PushBack(1);
74 
75  return *this;
76  }
77 
78  BigInteger& operator*=(uint64_t u) {
79  if (u == 0) return *this = 0;
80  if (u == 1) return *this;
81  if (*this == 1) return *this = u;
82 
83  uint64_t k = 0;
84  for (size_t i = 0; i < count_; i++) {
85  uint64_t hi;
86  digits_[i] = MulAdd64(digits_[i], u, k, &hi);
87  k = hi;
88  }
89 
90  if (k > 0)
91  PushBack(k);
92 
93  return *this;
94  }
95 
96  BigInteger& operator*=(uint32_t u) {
97  if (u == 0) return *this = 0;
98  if (u == 1) return *this;
99  if (*this == 1) return *this = u;
100 
101  uint64_t k = 0;
102  for (size_t i = 0; i < count_; i++) {
103  const uint64_t c = digits_[i] >> 32;
104  const uint64_t d = digits_[i] & 0xFFFFFFFF;
105  const uint64_t uc = u * c;
106  const uint64_t ud = u * d;
107  const uint64_t p0 = ud + k;
108  const uint64_t p1 = uc + (p0 >> 32);
109  digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
110  k = p1 >> 32;
111  }
112 
113  if (k > 0)
114  PushBack(k);
115 
116  return *this;
117  }
118 
119  BigInteger& operator<<=(size_t shift) {
120  if (IsZero() || shift == 0) return *this;
121 
122  size_t offset = shift / kTypeBit;
123  size_t interShift = shift % kTypeBit;
124  RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
125 
126  if (interShift == 0) {
127  std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
128  count_ += offset;
129  }
130  else {
131  digits_[count_] = 0;
132  for (size_t i = count_; i > 0; i--)
133  digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
134  digits_[offset] = digits_[0] << interShift;
135  count_ += offset;
136  if (digits_[count_])
137  count_++;
138  }
139 
140  std::memset(digits_, 0, offset * sizeof(Type));
141 
142  return *this;
143  }
144 
145  bool operator==(const BigInteger& rhs) const {
146  return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
147  }
148 
149  bool operator==(const Type rhs) const {
150  return count_ == 1 && digits_[0] == rhs;
151  }
152 
153  BigInteger& MultiplyPow5(unsigned exp) {
154  static const uint32_t kPow5[12] = {
155  5,
156  5 * 5,
157  5 * 5 * 5,
158  5 * 5 * 5 * 5,
159  5 * 5 * 5 * 5 * 5,
160  5 * 5 * 5 * 5 * 5 * 5,
161  5 * 5 * 5 * 5 * 5 * 5 * 5,
162  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
163  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
164  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
165  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
166  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
167  };
168  if (exp == 0) return *this;
169  for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
170  for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
171  if (exp > 0) *this *= kPow5[exp - 1];
172  return *this;
173  }
174 
175  // Compute absolute difference of this and rhs.
176  // Assume this != rhs
177  bool Difference(const BigInteger& rhs, BigInteger* out) const {
178  int cmp = Compare(rhs);
179  RAPIDJSON_ASSERT(cmp != 0);
180  const BigInteger *a, *b; // Makes a > b
181  bool ret;
182  if (cmp < 0) { a = &rhs; b = this; ret = true; }
183  else { a = this; b = &rhs; ret = false; }
184 
185  Type borrow = 0;
186  for (size_t i = 0; i < a->count_; i++) {
187  Type d = a->digits_[i] - borrow;
188  if (i < b->count_)
189  d -= b->digits_[i];
190  borrow = (d > a->digits_[i]) ? 1 : 0;
191  out->digits_[i] = d;
192  if (d != 0)
193  out->count_ = i + 1;
194  }
195 
196  return ret;
197  }
198 
199  int Compare(const BigInteger& rhs) const {
200  if (count_ != rhs.count_)
201  return count_ < rhs.count_ ? -1 : 1;
202 
203  for (size_t i = count_; i-- > 0;)
204  if (digits_[i] != rhs.digits_[i])
205  return digits_[i] < rhs.digits_[i] ? -1 : 1;
206 
207  return 0;
208  }
209 
210  size_t GetCount() const { return count_; }
211  Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
212  bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
213 
214 private:
215  void AppendDecimal64(const char* begin, const char* end) {
216  uint64_t u = ParseUint64(begin, end);
217  if (IsZero())
218  *this = u;
219  else {
220  unsigned exp = static_cast<unsigned>(end - begin);
221  (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
222  }
223  }
224 
225  void PushBack(Type digit) {
226  RAPIDJSON_ASSERT(count_ < kCapacity);
227  digits_[count_++] = digit;
228  }
229 
230  static uint64_t ParseUint64(const char* begin, const char* end) {
231  uint64_t r = 0;
232  for (const char* p = begin; p != end; ++p) {
233  RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
234  r = r * 10u + (unsigned)(*p - '0');
235  }
236  return r;
237  }
238 
239  // Assume a * b + k < 2^128
240  static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
241 #if defined(_MSC_VER) && defined(_M_AMD64)
242  uint64_t low = _umul128(a, b, outHigh) + k;
243  if (low < k)
244  (*outHigh)++;
245  return low;
246 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
247  __extension__ typedef unsigned __int128 uint128;
248  uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
249  p += k;
250  *outHigh = static_cast<uint64_t>(p >> 64);
251  return static_cast<uint64_t>(p);
252 #else
253  const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
254  uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
255  x1 += (x0 >> 32); // can't give carry
256  x1 += x2;
257  if (x1 < x2)
258  x3 += (static_cast<uint64_t>(1) << 32);
259  uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
260  uint64_t hi = x3 + (x1 >> 32);
261 
262  lo += k;
263  if (lo < k)
264  hi++;
265  *outHigh = hi;
266  return lo;
267 #endif
268  }
269 
270  static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
271  static const size_t kCapacity = kBitCount / sizeof(Type);
272  static const size_t kTypeBit = sizeof(Type) * 8;
273 
274  Type digits_[kCapacity];
275  size_t count_;
276 };
277 
278 } // namespace internal
279 RAPIDJSON_NAMESPACE_END
280 
281 #endif // RAPIDJSON_BIGINTEGER_H_
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
Type
Type of JSON value.
Definition: rapidjson.h:642
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344