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