OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 717 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 carry = new_carry; | 728 carry = new_carry; |
729 } | 729 } |
730 if (carry != 0) { | 730 if (carry != 0) { |
731 bigits_[used_digits_] = carry; | 731 bigits_[used_digits_] = carry; |
732 used_digits_++; | 732 used_digits_++; |
733 } | 733 } |
734 } | 734 } |
735 | 735 |
736 | 736 |
737 void Bignum::SubtractTimes(const Bignum& other, int factor) { | 737 void Bignum::SubtractTimes(const Bignum& other, int factor) { |
| 738 #ifdef DEBUG |
| 739 Bignum a, b; |
| 740 a.AssignBignum(*this); |
| 741 b.AssignBignum(other); |
| 742 b.MultiplyByUInt32(factor); |
| 743 a.SubtractBignum(b); |
| 744 #endif |
738 ASSERT(exponent_ <= other.exponent_); | 745 ASSERT(exponent_ <= other.exponent_); |
739 if (factor < 3) { | 746 if (factor < 3) { |
740 for (int i = 0; i < factor; ++i) { | 747 for (int i = 0; i < factor; ++i) { |
741 SubtractBignum(other); | 748 SubtractBignum(other); |
742 } | 749 } |
743 return; | 750 return; |
744 } | 751 } |
745 Chunk borrow = 0; | 752 Chunk borrow = 0; |
746 int exponent_diff = other.exponent_ - exponent_; | 753 int exponent_diff = other.exponent_ - exponent_; |
747 for (int i = 0; i < other.used_digits_; ++i) { | 754 for (int i = 0; i < other.used_digits_; ++i) { |
748 DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i]; | 755 DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i]; |
749 DoubleChunk remove = borrow + product; | 756 DoubleChunk remove = borrow + product; |
750 Chunk difference = | 757 Chunk difference = |
751 bigits_[i + exponent_diff] - static_cast<Chunk>(remove & kBigitMask); | 758 bigits_[i + exponent_diff] - static_cast<Chunk>(remove & kBigitMask); |
752 bigits_[i + exponent_diff] = difference & kBigitMask; | 759 bigits_[i + exponent_diff] = difference & kBigitMask; |
753 borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) + | 760 borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) + |
754 (remove >> kBigitSize)); | 761 (remove >> kBigitSize)); |
755 } | 762 } |
756 for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { | 763 for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { |
757 if (borrow == 0) return; | 764 if (borrow == 0) return; |
758 Chunk difference = bigits_[i] - borrow; | 765 Chunk difference = bigits_[i] - borrow; |
759 bigits_[i] = difference & kBigitMask; | 766 bigits_[i] = difference & kBigitMask; |
760 borrow = difference >> (kChunkSize - 1); | 767 borrow = difference >> (kChunkSize - 1); |
761 ++i; | |
762 } | 768 } |
763 Clamp(); | 769 Clamp(); |
| 770 ASSERT(Bignum::Equal(a, *this)); |
764 } | 771 } |
765 | 772 |
766 | 773 |
767 } } // namespace v8::internal | 774 } } // namespace v8::internal |
OLD | NEW |