Chromium Code Reviews| 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 #ifndef V8_CONVERSIONS_INL_H_ | 5 #ifndef V8_CONVERSIONS_INL_H_ |
| 6 #define V8_CONVERSIONS_INL_H_ | 6 #define V8_CONVERSIONS_INL_H_ |
| 7 | 7 |
| 8 #include <float.h> // Required for DBL_MAX and on Win32 for finite() | 8 #include <float.h> // Required for DBL_MAX and on Win32 for finite() |
| 9 #include <limits.h> // Required for INT_MAX etc. | 9 #include <limits.h> // Required for INT_MAX etc. |
| 10 #include <stdarg.h> | 10 #include <stdarg.h> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 bool negative = x < 0; | 50 bool negative = x < 0; |
| 51 if (negative) { | 51 if (negative) { |
| 52 x = -x; | 52 x = -x; |
| 53 } | 53 } |
| 54 if (x < k2Pow52) { | 54 if (x < k2Pow52) { |
| 55 x += k2Pow52; | 55 x += k2Pow52; |
| 56 uint32_t result; | 56 uint32_t result; |
| 57 #ifndef V8_TARGET_BIG_ENDIAN | 57 #ifndef V8_TARGET_BIG_ENDIAN |
| 58 Address mantissa_ptr = reinterpret_cast<Address>(&x); | 58 Address mantissa_ptr = reinterpret_cast<Address>(&x); |
| 59 #else | 59 #else |
| 60 Address mantissa_ptr = reinterpret_cast<Address>(&x) + kIntSize; | 60 Address mantissa_ptr = reinterpret_cast<Address>(&x) + kInt32Size; |
| 61 #endif | 61 #endif |
| 62 // Copy least significant 32 bits of mantissa. | 62 // Copy least significant 32 bits of mantissa. |
| 63 memcpy(&result, mantissa_ptr, sizeof(result)); | 63 memcpy(&result, mantissa_ptr, sizeof(result)); |
| 64 return negative ? ~result + 1 : result; | 64 return negative ? ~result + 1 : result; |
| 65 } | 65 } |
| 66 // Large number (outside uint32 range), Infinity or NaN. | 66 // Large number (outside uint32 range), Infinity or NaN. |
| 67 return 0x80000000u; // Return integer indefinite. | 67 return 0x80000000u; // Return integer indefinite. |
| 68 } | 68 } |
| 69 | 69 |
| 70 | 70 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 value == FastI2D(FastD2I(value)); | 116 value == FastI2D(FastD2I(value)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 | 119 |
| 120 bool IsUint32Double(double value) { | 120 bool IsUint32Double(double value) { |
| 121 return !IsMinusZero(value) && value >= 0 && value <= kMaxUInt32 && | 121 return !IsMinusZero(value) && value >= 0 && value <= kMaxUInt32 && |
| 122 value == FastUI2D(FastD2UI(value)); | 122 value == FastUI2D(FastD2UI(value)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 bool DoubleToUint32IfEqualToSelf(double value, uint32_t* uint32_value) { | 125 bool DoubleToUint32IfEqualToSelf(double value, uint32_t* uint32_value) { |
| 126 if (value < 0) return false; | 126 const double k2Pow52 = 4503599627370496.0; |
| 127 // TODO(leszeks): We maybe could be faster than FastD2UI here, since we only | 127 const uint32_t kValidTopBits = 0x43300000; |
| 128 // care about the value being valid if the conversion is valid. | 128 const uint64_t kBottomBitMask = V8_2PART_UINT64_C(0x00000000, FFFFFFFF); |
| 129 uint32_t converted_value = FastD2UI(value); | 129 |
| 130 if (FastUI2D(converted_value) == value) { | 130 // Add 2^52 to the double, to place valid uint32 values in the low-significant |
| 131 *uint32_value = converted_value; | 131 // bits of the exponent, by effectively setting the (implicit) top bit of the |
| 132 return true; | 132 // significand. Note that this addition also normalises 0.0 and -0.0. |
| 133 double shifted_value = value + k2Pow52; | |
| 134 | |
| 135 // At this point, a valid uint32 valued double will be represented as: | |
| 136 // | |
| 137 // sign = 0 | |
| 138 // exponent = 52 | |
| 139 // significand = 1. 00...00 <value> | |
| 140 // implicit^ ^^^^^^^ 32 bits | |
| 141 // ^^^^^^^^^^^^^^^ 52 bits | |
| 142 // | |
| 143 // Therefore, we can first check the top 32 bits to make sure that the sign, | |
| 144 // exponent and remaining significand bits are valid, and only then check the | |
| 145 // value in the bottom 32 bits. | |
| 146 | |
| 147 uint64_t result = bit_cast<uint64_t>(shifted_value); | |
| 148 if ((result >> 32) == kValidTopBits) { | |
| 149 *uint32_value = result & kBottomBitMask; | |
| 150 return (result & kBottomBitMask) == value; | |
|
rmcilroy
2017/01/17 15:46:07
nit - please do:
return FastUI2D(*uint32_value) =
Leszek Swirski
2017/01/17 16:06:45
Changed to FastUI2D, but still using "result & kBo
| |
| 133 } | 151 } |
| 134 return false; | 152 return false; |
| 135 } | 153 } |
| 136 | 154 |
| 137 int32_t NumberToInt32(Object* number) { | 155 int32_t NumberToInt32(Object* number) { |
| 138 if (number->IsSmi()) return Smi::cast(number)->value(); | 156 if (number->IsSmi()) return Smi::cast(number)->value(); |
| 139 return DoubleToInt32(number->Number()); | 157 return DoubleToInt32(number->Number()); |
| 140 } | 158 } |
| 141 | 159 |
| 142 uint32_t NumberToUint32(Object* number) { | 160 uint32_t NumberToUint32(Object* number) { |
| (...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 795 buffer[buffer_pos] = '\0'; | 813 buffer[buffer_pos] = '\0'; |
| 796 | 814 |
| 797 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); | 815 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); |
| 798 return (sign == NEGATIVE) ? -converted : converted; | 816 return (sign == NEGATIVE) ? -converted : converted; |
| 799 } | 817 } |
| 800 | 818 |
| 801 } // namespace internal | 819 } // namespace internal |
| 802 } // namespace v8 | 820 } // namespace v8 |
| 803 | 821 |
| 804 #endif // V8_CONVERSIONS_INL_H_ | 822 #endif // V8_CONVERSIONS_INL_H_ |
| OLD | NEW |