| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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_ARM64_UTILS_ARM64_H_ | 5 #ifndef V8_ARM64_UTILS_ARM64_H_ |
| 6 #define V8_ARM64_UTILS_ARM64_H_ | 6 #define V8_ARM64_UTILS_ARM64_H_ |
| 7 | 7 |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "src/arm64/constants-arm64.h" | 10 #include "src/arm64/constants-arm64.h" |
| 11 #include "src/utils.h" |
| 11 | 12 |
| 12 namespace v8 { | 13 namespace v8 { |
| 13 namespace internal { | 14 namespace internal { |
| 14 | 15 |
| 15 // These are global assumptions in v8. | 16 // These are global assumptions in v8. |
| 16 STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1); | 17 STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1); |
| 17 STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF); | 18 STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF); |
| 18 | 19 |
| 19 // Floating point representation. | 20 uint32_t float_sign(float val); |
| 20 static inline uint32_t float_to_rawbits(float value) { | 21 uint32_t float_exp(float val); |
| 21 uint32_t bits = 0; | 22 uint32_t float_mantissa(float val); |
| 22 memcpy(&bits, &value, 4); | 23 uint32_t double_sign(double val); |
| 23 return bits; | 24 uint32_t double_exp(double val); |
| 24 } | 25 uint64_t double_mantissa(double val); |
| 25 | 26 |
| 27 float float_pack(uint32_t sign, uint32_t exp, uint32_t mantissa); |
| 28 double double_pack(uint64_t sign, uint64_t exp, uint64_t mantissa); |
| 26 | 29 |
| 27 static inline uint64_t double_to_rawbits(double value) { | 30 // An fpclassify() function for 16-bit half-precision floats. |
| 28 uint64_t bits = 0; | 31 int float16classify(float16 value); |
| 29 memcpy(&bits, &value, 8); | |
| 30 return bits; | |
| 31 } | |
| 32 | |
| 33 | |
| 34 static inline float rawbits_to_float(uint32_t bits) { | |
| 35 float value = 0.0; | |
| 36 memcpy(&value, &bits, 4); | |
| 37 return value; | |
| 38 } | |
| 39 | |
| 40 | |
| 41 static inline double rawbits_to_double(uint64_t bits) { | |
| 42 double value = 0.0; | |
| 43 memcpy(&value, &bits, 8); | |
| 44 return value; | |
| 45 } | |
| 46 | |
| 47 | 32 |
| 48 // Bit counting. | 33 // Bit counting. |
| 49 int CountLeadingZeros(uint64_t value, int width); | 34 int CountLeadingZeros(uint64_t value, int width); |
| 50 int CountLeadingSignBits(int64_t value, int width); | 35 int CountLeadingSignBits(int64_t value, int width); |
| 51 int CountTrailingZeros(uint64_t value, int width); | 36 int CountTrailingZeros(uint64_t value, int width); |
| 52 int CountSetBits(uint64_t value, int width); | 37 int CountSetBits(uint64_t value, int width); |
| 38 int LowestSetBitPosition(uint64_t value); |
| 39 int HighestSetBitPosition(uint64_t value); |
| 53 uint64_t LargestPowerOf2Divisor(uint64_t value); | 40 uint64_t LargestPowerOf2Divisor(uint64_t value); |
| 54 int MaskToBit(uint64_t mask); | 41 int MaskToBit(uint64_t mask); |
| 55 | 42 |
| 56 | 43 |
| 57 template <typename T> | 44 template <typename T> |
| 58 T ReverseBytes(T value, int block_bytes_log2) { | 45 T ReverseBytes(T value, int block_bytes_log2) { |
| 59 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8)); | 46 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8)); |
| 60 DCHECK((1U << block_bytes_log2) <= sizeof(value)); | 47 DCHECK((1U << block_bytes_log2) <= sizeof(value)); |
| 61 // Split the 64-bit value into an 8-bit array, where b[0] is the least | 48 // Split the 64-bit value into an 8-bit array, where b[0] is the least |
| 62 // significant byte, and b[7] is the most significant. | 49 // significant byte, and b[7] is the most significant. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 79 for (int i = 0; i < 8; i++) { | 66 for (int i = 0; i < 8; i++) { |
| 80 result <<= 8; | 67 result <<= 8; |
| 81 result |= bytes[permute_table[block_bytes_log2 - 1][i]]; | 68 result |= bytes[permute_table[block_bytes_log2 - 1][i]]; |
| 82 } | 69 } |
| 83 return result; | 70 return result; |
| 84 } | 71 } |
| 85 | 72 |
| 86 | 73 |
| 87 // NaN tests. | 74 // NaN tests. |
| 88 inline bool IsSignallingNaN(double num) { | 75 inline bool IsSignallingNaN(double num) { |
| 89 uint64_t raw = double_to_rawbits(num); | 76 uint64_t raw = bit_cast<uint64_t>(num); |
| 90 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) { | 77 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) { |
| 91 return true; | 78 return true; |
| 92 } | 79 } |
| 93 return false; | 80 return false; |
| 94 } | 81 } |
| 95 | 82 |
| 96 | 83 |
| 97 inline bool IsSignallingNaN(float num) { | 84 inline bool IsSignallingNaN(float num) { |
| 98 uint32_t raw = float_to_rawbits(num); | 85 uint32_t raw = bit_cast<uint32_t>(num); |
| 99 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) { | 86 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) { |
| 100 return true; | 87 return true; |
| 101 } | 88 } |
| 102 return false; | 89 return false; |
| 103 } | 90 } |
| 104 | 91 |
| 92 inline bool IsSignallingNaN(float16 num) { |
| 93 const uint16_t kFP16QuietNaNMask = 0x0200; |
| 94 return (float16classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0); |
| 95 } |
| 105 | 96 |
| 106 template <typename T> | 97 template <typename T> |
| 107 inline bool IsQuietNaN(T num) { | 98 inline bool IsQuietNaN(T num) { |
| 108 return std::isnan(num) && !IsSignallingNaN(num); | 99 return std::isnan(num) && !IsSignallingNaN(num); |
| 109 } | 100 } |
| 110 | 101 |
| 111 | 102 |
| 112 // Convert the NaN in 'num' to a quiet NaN. | 103 // Convert the NaN in 'num' to a quiet NaN. |
| 113 inline double ToQuietNaN(double num) { | 104 inline double ToQuietNaN(double num) { |
| 114 DCHECK(std::isnan(num)); | 105 DCHECK(std::isnan(num)); |
| 115 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask); | 106 return bit_cast<double>(bit_cast<uint64_t>(num) | kDQuietNanMask); |
| 116 } | 107 } |
| 117 | 108 |
| 118 | 109 |
| 119 inline float ToQuietNaN(float num) { | 110 inline float ToQuietNaN(float num) { |
| 120 DCHECK(std::isnan(num)); | 111 DCHECK(std::isnan(num)); |
| 121 return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask); | 112 return bit_cast<float>(bit_cast<uint32_t>(num) | |
| 113 static_cast<uint32_t>(kSQuietNanMask)); |
| 122 } | 114 } |
| 123 | 115 |
| 124 | 116 |
| 125 // Fused multiply-add. | 117 // Fused multiply-add. |
| 126 inline double FusedMultiplyAdd(double op1, double op2, double a) { | 118 inline double FusedMultiplyAdd(double op1, double op2, double a) { |
| 127 return fma(op1, op2, a); | 119 return fma(op1, op2, a); |
| 128 } | 120 } |
| 129 | 121 |
| 130 | 122 |
| 131 inline float FusedMultiplyAdd(float op1, float op2, float a) { | 123 inline float FusedMultiplyAdd(float op1, float op2, float a) { |
| 132 return fmaf(op1, op2, a); | 124 return fmaf(op1, op2, a); |
| 133 } | 125 } |
| 134 | 126 |
| 135 } // namespace internal | 127 } // namespace internal |
| 136 } // namespace v8 | 128 } // namespace v8 |
| 137 | 129 |
| 138 #endif // V8_ARM64_UTILS_ARM64_H_ | 130 #endif // V8_ARM64_UTILS_ARM64_H_ |
| OLD | NEW |