michael@0: // Copyright 2010 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include "bignum.h" michael@0: #include "utils.h" michael@0: michael@0: namespace double_conversion { michael@0: michael@0: Bignum::Bignum() michael@0: : bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) { michael@0: for (int i = 0; i < kBigitCapacity; ++i) { michael@0: bigits_[i] = 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: template michael@0: static int BitSize(S value) { michael@0: return 8 * sizeof(value); michael@0: } michael@0: michael@0: // Guaranteed to lie in one Bigit. michael@0: void Bignum::AssignUInt16(uint16_t value) { michael@0: ASSERT(kBigitSize >= BitSize(value)); michael@0: Zero(); michael@0: if (value == 0) return; michael@0: michael@0: EnsureCapacity(1); michael@0: bigits_[0] = value; michael@0: used_digits_ = 1; michael@0: } michael@0: michael@0: michael@0: void Bignum::AssignUInt64(uint64_t value) { michael@0: const int kUInt64Size = 64; michael@0: michael@0: Zero(); michael@0: if (value == 0) return; michael@0: michael@0: int needed_bigits = kUInt64Size / kBigitSize + 1; michael@0: EnsureCapacity(needed_bigits); michael@0: for (int i = 0; i < needed_bigits; ++i) { michael@0: bigits_[i] = value & kBigitMask; michael@0: value = value >> kBigitSize; michael@0: } michael@0: used_digits_ = needed_bigits; michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: void Bignum::AssignBignum(const Bignum& other) { michael@0: exponent_ = other.exponent_; michael@0: for (int i = 0; i < other.used_digits_; ++i) { michael@0: bigits_[i] = other.bigits_[i]; michael@0: } michael@0: // Clear the excess digits (if there were any). michael@0: for (int i = other.used_digits_; i < used_digits_; ++i) { michael@0: bigits_[i] = 0; michael@0: } michael@0: used_digits_ = other.used_digits_; michael@0: } michael@0: michael@0: michael@0: static uint64_t ReadUInt64(Vector buffer, michael@0: int from, michael@0: int digits_to_read) { michael@0: uint64_t result = 0; michael@0: for (int i = from; i < from + digits_to_read; ++i) { michael@0: int digit = buffer[i] - '0'; michael@0: ASSERT(0 <= digit && digit <= 9); michael@0: result = result * 10 + digit; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: void Bignum::AssignDecimalString(Vector value) { michael@0: // 2^64 = 18446744073709551616 > 10^19 michael@0: const int kMaxUint64DecimalDigits = 19; michael@0: Zero(); michael@0: int length = value.length(); michael@0: int pos = 0; michael@0: // Let's just say that each digit needs 4 bits. michael@0: while (length >= kMaxUint64DecimalDigits) { michael@0: uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits); michael@0: pos += kMaxUint64DecimalDigits; michael@0: length -= kMaxUint64DecimalDigits; michael@0: MultiplyByPowerOfTen(kMaxUint64DecimalDigits); michael@0: AddUInt64(digits); michael@0: } michael@0: uint64_t digits = ReadUInt64(value, pos, length); michael@0: MultiplyByPowerOfTen(length); michael@0: AddUInt64(digits); michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: static int HexCharValue(char c) { michael@0: if ('0' <= c && c <= '9') return c - '0'; michael@0: if ('a' <= c && c <= 'f') return 10 + c - 'a'; michael@0: if ('A' <= c && c <= 'F') return 10 + c - 'A'; michael@0: UNREACHABLE(); michael@0: return 0; // To make compiler happy. michael@0: } michael@0: michael@0: michael@0: void Bignum::AssignHexString(Vector value) { michael@0: Zero(); michael@0: int length = value.length(); michael@0: michael@0: int needed_bigits = length * 4 / kBigitSize + 1; michael@0: EnsureCapacity(needed_bigits); michael@0: int string_index = length - 1; michael@0: for (int i = 0; i < needed_bigits - 1; ++i) { michael@0: // These bigits are guaranteed to be "full". michael@0: Chunk current_bigit = 0; michael@0: for (int j = 0; j < kBigitSize / 4; j++) { michael@0: current_bigit += HexCharValue(value[string_index--]) << (j * 4); michael@0: } michael@0: bigits_[i] = current_bigit; michael@0: } michael@0: used_digits_ = needed_bigits - 1; michael@0: michael@0: Chunk most_significant_bigit = 0; // Could be = 0; michael@0: for (int j = 0; j <= string_index; ++j) { michael@0: most_significant_bigit <<= 4; michael@0: most_significant_bigit += HexCharValue(value[j]); michael@0: } michael@0: if (most_significant_bigit != 0) { michael@0: bigits_[used_digits_] = most_significant_bigit; michael@0: used_digits_++; michael@0: } michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: void Bignum::AddUInt64(uint64_t operand) { michael@0: if (operand == 0) return; michael@0: Bignum other; michael@0: other.AssignUInt64(operand); michael@0: AddBignum(other); michael@0: } michael@0: michael@0: michael@0: void Bignum::AddBignum(const Bignum& other) { michael@0: ASSERT(IsClamped()); michael@0: ASSERT(other.IsClamped()); michael@0: michael@0: // If this has a greater exponent than other append zero-bigits to this. michael@0: // After this call exponent_ <= other.exponent_. michael@0: Align(other); michael@0: michael@0: // There are two possibilities: michael@0: // aaaaaaaaaaa 0000 (where the 0s represent a's exponent) michael@0: // bbbbb 00000000 michael@0: // ---------------- michael@0: // ccccccccccc 0000 michael@0: // or michael@0: // aaaaaaaaaa 0000 michael@0: // bbbbbbbbb 0000000 michael@0: // ----------------- michael@0: // cccccccccccc 0000 michael@0: // In both cases we might need a carry bigit. michael@0: michael@0: EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_); michael@0: Chunk carry = 0; michael@0: int bigit_pos = other.exponent_ - exponent_; michael@0: ASSERT(bigit_pos >= 0); michael@0: for (int i = 0; i < other.used_digits_; ++i) { michael@0: Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry; michael@0: bigits_[bigit_pos] = sum & kBigitMask; michael@0: carry = sum >> kBigitSize; michael@0: bigit_pos++; michael@0: } michael@0: michael@0: while (carry != 0) { michael@0: Chunk sum = bigits_[bigit_pos] + carry; michael@0: bigits_[bigit_pos] = sum & kBigitMask; michael@0: carry = sum >> kBigitSize; michael@0: bigit_pos++; michael@0: } michael@0: used_digits_ = Max(bigit_pos, used_digits_); michael@0: ASSERT(IsClamped()); michael@0: } michael@0: michael@0: michael@0: void Bignum::SubtractBignum(const Bignum& other) { michael@0: ASSERT(IsClamped()); michael@0: ASSERT(other.IsClamped()); michael@0: // We require this to be bigger than other. michael@0: ASSERT(LessEqual(other, *this)); michael@0: michael@0: Align(other); michael@0: michael@0: int offset = other.exponent_ - exponent_; michael@0: Chunk borrow = 0; michael@0: int i; michael@0: for (i = 0; i < other.used_digits_; ++i) { michael@0: ASSERT((borrow == 0) || (borrow == 1)); michael@0: Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow; michael@0: bigits_[i + offset] = difference & kBigitMask; michael@0: borrow = difference >> (kChunkSize - 1); michael@0: } michael@0: while (borrow != 0) { michael@0: Chunk difference = bigits_[i + offset] - borrow; michael@0: bigits_[i + offset] = difference & kBigitMask; michael@0: borrow = difference >> (kChunkSize - 1); michael@0: ++i; michael@0: } michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: void Bignum::ShiftLeft(int shift_amount) { michael@0: if (used_digits_ == 0) return; michael@0: exponent_ += shift_amount / kBigitSize; michael@0: int local_shift = shift_amount % kBigitSize; michael@0: EnsureCapacity(used_digits_ + 1); michael@0: BigitsShiftLeft(local_shift); michael@0: } michael@0: michael@0: michael@0: void Bignum::MultiplyByUInt32(uint32_t factor) { michael@0: if (factor == 1) return; michael@0: if (factor == 0) { michael@0: Zero(); michael@0: return; michael@0: } michael@0: if (used_digits_ == 0) return; michael@0: michael@0: // The product of a bigit with the factor is of size kBigitSize + 32. michael@0: // Assert that this number + 1 (for the carry) fits into double chunk. michael@0: ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1); michael@0: DoubleChunk carry = 0; michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: DoubleChunk product = static_cast(factor) * bigits_[i] + carry; michael@0: bigits_[i] = static_cast(product & kBigitMask); michael@0: carry = (product >> kBigitSize); michael@0: } michael@0: while (carry != 0) { michael@0: EnsureCapacity(used_digits_ + 1); michael@0: bigits_[used_digits_] = carry & kBigitMask; michael@0: used_digits_++; michael@0: carry >>= kBigitSize; michael@0: } michael@0: } michael@0: michael@0: michael@0: void Bignum::MultiplyByUInt64(uint64_t factor) { michael@0: if (factor == 1) return; michael@0: if (factor == 0) { michael@0: Zero(); michael@0: return; michael@0: } michael@0: ASSERT(kBigitSize < 32); michael@0: uint64_t carry = 0; michael@0: uint64_t low = factor & 0xFFFFFFFF; michael@0: uint64_t high = factor >> 32; michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: uint64_t product_low = low * bigits_[i]; michael@0: uint64_t product_high = high * bigits_[i]; michael@0: uint64_t tmp = (carry & kBigitMask) + product_low; michael@0: bigits_[i] = tmp & kBigitMask; michael@0: carry = (carry >> kBigitSize) + (tmp >> kBigitSize) + michael@0: (product_high << (32 - kBigitSize)); michael@0: } michael@0: while (carry != 0) { michael@0: EnsureCapacity(used_digits_ + 1); michael@0: bigits_[used_digits_] = carry & kBigitMask; michael@0: used_digits_++; michael@0: carry >>= kBigitSize; michael@0: } michael@0: } michael@0: michael@0: michael@0: void Bignum::MultiplyByPowerOfTen(int exponent) { michael@0: const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d); michael@0: const uint16_t kFive1 = 5; michael@0: const uint16_t kFive2 = kFive1 * 5; michael@0: const uint16_t kFive3 = kFive2 * 5; michael@0: const uint16_t kFive4 = kFive3 * 5; michael@0: const uint16_t kFive5 = kFive4 * 5; michael@0: const uint16_t kFive6 = kFive5 * 5; michael@0: const uint32_t kFive7 = kFive6 * 5; michael@0: const uint32_t kFive8 = kFive7 * 5; michael@0: const uint32_t kFive9 = kFive8 * 5; michael@0: const uint32_t kFive10 = kFive9 * 5; michael@0: const uint32_t kFive11 = kFive10 * 5; michael@0: const uint32_t kFive12 = kFive11 * 5; michael@0: const uint32_t kFive13 = kFive12 * 5; michael@0: const uint32_t kFive1_to_12[] = michael@0: { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6, michael@0: kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 }; michael@0: michael@0: ASSERT(exponent >= 0); michael@0: if (exponent == 0) return; michael@0: if (used_digits_ == 0) return; michael@0: michael@0: // We shift by exponent at the end just before returning. michael@0: int remaining_exponent = exponent; michael@0: while (remaining_exponent >= 27) { michael@0: MultiplyByUInt64(kFive27); michael@0: remaining_exponent -= 27; michael@0: } michael@0: while (remaining_exponent >= 13) { michael@0: MultiplyByUInt32(kFive13); michael@0: remaining_exponent -= 13; michael@0: } michael@0: if (remaining_exponent > 0) { michael@0: MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]); michael@0: } michael@0: ShiftLeft(exponent); michael@0: } michael@0: michael@0: michael@0: void Bignum::Square() { michael@0: ASSERT(IsClamped()); michael@0: int product_length = 2 * used_digits_; michael@0: EnsureCapacity(product_length); michael@0: michael@0: // Comba multiplication: compute each column separately. michael@0: // Example: r = a2a1a0 * b2b1b0. michael@0: // r = 1 * a0b0 + michael@0: // 10 * (a1b0 + a0b1) + michael@0: // 100 * (a2b0 + a1b1 + a0b2) + michael@0: // 1000 * (a2b1 + a1b2) + michael@0: // 10000 * a2b2 michael@0: // michael@0: // In the worst case we have to accumulate nb-digits products of digit*digit. michael@0: // michael@0: // Assert that the additional number of bits in a DoubleChunk are enough to michael@0: // sum up used_digits of Bigit*Bigit. michael@0: if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) { michael@0: UNIMPLEMENTED(); michael@0: } michael@0: DoubleChunk accumulator = 0; michael@0: // First shift the digits so we don't overwrite them. michael@0: int copy_offset = used_digits_; michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: bigits_[copy_offset + i] = bigits_[i]; michael@0: } michael@0: // We have two loops to avoid some 'if's in the loop. michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: // Process temporary digit i with power i. michael@0: // The sum of the two indices must be equal to i. michael@0: int bigit_index1 = i; michael@0: int bigit_index2 = 0; michael@0: // Sum all of the sub-products. michael@0: while (bigit_index1 >= 0) { michael@0: Chunk chunk1 = bigits_[copy_offset + bigit_index1]; michael@0: Chunk chunk2 = bigits_[copy_offset + bigit_index2]; michael@0: accumulator += static_cast(chunk1) * chunk2; michael@0: bigit_index1--; michael@0: bigit_index2++; michael@0: } michael@0: bigits_[i] = static_cast(accumulator) & kBigitMask; michael@0: accumulator >>= kBigitSize; michael@0: } michael@0: for (int i = used_digits_; i < product_length; ++i) { michael@0: int bigit_index1 = used_digits_ - 1; michael@0: int bigit_index2 = i - bigit_index1; michael@0: // Invariant: sum of both indices is again equal to i. michael@0: // Inner loop runs 0 times on last iteration, emptying accumulator. michael@0: while (bigit_index2 < used_digits_) { michael@0: Chunk chunk1 = bigits_[copy_offset + bigit_index1]; michael@0: Chunk chunk2 = bigits_[copy_offset + bigit_index2]; michael@0: accumulator += static_cast(chunk1) * chunk2; michael@0: bigit_index1--; michael@0: bigit_index2++; michael@0: } michael@0: // The overwritten bigits_[i] will never be read in further loop iterations, michael@0: // because bigit_index1 and bigit_index2 are always greater michael@0: // than i - used_digits_. michael@0: bigits_[i] = static_cast(accumulator) & kBigitMask; michael@0: accumulator >>= kBigitSize; michael@0: } michael@0: // Since the result was guaranteed to lie inside the number the michael@0: // accumulator must be 0 now. michael@0: ASSERT(accumulator == 0); michael@0: michael@0: // Don't forget to update the used_digits and the exponent. michael@0: used_digits_ = product_length; michael@0: exponent_ *= 2; michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) { michael@0: ASSERT(base != 0); michael@0: ASSERT(power_exponent >= 0); michael@0: if (power_exponent == 0) { michael@0: AssignUInt16(1); michael@0: return; michael@0: } michael@0: Zero(); michael@0: int shifts = 0; michael@0: // We expect base to be in range 2-32, and most often to be 10. michael@0: // It does not make much sense to implement different algorithms for counting michael@0: // the bits. michael@0: while ((base & 1) == 0) { michael@0: base >>= 1; michael@0: shifts++; michael@0: } michael@0: int bit_size = 0; michael@0: int tmp_base = base; michael@0: while (tmp_base != 0) { michael@0: tmp_base >>= 1; michael@0: bit_size++; michael@0: } michael@0: int final_size = bit_size * power_exponent; michael@0: // 1 extra bigit for the shifting, and one for rounded final_size. michael@0: EnsureCapacity(final_size / kBigitSize + 2); michael@0: michael@0: // Left to Right exponentiation. michael@0: int mask = 1; michael@0: while (power_exponent >= mask) mask <<= 1; michael@0: michael@0: // The mask is now pointing to the bit above the most significant 1-bit of michael@0: // power_exponent. michael@0: // Get rid of first 1-bit; michael@0: mask >>= 2; michael@0: uint64_t this_value = base; michael@0: michael@0: bool delayed_multipliciation = false; michael@0: const uint64_t max_32bits = 0xFFFFFFFF; michael@0: while (mask != 0 && this_value <= max_32bits) { michael@0: this_value = this_value * this_value; michael@0: // Verify that there is enough space in this_value to perform the michael@0: // multiplication. The first bit_size bits must be 0. michael@0: if ((power_exponent & mask) != 0) { michael@0: uint64_t base_bits_mask = michael@0: ~((static_cast(1) << (64 - bit_size)) - 1); michael@0: bool high_bits_zero = (this_value & base_bits_mask) == 0; michael@0: if (high_bits_zero) { michael@0: this_value *= base; michael@0: } else { michael@0: delayed_multipliciation = true; michael@0: } michael@0: } michael@0: mask >>= 1; michael@0: } michael@0: AssignUInt64(this_value); michael@0: if (delayed_multipliciation) { michael@0: MultiplyByUInt32(base); michael@0: } michael@0: michael@0: // Now do the same thing as a bignum. michael@0: while (mask != 0) { michael@0: Square(); michael@0: if ((power_exponent & mask) != 0) { michael@0: MultiplyByUInt32(base); michael@0: } michael@0: mask >>= 1; michael@0: } michael@0: michael@0: // And finally add the saved shifts. michael@0: ShiftLeft(shifts * power_exponent); michael@0: } michael@0: michael@0: michael@0: // Precondition: this/other < 16bit. michael@0: uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) { michael@0: ASSERT(IsClamped()); michael@0: ASSERT(other.IsClamped()); michael@0: ASSERT(other.used_digits_ > 0); michael@0: michael@0: // Easy case: if we have less digits than the divisor than the result is 0. michael@0: // Note: this handles the case where this == 0, too. michael@0: if (BigitLength() < other.BigitLength()) { michael@0: return 0; michael@0: } michael@0: michael@0: Align(other); michael@0: michael@0: uint16_t result = 0; michael@0: michael@0: // Start by removing multiples of 'other' until both numbers have the same michael@0: // number of digits. michael@0: while (BigitLength() > other.BigitLength()) { michael@0: // This naive approach is extremely inefficient if `this` divided by other michael@0: // is big. This function is implemented for doubleToString where michael@0: // the result should be small (less than 10). michael@0: ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16)); michael@0: // Remove the multiples of the first digit. michael@0: // Example this = 23 and other equals 9. -> Remove 2 multiples. michael@0: result += bigits_[used_digits_ - 1]; michael@0: SubtractTimes(other, bigits_[used_digits_ - 1]); michael@0: } michael@0: michael@0: ASSERT(BigitLength() == other.BigitLength()); michael@0: michael@0: // Both bignums are at the same length now. michael@0: // Since other has more than 0 digits we know that the access to michael@0: // bigits_[used_digits_ - 1] is safe. michael@0: Chunk this_bigit = bigits_[used_digits_ - 1]; michael@0: Chunk other_bigit = other.bigits_[other.used_digits_ - 1]; michael@0: michael@0: if (other.used_digits_ == 1) { michael@0: // Shortcut for easy (and common) case. michael@0: int quotient = this_bigit / other_bigit; michael@0: bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient; michael@0: result += quotient; michael@0: Clamp(); michael@0: return result; michael@0: } michael@0: michael@0: int division_estimate = this_bigit / (other_bigit + 1); michael@0: result += division_estimate; michael@0: SubtractTimes(other, division_estimate); michael@0: michael@0: if (other_bigit * (division_estimate + 1) > this_bigit) { michael@0: // No need to even try to subtract. Even if other's remaining digits were 0 michael@0: // another subtraction would be too much. michael@0: return result; michael@0: } michael@0: michael@0: while (LessEqual(other, *this)) { michael@0: SubtractBignum(other); michael@0: result++; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: template michael@0: static int SizeInHexChars(S number) { michael@0: ASSERT(number > 0); michael@0: int result = 0; michael@0: while (number != 0) { michael@0: number >>= 4; michael@0: result++; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: static char HexCharOfValue(int value) { michael@0: ASSERT(0 <= value && value <= 16); michael@0: if (value < 10) return value + '0'; michael@0: return value - 10 + 'A'; michael@0: } michael@0: michael@0: michael@0: bool Bignum::ToHexString(char* buffer, int buffer_size) const { michael@0: ASSERT(IsClamped()); michael@0: // Each bigit must be printable as separate hex-character. michael@0: ASSERT(kBigitSize % 4 == 0); michael@0: const int kHexCharsPerBigit = kBigitSize / 4; michael@0: michael@0: if (used_digits_ == 0) { michael@0: if (buffer_size < 2) return false; michael@0: buffer[0] = '0'; michael@0: buffer[1] = '\0'; michael@0: return true; michael@0: } michael@0: // We add 1 for the terminating '\0' character. michael@0: int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit + michael@0: SizeInHexChars(bigits_[used_digits_ - 1]) + 1; michael@0: if (needed_chars > buffer_size) return false; michael@0: int string_index = needed_chars - 1; michael@0: buffer[string_index--] = '\0'; michael@0: for (int i = 0; i < exponent_; ++i) { michael@0: for (int j = 0; j < kHexCharsPerBigit; ++j) { michael@0: buffer[string_index--] = '0'; michael@0: } michael@0: } michael@0: for (int i = 0; i < used_digits_ - 1; ++i) { michael@0: Chunk current_bigit = bigits_[i]; michael@0: for (int j = 0; j < kHexCharsPerBigit; ++j) { michael@0: buffer[string_index--] = HexCharOfValue(current_bigit & 0xF); michael@0: current_bigit >>= 4; michael@0: } michael@0: } michael@0: // And finally the last bigit. michael@0: Chunk most_significant_bigit = bigits_[used_digits_ - 1]; michael@0: while (most_significant_bigit != 0) { michael@0: buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF); michael@0: most_significant_bigit >>= 4; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: michael@0: Bignum::Chunk Bignum::BigitAt(int index) const { michael@0: if (index >= BigitLength()) return 0; michael@0: if (index < exponent_) return 0; michael@0: return bigits_[index - exponent_]; michael@0: } michael@0: michael@0: michael@0: int Bignum::Compare(const Bignum& a, const Bignum& b) { michael@0: ASSERT(a.IsClamped()); michael@0: ASSERT(b.IsClamped()); michael@0: int bigit_length_a = a.BigitLength(); michael@0: int bigit_length_b = b.BigitLength(); michael@0: if (bigit_length_a < bigit_length_b) return -1; michael@0: if (bigit_length_a > bigit_length_b) return +1; michael@0: for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) { michael@0: Chunk bigit_a = a.BigitAt(i); michael@0: Chunk bigit_b = b.BigitAt(i); michael@0: if (bigit_a < bigit_b) return -1; michael@0: if (bigit_a > bigit_b) return +1; michael@0: // Otherwise they are equal up to this digit. Try the next digit. michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) { michael@0: ASSERT(a.IsClamped()); michael@0: ASSERT(b.IsClamped()); michael@0: ASSERT(c.IsClamped()); michael@0: if (a.BigitLength() < b.BigitLength()) { michael@0: return PlusCompare(b, a, c); michael@0: } michael@0: if (a.BigitLength() + 1 < c.BigitLength()) return -1; michael@0: if (a.BigitLength() > c.BigitLength()) return +1; michael@0: // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than michael@0: // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one michael@0: // of 'a'. michael@0: if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { michael@0: return -1; michael@0: } michael@0: michael@0: Chunk borrow = 0; michael@0: // Starting at min_exponent all digits are == 0. So no need to compare them. michael@0: int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_); michael@0: for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { michael@0: Chunk chunk_a = a.BigitAt(i); michael@0: Chunk chunk_b = b.BigitAt(i); michael@0: Chunk chunk_c = c.BigitAt(i); michael@0: Chunk sum = chunk_a + chunk_b; michael@0: if (sum > chunk_c + borrow) { michael@0: return +1; michael@0: } else { michael@0: borrow = chunk_c + borrow - sum; michael@0: if (borrow > 1) return -1; michael@0: borrow <<= kBigitSize; michael@0: } michael@0: } michael@0: if (borrow == 0) return 0; michael@0: return -1; michael@0: } michael@0: michael@0: michael@0: void Bignum::Clamp() { michael@0: while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) { michael@0: used_digits_--; michael@0: } michael@0: if (used_digits_ == 0) { michael@0: // Zero. michael@0: exponent_ = 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: bool Bignum::IsClamped() const { michael@0: return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0; michael@0: } michael@0: michael@0: michael@0: void Bignum::Zero() { michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: bigits_[i] = 0; michael@0: } michael@0: used_digits_ = 0; michael@0: exponent_ = 0; michael@0: } michael@0: michael@0: michael@0: void Bignum::Align(const Bignum& other) { michael@0: if (exponent_ > other.exponent_) { michael@0: // If "X" represents a "hidden" digit (by the exponent) then we are in the michael@0: // following case (a == this, b == other): michael@0: // a: aaaaaaXXXX or a: aaaaaXXX michael@0: // b: bbbbbbX b: bbbbbbbbXX michael@0: // We replace some of the hidden digits (X) of a with 0 digits. michael@0: // a: aaaaaa000X or a: aaaaa0XX michael@0: int zero_digits = exponent_ - other.exponent_; michael@0: EnsureCapacity(used_digits_ + zero_digits); michael@0: for (int i = used_digits_ - 1; i >= 0; --i) { michael@0: bigits_[i + zero_digits] = bigits_[i]; michael@0: } michael@0: for (int i = 0; i < zero_digits; ++i) { michael@0: bigits_[i] = 0; michael@0: } michael@0: used_digits_ += zero_digits; michael@0: exponent_ -= zero_digits; michael@0: ASSERT(used_digits_ >= 0); michael@0: ASSERT(exponent_ >= 0); michael@0: } michael@0: } michael@0: michael@0: michael@0: void Bignum::BigitsShiftLeft(int shift_amount) { michael@0: ASSERT(shift_amount < kBigitSize); michael@0: ASSERT(shift_amount >= 0); michael@0: Chunk carry = 0; michael@0: for (int i = 0; i < used_digits_; ++i) { michael@0: Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount); michael@0: bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask; michael@0: carry = new_carry; michael@0: } michael@0: if (carry != 0) { michael@0: bigits_[used_digits_] = carry; michael@0: used_digits_++; michael@0: } michael@0: } michael@0: michael@0: michael@0: void Bignum::SubtractTimes(const Bignum& other, int factor) { michael@0: ASSERT(exponent_ <= other.exponent_); michael@0: if (factor < 3) { michael@0: for (int i = 0; i < factor; ++i) { michael@0: SubtractBignum(other); michael@0: } michael@0: return; michael@0: } michael@0: Chunk borrow = 0; michael@0: int exponent_diff = other.exponent_ - exponent_; michael@0: for (int i = 0; i < other.used_digits_; ++i) { michael@0: DoubleChunk product = static_cast(factor) * other.bigits_[i]; michael@0: DoubleChunk remove = borrow + product; michael@0: Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask); michael@0: bigits_[i + exponent_diff] = difference & kBigitMask; michael@0: borrow = static_cast((difference >> (kChunkSize - 1)) + michael@0: (remove >> kBigitSize)); michael@0: } michael@0: for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { michael@0: if (borrow == 0) return; michael@0: Chunk difference = bigits_[i] - borrow; michael@0: bigits_[i] = difference & kBigitMask; michael@0: borrow = difference >> (kChunkSize - 1); michael@0: } michael@0: Clamp(); michael@0: } michael@0: michael@0: michael@0: } // namespace double_conversion