Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(651)

Side by Side Diff: runtime/vm/bigint_operations.cc

Issue 23455014: Remove limit on bigint multiplication - fixes issue 12833. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/big_integer_vm_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 Google Inc. All Rights Reserved. 1 // Copyright 2012 Google Inc. All Rights Reserved.
2 2
3 #include "vm/bigint_operations.h" 3 #include "vm/bigint_operations.h"
4 4
5 #include "platform/utils.h" 5 #include "platform/utils.h"
6 6
7 #include "vm/double_internals.h" 7 #include "vm/double_internals.h"
8 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
9 #include "vm/object_store.h" 9 #include "vm/object_store.h"
10 #include "vm/zone.h" 10 #include "vm/zone.h"
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 } 714 }
715 715
716 // Comba multiplication: compute each column separately. 716 // Comba multiplication: compute each column separately.
717 // Example: r = a2a1a0 * b2b1b0. 717 // Example: r = a2a1a0 * b2b1b0.
718 // r = 1 * a0b0 + 718 // r = 1 * a0b0 +
719 // 10 * (a1b0 + a0b1) + 719 // 10 * (a1b0 + a0b1) +
720 // 100 * (a2b0 + a1b1 + a0b2) + 720 // 100 * (a2b0 + a1b1 + a0b2) +
721 // 1000 * (a2b1 + a1b2) + 721 // 1000 * (a2b1 + a1b2) +
722 // 10000 * a2b2 722 // 10000 * a2b2
723 // 723 //
724 // Each column will be accumulated in an integer of type DoubleChunk. We 724 // Each column will be accumulated in an integer of type DoubleChunk. We must
725 // must guarantee that the column-sum will not overflow. 725 // guarantee that the column-sum will not overflow. We achieve this by
726 // 'blocking' the sum into overflow-free sums followed by propagating the
727 // overflow.
726 // 728 //
727 // In the worst case we have to accumulate k = Min(a.length, b.length) 729 // In the worst case we have to accumulate k = Min(a.length, b.length)
728 // products plus the carry from the previous round. 730 // products plus the carry from the previous round.
729 // Each bigint-digit is smaller than beta = 2^kDigitBitSize. 731 // Each bigint-digit is smaller than beta = 2^kDigitBitSize.
730 // Each product is at most (beta - 1)^2. 732 // Each product is at most (beta - 1)^2.
731 // If we want to use Comba multiplication the following condition must hold: 733
734 // If we want to use Comba multiplication and accumulate into a single
735 // register without overflow, the following condition must hold:
732 // k * (beta - 1)^2 + (2^(kDoubleChunkBitSize - kDigitBitSize) - 1) < 736 // k * (beta - 1)^2 + (2^(kDoubleChunkBitSize - kDigitBitSize) - 1) <
733 // 2^kDoubleChunkBitSize. 737 // 2^kDoubleChunkBitSize.
738 // Solving this give the maximum number of digits that can be summed in
739 // between handling the overflow.
734 const DoubleChunk square = 740 const DoubleChunk square =
735 static_cast<DoubleChunk>(kDigitMaxValue) * kDigitMaxValue; 741 static_cast<DoubleChunk>(kDigitMaxValue) * kDigitMaxValue;
736 const DoubleChunk kDoubleChunkMaxValue = static_cast<DoubleChunk>(-1); 742 const DoubleChunk kDoubleChunkMaxValue = static_cast<DoubleChunk>(-1);
737 const DoubleChunk left_over_carry = kDoubleChunkMaxValue >> kDigitBitSize; 743 const DoubleChunk left_over_carry = kDoubleChunkMaxValue >> kDigitBitSize;
738 const intptr_t kMaxDigits = (kDoubleChunkMaxValue - left_over_carry) / square; 744 const intptr_t kMaxDigits = (kDoubleChunkMaxValue - left_over_carry) / square;
739 if (Utils::Minimum(a_length, b_length) > kMaxDigits) { 745 const intptr_t kBlockSize = kMaxDigits - 1; // -1 for incomming carry.
740 // Use the preallocated out of memory exception to avoid calling
741 // into dart code or allocating any code.
742 Isolate* isolate = Isolate::Current();
743 const Instance& exception =
744 Instance::Handle(isolate->object_store()->out_of_memory());
745 Exceptions::Throw(exception);
746 UNREACHABLE();
747 }
748 746
749 DoubleChunk accumulator = 0; // Accumulates the result of one column. 747 DoubleChunk accumulator = 0; // Accumulates the result of one column.
750 for (intptr_t i = 0; i < result_length; i++) { 748 for (intptr_t i = 0; i < result_length; i++) {
751 // Example: r = a2a1a0 * b2b1b0. 749 // Example: r = a2a1a0 * b2b1b0.
752 // For i == 0, compute a0b0. 750 // For i == 0, compute a0b0.
753 // i == 1, a1b0 + a0b1 + overflow from i == 0. 751 // i == 1, a1b0 + a0b1 + overflow from i == 0.
754 // i == 2, a2b0 + a1b1 + a0b2 + overflow from i == 1. 752 // i == 2, a2b0 + a1b1 + a0b2 + overflow from i == 1.
755 // ... 753 // ...
756 // The indices into a and b are such that their sum equals i. 754 // The indices into a and b are such that their sum equals i.
757 intptr_t a_index = Utils::Minimum(a_length - 1, i); 755 intptr_t a_index = Utils::Minimum(a_length - 1, i);
758 intptr_t b_index = i - a_index; 756 intptr_t b_index = i - a_index;
759 ASSERT(a_index + b_index == i); 757 ASSERT(a_index + b_index == i);
760 758
761 // Instead of testing for a_index >= 0 && b_index < b_length we compute the 759 // Instead of testing for a_index >= 0 && b_index < b_length we compute the
762 // number of iterations first. 760 // number of iterations first.
763 intptr_t iterations = Utils::Minimum(b_length - b_index, a_index + 1); 761 intptr_t iterations = Utils::Minimum(b_length - b_index, a_index + 1);
764 for (intptr_t j = 0; j < iterations; j++) { 762
765 DoubleChunk chunk_a = a.GetChunkAt(a_index); 763 // For large products we need extra bit for the overflow. The sum is broken
766 DoubleChunk chunk_b = b.GetChunkAt(b_index); 764 // into blocks to avoid dealing with the overflow on each iteration.
767 accumulator += chunk_a * chunk_b; 765 DoubleChunk accumulator_overflow = 0;
768 a_index--; 766 for (intptr_t j_block = 0; j_block < iterations; j_block += kBlockSize) {
769 b_index++; 767 intptr_t j_end = Utils::Minimum(j_block + kBlockSize, iterations);
768 for (intptr_t j = j_block; j < j_end; j++) {
769 DoubleChunk chunk_a = a.GetChunkAt(a_index);
770 DoubleChunk chunk_b = b.GetChunkAt(b_index);
771 accumulator += chunk_a * chunk_b;
floitsch 2013/08/30 07:38:05 In theory the accumulator from the previous run ca
sra1 2013/08/30 18:24:54 I split the overflow into a digit and an extra ove
772 a_index--;
773 b_index++;
774 }
775 accumulator_overflow += (accumulator >> kDigitBitSize);
776 accumulator &= kDigitMask;
770 } 777 }
771 result.SetChunkAt(i, static_cast<Chunk>(accumulator & kDigitMask)); 778 result.SetChunkAt(i, static_cast<Chunk>(accumulator));
772 accumulator >>= kDigitBitSize; 779 accumulator = accumulator_overflow;
780 accumulator_overflow = 0;
773 } 781 }
774 ASSERT(accumulator == 0); 782 ASSERT(accumulator == 0);
775 783
776 Clamp(result); 784 Clamp(result);
777 return result.raw(); 785 return result.raw();
778 } 786 }
779 787
780 788
781 RawBigint* BigintOperations::Divide(const Bigint& a, const Bigint& b) { 789 RawBigint* BigintOperations::Divide(const Bigint& a, const Bigint& b) {
782 Bigint& quotient = Bigint::Handle(); 790 Bigint& quotient = Bigint::Handle();
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 int BigintOperations::CountBits(Chunk digit) { 1680 int BigintOperations::CountBits(Chunk digit) {
1673 int result = 0; 1681 int result = 0;
1674 while (digit != 0) { 1682 while (digit != 0) {
1675 digit >>= 1; 1683 digit >>= 1;
1676 result++; 1684 result++;
1677 } 1685 }
1678 return result; 1686 return result;
1679 } 1687 }
1680 1688
1681 } // namespace dart 1689 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/big_integer_vm_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698