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
3 // found in the LICENSE file.
5 #ifndef V8_ARM64_UTILS_ARM64_H_
6 #define V8_ARM64_UTILS_ARM64_H_
10 #include "src/arm64/constants-arm64.h"
12 #define REGISTER_CODE_LIST(R) \
13 R(0) R(1) R(2) R(3) R(4) R(5) R(6) R(7) \
14 R(8) R(9) R(10) R(11) R(12) R(13) R(14) R(15) \
15 R(16) R(17) R(18) R(19) R(20) R(21) R(22) R(23) \
16 R(24) R(25) R(26) R(27) R(28) R(29) R(30) R(31)
21 // These are global assumptions in v8.
22 STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1);
23 STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF);
25 // Floating point representation.
26 static inline uint32_t float_to_rawbits(float value) {
28 memcpy(&bits, &value, 4);
33 static inline uint64_t double_to_rawbits(double value) {
35 memcpy(&bits, &value, 8);
40 static inline float rawbits_to_float(uint32_t bits) {
42 memcpy(&value, &bits, 4);
47 static inline double rawbits_to_double(uint64_t bits) {
49 memcpy(&value, &bits, 8);
55 int CountLeadingZeros(uint64_t value, int width);
56 int CountLeadingSignBits(int64_t value, int width);
57 int CountTrailingZeros(uint64_t value, int width);
58 int CountSetBits(uint64_t value, int width);
59 uint64_t LargestPowerOf2Divisor(uint64_t value);
60 int MaskToBit(uint64_t mask);
64 T ReverseBits(T value) {
65 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) ||
66 (sizeof(value) == 8));
68 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
69 result = (result << 1) | (value & 1);
77 T ReverseBytes(T value, int block_bytes_log2) {
78 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8));
79 DCHECK((1U << block_bytes_log2) <= sizeof(value));
80 // Split the 64-bit value into an 8-bit array, where b[0] is the least
81 // significant byte, and b[7] is the most significant.
83 uint64_t mask = 0xff00000000000000;
84 for (int i = 7; i >= 0; i--) {
85 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
89 // Permutation tables for REV instructions.
90 // permute_table[0] is used by REV16_x, REV16_w
91 // permute_table[1] is used by REV32_x, REV_w
92 // permute_table[2] is used by REV_x
93 DCHECK((0 < block_bytes_log2) && (block_bytes_log2 < 4));
94 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
95 {4, 5, 6, 7, 0, 1, 2, 3},
96 {0, 1, 2, 3, 4, 5, 6, 7}};
98 for (int i = 0; i < 8; i++) {
100 result |= bytes[permute_table[block_bytes_log2 - 1][i]];
107 inline bool IsSignallingNaN(double num) {
108 uint64_t raw = double_to_rawbits(num);
109 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) {
116 inline bool IsSignallingNaN(float num) {
117 uint32_t raw = float_to_rawbits(num);
118 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) {
125 template <typename T>
126 inline bool IsQuietNaN(T num) {
127 return std::isnan(num) && !IsSignallingNaN(num);
131 // Convert the NaN in 'num' to a quiet NaN.
132 inline double ToQuietNaN(double num) {
133 DCHECK(std::isnan(num));
134 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask);
138 inline float ToQuietNaN(float num) {
139 DCHECK(std::isnan(num));
140 return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask);
144 // Fused multiply-add.
145 inline double FusedMultiplyAdd(double op1, double op2, double a) {
146 return fma(op1, op2, a);
150 inline float FusedMultiplyAdd(float op1, float op2, float a) {
151 return fmaf(op1, op2, a);
154 } // namespace internal
157 #endif // V8_ARM64_UTILS_ARM64_H_