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