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

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

Issue 11299173: Throw OutOfMemory instead of crashing when bigint multiply overflows. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years 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 | no next file » | 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 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 // Each product is at most (beta - 1)^2. 724 // Each product is at most (beta - 1)^2.
725 // If we want to use Comba multiplication the following condition must hold: 725 // If we want to use Comba multiplication the following condition must hold:
726 // k * (beta - 1)^2 + (2^(kDoubleChunkBitSize - kDigitBitSize) - 1) < 726 // k * (beta - 1)^2 + (2^(kDoubleChunkBitSize - kDigitBitSize) - 1) <
727 // 2^kDoubleChunkBitSize. 727 // 2^kDoubleChunkBitSize.
728 const DoubleChunk square = 728 const DoubleChunk square =
729 static_cast<DoubleChunk>(kDigitMaxValue) * kDigitMaxValue; 729 static_cast<DoubleChunk>(kDigitMaxValue) * kDigitMaxValue;
730 const DoubleChunk kDoubleChunkMaxValue = static_cast<DoubleChunk>(-1); 730 const DoubleChunk kDoubleChunkMaxValue = static_cast<DoubleChunk>(-1);
731 const DoubleChunk left_over_carry = kDoubleChunkMaxValue >> kDigitBitSize; 731 const DoubleChunk left_over_carry = kDoubleChunkMaxValue >> kDigitBitSize;
732 const intptr_t kMaxDigits = (kDoubleChunkMaxValue - left_over_carry) / square; 732 const intptr_t kMaxDigits = (kDoubleChunkMaxValue - left_over_carry) / square;
733 if (Utils::Minimum(a_length, b_length) > kMaxDigits) { 733 if (Utils::Minimum(a_length, b_length) > kMaxDigits) {
734 UNIMPLEMENTED(); 734 // Use the preallocated out of memory exception to avoid calling
735 // into dart code or allocating any code.
736 Isolate* isolate = Isolate::Current();
737 const Instance& exception =
738 Instance::Handle(isolate->object_store()->out_of_memory());
739 Exceptions::Throw(exception);
740 UNREACHABLE();
735 } 741 }
736 742
737 DoubleChunk accumulator = 0; // Accumulates the result of one column. 743 DoubleChunk accumulator = 0; // Accumulates the result of one column.
738 for (intptr_t i = 0; i < result_length; i++) { 744 for (intptr_t i = 0; i < result_length; i++) {
739 // Example: r = a2a1a0 * b2b1b0. 745 // Example: r = a2a1a0 * b2b1b0.
740 // For i == 0, compute a0b0. 746 // For i == 0, compute a0b0.
741 // i == 1, a1b0 + a0b1 + overflow from i == 0. 747 // i == 1, a1b0 + a0b1 + overflow from i == 0.
742 // i == 2, a2b0 + a1b1 + a0b2 + overflow from i == 1. 748 // i == 2, a2b0 + a1b1 + a0b2 + overflow from i == 1.
743 // ... 749 // ...
744 // The indices into a and b are such that their sum equals i. 750 // The indices into a and b are such that their sum equals i.
(...skipping 911 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 int BigintOperations::CountBits(Chunk digit) { 1662 int BigintOperations::CountBits(Chunk digit) {
1657 int result = 0; 1663 int result = 0;
1658 while (digit != 0) { 1664 while (digit != 0) {
1659 digit >>= 1; 1665 digit >>= 1;
1660 result++; 1666 result++;
1661 } 1667 }
1662 return result; 1668 return result;
1663 } 1669 }
1664 1670
1665 } // namespace dart 1671 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698