v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
utils-arm64.h
Go to the documentation of this file.
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_CODEGEN_ARM64_UTILS_ARM64_H_
6#define V8_CODEGEN_ARM64_UTILS_ARM64_H_
7
8#include <cmath>
9
11#include "src/utils/utils.h"
12
13namespace v8 {
14namespace internal {
15
16// These are global assumptions in v8.
17static_assert((static_cast<int32_t>(-1) >> 1) == -1);
18static_assert((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF);
19
20uint32_t float_sign(float val);
21uint32_t float_exp(float val);
22uint32_t float_mantissa(float val);
23uint32_t double_sign(double val);
24uint32_t double_exp(double val);
25uint64_t double_mantissa(double val);
26
27float float_pack(uint32_t sign, uint32_t exp, uint32_t mantissa);
28double double_pack(uint64_t sign, uint64_t exp, uint64_t mantissa);
29
30// An fpclassify() function for 16-bit half-precision floats.
32
33// Bit counting.
34inline static int CountLeadingZeros(uint64_t value, int width) {
35 DCHECK(base::bits::IsPowerOfTwo(width) && (width <= 64));
36 if (value == 0) {
37 return width;
38 }
39 return base::bits::CountLeadingZeros64(value << (64 - width));
40}
41int CountLeadingSignBits(int64_t value, int width);
42V8_EXPORT_PRIVATE int CountSetBits(uint64_t value, int width);
43int LowestSetBitPosition(uint64_t value);
44int HighestSetBitPosition(uint64_t value);
45inline static uint64_t LargestPowerOf2Divisor(uint64_t value) {
46 // Simulate two's complement (instead of casting to signed and negating) to
47 // avoid undefined behavior on signed overflow.
48 return value & ((~value) + 1);
49}
50int MaskToBit(uint64_t mask);
51
52template <typename T>
53T ReverseBytes(T value, int block_bytes_log2) {
54 DCHECK((sizeof(value) == 4) || (sizeof(value) == 8));
55 DCHECK((1ULL << block_bytes_log2) <= sizeof(value));
56 // Split the 64-bit value into an 8-bit array, where b[0] is the least
57 // significant byte, and b[7] is the most significant.
58 uint8_t bytes[8];
59 uint64_t mask = 0xff00000000000000;
60 for (int i = 7; i >= 0; i--) {
61 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
62 mask >>= 8;
63 }
64
65 // Permutation tables for REV instructions.
66 // permute_table[0] is used by REV16_x, REV16_w
67 // permute_table[1] is used by REV32_x, REV_w
68 // permute_table[2] is used by REV_x
69 DCHECK((0 < block_bytes_log2) && (block_bytes_log2 < 4));
70 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
71 {4, 5, 6, 7, 0, 1, 2, 3},
72 {0, 1, 2, 3, 4, 5, 6, 7}};
73 typename std::make_unsigned<T>::type result = 0;
74 for (int i = 0; i < 8; i++) {
75 result <<= 8;
76 result |= bytes[permute_table[block_bytes_log2 - 1][i]];
77 }
78 return result;
79}
80
81// NaN tests.
82inline bool IsSignallingNaN(double num) {
83 uint64_t raw = base::bit_cast<uint64_t>(num);
84 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) {
85 return true;
86 }
87 return false;
88}
89
90inline bool IsSignallingNaN(float num) {
91 uint32_t raw = base::bit_cast<uint32_t>(num);
92 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) {
93 return true;
94 }
95 return false;
96}
97
98inline bool IsSignallingNaN(float16 num) {
99 const uint16_t kFP16QuietNaNMask = 0x0200;
100 return (float16classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0);
101}
102
103template <typename T>
104inline bool IsQuietNaN(T num) {
105 return std::isnan(num) && !IsSignallingNaN(num);
106}
107
108// Convert the NaN in 'num' to a quiet NaN.
109inline double ToQuietNaN(double num) {
110 DCHECK(std::isnan(num));
112}
113
114inline float ToQuietNaN(float num) {
115 DCHECK(std::isnan(num));
117 static_cast<uint32_t>(kSQuietNanMask));
118}
119
120// Fused multiply-add.
121inline double FusedMultiplyAdd(double op1, double op2, double a) {
122 return fma(op1, op2, a);
123}
124
125inline float FusedMultiplyAdd(float op1, float op2, float a) {
126 return fmaf(op1, op2, a);
127}
128
129} // namespace internal
130} // namespace v8
131
132#endif // V8_CODEGEN_ARM64_UTILS_ARM64_H_
ZoneVector< RpoNumber > & result
uint32_t const mask
int int32_t
Definition unicode.cc:40
constexpr unsigned CountLeadingZeros64(uint64_t value)
Definition bits.h:125
constexpr bool IsPowerOfTwo(T value)
Definition bits.h:187
V8_INLINE Dest bit_cast(Source const &source)
Definition macros.h:95
int CountLeadingSignBits(int64_t value, int width)
V8_EXPORT_PRIVATE int CountSetBits(uint64_t value, int width)
bool IsSignallingNaN(double num)
Definition utils-arm64.h:82
int LowestSetBitPosition(uint64_t value)
static uint64_t LargestPowerOf2Divisor(uint64_t value)
Definition utils-arm64.h:45
int MaskToBit(uint64_t mask)
uint32_t float_sign(float val)
double ToQuietNaN(double num)
constexpr int64_t kDQuietNanMask
bool IsQuietNaN(T num)
int HighestSetBitPosition(uint64_t value)
double FusedMultiplyAdd(double op1, double op2, double a)
uint32_t double_sign(double val)
T ReverseBytes(T value, int block_bytes_log2)
Definition utils-arm64.h:53
float float_pack(uint32_t sign, uint32_t exp, uint32_t mantissa)
constexpr int64_t kSQuietNanMask
double double_pack(uint64_t sign, uint64_t exp, uint64_t mantissa)
int float16classify(float16 value)
uint32_t float_mantissa(float val)
uint32_t double_exp(double val)
uint32_t float_exp(float val)
return value
Definition map-inl.h:893
uint64_t double_mantissa(double val)
static int CountLeadingZeros(uint64_t value, int width)
Definition utils-arm64.h:34
#define DCHECK(condition)
Definition logging.h:482
#define V8_EXPORT_PRIVATE
Definition macros.h:460