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