/src/qtbase/src/3rdparty/double-conversion/bignum.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2010 the V8 project authors. All rights reserved. |
2 | | // Redistribution and use in source and binary forms, with or without |
3 | | // modification, are permitted provided that the following conditions are |
4 | | // met: |
5 | | // |
6 | | // * Redistributions of source code must retain the above copyright |
7 | | // notice, this list of conditions and the following disclaimer. |
8 | | // * Redistributions in binary form must reproduce the above |
9 | | // copyright notice, this list of conditions and the following |
10 | | // disclaimer in the documentation and/or other materials provided |
11 | | // with the distribution. |
12 | | // * Neither the name of Google Inc. nor the names of its |
13 | | // contributors may be used to endorse or promote products derived |
14 | | // from this software without specific prior written permission. |
15 | | // |
16 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | |
28 | | #include <double-conversion/bignum.h> |
29 | | #include <double-conversion/utils.h> |
30 | | |
31 | | namespace double_conversion { |
32 | | |
33 | | Bignum::Bignum() |
34 | 0 | : bigits_buffer_(), bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) { |
35 | 0 | for (int i = 0; i < kBigitCapacity; ++i) { |
36 | 0 | bigits_[i] = 0; |
37 | 0 | } |
38 | 0 | } |
39 | | |
40 | | |
41 | | template<typename S> |
42 | 0 | static int BitSize(S value) { |
43 | 0 | (void) value; // Mark variable as used. |
44 | 0 | return 8 * sizeof(value); |
45 | 0 | } |
46 | | |
47 | | // Guaranteed to lie in one Bigit. |
48 | 0 | void Bignum::AssignUInt16(uint16_t value) { |
49 | 0 | ASSERT(kBigitSize >= BitSize(value)); |
50 | 0 | Zero(); |
51 | 0 | if (value == 0) return; |
52 | | |
53 | 0 | EnsureCapacity(1); |
54 | 0 | bigits_[0] = value; |
55 | 0 | used_digits_ = 1; |
56 | 0 | } |
57 | | |
58 | | |
59 | 0 | void Bignum::AssignUInt64(uint64_t value) { |
60 | 0 | const int kUInt64Size = 64; |
61 | |
|
62 | 0 | Zero(); |
63 | 0 | if (value == 0) return; |
64 | | |
65 | 0 | int needed_bigits = kUInt64Size / kBigitSize + 1; |
66 | 0 | EnsureCapacity(needed_bigits); |
67 | 0 | for (int i = 0; i < needed_bigits; ++i) { |
68 | 0 | bigits_[i] = value & kBigitMask; |
69 | 0 | value = value >> kBigitSize; |
70 | 0 | } |
71 | 0 | used_digits_ = needed_bigits; |
72 | 0 | Clamp(); |
73 | 0 | } |
74 | | |
75 | | |
76 | 0 | void Bignum::AssignBignum(const Bignum& other) { |
77 | 0 | exponent_ = other.exponent_; |
78 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
79 | 0 | bigits_[i] = other.bigits_[i]; |
80 | 0 | } |
81 | | // Clear the excess digits (if there were any). |
82 | 0 | for (int i = other.used_digits_; i < used_digits_; ++i) { |
83 | 0 | bigits_[i] = 0; |
84 | 0 | } |
85 | 0 | used_digits_ = other.used_digits_; |
86 | 0 | } |
87 | | |
88 | | |
89 | | static uint64_t ReadUInt64(Vector<const char> buffer, |
90 | | int from, |
91 | 0 | int digits_to_read) { |
92 | 0 | uint64_t result = 0; |
93 | 0 | for (int i = from; i < from + digits_to_read; ++i) { |
94 | 0 | int digit = buffer[i] - '0'; |
95 | 0 | ASSERT(0 <= digit && digit <= 9); |
96 | 0 | result = result * 10 + digit; |
97 | 0 | } |
98 | 0 | return result; |
99 | 0 | } |
100 | | |
101 | | |
102 | 0 | void Bignum::AssignDecimalString(Vector<const char> value) { |
103 | | // 2^64 = 18446744073709551616 > 10^19 |
104 | 0 | const int kMaxUint64DecimalDigits = 19; |
105 | 0 | Zero(); |
106 | 0 | int length = value.length(); |
107 | 0 | unsigned int pos = 0; |
108 | | // Let's just say that each digit needs 4 bits. |
109 | 0 | while (length >= kMaxUint64DecimalDigits) { |
110 | 0 | uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits); |
111 | 0 | pos += kMaxUint64DecimalDigits; |
112 | 0 | length -= kMaxUint64DecimalDigits; |
113 | 0 | MultiplyByPowerOfTen(kMaxUint64DecimalDigits); |
114 | 0 | AddUInt64(digits); |
115 | 0 | } |
116 | 0 | uint64_t digits = ReadUInt64(value, pos, length); |
117 | 0 | MultiplyByPowerOfTen(length); |
118 | 0 | AddUInt64(digits); |
119 | 0 | Clamp(); |
120 | 0 | } |
121 | | |
122 | | |
123 | 0 | static int HexCharValue(char c) { |
124 | 0 | if ('0' <= c && c <= '9') return c - '0'; |
125 | 0 | if ('a' <= c && c <= 'f') return 10 + c - 'a'; |
126 | 0 | ASSERT('A' <= c && c <= 'F'); |
127 | 0 | return 10 + c - 'A'; |
128 | 0 | } |
129 | | |
130 | | |
131 | 0 | void Bignum::AssignHexString(Vector<const char> value) { |
132 | 0 | Zero(); |
133 | 0 | int length = value.length(); |
134 | |
|
135 | 0 | int needed_bigits = length * 4 / kBigitSize + 1; |
136 | 0 | EnsureCapacity(needed_bigits); |
137 | 0 | int string_index = length - 1; |
138 | 0 | for (int i = 0; i < needed_bigits - 1; ++i) { |
139 | | // These bigits are guaranteed to be "full". |
140 | 0 | Chunk current_bigit = 0; |
141 | 0 | for (int j = 0; j < kBigitSize / 4; j++) { |
142 | 0 | current_bigit += HexCharValue(value[string_index--]) << (j * 4); |
143 | 0 | } |
144 | 0 | bigits_[i] = current_bigit; |
145 | 0 | } |
146 | 0 | used_digits_ = needed_bigits - 1; |
147 | |
|
148 | 0 | Chunk most_significant_bigit = 0; // Could be = 0; |
149 | 0 | for (int j = 0; j <= string_index; ++j) { |
150 | 0 | most_significant_bigit <<= 4; |
151 | 0 | most_significant_bigit += HexCharValue(value[j]); |
152 | 0 | } |
153 | 0 | if (most_significant_bigit != 0) { |
154 | 0 | bigits_[used_digits_] = most_significant_bigit; |
155 | 0 | used_digits_++; |
156 | 0 | } |
157 | 0 | Clamp(); |
158 | 0 | } |
159 | | |
160 | | |
161 | 0 | void Bignum::AddUInt64(uint64_t operand) { |
162 | 0 | if (operand == 0) return; |
163 | 0 | Bignum other; |
164 | 0 | other.AssignUInt64(operand); |
165 | 0 | AddBignum(other); |
166 | 0 | } |
167 | | |
168 | | |
169 | 0 | void Bignum::AddBignum(const Bignum& other) { |
170 | 0 | ASSERT(IsClamped()); |
171 | 0 | ASSERT(other.IsClamped()); |
172 | | |
173 | | // If this has a greater exponent than other append zero-bigits to this. |
174 | | // After this call exponent_ <= other.exponent_. |
175 | 0 | Align(other); |
176 | | |
177 | | // There are two possibilities: |
178 | | // aaaaaaaaaaa 0000 (where the 0s represent a's exponent) |
179 | | // bbbbb 00000000 |
180 | | // ---------------- |
181 | | // ccccccccccc 0000 |
182 | | // or |
183 | | // aaaaaaaaaa 0000 |
184 | | // bbbbbbbbb 0000000 |
185 | | // ----------------- |
186 | | // cccccccccccc 0000 |
187 | | // In both cases we might need a carry bigit. |
188 | |
|
189 | 0 | EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_); |
190 | 0 | Chunk carry = 0; |
191 | 0 | int bigit_pos = other.exponent_ - exponent_; |
192 | 0 | ASSERT(bigit_pos >= 0); |
193 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
194 | 0 | Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry; |
195 | 0 | bigits_[bigit_pos] = sum & kBigitMask; |
196 | 0 | carry = sum >> kBigitSize; |
197 | 0 | bigit_pos++; |
198 | 0 | } |
199 | |
|
200 | 0 | while (carry != 0) { |
201 | 0 | Chunk sum = bigits_[bigit_pos] + carry; |
202 | 0 | bigits_[bigit_pos] = sum & kBigitMask; |
203 | 0 | carry = sum >> kBigitSize; |
204 | 0 | bigit_pos++; |
205 | 0 | } |
206 | 0 | used_digits_ = Max(bigit_pos, used_digits_); |
207 | 0 | ASSERT(IsClamped()); |
208 | 0 | } |
209 | | |
210 | | |
211 | 0 | void Bignum::SubtractBignum(const Bignum& other) { |
212 | 0 | ASSERT(IsClamped()); |
213 | 0 | ASSERT(other.IsClamped()); |
214 | | // We require this to be bigger than other. |
215 | 0 | ASSERT(LessEqual(other, *this)); |
216 | |
|
217 | 0 | Align(other); |
218 | |
|
219 | 0 | int offset = other.exponent_ - exponent_; |
220 | 0 | Chunk borrow = 0; |
221 | 0 | int i; |
222 | 0 | for (i = 0; i < other.used_digits_; ++i) { |
223 | 0 | ASSERT((borrow == 0) || (borrow == 1)); |
224 | 0 | Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow; |
225 | 0 | bigits_[i + offset] = difference & kBigitMask; |
226 | 0 | borrow = difference >> (kChunkSize - 1); |
227 | 0 | } |
228 | 0 | while (borrow != 0) { |
229 | 0 | Chunk difference = bigits_[i + offset] - borrow; |
230 | 0 | bigits_[i + offset] = difference & kBigitMask; |
231 | 0 | borrow = difference >> (kChunkSize - 1); |
232 | 0 | ++i; |
233 | 0 | } |
234 | 0 | Clamp(); |
235 | 0 | } |
236 | | |
237 | | |
238 | 0 | void Bignum::ShiftLeft(int shift_amount) { |
239 | 0 | if (used_digits_ == 0) return; |
240 | 0 | exponent_ += shift_amount / kBigitSize; |
241 | 0 | int local_shift = shift_amount % kBigitSize; |
242 | 0 | EnsureCapacity(used_digits_ + 1); |
243 | 0 | BigitsShiftLeft(local_shift); |
244 | 0 | } |
245 | | |
246 | | |
247 | 0 | void Bignum::MultiplyByUInt32(uint32_t factor) { |
248 | 0 | if (factor == 1) return; |
249 | 0 | if (factor == 0) { |
250 | 0 | Zero(); |
251 | 0 | return; |
252 | 0 | } |
253 | 0 | if (used_digits_ == 0) return; |
254 | | |
255 | | // The product of a bigit with the factor is of size kBigitSize + 32. |
256 | | // Assert that this number + 1 (for the carry) fits into double chunk. |
257 | 0 | ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1); |
258 | 0 | DoubleChunk carry = 0; |
259 | 0 | for (int i = 0; i < used_digits_; ++i) { |
260 | 0 | DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry; |
261 | 0 | bigits_[i] = static_cast<Chunk>(product & kBigitMask); |
262 | 0 | carry = (product >> kBigitSize); |
263 | 0 | } |
264 | 0 | while (carry != 0) { |
265 | 0 | EnsureCapacity(used_digits_ + 1); |
266 | 0 | bigits_[used_digits_] = carry & kBigitMask; |
267 | 0 | used_digits_++; |
268 | 0 | carry >>= kBigitSize; |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | | |
273 | 0 | void Bignum::MultiplyByUInt64(uint64_t factor) { |
274 | 0 | if (factor == 1) return; |
275 | 0 | if (factor == 0) { |
276 | 0 | Zero(); |
277 | 0 | return; |
278 | 0 | } |
279 | 0 | ASSERT(kBigitSize < 32); |
280 | 0 | uint64_t carry = 0; |
281 | 0 | uint64_t low = factor & 0xFFFFFFFF; |
282 | 0 | uint64_t high = factor >> 32; |
283 | 0 | for (int i = 0; i < used_digits_; ++i) { |
284 | 0 | uint64_t product_low = low * bigits_[i]; |
285 | 0 | uint64_t product_high = high * bigits_[i]; |
286 | 0 | uint64_t tmp = (carry & kBigitMask) + product_low; |
287 | 0 | bigits_[i] = tmp & kBigitMask; |
288 | 0 | carry = (carry >> kBigitSize) + (tmp >> kBigitSize) + |
289 | 0 | (product_high << (32 - kBigitSize)); |
290 | 0 | } |
291 | 0 | while (carry != 0) { |
292 | 0 | EnsureCapacity(used_digits_ + 1); |
293 | 0 | bigits_[used_digits_] = carry & kBigitMask; |
294 | 0 | used_digits_++; |
295 | 0 | carry >>= kBigitSize; |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | | |
300 | 0 | void Bignum::MultiplyByPowerOfTen(int exponent) { |
301 | 0 | const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d); |
302 | 0 | const uint16_t kFive1 = 5; |
303 | 0 | const uint16_t kFive2 = kFive1 * 5; |
304 | 0 | const uint16_t kFive3 = kFive2 * 5; |
305 | 0 | const uint16_t kFive4 = kFive3 * 5; |
306 | 0 | const uint16_t kFive5 = kFive4 * 5; |
307 | 0 | const uint16_t kFive6 = kFive5 * 5; |
308 | 0 | const uint32_t kFive7 = kFive6 * 5; |
309 | 0 | const uint32_t kFive8 = kFive7 * 5; |
310 | 0 | const uint32_t kFive9 = kFive8 * 5; |
311 | 0 | const uint32_t kFive10 = kFive9 * 5; |
312 | 0 | const uint32_t kFive11 = kFive10 * 5; |
313 | 0 | const uint32_t kFive12 = kFive11 * 5; |
314 | 0 | const uint32_t kFive13 = kFive12 * 5; |
315 | 0 | const uint32_t kFive1_to_12[] = |
316 | 0 | { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6, |
317 | 0 | kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 }; |
318 | |
|
319 | 0 | ASSERT(exponent >= 0); |
320 | 0 | if (exponent == 0) return; |
321 | 0 | if (used_digits_ == 0) return; |
322 | | |
323 | | // We shift by exponent at the end just before returning. |
324 | 0 | int remaining_exponent = exponent; |
325 | 0 | while (remaining_exponent >= 27) { |
326 | 0 | MultiplyByUInt64(kFive27); |
327 | 0 | remaining_exponent -= 27; |
328 | 0 | } |
329 | 0 | while (remaining_exponent >= 13) { |
330 | 0 | MultiplyByUInt32(kFive13); |
331 | 0 | remaining_exponent -= 13; |
332 | 0 | } |
333 | 0 | if (remaining_exponent > 0) { |
334 | 0 | MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]); |
335 | 0 | } |
336 | 0 | ShiftLeft(exponent); |
337 | 0 | } |
338 | | |
339 | | |
340 | 0 | void Bignum::Square() { |
341 | 0 | ASSERT(IsClamped()); |
342 | 0 | int product_length = 2 * used_digits_; |
343 | 0 | EnsureCapacity(product_length); |
344 | | |
345 | | // Comba multiplication: compute each column separately. |
346 | | // Example: r = a2a1a0 * b2b1b0. |
347 | | // r = 1 * a0b0 + |
348 | | // 10 * (a1b0 + a0b1) + |
349 | | // 100 * (a2b0 + a1b1 + a0b2) + |
350 | | // 1000 * (a2b1 + a1b2) + |
351 | | // 10000 * a2b2 |
352 | | // |
353 | | // In the worst case we have to accumulate nb-digits products of digit*digit. |
354 | | // |
355 | | // Assert that the additional number of bits in a DoubleChunk are enough to |
356 | | // sum up used_digits of Bigit*Bigit. |
357 | 0 | if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) { |
358 | 0 | UNIMPLEMENTED(); |
359 | 0 | } |
360 | 0 | DoubleChunk accumulator = 0; |
361 | | // First shift the digits so we don't overwrite them. |
362 | 0 | int copy_offset = used_digits_; |
363 | 0 | for (int i = 0; i < used_digits_; ++i) { |
364 | 0 | bigits_[copy_offset + i] = bigits_[i]; |
365 | 0 | } |
366 | | // We have two loops to avoid some 'if's in the loop. |
367 | 0 | for (int i = 0; i < used_digits_; ++i) { |
368 | | // Process temporary digit i with power i. |
369 | | // The sum of the two indices must be equal to i. |
370 | 0 | int bigit_index1 = i; |
371 | 0 | int bigit_index2 = 0; |
372 | | // Sum all of the sub-products. |
373 | 0 | while (bigit_index1 >= 0) { |
374 | 0 | Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
375 | 0 | Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
376 | 0 | accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
377 | 0 | bigit_index1--; |
378 | 0 | bigit_index2++; |
379 | 0 | } |
380 | 0 | bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
381 | 0 | accumulator >>= kBigitSize; |
382 | 0 | } |
383 | 0 | for (int i = used_digits_; i < product_length; ++i) { |
384 | 0 | int bigit_index1 = used_digits_ - 1; |
385 | 0 | int bigit_index2 = i - bigit_index1; |
386 | | // Invariant: sum of both indices is again equal to i. |
387 | | // Inner loop runs 0 times on last iteration, emptying accumulator. |
388 | 0 | while (bigit_index2 < used_digits_) { |
389 | 0 | Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
390 | 0 | Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
391 | 0 | accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
392 | 0 | bigit_index1--; |
393 | 0 | bigit_index2++; |
394 | 0 | } |
395 | | // The overwritten bigits_[i] will never be read in further loop iterations, |
396 | | // because bigit_index1 and bigit_index2 are always greater |
397 | | // than i - used_digits_. |
398 | 0 | bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
399 | 0 | accumulator >>= kBigitSize; |
400 | 0 | } |
401 | | // Since the result was guaranteed to lie inside the number the |
402 | | // accumulator must be 0 now. |
403 | 0 | ASSERT(accumulator == 0); |
404 | | |
405 | | // Don't forget to update the used_digits and the exponent. |
406 | 0 | used_digits_ = product_length; |
407 | 0 | exponent_ *= 2; |
408 | 0 | Clamp(); |
409 | 0 | } |
410 | | |
411 | | |
412 | 0 | void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) { |
413 | 0 | ASSERT(base != 0); |
414 | 0 | ASSERT(power_exponent >= 0); |
415 | 0 | if (power_exponent == 0) { |
416 | 0 | AssignUInt16(1); |
417 | 0 | return; |
418 | 0 | } |
419 | 0 | Zero(); |
420 | 0 | int shifts = 0; |
421 | | // We expect base to be in range 2-32, and most often to be 10. |
422 | | // It does not make much sense to implement different algorithms for counting |
423 | | // the bits. |
424 | 0 | while ((base & 1) == 0) { |
425 | 0 | base >>= 1; |
426 | 0 | shifts++; |
427 | 0 | } |
428 | 0 | int bit_size = 0; |
429 | 0 | int tmp_base = base; |
430 | 0 | while (tmp_base != 0) { |
431 | 0 | tmp_base >>= 1; |
432 | 0 | bit_size++; |
433 | 0 | } |
434 | 0 | int final_size = bit_size * power_exponent; |
435 | | // 1 extra bigit for the shifting, and one for rounded final_size. |
436 | 0 | EnsureCapacity(final_size / kBigitSize + 2); |
437 | | |
438 | | // Left to Right exponentiation. |
439 | 0 | int mask = 1; |
440 | 0 | while (power_exponent >= mask) mask <<= 1; |
441 | | |
442 | | // The mask is now pointing to the bit above the most significant 1-bit of |
443 | | // power_exponent. |
444 | | // Get rid of first 1-bit; |
445 | 0 | mask >>= 2; |
446 | 0 | uint64_t this_value = base; |
447 | |
|
448 | 0 | bool delayed_multiplication = false; |
449 | 0 | const uint64_t max_32bits = 0xFFFFFFFF; |
450 | 0 | while (mask != 0 && this_value <= max_32bits) { |
451 | 0 | this_value = this_value * this_value; |
452 | | // Verify that there is enough space in this_value to perform the |
453 | | // multiplication. The first bit_size bits must be 0. |
454 | 0 | if ((power_exponent & mask) != 0) { |
455 | 0 | ASSERT(bit_size > 0); |
456 | 0 | uint64_t base_bits_mask = |
457 | 0 | ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1); |
458 | 0 | bool high_bits_zero = (this_value & base_bits_mask) == 0; |
459 | 0 | if (high_bits_zero) { |
460 | 0 | this_value *= base; |
461 | 0 | } else { |
462 | 0 | delayed_multiplication = true; |
463 | 0 | } |
464 | 0 | } |
465 | 0 | mask >>= 1; |
466 | 0 | } |
467 | 0 | AssignUInt64(this_value); |
468 | 0 | if (delayed_multiplication) { |
469 | 0 | MultiplyByUInt32(base); |
470 | 0 | } |
471 | | |
472 | | // Now do the same thing as a bignum. |
473 | 0 | while (mask != 0) { |
474 | 0 | Square(); |
475 | 0 | if ((power_exponent & mask) != 0) { |
476 | 0 | MultiplyByUInt32(base); |
477 | 0 | } |
478 | 0 | mask >>= 1; |
479 | 0 | } |
480 | | |
481 | | // And finally add the saved shifts. |
482 | 0 | ShiftLeft(shifts * power_exponent); |
483 | 0 | } |
484 | | |
485 | | |
486 | | // Precondition: this/other < 16bit. |
487 | 0 | uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) { |
488 | 0 | ASSERT(IsClamped()); |
489 | 0 | ASSERT(other.IsClamped()); |
490 | 0 | ASSERT(other.used_digits_ > 0); |
491 | | |
492 | | // Easy case: if we have less digits than the divisor than the result is 0. |
493 | | // Note: this handles the case where this == 0, too. |
494 | 0 | if (BigitLength() < other.BigitLength()) { |
495 | 0 | return 0; |
496 | 0 | } |
497 | | |
498 | 0 | Align(other); |
499 | |
|
500 | 0 | uint16_t result = 0; |
501 | | |
502 | | // Start by removing multiples of 'other' until both numbers have the same |
503 | | // number of digits. |
504 | 0 | while (BigitLength() > other.BigitLength()) { |
505 | | // This naive approach is extremely inefficient if `this` divided by other |
506 | | // is big. This function is implemented for doubleToString where |
507 | | // the result should be small (less than 10). |
508 | 0 | ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16)); |
509 | 0 | ASSERT(bigits_[used_digits_ - 1] < 0x10000); |
510 | | // Remove the multiples of the first digit. |
511 | | // Example this = 23 and other equals 9. -> Remove 2 multiples. |
512 | 0 | result += static_cast<uint16_t>(bigits_[used_digits_ - 1]); |
513 | 0 | SubtractTimes(other, bigits_[used_digits_ - 1]); |
514 | 0 | } |
515 | |
|
516 | 0 | ASSERT(BigitLength() == other.BigitLength()); |
517 | | |
518 | | // Both bignums are at the same length now. |
519 | | // Since other has more than 0 digits we know that the access to |
520 | | // bigits_[used_digits_ - 1] is safe. |
521 | 0 | Chunk this_bigit = bigits_[used_digits_ - 1]; |
522 | 0 | Chunk other_bigit = other.bigits_[other.used_digits_ - 1]; |
523 | |
|
524 | 0 | if (other.used_digits_ == 1) { |
525 | | // Shortcut for easy (and common) case. |
526 | 0 | int quotient = this_bigit / other_bigit; |
527 | 0 | bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient; |
528 | 0 | ASSERT(quotient < 0x10000); |
529 | 0 | result += static_cast<uint16_t>(quotient); |
530 | 0 | Clamp(); |
531 | 0 | return result; |
532 | 0 | } |
533 | | |
534 | 0 | int division_estimate = this_bigit / (other_bigit + 1); |
535 | 0 | ASSERT(division_estimate < 0x10000); |
536 | 0 | result += static_cast<uint16_t>(division_estimate); |
537 | 0 | SubtractTimes(other, division_estimate); |
538 | |
|
539 | 0 | if (other_bigit * (division_estimate + 1) > this_bigit) { |
540 | | // No need to even try to subtract. Even if other's remaining digits were 0 |
541 | | // another subtraction would be too much. |
542 | 0 | return result; |
543 | 0 | } |
544 | | |
545 | 0 | while (LessEqual(other, *this)) { |
546 | 0 | SubtractBignum(other); |
547 | 0 | result++; |
548 | 0 | } |
549 | 0 | return result; |
550 | 0 | } |
551 | | |
552 | | |
553 | | template<typename S> |
554 | 0 | static int SizeInHexChars(S number) { |
555 | 0 | ASSERT(number > 0); |
556 | 0 | int result = 0; |
557 | 0 | while (number != 0) { |
558 | 0 | number >>= 4; |
559 | 0 | result++; |
560 | 0 | } |
561 | 0 | return result; |
562 | 0 | } |
563 | | |
564 | | |
565 | 0 | static char HexCharOfValue(int value) { |
566 | 0 | ASSERT(0 <= value && value <= 16); |
567 | 0 | if (value < 10) return static_cast<char>(value + '0'); |
568 | 0 | return static_cast<char>(value - 10 + 'A'); |
569 | 0 | } |
570 | | |
571 | | |
572 | 0 | bool Bignum::ToHexString(char* buffer, int buffer_size) const { |
573 | 0 | ASSERT(IsClamped()); |
574 | | // Each bigit must be printable as separate hex-character. |
575 | 0 | ASSERT(kBigitSize % 4 == 0); |
576 | 0 | const int kHexCharsPerBigit = kBigitSize / 4; |
577 | |
|
578 | 0 | if (used_digits_ == 0) { |
579 | 0 | if (buffer_size < 2) return false; |
580 | 0 | buffer[0] = '0'; |
581 | 0 | buffer[1] = '\0'; |
582 | 0 | return true; |
583 | 0 | } |
584 | | // We add 1 for the terminating '\0' character. |
585 | 0 | int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit + |
586 | 0 | SizeInHexChars(bigits_[used_digits_ - 1]) + 1; |
587 | 0 | if (needed_chars > buffer_size) return false; |
588 | 0 | int string_index = needed_chars - 1; |
589 | 0 | buffer[string_index--] = '\0'; |
590 | 0 | for (int i = 0; i < exponent_; ++i) { |
591 | 0 | for (int j = 0; j < kHexCharsPerBigit; ++j) { |
592 | 0 | buffer[string_index--] = '0'; |
593 | 0 | } |
594 | 0 | } |
595 | 0 | for (int i = 0; i < used_digits_ - 1; ++i) { |
596 | 0 | Chunk current_bigit = bigits_[i]; |
597 | 0 | for (int j = 0; j < kHexCharsPerBigit; ++j) { |
598 | 0 | buffer[string_index--] = HexCharOfValue(current_bigit & 0xF); |
599 | 0 | current_bigit >>= 4; |
600 | 0 | } |
601 | 0 | } |
602 | | // And finally the last bigit. |
603 | 0 | Chunk most_significant_bigit = bigits_[used_digits_ - 1]; |
604 | 0 | while (most_significant_bigit != 0) { |
605 | 0 | buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF); |
606 | 0 | most_significant_bigit >>= 4; |
607 | 0 | } |
608 | 0 | return true; |
609 | 0 | } |
610 | | |
611 | | |
612 | 0 | Bignum::Chunk Bignum::BigitAt(int index) const { |
613 | 0 | if (index >= BigitLength()) return 0; |
614 | 0 | if (index < exponent_) return 0; |
615 | 0 | return bigits_[index - exponent_]; |
616 | 0 | } |
617 | | |
618 | | |
619 | 0 | int Bignum::Compare(const Bignum& a, const Bignum& b) { |
620 | 0 | ASSERT(a.IsClamped()); |
621 | 0 | ASSERT(b.IsClamped()); |
622 | 0 | int bigit_length_a = a.BigitLength(); |
623 | 0 | int bigit_length_b = b.BigitLength(); |
624 | 0 | if (bigit_length_a < bigit_length_b) return -1; |
625 | 0 | if (bigit_length_a > bigit_length_b) return +1; |
626 | 0 | for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) { |
627 | 0 | Chunk bigit_a = a.BigitAt(i); |
628 | 0 | Chunk bigit_b = b.BigitAt(i); |
629 | 0 | if (bigit_a < bigit_b) return -1; |
630 | 0 | if (bigit_a > bigit_b) return +1; |
631 | | // Otherwise they are equal up to this digit. Try the next digit. |
632 | 0 | } |
633 | 0 | return 0; |
634 | 0 | } |
635 | | |
636 | | |
637 | 0 | int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) { |
638 | 0 | ASSERT(a.IsClamped()); |
639 | 0 | ASSERT(b.IsClamped()); |
640 | 0 | ASSERT(c.IsClamped()); |
641 | 0 | if (a.BigitLength() < b.BigitLength()) { |
642 | 0 | return PlusCompare(b, a, c); |
643 | 0 | } |
644 | 0 | if (a.BigitLength() + 1 < c.BigitLength()) return -1; |
645 | 0 | if (a.BigitLength() > c.BigitLength()) return +1; |
646 | | // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than |
647 | | // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one |
648 | | // of 'a'. |
649 | 0 | if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { |
650 | 0 | return -1; |
651 | 0 | } |
652 | | |
653 | 0 | Chunk borrow = 0; |
654 | | // Starting at min_exponent all digits are == 0. So no need to compare them. |
655 | 0 | int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_); |
656 | 0 | for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { |
657 | 0 | Chunk chunk_a = a.BigitAt(i); |
658 | 0 | Chunk chunk_b = b.BigitAt(i); |
659 | 0 | Chunk chunk_c = c.BigitAt(i); |
660 | 0 | Chunk sum = chunk_a + chunk_b; |
661 | 0 | if (sum > chunk_c + borrow) { |
662 | 0 | return +1; |
663 | 0 | } else { |
664 | 0 | borrow = chunk_c + borrow - sum; |
665 | 0 | if (borrow > 1) return -1; |
666 | 0 | borrow <<= kBigitSize; |
667 | 0 | } |
668 | 0 | } |
669 | 0 | if (borrow == 0) return 0; |
670 | 0 | return -1; |
671 | 0 | } |
672 | | |
673 | | |
674 | 0 | void Bignum::Clamp() { |
675 | 0 | while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) { |
676 | 0 | used_digits_--; |
677 | 0 | } |
678 | 0 | if (used_digits_ == 0) { |
679 | | // Zero. |
680 | 0 | exponent_ = 0; |
681 | 0 | } |
682 | 0 | } |
683 | | |
684 | | |
685 | 0 | bool Bignum::IsClamped() const { |
686 | 0 | return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0; |
687 | 0 | } |
688 | | |
689 | | |
690 | 0 | void Bignum::Zero() { |
691 | 0 | for (int i = 0; i < used_digits_; ++i) { |
692 | 0 | bigits_[i] = 0; |
693 | 0 | } |
694 | 0 | used_digits_ = 0; |
695 | 0 | exponent_ = 0; |
696 | 0 | } |
697 | | |
698 | | |
699 | 0 | void Bignum::Align(const Bignum& other) { |
700 | 0 | if (exponent_ > other.exponent_) { |
701 | | // If "X" represents a "hidden" digit (by the exponent) then we are in the |
702 | | // following case (a == this, b == other): |
703 | | // a: aaaaaaXXXX or a: aaaaaXXX |
704 | | // b: bbbbbbX b: bbbbbbbbXX |
705 | | // We replace some of the hidden digits (X) of a with 0 digits. |
706 | | // a: aaaaaa000X or a: aaaaa0XX |
707 | 0 | int zero_digits = exponent_ - other.exponent_; |
708 | 0 | EnsureCapacity(used_digits_ + zero_digits); |
709 | 0 | for (int i = used_digits_ - 1; i >= 0; --i) { |
710 | 0 | bigits_[i + zero_digits] = bigits_[i]; |
711 | 0 | } |
712 | 0 | for (int i = 0; i < zero_digits; ++i) { |
713 | 0 | bigits_[i] = 0; |
714 | 0 | } |
715 | 0 | used_digits_ += zero_digits; |
716 | 0 | exponent_ -= zero_digits; |
717 | 0 | ASSERT(used_digits_ >= 0); |
718 | 0 | ASSERT(exponent_ >= 0); |
719 | 0 | } |
720 | 0 | } |
721 | | |
722 | | |
723 | 0 | void Bignum::BigitsShiftLeft(int shift_amount) { |
724 | 0 | ASSERT(shift_amount < kBigitSize); |
725 | 0 | ASSERT(shift_amount >= 0); |
726 | 0 | Chunk carry = 0; |
727 | 0 | for (int i = 0; i < used_digits_; ++i) { |
728 | 0 | Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount); |
729 | 0 | bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask; |
730 | 0 | carry = new_carry; |
731 | 0 | } |
732 | 0 | if (carry != 0) { |
733 | 0 | bigits_[used_digits_] = carry; |
734 | 0 | used_digits_++; |
735 | 0 | } |
736 | 0 | } |
737 | | |
738 | | |
739 | 0 | void Bignum::SubtractTimes(const Bignum& other, int factor) { |
740 | 0 | ASSERT(exponent_ <= other.exponent_); |
741 | 0 | if (factor < 3) { |
742 | 0 | for (int i = 0; i < factor; ++i) { |
743 | 0 | SubtractBignum(other); |
744 | 0 | } |
745 | 0 | return; |
746 | 0 | } |
747 | 0 | Chunk borrow = 0; |
748 | 0 | int exponent_diff = other.exponent_ - exponent_; |
749 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
750 | 0 | DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i]; |
751 | 0 | DoubleChunk remove = borrow + product; |
752 | 0 | Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask); |
753 | 0 | bigits_[i + exponent_diff] = difference & kBigitMask; |
754 | 0 | borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) + |
755 | 0 | (remove >> kBigitSize)); |
756 | 0 | } |
757 | 0 | for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { |
758 | 0 | if (borrow == 0) return; |
759 | 0 | Chunk difference = bigits_[i] - borrow; |
760 | 0 | bigits_[i] = difference & kBigitMask; |
761 | 0 | borrow = difference >> (kChunkSize - 1); |
762 | 0 | } |
763 | 0 | Clamp(); |
764 | 0 | } |
765 | | |
766 | | |
767 | | } // namespace double_conversion |