da91fd5d6000aea74b6532b9233c58beebf852d3
[platform/upstream/v8.git] / src / arm64 / utils-arm64.h
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 #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)
17
18 namespace v8 {
19 namespace internal {
20
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);
24
25 // Floating point representation.
26 static inline uint32_t float_to_rawbits(float value) {
27   uint32_t bits = 0;
28   memcpy(&bits, &value, 4);
29   return bits;
30 }
31
32
33 static inline uint64_t double_to_rawbits(double value) {
34   uint64_t bits = 0;
35   memcpy(&bits, &value, 8);
36   return bits;
37 }
38
39
40 static inline float rawbits_to_float(uint32_t bits) {
41   float value = 0.0;
42   memcpy(&value, &bits, 4);
43   return value;
44 }
45
46
47 static inline double rawbits_to_double(uint64_t bits) {
48   double value = 0.0;
49   memcpy(&value, &bits, 8);
50   return value;
51 }
52
53
54 // Bit counting.
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);
61
62
63 template <typename T>
64 T ReverseBits(T value) {
65   DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) ||
66          (sizeof(value) == 8));
67   T result = 0;
68   for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
69     result = (result << 1) | (value & 1);
70     value >>= 1;
71   }
72   return result;
73 }
74
75
76 template <typename T>
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.
82   uint8_t bytes[8];
83   uint64_t mask = 0xff00000000000000;
84   for (int i = 7; i >= 0; i--) {
85     bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
86     mask >>= 8;
87   }
88
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}};
97   T result = 0;
98   for (int i = 0; i < 8; i++) {
99     result <<= 8;
100     result |= bytes[permute_table[block_bytes_log2 - 1][i]];
101   }
102   return result;
103 }
104
105
106 // NaN tests.
107 inline bool IsSignallingNaN(double num) {
108   uint64_t raw = double_to_rawbits(num);
109   if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) {
110     return true;
111   }
112   return false;
113 }
114
115
116 inline bool IsSignallingNaN(float num) {
117   uint32_t raw = float_to_rawbits(num);
118   if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) {
119     return true;
120   }
121   return false;
122 }
123
124
125 template <typename T>
126 inline bool IsQuietNaN(T num) {
127   return std::isnan(num) && !IsSignallingNaN(num);
128 }
129
130
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);
135 }
136
137
138 inline float ToQuietNaN(float num) {
139   DCHECK(std::isnan(num));
140   return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask);
141 }
142
143
144 // Fused multiply-add.
145 inline double FusedMultiplyAdd(double op1, double op2, double a) {
146   return fma(op1, op2, a);
147 }
148
149
150 inline float FusedMultiplyAdd(float op1, float op2, float a) {
151   return fmaf(op1, op2, a);
152 }
153
154 } }  // namespace v8::internal
155
156 #endif  // V8_ARM64_UTILS_ARM64_H_