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) && !__INTEL_COMPILER && defined(_M_AMD64)
21 #include <intrin.h> // for _umul128
22 #pragma intrinsic(_umul128)
23 #endif
24 
26 namespace internal
27 {
29 {
30 public:
31  typedef uint64_t Type;
32 
33  BigInteger(const BigInteger& rhs) : count_(rhs.count_)
34  {
35  std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
36  }
37 
38  explicit BigInteger(uint64_t u) : count_(1)
39  {
40  digits_[0] = u;
41  }
42 
43  BigInteger(const char* decimals, size_t length) : count_(1)
44  {
45  RAPIDJSON_ASSERT(length > 0);
46  digits_[0] = 0;
47  size_t i = 0;
48  const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
49  while (length >= kMaxDigitPerIteration)
50  {
51  AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
52  length -= kMaxDigitPerIteration;
53  i += kMaxDigitPerIteration;
54  }
55 
56  if (length > 0)
57  AppendDecimal64(decimals + i, decimals + i + length);
58  }
59 
61  {
62  if (this != &rhs)
63  {
64  count_ = rhs.count_;
65  std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
66  }
67  return *this;
68  }
69 
71  {
72  digits_[0] = u;
73  count_ = 1;
74  return *this;
75  }
76 
78  {
79  Type backup = digits_[0];
80  digits_[0] += u;
81  for (size_t i = 0; i < count_ - 1; i++)
82  {
83  if (digits_[i] >= backup)
84  return *this; // no carry
85  backup = digits_[i + 1];
86  digits_[i + 1] += 1;
87  }
88 
89  // Last carry
90  if (digits_[count_ - 1] < backup)
91  PushBack(1);
92 
93  return *this;
94  }
95 
97  {
98  if (u == 0)
99  return *this = 0;
100  if (u == 1)
101  return *this;
102  if (*this == 1)
103  return *this = u;
104 
105  uint64_t k = 0;
106  for (size_t i = 0; i < count_; i++)
107  {
108  uint64_t hi;
109  digits_[i] = MulAdd64(digits_[i], u, k, &hi);
110  k = hi;
111  }
112 
113  if (k > 0)
114  PushBack(k);
115 
116  return *this;
117  }
118 
120  {
121  if (u == 0)
122  return *this = 0;
123  if (u == 1)
124  return *this;
125  if (*this == 1)
126  return *this = u;
127 
128  uint64_t k = 0;
129  for (size_t i = 0; i < count_; i++)
130  {
131  const uint64_t c = digits_[i] >> 32;
132  const uint64_t d = digits_[i] & 0xFFFFFFFF;
133  const uint64_t uc = u * c;
134  const uint64_t ud = u * d;
135  const uint64_t p0 = ud + k;
136  const uint64_t p1 = uc + (p0 >> 32);
137  digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
138  k = p1 >> 32;
139  }
140 
141  if (k > 0)
142  PushBack(k);
143 
144  return *this;
145  }
146 
147  BigInteger& operator<<=(size_t shift)
148  {
149  if (IsZero() || shift == 0)
150  return *this;
151 
152  size_t offset = shift / kTypeBit;
153  size_t interShift = shift % kTypeBit;
154  RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
155 
156  if (interShift == 0)
157  {
158  std::memmove(digits_ + offset, digits_, count_ * sizeof(Type));
159  count_ += offset;
160  }
161  else
162  {
163  digits_[count_] = 0;
164  for (size_t i = count_; i > 0; i--)
165  digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
166  digits_[offset] = digits_[0] << interShift;
167  count_ += offset;
168  if (digits_[count_])
169  count_++;
170  }
171 
172  std::memset(digits_, 0, offset * sizeof(Type));
173 
174  return *this;
175  }
176 
177  bool operator==(const BigInteger& rhs) const
178  {
179  return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
180  }
181 
182  bool operator==(const Type rhs) const
183  {
184  return count_ == 1 && digits_[0] == rhs;
185  }
186 
187  BigInteger& MultiplyPow5(unsigned exp)
188  {
189  static const uint32_t kPow5[12] = { 5,
190  5 * 5,
191  5 * 5 * 5,
192  5 * 5 * 5 * 5,
193  5 * 5 * 5 * 5 * 5,
194  5 * 5 * 5 * 5 * 5 * 5,
195  5 * 5 * 5 * 5 * 5 * 5 * 5,
196  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
197  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
198  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
199  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
200  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 };
201  if (exp == 0)
202  return *this;
203  for (; exp >= 27; exp -= 27)
204  *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
205  for (; exp >= 13; exp -= 13)
206  *this *= static_cast<uint32_t>(1220703125u); // 5^13
207  if (exp > 0)
208  *this *= kPow5[exp - 1];
209  return *this;
210  }
211 
212  // Compute absolute difference of this and rhs.
213  // Assume this != rhs
214  bool Difference(const BigInteger& rhs, BigInteger* out) const
215  {
216  int cmp = Compare(rhs);
217  RAPIDJSON_ASSERT(cmp != 0);
218  const BigInteger *a, *b; // Makes a > b
219  bool ret;
220  if (cmp < 0)
221  {
222  a = &rhs;
223  b = this;
224  ret = true;
225  }
226  else
227  {
228  a = this;
229  b = &rhs;
230  ret = false;
231  }
232 
233  Type borrow = 0;
234  for (size_t i = 0; i < a->count_; i++)
235  {
236  Type d = a->digits_[i] - borrow;
237  if (i < b->count_)
238  d -= b->digits_[i];
239  borrow = (d > a->digits_[i]) ? 1 : 0;
240  out->digits_[i] = d;
241  if (d != 0)
242  out->count_ = i + 1;
243  }
244 
245  return ret;
246  }
247 
248  int Compare(const BigInteger& rhs) const
249  {
250  if (count_ != rhs.count_)
251  return count_ < rhs.count_ ? -1 : 1;
252 
253  for (size_t i = count_; i-- > 0;)
254  if (digits_[i] != rhs.digits_[i])
255  return digits_[i] < rhs.digits_[i] ? -1 : 1;
256 
257  return 0;
258  }
259 
260  size_t GetCount() const
261  {
262  return count_;
263  }
264  Type GetDigit(size_t index) const
265  {
266  RAPIDJSON_ASSERT(index < count_);
267  return digits_[index];
268  }
269  bool IsZero() const
270  {
271  return count_ == 1 && digits_[0] == 0;
272  }
273 
274 private:
275  void AppendDecimal64(const char* begin, const char* end)
276  {
277  uint64_t u = ParseUint64(begin, end);
278  if (IsZero())
279  *this = u;
280  else
281  {
282  unsigned exp = static_cast<unsigned>(end - begin);
283  (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
284  }
285  }
286 
287  void PushBack(Type digit)
288  {
290  digits_[count_++] = digit;
291  }
292 
293  static uint64_t ParseUint64(const char* begin, const char* end)
294  {
295  uint64_t r = 0;
296  for (const char* p = begin; p != end; ++p)
297  {
298  RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
299  r = r * 10u + static_cast<unsigned>(*p - '0');
300  }
301  return r;
302  }
303 
304  // Assume a * b + k < 2^128
306  {
307 #if defined(_MSC_VER) && defined(_M_AMD64)
308  uint64_t low = _umul128(a, b, outHigh) + k;
309  if (low < k)
310  (*outHigh)++;
311  return low;
312 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
313  __extension__ typedef unsigned __int128 uint128;
314  uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
315  p += k;
316  *outHigh = static_cast<uint64_t>(p >> 64);
317  return static_cast<uint64_t>(p);
318 #else
319  const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
320  uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
321  x1 += (x0 >> 32); // can't give carry
322  x1 += x2;
323  if (x1 < x2)
324  x3 += (static_cast<uint64_t>(1) << 32);
325  uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
326  uint64_t hi = x3 + (x1 >> 32);
327 
328  lo += k;
329  if (lo < k)
330  hi++;
331  *outHigh = hi;
332  return lo;
333 #endif
334  }
335 
336  static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
337  static const size_t kCapacity = kBitCount / sizeof(Type);
338  static const size_t kTypeBit = sizeof(Type) * 8;
339 
341  size_t count_;
342 };
343 
344 } // namespace internal
346 
347 #endif // RAPIDJSON_BIGINTEGER_H_
static const size_t kCapacity
Definition: biginteger.h:337
d
BigInteger & operator<<=(size_t shift)
Definition: biginteger.h:147
BigInteger & operator+=(uint64_t u)
Definition: biginteger.h:77
bool IsZero() const
Definition: biginteger.h:269
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
Type GetDigit(size_t index) const
Definition: biginteger.h:264
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
void AppendDecimal64(const char *begin, const char *end)
Definition: biginteger.h:275
BigInteger & operator=(uint64_t u)
Definition: biginteger.h:70
BigInteger & operator*=(uint32_t u)
Definition: biginteger.h:119
size_t GetCount() const
Definition: biginteger.h:260
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:248
void PushBack(Type digit)
Definition: biginteger.h:287
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
BigInteger(const BigInteger &rhs)
Definition: biginteger.h:33
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
Definition: biginteger.h:305
BigInteger(const char *decimals, size_t length)
Definition: biginteger.h:43
unsigned int uint32_t
Definition: stdint.h:126
static uint64_t ParseUint64(const char *begin, const char *end)
Definition: biginteger.h:293
BigInteger(uint64_t u)
Definition: biginteger.h:38
unsigned __int64 uint64_t
Definition: stdint.h:136
BigInteger & operator=(const BigInteger &rhs)
Definition: biginteger.h:60
BigInteger & operator*=(uint64_t u)
Definition: biginteger.h:96
Type digits_[kCapacity]
Definition: biginteger.h:340
bool operator==(const Type rhs) const
Definition: biginteger.h:182
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1422
static const size_t kTypeBit
Definition: biginteger.h:338
bool operator==(const BigInteger &rhs) const
Definition: biginteger.h:177
static const size_t kBitCount
Definition: biginteger.h:336
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:187
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:214


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53