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

Unified Diff: runtime/vm/bigint_operations.cc

Issue 23479024: Implement BigintOperations::MultiplyWithDigit (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/bigint_operations.cc
diff --git a/runtime/vm/bigint_operations.cc b/runtime/vm/bigint_operations.cc
index fc544427551949520e07ee7ea7f801170168eda0..7fba196e17bbb3573b825f9603dba63b86a15ed6 100644
--- a/runtime/vm/bigint_operations.cc
+++ b/runtime/vm/bigint_operations.cc
@@ -1500,13 +1500,26 @@ RawBigint* BigintOperations::UnsignedSubtract(const Bigint& a,
RawBigint* BigintOperations::MultiplyWithDigit(
const Bigint& bigint, Chunk digit) {
- // TODO(floitsch): implement MultiplyWithDigit.
ASSERT(digit <= kDigitMaxValue);
if (digit == 0) return Zero();
+ if (bigint.IsZero()) return Zero();
- Bigint& tmp = Bigint::Handle(Bigint::Allocate(1));
- tmp.SetChunkAt(0, digit);
- return Multiply(bigint, tmp);
+ intptr_t length = bigint.Length();
+ intptr_t result_length = length + 1;
+ const Bigint& result = Bigint::Handle(Bigint::Allocate(result_length));
+
+ Chunk carry = 0;
+ for (intptr_t i = 0; i < length; i++) {
+ Chunk chunk = bigint.GetChunkAt(i);
+ DoubleChunk product = (static_cast<DoubleChunk>(chunk) * digit) + carry;
+ result.SetChunkAt(i, static_cast<Chunk>(product & kDigitMask));
+ carry = static_cast<Chunk>(product >> kDigitBitSize);
+ }
+ result.SetChunkAt(length, carry);
+
+ result.SetSign(bigint.IsNegative());
+ Clamp(result);
+ return result.raw();
}
« 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