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 #if V8_TARGET_ARCH_ARM64
6
7 #include "src/codegen/arm64/utils-arm64.h"
8
9 namespace v8 {
10 namespace internal {
11
12 #define __ assm->
13
float_sign(float val)14 uint32_t float_sign(float val) {
15 uint32_t bits = bit_cast<uint32_t>(val);
16 return unsigned_bitextract_32(31, 31, bits);
17 }
18
float_exp(float val)19 uint32_t float_exp(float val) {
20 uint32_t bits = bit_cast<uint32_t>(val);
21 return unsigned_bitextract_32(30, 23, bits);
22 }
23
float_mantissa(float val)24 uint32_t float_mantissa(float val) {
25 uint32_t bits = bit_cast<uint32_t>(val);
26 return unsigned_bitextract_32(22, 0, bits);
27 }
28
double_sign(double val)29 uint32_t double_sign(double val) {
30 uint64_t bits = bit_cast<uint64_t>(val);
31 return static_cast<uint32_t>(unsigned_bitextract_64(63, 63, bits));
32 }
33
double_exp(double val)34 uint32_t double_exp(double val) {
35 uint64_t bits = bit_cast<uint64_t>(val);
36 return static_cast<uint32_t>(unsigned_bitextract_64(62, 52, bits));
37 }
38
double_mantissa(double val)39 uint64_t double_mantissa(double val) {
40 uint64_t bits = bit_cast<uint64_t>(val);
41 return unsigned_bitextract_64(51, 0, bits);
42 }
43
float_pack(uint32_t sign,uint32_t exp,uint32_t mantissa)44 float float_pack(uint32_t sign, uint32_t exp, uint32_t mantissa) {
45 uint32_t bits = sign << kFloatExponentBits | exp;
46 return bit_cast<float>((bits << kFloatMantissaBits) | mantissa);
47 }
48
double_pack(uint64_t sign,uint64_t exp,uint64_t mantissa)49 double double_pack(uint64_t sign, uint64_t exp, uint64_t mantissa) {
50 uint64_t bits = sign << kDoubleExponentBits | exp;
51 return bit_cast<double>((bits << kDoubleMantissaBits) | mantissa);
52 }
53
float16classify(float16 value)54 int float16classify(float16 value) {
55 const uint16_t exponent_max = (1 << kFloat16ExponentBits) - 1;
56 const uint16_t exponent_mask = exponent_max << kFloat16MantissaBits;
57 const uint16_t mantissa_mask = (1 << kFloat16MantissaBits) - 1;
58
59 const uint16_t exponent = (value & exponent_mask) >> kFloat16MantissaBits;
60 const uint16_t mantissa = value & mantissa_mask;
61 if (exponent == 0) {
62 if (mantissa == 0) {
63 return FP_ZERO;
64 }
65 return FP_SUBNORMAL;
66 } else if (exponent == exponent_max) {
67 if (mantissa == 0) {
68 return FP_INFINITE;
69 }
70 return FP_NAN;
71 }
72 return FP_NORMAL;
73 }
74
CountLeadingZeros(uint64_t value,int width)75 int CountLeadingZeros(uint64_t value, int width) {
76 DCHECK(base::bits::IsPowerOfTwo(width) && (width <= 64));
77 if (value == 0) {
78 return width;
79 }
80 return base::bits::CountLeadingZeros64(value << (64 - width));
81 }
82
CountLeadingSignBits(int64_t value,int width)83 int CountLeadingSignBits(int64_t value, int width) {
84 DCHECK(base::bits::IsPowerOfTwo(width) && (width <= 64));
85 if (value >= 0) {
86 return CountLeadingZeros(value, width) - 1;
87 } else {
88 return CountLeadingZeros(~value, width) - 1;
89 }
90 }
91
CountSetBits(uint64_t value,int width)92 int CountSetBits(uint64_t value, int width) {
93 DCHECK((width == 32) || (width == 64));
94 if (width == 64) {
95 return static_cast<int>(base::bits::CountPopulation(value));
96 }
97 return static_cast<int>(
98 base::bits::CountPopulation(static_cast<uint32_t>(value & 0xFFFFFFFFF)));
99 }
100
LowestSetBitPosition(uint64_t value)101 int LowestSetBitPosition(uint64_t value) {
102 DCHECK_NE(value, 0U);
103 return base::bits::CountTrailingZeros(value) + 1;
104 }
105
HighestSetBitPosition(uint64_t value)106 int HighestSetBitPosition(uint64_t value) {
107 DCHECK_NE(value, 0U);
108 return 63 - CountLeadingZeros(value, 64);
109 }
110
LargestPowerOf2Divisor(uint64_t value)111 uint64_t LargestPowerOf2Divisor(uint64_t value) {
112 // Simulate two's complement (instead of casting to signed and negating) to
113 // avoid undefined behavior on signed overflow.
114 return value & ((~value) + 1);
115 }
116
MaskToBit(uint64_t mask)117 int MaskToBit(uint64_t mask) {
118 DCHECK_EQ(CountSetBits(mask, 64), 1);
119 return base::bits::CountTrailingZeros(mask);
120 }
121
122 #undef __
123
124 } // namespace internal
125 } // namespace v8
126
127 #endif // V8_TARGET_ARCH_ARM64
128