| OLD | NEW |
| 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 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1493 ASSERT((borrow == 0) || (borrow == 1)); | 1493 ASSERT((borrow == 0) || (borrow == 1)); |
| 1494 } | 1494 } |
| 1495 ASSERT(borrow == 0); | 1495 ASSERT(borrow == 0); |
| 1496 Clamp(result); | 1496 Clamp(result); |
| 1497 return result.raw(); | 1497 return result.raw(); |
| 1498 } | 1498 } |
| 1499 | 1499 |
| 1500 | 1500 |
| 1501 RawBigint* BigintOperations::MultiplyWithDigit( | 1501 RawBigint* BigintOperations::MultiplyWithDigit( |
| 1502 const Bigint& bigint, Chunk digit) { | 1502 const Bigint& bigint, Chunk digit) { |
| 1503 // TODO(floitsch): implement MultiplyWithDigit. | |
| 1504 ASSERT(digit <= kDigitMaxValue); | 1503 ASSERT(digit <= kDigitMaxValue); |
| 1505 if (digit == 0) return Zero(); | 1504 if (digit == 0) return Zero(); |
| 1505 if (bigint.IsZero()) return Zero(); |
| 1506 | 1506 |
| 1507 Bigint& tmp = Bigint::Handle(Bigint::Allocate(1)); | 1507 intptr_t length = bigint.Length(); |
| 1508 tmp.SetChunkAt(0, digit); | 1508 intptr_t result_length = length + 1; |
| 1509 return Multiply(bigint, tmp); | 1509 const Bigint& result = Bigint::Handle(Bigint::Allocate(result_length)); |
| 1510 |
| 1511 Chunk carry = 0; |
| 1512 for (intptr_t i = 0; i < length; i++) { |
| 1513 Chunk chunk = bigint.GetChunkAt(i); |
| 1514 DoubleChunk product = (static_cast<DoubleChunk>(chunk) * digit) + carry; |
| 1515 result.SetChunkAt(i, static_cast<Chunk>(product & kDigitMask)); |
| 1516 carry = static_cast<Chunk>(product >> kDigitBitSize); |
| 1517 } |
| 1518 result.SetChunkAt(length, carry); |
| 1519 |
| 1520 result.SetSign(bigint.IsNegative()); |
| 1521 Clamp(result); |
| 1522 return result.raw(); |
| 1510 } | 1523 } |
| 1511 | 1524 |
| 1512 | 1525 |
| 1513 void BigintOperations::DivideRemainder( | 1526 void BigintOperations::DivideRemainder( |
| 1514 const Bigint& a, const Bigint& b, Bigint* quotient, Bigint* remainder) { | 1527 const Bigint& a, const Bigint& b, Bigint* quotient, Bigint* remainder) { |
| 1515 // TODO(floitsch): This function is very memory-intensive since all | 1528 // TODO(floitsch): This function is very memory-intensive since all |
| 1516 // intermediate bigint results are allocated in new memory. It would be | 1529 // intermediate bigint results are allocated in new memory. It would be |
| 1517 // much more efficient to reuse the space of temporary intermediate variables. | 1530 // much more efficient to reuse the space of temporary intermediate variables. |
| 1518 ASSERT(IsClamped(a)); | 1531 ASSERT(IsClamped(a)); |
| 1519 ASSERT(IsClamped(b)); | 1532 ASSERT(IsClamped(b)); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1707 int BigintOperations::CountBits(Chunk digit) { | 1720 int BigintOperations::CountBits(Chunk digit) { |
| 1708 int result = 0; | 1721 int result = 0; |
| 1709 while (digit != 0) { | 1722 while (digit != 0) { |
| 1710 digit >>= 1; | 1723 digit >>= 1; |
| 1711 result++; | 1724 result++; |
| 1712 } | 1725 } |
| 1713 return result; | 1726 return result; |
| 1714 } | 1727 } |
| 1715 | 1728 |
| 1716 } // namespace dart | 1729 } // namespace dart |
| OLD | NEW |