blob: 35d982483750d65647980b274025f88598837d09 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009
10#include "src/arm64/constants-arm64.h"
11
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012namespace v8 {
13namespace internal {
14
15// These are global assumptions in v8.
16STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1);
17STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF);
18
19// Floating point representation.
20static inline uint32_t float_to_rawbits(float value) {
21 uint32_t bits = 0;
22 memcpy(&bits, &value, 4);
23 return bits;
24}
25
26
27static inline uint64_t double_to_rawbits(double value) {
28 uint64_t bits = 0;
29 memcpy(&bits, &value, 8);
30 return bits;
31}
32
33
34static inline float rawbits_to_float(uint32_t bits) {
35 float value = 0.0;
36 memcpy(&value, &bits, 4);
37 return value;
38}
39
40
41static 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.
49int CountLeadingZeros(uint64_t value, int width);
50int CountLeadingSignBits(int64_t value, int width);
51int CountTrailingZeros(uint64_t value, int width);
52int CountSetBits(uint64_t value, int width);
53uint64_t LargestPowerOf2Divisor(uint64_t value);
54int MaskToBit(uint64_t mask);
55
56
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058T 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087// NaN tests.
88inline 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
97inline 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
106template <typename T>
107inline bool IsQuietNaN(T num) {
108 return std::isnan(num) && !IsSignallingNaN(num);
109}
110
111
112// Convert the NaN in 'num' to a quiet NaN.
113inline double ToQuietNaN(double num) {
114 DCHECK(std::isnan(num));
115 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask);
116}
117
118
119inline 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.
126inline double FusedMultiplyAdd(double op1, double op2, double a) {
127 return fma(op1, op2, a);
128}
129
130
131inline float FusedMultiplyAdd(float op1, float op2, float a) {
132 return fmaf(op1, op2, a);
133}
134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135} // namespace internal
136} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137
138#endif // V8_ARM64_UTILS_ARM64_H_