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.
4
5 #ifndef V8_ARM64_UTILS_ARM64_H_
6 #define V8_ARM64_UTILS_ARM64_H_
7
8 #include <cmath>
9
10 #include "src/arm64/constants-arm64.h"
11
12 namespace v8 {
13 namespace internal {
14
15 // These are global assumptions in v8.
16 STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1);
17 STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF);
18
19 // Floating point representation.
float_to_rawbits(float value)20 static inline uint32_t float_to_rawbits(float value) {
21 uint32_t bits = 0;
22 memcpy(&bits, &value, 4);
23 return bits;
24 }
25
26
double_to_rawbits(double value)27 static inline uint64_t double_to_rawbits(double value) {
28 uint64_t bits = 0;
29 memcpy(&bits, &value, 8);
30 return bits;
31 }
32
33
rawbits_to_float(uint32_t bits)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
rawbits_to_double(uint64_t bits)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
48 // Bit counting.
49 int CountLeadingZeros(uint64_t value, int width);
50 int CountLeadingSignBits(int64_t value, int width);
51 int CountTrailingZeros(uint64_t value, int width);
52 int CountSetBits(uint64_t value, int width);
53 uint64_t LargestPowerOf2Divisor(uint64_t value);
54 int MaskToBit(uint64_t mask);
55
56
57 template <typename T>
ReverseBits(T value)58 T ReverseBits(T value) {
59 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) ||
60 (sizeof(value) == 8));
61 T result = 0;
62 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
63 result = (result << 1) | (value & 1);
64 value >>= 1;
65 }
66 return result;
67 }
68
69
70 template <typename T>
ReverseBytes(T value,int block_bytes_log2)71 T ReverseBytes(T value, int block_bytes_log2) {
72 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8));
73 DCHECK((1U << block_bytes_log2) <= sizeof(value));
74 // Split the 64-bit value into an 8-bit array, where b[0] is the least
75 // significant byte, and b[7] is the most significant.
76 uint8_t bytes[8];
77 uint64_t mask = 0xff00000000000000;
78 for (int i = 7; i >= 0; i--) {
79 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
80 mask >>= 8;
81 }
82
83 // Permutation tables for REV instructions.
84 // permute_table[0] is used by REV16_x, REV16_w
85 // permute_table[1] is used by REV32_x, REV_w
86 // permute_table[2] is used by REV_x
87 DCHECK((0 < block_bytes_log2) && (block_bytes_log2 < 4));
88 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
89 {4, 5, 6, 7, 0, 1, 2, 3},
90 {0, 1, 2, 3, 4, 5, 6, 7}};
91 T result = 0;
92 for (int i = 0; i < 8; i++) {
93 result <<= 8;
94 result |= bytes[permute_table[block_bytes_log2 - 1][i]];
95 }
96 return result;
97 }
98
99
100 // NaN tests.
IsSignallingNaN(double num)101 inline bool IsSignallingNaN(double num) {
102 uint64_t raw = double_to_rawbits(num);
103 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) {
104 return true;
105 }
106 return false;
107 }
108
109
IsSignallingNaN(float num)110 inline bool IsSignallingNaN(float num) {
111 uint32_t raw = float_to_rawbits(num);
112 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) {
113 return true;
114 }
115 return false;
116 }
117
118
119 template <typename T>
IsQuietNaN(T num)120 inline bool IsQuietNaN(T num) {
121 return std::isnan(num) && !IsSignallingNaN(num);
122 }
123
124
125 // Convert the NaN in 'num' to a quiet NaN.
ToQuietNaN(double num)126 inline double ToQuietNaN(double num) {
127 DCHECK(std::isnan(num));
128 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask);
129 }
130
131
ToQuietNaN(float num)132 inline float ToQuietNaN(float num) {
133 DCHECK(std::isnan(num));
134 return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask);
135 }
136
137
138 // Fused multiply-add.
FusedMultiplyAdd(double op1,double op2,double a)139 inline double FusedMultiplyAdd(double op1, double op2, double a) {
140 return fma(op1, op2, a);
141 }
142
143
FusedMultiplyAdd(float op1,float op2,float a)144 inline float FusedMultiplyAdd(float op1, float op2, float a) {
145 return fmaf(op1, op2, a);
146 }
147
148 } // namespace internal
149 } // namespace v8
150
151 #endif // V8_ARM64_UTILS_ARM64_H_
152