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>
ReverseBytes(T value,int block_bytes_log2)58 T ReverseBytes(T value, int block_bytes_log2) {
59 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8));
60 DCHECK((1U << block_bytes_log2) <= sizeof(value));
61 // 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.
63 uint8_t bytes[8];
64 uint64_t mask = 0xff00000000000000;
65 for (int i = 7; i >= 0; i--) {
66 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
67 mask >>= 8;
68 }
69
70 // Permutation tables for REV instructions.
71 // permute_table[0] is used by REV16_x, REV16_w
72 // permute_table[1] is used by REV32_x, REV_w
73 // permute_table[2] is used by REV_x
74 DCHECK((0 < block_bytes_log2) && (block_bytes_log2 < 4));
75 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
76 {4, 5, 6, 7, 0, 1, 2, 3},
77 {0, 1, 2, 3, 4, 5, 6, 7}};
78 T result = 0;
79 for (int i = 0; i < 8; i++) {
80 result <<= 8;
81 result |= bytes[permute_table[block_bytes_log2 - 1][i]];
82 }
83 return result;
84 }
85
86
87 // NaN tests.
IsSignallingNaN(double num)88 inline bool IsSignallingNaN(double num) {
89 uint64_t raw = double_to_rawbits(num);
90 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) {
91 return true;
92 }
93 return false;
94 }
95
96
IsSignallingNaN(float num)97 inline bool IsSignallingNaN(float num) {
98 uint32_t raw = float_to_rawbits(num);
99 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) {
100 return true;
101 }
102 return false;
103 }
104
105
106 template <typename T>
IsQuietNaN(T num)107 inline bool IsQuietNaN(T num) {
108 return std::isnan(num) && !IsSignallingNaN(num);
109 }
110
111
112 // Convert the NaN in 'num' to a quiet NaN.
ToQuietNaN(double num)113 inline double ToQuietNaN(double num) {
114 DCHECK(std::isnan(num));
115 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask);
116 }
117
118
ToQuietNaN(float num)119 inline float ToQuietNaN(float num) {
120 DCHECK(std::isnan(num));
121 return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask);
122 }
123
124
125 // Fused multiply-add.
FusedMultiplyAdd(double op1,double op2,double a)126 inline double FusedMultiplyAdd(double op1, double op2, double a) {
127 return fma(op1, op2, a);
128 }
129
130
FusedMultiplyAdd(float op1,float op2,float a)131 inline float FusedMultiplyAdd(float op1, float op2, float a) {
132 return fmaf(op1, op2, a);
133 }
134
135 } // namespace internal
136 } // namespace v8
137
138 #endif // V8_ARM64_UTILS_ARM64_H_
139