| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #include "include/v8stdint.h" | 7 #include "include/v8stdint.h" |
| 8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
| 9 #include "src/utils.h" | 9 #include "src/utils.h" |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 uint32_t part = static_cast<uint32_t>(accumulator & kMask32); | 28 uint32_t part = static_cast<uint32_t>(accumulator & kMask32); |
| 29 accumulator >>= 32; | 29 accumulator >>= 32; |
| 30 accumulator = accumulator + (low_bits_ >> 32) * multiplicand; | 30 accumulator = accumulator + (low_bits_ >> 32) * multiplicand; |
| 31 low_bits_ = (accumulator << 32) + part; | 31 low_bits_ = (accumulator << 32) + part; |
| 32 accumulator >>= 32; | 32 accumulator >>= 32; |
| 33 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; | 33 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; |
| 34 part = static_cast<uint32_t>(accumulator & kMask32); | 34 part = static_cast<uint32_t>(accumulator & kMask32); |
| 35 accumulator >>= 32; | 35 accumulator >>= 32; |
| 36 accumulator = accumulator + (high_bits_ >> 32) * multiplicand; | 36 accumulator = accumulator + (high_bits_ >> 32) * multiplicand; |
| 37 high_bits_ = (accumulator << 32) + part; | 37 high_bits_ = (accumulator << 32) + part; |
| 38 ASSERT((accumulator >> 32) == 0); | 38 DCHECK((accumulator >> 32) == 0); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void Shift(int shift_amount) { | 41 void Shift(int shift_amount) { |
| 42 ASSERT(-64 <= shift_amount && shift_amount <= 64); | 42 DCHECK(-64 <= shift_amount && shift_amount <= 64); |
| 43 if (shift_amount == 0) { | 43 if (shift_amount == 0) { |
| 44 return; | 44 return; |
| 45 } else if (shift_amount == -64) { | 45 } else if (shift_amount == -64) { |
| 46 high_bits_ = low_bits_; | 46 high_bits_ = low_bits_; |
| 47 low_bits_ = 0; | 47 low_bits_ = 0; |
| 48 } else if (shift_amount == 64) { | 48 } else if (shift_amount == 64) { |
| 49 low_bits_ = high_bits_; | 49 low_bits_ = high_bits_; |
| 50 high_bits_ = 0; | 50 high_bits_ = 0; |
| 51 } else if (shift_amount <= 0) { | 51 } else if (shift_amount <= 0) { |
| 52 high_bits_ <<= -shift_amount; | 52 high_bits_ <<= -shift_amount; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // 0 <= fractionals * 2^exponent < 1 | 205 // 0 <= fractionals * 2^exponent < 1 |
| 206 // The buffer holds the result. | 206 // The buffer holds the result. |
| 207 // The function will round its result. During the rounding-process digits not | 207 // The function will round its result. During the rounding-process digits not |
| 208 // generated by this function might be updated, and the decimal-point variable | 208 // generated by this function might be updated, and the decimal-point variable |
| 209 // might be updated. If this function generates the digits 99 and the buffer | 209 // might be updated. If this function generates the digits 99 and the buffer |
| 210 // already contained "199" (thus yielding a buffer of "19999") then a | 210 // already contained "199" (thus yielding a buffer of "19999") then a |
| 211 // rounding-up will change the contents of the buffer to "20000". | 211 // rounding-up will change the contents of the buffer to "20000". |
| 212 static void FillFractionals(uint64_t fractionals, int exponent, | 212 static void FillFractionals(uint64_t fractionals, int exponent, |
| 213 int fractional_count, Vector<char> buffer, | 213 int fractional_count, Vector<char> buffer, |
| 214 int* length, int* decimal_point) { | 214 int* length, int* decimal_point) { |
| 215 ASSERT(-128 <= exponent && exponent <= 0); | 215 DCHECK(-128 <= exponent && exponent <= 0); |
| 216 // 'fractionals' is a fixed-point number, with binary point at bit | 216 // 'fractionals' is a fixed-point number, with binary point at bit |
| 217 // (-exponent). Inside the function the non-converted remainder of fractionals | 217 // (-exponent). Inside the function the non-converted remainder of fractionals |
| 218 // is a fixed-point number, with binary point at bit 'point'. | 218 // is a fixed-point number, with binary point at bit 'point'. |
| 219 if (-exponent <= 64) { | 219 if (-exponent <= 64) { |
| 220 // One 64 bit number is sufficient. | 220 // One 64 bit number is sufficient. |
| 221 ASSERT(fractionals >> 56 == 0); | 221 DCHECK(fractionals >> 56 == 0); |
| 222 int point = -exponent; | 222 int point = -exponent; |
| 223 for (int i = 0; i < fractional_count; ++i) { | 223 for (int i = 0; i < fractional_count; ++i) { |
| 224 if (fractionals == 0) break; | 224 if (fractionals == 0) break; |
| 225 // Instead of multiplying by 10 we multiply by 5 and adjust the point | 225 // Instead of multiplying by 10 we multiply by 5 and adjust the point |
| 226 // location. This way the fractionals variable will not overflow. | 226 // location. This way the fractionals variable will not overflow. |
| 227 // Invariant at the beginning of the loop: fractionals < 2^point. | 227 // Invariant at the beginning of the loop: fractionals < 2^point. |
| 228 // Initially we have: point <= 64 and fractionals < 2^56 | 228 // Initially we have: point <= 64 and fractionals < 2^56 |
| 229 // After each iteration the point is decremented by one. | 229 // After each iteration the point is decremented by one. |
| 230 // Note that 5^3 = 125 < 128 = 2^7. | 230 // Note that 5^3 = 125 < 128 = 2^7. |
| 231 // Therefore three iterations of this loop will not overflow fractionals | 231 // Therefore three iterations of this loop will not overflow fractionals |
| 232 // (even without the subtraction at the end of the loop body). At this | 232 // (even without the subtraction at the end of the loop body). At this |
| 233 // time point will satisfy point <= 61 and therefore fractionals < 2^point | 233 // time point will satisfy point <= 61 and therefore fractionals < 2^point |
| 234 // and any further multiplication of fractionals by 5 will not overflow. | 234 // and any further multiplication of fractionals by 5 will not overflow. |
| 235 fractionals *= 5; | 235 fractionals *= 5; |
| 236 point--; | 236 point--; |
| 237 int digit = static_cast<int>(fractionals >> point); | 237 int digit = static_cast<int>(fractionals >> point); |
| 238 buffer[*length] = '0' + digit; | 238 buffer[*length] = '0' + digit; |
| 239 (*length)++; | 239 (*length)++; |
| 240 fractionals -= static_cast<uint64_t>(digit) << point; | 240 fractionals -= static_cast<uint64_t>(digit) << point; |
| 241 } | 241 } |
| 242 // If the first bit after the point is set we have to round up. | 242 // If the first bit after the point is set we have to round up. |
| 243 if (((fractionals >> (point - 1)) & 1) == 1) { | 243 if (((fractionals >> (point - 1)) & 1) == 1) { |
| 244 RoundUp(buffer, length, decimal_point); | 244 RoundUp(buffer, length, decimal_point); |
| 245 } | 245 } |
| 246 } else { // We need 128 bits. | 246 } else { // We need 128 bits. |
| 247 ASSERT(64 < -exponent && -exponent <= 128); | 247 DCHECK(64 < -exponent && -exponent <= 128); |
| 248 UInt128 fractionals128 = UInt128(fractionals, 0); | 248 UInt128 fractionals128 = UInt128(fractionals, 0); |
| 249 fractionals128.Shift(-exponent - 64); | 249 fractionals128.Shift(-exponent - 64); |
| 250 int point = 128; | 250 int point = 128; |
| 251 for (int i = 0; i < fractional_count; ++i) { | 251 for (int i = 0; i < fractional_count; ++i) { |
| 252 if (fractionals128.IsZero()) break; | 252 if (fractionals128.IsZero()) break; |
| 253 // As before: instead of multiplying by 10 we multiply by 5 and adjust the | 253 // As before: instead of multiplying by 10 we multiply by 5 and adjust the |
| 254 // point location. | 254 // point location. |
| 255 // This multiplication will not overflow for the same reasons as before. | 255 // This multiplication will not overflow for the same reasons as before. |
| 256 fractionals128.Multiply(5); | 256 fractionals128.Multiply(5); |
| 257 point--; | 257 point--; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 FillDigits64(integrals, buffer, length); | 355 FillDigits64(integrals, buffer, length); |
| 356 } else { | 356 } else { |
| 357 FillDigits32(static_cast<uint32_t>(integrals), buffer, length); | 357 FillDigits32(static_cast<uint32_t>(integrals), buffer, length); |
| 358 } | 358 } |
| 359 *decimal_point = *length; | 359 *decimal_point = *length; |
| 360 FillFractionals(fractionals, exponent, fractional_count, | 360 FillFractionals(fractionals, exponent, fractional_count, |
| 361 buffer, length, decimal_point); | 361 buffer, length, decimal_point); |
| 362 } else if (exponent < -128) { | 362 } else if (exponent < -128) { |
| 363 // This configuration (with at most 20 digits) means that all digits must be | 363 // This configuration (with at most 20 digits) means that all digits must be |
| 364 // 0. | 364 // 0. |
| 365 ASSERT(fractional_count <= 20); | 365 DCHECK(fractional_count <= 20); |
| 366 buffer[0] = '\0'; | 366 buffer[0] = '\0'; |
| 367 *length = 0; | 367 *length = 0; |
| 368 *decimal_point = -fractional_count; | 368 *decimal_point = -fractional_count; |
| 369 } else { | 369 } else { |
| 370 *decimal_point = 0; | 370 *decimal_point = 0; |
| 371 FillFractionals(significand, exponent, fractional_count, | 371 FillFractionals(significand, exponent, fractional_count, |
| 372 buffer, length, decimal_point); | 372 buffer, length, decimal_point); |
| 373 } | 373 } |
| 374 TrimZeros(buffer, length, decimal_point); | 374 TrimZeros(buffer, length, decimal_point); |
| 375 buffer[*length] = '\0'; | 375 buffer[*length] = '\0'; |
| 376 if ((*length) == 0) { | 376 if ((*length) == 0) { |
| 377 // The string is empty and the decimal_point thus has no importance. Mimick | 377 // The string is empty and the decimal_point thus has no importance. Mimick |
| 378 // Gay's dtoa and and set it to -fractional_count. | 378 // Gay's dtoa and and set it to -fractional_count. |
| 379 *decimal_point = -fractional_count; | 379 *decimal_point = -fractional_count; |
| 380 } | 380 } |
| 381 return true; | 381 return true; |
| 382 } | 382 } |
| 383 | 383 |
| 384 } } // namespace v8::internal | 384 } } // namespace v8::internal |
| OLD | NEW |