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 michael@0: michael@0: #include "fixed-dtoa.h" michael@0: #include "ieee.h" michael@0: michael@0: namespace double_conversion { michael@0: michael@0: // Represents a 128bit type. This class should be replaced by a native type on michael@0: // platforms that support 128bit integers. michael@0: class UInt128 { michael@0: public: michael@0: UInt128() : high_bits_(0), low_bits_(0) { } michael@0: UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { } michael@0: michael@0: void Multiply(uint32_t multiplicand) { michael@0: uint64_t accumulator; michael@0: michael@0: accumulator = (low_bits_ & kMask32) * multiplicand; michael@0: uint32_t part = static_cast(accumulator & kMask32); michael@0: accumulator >>= 32; michael@0: accumulator = accumulator + (low_bits_ >> 32) * multiplicand; michael@0: low_bits_ = (accumulator << 32) + part; michael@0: accumulator >>= 32; michael@0: accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; michael@0: part = static_cast(accumulator & kMask32); michael@0: accumulator >>= 32; michael@0: accumulator = accumulator + (high_bits_ >> 32) * multiplicand; michael@0: high_bits_ = (accumulator << 32) + part; michael@0: ASSERT((accumulator >> 32) == 0); michael@0: } michael@0: michael@0: void Shift(int shift_amount) { michael@0: ASSERT(-64 <= shift_amount && shift_amount <= 64); michael@0: if (shift_amount == 0) { michael@0: return; michael@0: } else if (shift_amount == -64) { michael@0: high_bits_ = low_bits_; michael@0: low_bits_ = 0; michael@0: } else if (shift_amount == 64) { michael@0: low_bits_ = high_bits_; michael@0: high_bits_ = 0; michael@0: } else if (shift_amount <= 0) { michael@0: high_bits_ <<= -shift_amount; michael@0: high_bits_ += low_bits_ >> (64 + shift_amount); michael@0: low_bits_ <<= -shift_amount; michael@0: } else { michael@0: low_bits_ >>= shift_amount; michael@0: low_bits_ += high_bits_ << (64 - shift_amount); michael@0: high_bits_ >>= shift_amount; michael@0: } michael@0: } michael@0: michael@0: // Modifies *this to *this MOD (2^power). michael@0: // Returns *this DIV (2^power). michael@0: int DivModPowerOf2(int power) { michael@0: if (power >= 64) { michael@0: int result = static_cast(high_bits_ >> (power - 64)); michael@0: high_bits_ -= static_cast(result) << (power - 64); michael@0: return result; michael@0: } else { michael@0: uint64_t part_low = low_bits_ >> power; michael@0: uint64_t part_high = high_bits_ << (64 - power); michael@0: int result = static_cast(part_low + part_high); michael@0: high_bits_ = 0; michael@0: low_bits_ -= part_low << power; michael@0: return result; michael@0: } michael@0: } michael@0: michael@0: bool IsZero() const { michael@0: return high_bits_ == 0 && low_bits_ == 0; michael@0: } michael@0: michael@0: int BitAt(int position) { michael@0: if (position >= 64) { michael@0: return static_cast(high_bits_ >> (position - 64)) & 1; michael@0: } else { michael@0: return static_cast(low_bits_ >> position) & 1; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: static const uint64_t kMask32 = 0xFFFFFFFF; michael@0: // Value == (high_bits_ << 64) + low_bits_ michael@0: uint64_t high_bits_; michael@0: uint64_t low_bits_; michael@0: }; michael@0: michael@0: michael@0: static const int kDoubleSignificandSize = 53; // Includes the hidden bit. michael@0: michael@0: michael@0: static void FillDigits32FixedLength(uint32_t number, int requested_length, michael@0: Vector buffer, int* length) { michael@0: for (int i = requested_length - 1; i >= 0; --i) { michael@0: buffer[(*length) + i] = '0' + number % 10; michael@0: number /= 10; michael@0: } michael@0: *length += requested_length; michael@0: } michael@0: michael@0: michael@0: static void FillDigits32(uint32_t number, Vector buffer, int* length) { michael@0: int number_length = 0; michael@0: // We fill the digits in reverse order and exchange them afterwards. michael@0: while (number != 0) { michael@0: int digit = number % 10; michael@0: number /= 10; michael@0: buffer[(*length) + number_length] = '0' + digit; michael@0: number_length++; michael@0: } michael@0: // Exchange the digits. michael@0: int i = *length; michael@0: int j = *length + number_length - 1; michael@0: while (i < j) { michael@0: char tmp = buffer[i]; michael@0: buffer[i] = buffer[j]; michael@0: buffer[j] = tmp; michael@0: i++; michael@0: j--; michael@0: } michael@0: *length += number_length; michael@0: } michael@0: michael@0: michael@0: static void FillDigits64FixedLength(uint64_t number, int requested_length, michael@0: Vector buffer, int* length) { michael@0: const uint32_t kTen7 = 10000000; michael@0: // For efficiency cut the number into 3 uint32_t parts, and print those. michael@0: uint32_t part2 = static_cast(number % kTen7); michael@0: number /= kTen7; michael@0: uint32_t part1 = static_cast(number % kTen7); michael@0: uint32_t part0 = static_cast(number / kTen7); michael@0: michael@0: FillDigits32FixedLength(part0, 3, buffer, length); michael@0: FillDigits32FixedLength(part1, 7, buffer, length); michael@0: FillDigits32FixedLength(part2, 7, buffer, length); michael@0: } michael@0: michael@0: michael@0: static void FillDigits64(uint64_t number, Vector buffer, int* length) { michael@0: const uint32_t kTen7 = 10000000; michael@0: // For efficiency cut the number into 3 uint32_t parts, and print those. michael@0: uint32_t part2 = static_cast(number % kTen7); michael@0: number /= kTen7; michael@0: uint32_t part1 = static_cast(number % kTen7); michael@0: uint32_t part0 = static_cast(number / kTen7); michael@0: michael@0: if (part0 != 0) { michael@0: FillDigits32(part0, buffer, length); michael@0: FillDigits32FixedLength(part1, 7, buffer, length); michael@0: FillDigits32FixedLength(part2, 7, buffer, length); michael@0: } else if (part1 != 0) { michael@0: FillDigits32(part1, buffer, length); michael@0: FillDigits32FixedLength(part2, 7, buffer, length); michael@0: } else { michael@0: FillDigits32(part2, buffer, length); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void RoundUp(Vector buffer, int* length, int* decimal_point) { michael@0: // An empty buffer represents 0. michael@0: if (*length == 0) { michael@0: buffer[0] = '1'; michael@0: *decimal_point = 1; michael@0: *length = 1; michael@0: return; michael@0: } michael@0: // Round the last digit until we either have a digit that was not '9' or until michael@0: // we reached the first digit. michael@0: buffer[(*length) - 1]++; michael@0: for (int i = (*length) - 1; i > 0; --i) { michael@0: if (buffer[i] != '0' + 10) { michael@0: return; michael@0: } michael@0: buffer[i] = '0'; michael@0: buffer[i - 1]++; michael@0: } michael@0: // If the first digit is now '0' + 10, we would need to set it to '0' and add michael@0: // a '1' in front. However we reach the first digit only if all following michael@0: // digits had been '9' before rounding up. Now all trailing digits are '0' and michael@0: // we simply switch the first digit to '1' and update the decimal-point michael@0: // (indicating that the point is now one digit to the right). michael@0: if (buffer[0] == '0' + 10) { michael@0: buffer[0] = '1'; michael@0: (*decimal_point)++; michael@0: } michael@0: } michael@0: michael@0: michael@0: // The given fractionals number represents a fixed-point number with binary michael@0: // point at bit (-exponent). michael@0: // Preconditions: michael@0: // -128 <= exponent <= 0. michael@0: // 0 <= fractionals * 2^exponent < 1 michael@0: // The buffer holds the result. michael@0: // The function will round its result. During the rounding-process digits not michael@0: // generated by this function might be updated, and the decimal-point variable michael@0: // might be updated. If this function generates the digits 99 and the buffer michael@0: // already contained "199" (thus yielding a buffer of "19999") then a michael@0: // rounding-up will change the contents of the buffer to "20000". michael@0: static void FillFractionals(uint64_t fractionals, int exponent, michael@0: int fractional_count, Vector buffer, michael@0: int* length, int* decimal_point) { michael@0: ASSERT(-128 <= exponent && exponent <= 0); michael@0: // 'fractionals' is a fixed-point number, with binary point at bit michael@0: // (-exponent). Inside the function the non-converted remainder of fractionals michael@0: // is a fixed-point number, with binary point at bit 'point'. michael@0: if (-exponent <= 64) { michael@0: // One 64 bit number is sufficient. michael@0: ASSERT(fractionals >> 56 == 0); michael@0: int point = -exponent; michael@0: for (int i = 0; i < fractional_count; ++i) { michael@0: if (fractionals == 0) break; michael@0: // Instead of multiplying by 10 we multiply by 5 and adjust the point michael@0: // location. This way the fractionals variable will not overflow. michael@0: // Invariant at the beginning of the loop: fractionals < 2^point. michael@0: // Initially we have: point <= 64 and fractionals < 2^56 michael@0: // After each iteration the point is decremented by one. michael@0: // Note that 5^3 = 125 < 128 = 2^7. michael@0: // Therefore three iterations of this loop will not overflow fractionals michael@0: // (even without the subtraction at the end of the loop body). At this michael@0: // time point will satisfy point <= 61 and therefore fractionals < 2^point michael@0: // and any further multiplication of fractionals by 5 will not overflow. michael@0: fractionals *= 5; michael@0: point--; michael@0: int digit = static_cast(fractionals >> point); michael@0: buffer[*length] = '0' + digit; michael@0: (*length)++; michael@0: fractionals -= static_cast(digit) << point; michael@0: } michael@0: // If the first bit after the point is set we have to round up. michael@0: if (((fractionals >> (point - 1)) & 1) == 1) { michael@0: RoundUp(buffer, length, decimal_point); michael@0: } michael@0: } else { // We need 128 bits. michael@0: ASSERT(64 < -exponent && -exponent <= 128); michael@0: UInt128 fractionals128 = UInt128(fractionals, 0); michael@0: fractionals128.Shift(-exponent - 64); michael@0: int point = 128; michael@0: for (int i = 0; i < fractional_count; ++i) { michael@0: if (fractionals128.IsZero()) break; michael@0: // As before: instead of multiplying by 10 we multiply by 5 and adjust the michael@0: // point location. michael@0: // This multiplication will not overflow for the same reasons as before. michael@0: fractionals128.Multiply(5); michael@0: point--; michael@0: int digit = fractionals128.DivModPowerOf2(point); michael@0: buffer[*length] = '0' + digit; michael@0: (*length)++; michael@0: } michael@0: if (fractionals128.BitAt(point - 1) == 1) { michael@0: RoundUp(buffer, length, decimal_point); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: // Removes leading and trailing zeros. michael@0: // If leading zeros are removed then the decimal point position is adjusted. michael@0: static void TrimZeros(Vector buffer, int* length, int* decimal_point) { michael@0: while (*length > 0 && buffer[(*length) - 1] == '0') { michael@0: (*length)--; michael@0: } michael@0: int first_non_zero = 0; michael@0: while (first_non_zero < *length && buffer[first_non_zero] == '0') { michael@0: first_non_zero++; michael@0: } michael@0: if (first_non_zero != 0) { michael@0: for (int i = first_non_zero; i < *length; ++i) { michael@0: buffer[i - first_non_zero] = buffer[i]; michael@0: } michael@0: *length -= first_non_zero; michael@0: *decimal_point -= first_non_zero; michael@0: } michael@0: } michael@0: michael@0: michael@0: bool FastFixedDtoa(double v, michael@0: int fractional_count, michael@0: Vector buffer, michael@0: int* length, michael@0: int* decimal_point) { michael@0: const uint32_t kMaxUInt32 = 0xFFFFFFFF; michael@0: uint64_t significand = Double(v).Significand(); michael@0: int exponent = Double(v).Exponent(); michael@0: // v = significand * 2^exponent (with significand a 53bit integer). michael@0: // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we michael@0: // don't know how to compute the representation. 2^73 ~= 9.5*10^21. michael@0: // If necessary this limit could probably be increased, but we don't need michael@0: // more. michael@0: if (exponent > 20) return false; michael@0: if (fractional_count > 20) return false; michael@0: *length = 0; michael@0: // At most kDoubleSignificandSize bits of the significand are non-zero. michael@0: // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero michael@0: // bits: 0..11*..0xxx..53*..xx michael@0: if (exponent + kDoubleSignificandSize > 64) { michael@0: // The exponent must be > 11. michael@0: // michael@0: // We know that v = significand * 2^exponent. michael@0: // And the exponent > 11. michael@0: // We simplify the task by dividing v by 10^17. michael@0: // The quotient delivers the first digits, and the remainder fits into a 64 michael@0: // bit number. michael@0: // Dividing by 10^17 is equivalent to dividing by 5^17*2^17. michael@0: const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17 michael@0: uint64_t divisor = kFive17; michael@0: int divisor_power = 17; michael@0: uint64_t dividend = significand; michael@0: uint32_t quotient; michael@0: uint64_t remainder; michael@0: // Let v = f * 2^e with f == significand and e == exponent. michael@0: // Then need q (quotient) and r (remainder) as follows: michael@0: // v = q * 10^17 + r michael@0: // f * 2^e = q * 10^17 + r michael@0: // f * 2^e = q * 5^17 * 2^17 + r michael@0: // If e > 17 then michael@0: // f * 2^(e-17) = q * 5^17 + r/2^17 michael@0: // else michael@0: // f = q * 5^17 * 2^(17-e) + r/2^e michael@0: if (exponent > divisor_power) { michael@0: // We only allow exponents of up to 20 and therefore (17 - e) <= 3 michael@0: dividend <<= exponent - divisor_power; michael@0: quotient = static_cast(dividend / divisor); michael@0: remainder = (dividend % divisor) << divisor_power; michael@0: } else { michael@0: divisor <<= divisor_power - exponent; michael@0: quotient = static_cast(dividend / divisor); michael@0: remainder = (dividend % divisor) << exponent; michael@0: } michael@0: FillDigits32(quotient, buffer, length); michael@0: FillDigits64FixedLength(remainder, divisor_power, buffer, length); michael@0: *decimal_point = *length; michael@0: } else if (exponent >= 0) { michael@0: // 0 <= exponent <= 11 michael@0: significand <<= exponent; michael@0: FillDigits64(significand, buffer, length); michael@0: *decimal_point = *length; michael@0: } else if (exponent > -kDoubleSignificandSize) { michael@0: // We have to cut the number. michael@0: uint64_t integrals = significand >> -exponent; michael@0: uint64_t fractionals = significand - (integrals << -exponent); michael@0: if (integrals > kMaxUInt32) { michael@0: FillDigits64(integrals, buffer, length); michael@0: } else { michael@0: FillDigits32(static_cast(integrals), buffer, length); michael@0: } michael@0: *decimal_point = *length; michael@0: FillFractionals(fractionals, exponent, fractional_count, michael@0: buffer, length, decimal_point); michael@0: } else if (exponent < -128) { michael@0: // This configuration (with at most 20 digits) means that all digits must be michael@0: // 0. michael@0: ASSERT(fractional_count <= 20); michael@0: buffer[0] = '\0'; michael@0: *length = 0; michael@0: *decimal_point = -fractional_count; michael@0: } else { michael@0: *decimal_point = 0; michael@0: FillFractionals(significand, exponent, fractional_count, michael@0: buffer, length, decimal_point); michael@0: } michael@0: TrimZeros(buffer, length, decimal_point); michael@0: buffer[*length] = '\0'; michael@0: if ((*length) == 0) { michael@0: // The string is empty and the decimal_point thus has no importance. Mimick michael@0: // Gay's dtoa and and set it to -fractional_count. michael@0: *decimal_point = -fractional_count; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: } // namespace double_conversion