blob: 35a585cbd71cc928aba10e9dafce65927f3e811e [file] [log] [blame]
Alexandre Ramesb78f1392016-07-01 14:22:22 +01001// Copyright 2015, VIXL authors
armvixlad96eda2013-06-14 11:42:37 +01002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// * Neither the name of ARM Limited nor the names of its contributors may be
13// used to endorse or promote products derived from this software without
14// specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef VIXL_UTILS_H
28#define VIXL_UTILS_H
29
armvixl6e2c8272015-03-31 11:04:14 +010030#include <cmath>
Pierre Langlois78973f22016-08-10 14:35:56 +010031#include <cstring>
Pierre Langlois88c46b82016-06-02 18:15:32 +010032#include <vector>
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010033
Alexandre Rames1f9074d2016-05-23 15:50:01 +010034#include "compiler-intrinsics-vixl.h"
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010035#include "globals-vixl.h"
armvixlad96eda2013-06-14 11:42:37 +010036
37namespace vixl {
38
armvixl4a102ba2014-07-14 09:02:40 +010039// Macros for compile-time format checking.
armvixl788c84f2015-12-08 17:05:23 +000040#if GCC_VERSION_OR_NEWER(4, 4, 0)
armvixl4a102ba2014-07-14 09:02:40 +010041#define PRINTF_CHECK(format_index, varargs_index) \
armvixl788c84f2015-12-08 17:05:23 +000042 __attribute__((format(gnu_printf, format_index, varargs_index)))
armvixl4a102ba2014-07-14 09:02:40 +010043#else
44#define PRINTF_CHECK(format_index, varargs_index)
45#endif
46
Pierre Langlois88c46b82016-06-02 18:15:32 +010047#ifdef __GNUC__
48#define VIXL_HAS_DEPRECATED_WITH_MSG
49#elif defined(__clang__)
50#ifdef __has_extension(attribute_deprecated_with_message)
51#define VIXL_HAS_DEPRECATED_WITH_MSG
52#endif
53#endif
54
55#ifdef VIXL_HAS_DEPRECATED_WITH_MSG
56#define VIXL_DEPRECATED(replaced_by, declarator) \
57 __attribute__((deprecated("Use \"" replaced_by "\" instead"))) declarator
58#else
59#define VIXL_DEPRECATED(replaced_by, declarator) declarator
60#endif
61
62#ifdef VIXL_DEBUG
63#define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_UNREACHABLE()
64#else
65#define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_FALLTHROUGH()
66#endif
67
armvixlad96eda2013-06-14 11:42:37 +010068// Check number width.
Pierre Langlois88c46b82016-06-02 18:15:32 +010069inline bool IsIntN(unsigned n, int64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000070 VIXL_ASSERT((0 < n) && (n < 64));
71 int64_t limit = INT64_C(1) << (n - 1);
armvixlad96eda2013-06-14 11:42:37 +010072 return (-limit <= x) && (x < limit);
73}
Pierre Langlois88c46b82016-06-02 18:15:32 +010074VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
75 return IsIntN(n, x);
76}
armvixlad96eda2013-06-14 11:42:37 +010077
Pierre Langlois88c46b82016-06-02 18:15:32 +010078inline bool IsUintN(unsigned n, int64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000079 VIXL_ASSERT((0 < n) && (n < 64));
armvixlad96eda2013-06-14 11:42:37 +010080 return !(x >> n);
81}
Pierre Langlois88c46b82016-06-02 18:15:32 +010082VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
83 return IsUintN(n, x);
84}
armvixlad96eda2013-06-14 11:42:37 +010085
Jacob Bramley3976edb2016-10-18 10:51:43 +010086inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000087 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +010088 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +010089}
Jacob Bramley3976edb2016-10-18 10:51:43 +010090VIXL_DEPRECATED("TruncateToUintN",
91 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
92 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +010093}
armvixlad96eda2013-06-14 11:42:37 +010094
armvixl0f35e362016-05-10 13:57:58 +010095// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +010096#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +010097V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
98V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
99V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100100V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
101
102#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100103V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
104V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
105V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
106V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100107
Jacob Bramley3976edb2016-10-18 10:51:43 +0100108#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
109
armvixl0f35e362016-05-10 13:57:58 +0100110// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100111
Pierre Langlois88c46b82016-06-02 18:15:32 +0100112#define DECLARE_IS_INT_N(N) \
113 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
114 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
115 return IsIntN(N, x); \
116 }
117
118#define DECLARE_IS_UINT_N(N) \
119 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
120 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
121 return IsUintN(N, x); \
122 }
123
Jacob Bramley3976edb2016-10-18 10:51:43 +0100124#define DECLARE_TRUNCATE_TO_UINT_32(N) \
125 inline uint32_t TruncateToUint##N(uint64_t x) { \
126 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
127 } \
128 VIXL_DEPRECATED("TruncateToUint" #N, \
129 inline uint32_t truncate_to_int##N(int64_t x)) { \
130 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100131 }
132
armvixlad96eda2013-06-14 11:42:37 +0100133INT_1_TO_63_LIST(DECLARE_IS_INT_N)
134INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100135INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100136
armvixlad96eda2013-06-14 11:42:37 +0100137#undef DECLARE_IS_INT_N
138#undef DECLARE_IS_UINT_N
139#undef DECLARE_TRUNCATE_TO_INT_N
140
141// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100142inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000143 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
144 (msb >= lsb));
145 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100146 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100147}
148
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000149
150inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint32_t x) {
151 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
152 (msb >= lsb));
153 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100154}
155
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000156
Pierre Langlois88c46b82016-06-02 18:15:32 +0100157inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000158 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
159 (msb >= lsb));
160 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
161 // If the highest extracted bit is set, sign extend.
162 if ((temp >> (msb - lsb)) == 1) {
163 temp |= ~UINT64_C(0) << (msb - lsb);
164 }
165 int64_t result;
166 memcpy(&result, &temp, sizeof(result));
167 return result;
armvixlad96eda2013-06-14 11:42:37 +0100168}
169
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000170
171inline int32_t ExtractSignedBitfield32(int msb, int lsb, int32_t x) {
172 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
173 (msb >= lsb));
174 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
175 int32_t result;
176 memcpy(&result, &temp, sizeof(result));
177 return result;
178}
179
180
181inline uint64_t RotateRight(uint64_t value,
182 unsigned int rotate,
183 unsigned int width) {
184 VIXL_ASSERT((width > 0) && (width <= 64));
185 uint64_t width_mask = ~UINT64_C(0) >> (64 - width);
186 rotate &= 63;
187 if (rotate > 0) {
188 value &= width_mask;
189 value = (value << (width - rotate)) | (value >> rotate);
190 }
191 return value & width_mask;
192}
193
194
armvixlf37fdc02014-02-05 13:22:16 +0000195// Floating point representation.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100196uint32_t FloatToRawbits(float value);
197VIXL_DEPRECATED("FloatToRawbits",
198 inline uint32_t float_to_rawbits(float value)) {
199 return FloatToRawbits(value);
200}
armvixlad96eda2013-06-14 11:42:37 +0100201
Pierre Langlois88c46b82016-06-02 18:15:32 +0100202uint64_t DoubleToRawbits(double value);
203VIXL_DEPRECATED("DoubleToRawbits",
204 inline uint64_t double_to_rawbits(double value)) {
205 return DoubleToRawbits(value);
206}
armvixl5289c592015-03-02 13:52:04 +0000207
Pierre Langlois88c46b82016-06-02 18:15:32 +0100208float RawbitsToFloat(uint32_t bits);
209VIXL_DEPRECATED("RawbitsToFloat",
210 inline float rawbits_to_float(uint32_t bits)) {
211 return RawbitsToFloat(bits);
212}
213
214double RawbitsToDouble(uint64_t bits);
215VIXL_DEPRECATED("RawbitsToDouble",
216 inline double rawbits_to_double(uint64_t bits)) {
217 return RawbitsToDouble(bits);
218}
219
220uint32_t FloatSign(float value);
221VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
222 return FloatSign(value);
223}
224
225uint32_t FloatExp(float value);
226VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
227 return FloatExp(value);
228}
229
230uint32_t FloatMantissa(float value);
231VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
232 return FloatMantissa(value);
233}
234
235uint32_t DoubleSign(double value);
236VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
237 return DoubleSign(value);
238}
239
240uint32_t DoubleExp(double value);
241VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
242 return DoubleExp(value);
243}
244
245uint64_t DoubleMantissa(double value);
246VIXL_DEPRECATED("DoubleMantissa",
247 inline uint64_t double_mantissa(double value)) {
248 return DoubleMantissa(value);
249}
250
251float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
252VIXL_DEPRECATED("FloatPack",
253 inline float float_pack(uint32_t sign,
254 uint32_t exp,
255 uint32_t mantissa)) {
256 return FloatPack(sign, exp, mantissa);
257}
258
259double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
260VIXL_DEPRECATED("DoublePack",
261 inline double double_pack(uint32_t sign,
262 uint32_t exp,
263 uint64_t mantissa)) {
264 return DoublePack(sign, exp, mantissa);
265}
armvixl5289c592015-03-02 13:52:04 +0000266
267// An fpclassify() function for 16-bit half-precision floats.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100268int Float16Classify(float16 value);
269VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) {
270 return Float16Classify(value);
271}
armvixlf37fdc02014-02-05 13:22:16 +0000272
273// NaN tests.
274inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100275 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100276 uint64_t raw = DoubleToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100277 if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000278 return true;
279 }
280 return false;
281}
282
283
284inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000285 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100286 uint32_t raw = FloatToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100287 if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000288 return true;
289 }
290 return false;
291}
292
293
armvixl5289c592015-03-02 13:52:04 +0000294inline bool IsSignallingNaN(float16 num) {
295 const uint16_t kFP16QuietNaNMask = 0x0200;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100296 return (Float16Classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000297}
298
299
armvixlf37fdc02014-02-05 13:22:16 +0000300template <typename T>
301inline bool IsQuietNaN(T num) {
armvixl6e2c8272015-03-31 11:04:14 +0100302 return std::isnan(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000303}
304
305
armvixlb0c8ae22014-03-21 14:03:59 +0000306// Convert the NaN in 'num' to a quiet NaN.
307inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100308 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
armvixl6e2c8272015-03-31 11:04:14 +0100309 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100310 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000311}
312
313
314inline float ToQuietNaN(float num) {
315 const uint32_t kFP32QuietNaNMask = 0x00400000;
armvixl6e2c8272015-03-31 11:04:14 +0100316 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100317 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000318}
319
320
321// Fused multiply-add.
322inline double FusedMultiplyAdd(double op1, double op2, double a) {
323 return fma(op1, op2, a);
324}
325
326
327inline float FusedMultiplyAdd(float op1, float op2, float a) {
328 return fmaf(op1, op2, a);
329}
330
331
armvixl0f35e362016-05-10 13:57:58 +0100332inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100333
334
armvixl0f35e362016-05-10 13:57:58 +0100335template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100336inline int HighestSetBitPosition(T value) {
337 VIXL_ASSERT(value != 0);
338 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
339}
340
341
armvixl0f35e362016-05-10 13:57:58 +0100342template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100343inline int WhichPowerOf2(V value) {
344 VIXL_ASSERT(IsPowerOf2(value));
345 return CountTrailingZeros(value);
346}
armvixlad96eda2013-06-14 11:42:37 +0100347
armvixldb644342015-07-21 11:37:10 +0100348
armvixl330dc712014-11-25 10:38:32 +0000349unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
350
armvixldb644342015-07-21 11:37:10 +0100351
Pierre Langlois88c46b82016-06-02 18:15:32 +0100352int BitCount(uint64_t value);
353
354
armvixldb644342015-07-21 11:37:10 +0100355template <typename T>
356T ReverseBits(T value) {
357 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
358 (sizeof(value) == 4) || (sizeof(value) == 8));
359 T result = 0;
360 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
361 result = (result << 1) | (value & 1);
362 value >>= 1;
363 }
364 return result;
365}
366
367
368template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100369inline T SignExtend(T val, int bitSize) {
370 VIXL_ASSERT(bitSize > 0);
371 T mask = (T(2) << (bitSize - 1)) - T(1);
372 val &= mask;
373 T sign = -(val >> (bitSize - 1));
374 val |= (sign << bitSize);
375 return val;
376}
377
378
379template <typename T>
armvixldb644342015-07-21 11:37:10 +0100380T ReverseBytes(T value, int block_bytes_log2) {
381 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
382 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
383 // Split the 64-bit value into an 8-bit array, where b[0] is the least
384 // significant byte, and b[7] is the most significant.
385 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000386 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100387 for (int i = 7; i >= 0; i--) {
388 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
389 mask >>= 8;
390 }
391
392 // Permutation tables for REV instructions.
393 // permute_table[0] is used by REV16_x, REV16_w
394 // permute_table[1] is used by REV32_x, REV_w
395 // permute_table[2] is used by REV_x
396 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100397 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
398 {4, 5, 6, 7, 0, 1, 2, 3},
399 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000400 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100401 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000402 temp <<= 8;
403 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100404 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000405
406 T result;
407 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
408 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100409 return result;
410}
411
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700412template <unsigned MULTIPLE, typename T>
413inline bool IsMultiple(T value) {
414 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
415 return (value & (MULTIPLE - 1)) == 0;
416}
armvixldb644342015-07-21 11:37:10 +0100417
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000418template <typename T>
419inline bool IsMultiple(T value, unsigned multiple) {
420 VIXL_ASSERT(IsPowerOf2(multiple));
421 return (value & (multiple - 1)) == 0;
422}
423
armvixlad96eda2013-06-14 11:42:37 +0100424// Pointer alignment
425// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100426template <unsigned ALIGN, typename T>
427inline bool IsAligned(T pointer) {
428 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
429 VIXL_ASSERT(IsPowerOf2(ALIGN));
430 // Use C-style casts to get static_cast behaviour for integral types (T), and
431 // reinterpret_cast behaviour for other types.
432 return ((intptr_t)(pointer) & (ALIGN - 1)) == 0;
433}
434
armvixl0f35e362016-05-10 13:57:58 +0100435template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100436bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100437 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100438}
439
armvixl330dc712014-11-25 10:38:32 +0000440// Increment a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100441template <class T>
armvixlad96eda2013-06-14 11:42:37 +0100442T AlignUp(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100443 // Use C-style casts to get static_cast behaviour for integral types (T), and
444 // reinterpret_cast behaviour for other types.
445
armvixl330dc712014-11-25 10:38:32 +0000446 uint64_t pointer_raw = (uint64_t)pointer;
447 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100448
armvixlad96eda2013-06-14 11:42:37 +0100449 size_t align_step = (alignment - pointer_raw) % alignment;
armvixlb0c8ae22014-03-21 14:03:59 +0000450 VIXL_ASSERT((pointer_raw + align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100451
452 return (T)(pointer_raw + align_step);
armvixlad96eda2013-06-14 11:42:37 +0100453}
454
armvixl330dc712014-11-25 10:38:32 +0000455// Decrement a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100456template <class T>
armvixlb0c8ae22014-03-21 14:03:59 +0000457T AlignDown(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100458 // Use C-style casts to get static_cast behaviour for integral types (T), and
459 // reinterpret_cast behaviour for other types.
460
armvixl330dc712014-11-25 10:38:32 +0000461 uint64_t pointer_raw = (uint64_t)pointer;
462 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100463
armvixlb0c8ae22014-03-21 14:03:59 +0000464 size_t align_step = pointer_raw % alignment;
465 VIXL_ASSERT((pointer_raw - align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100466
467 return (T)(pointer_raw - align_step);
armvixlb0c8ae22014-03-21 14:03:59 +0000468}
469
Pierre Langlois88c46b82016-06-02 18:15:32 +0100470template <typename T>
471inline T ExtractBit(T value, unsigned bit) {
472 return (value >> bit) & T(1);
473}
474
475template <typename Ts, typename Td>
476inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
477 return Td((value >> least_significant_bit) & Ts(mask));
478}
479
480template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800481inline void AssignBit(Td& dst, // NOLINT(runtime/references)
482 int bit,
483 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100484 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
485 VIXL_ASSERT(bit >= 0);
486 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
487 Td mask(1);
488 dst &= ~(mask << bit);
489 dst |= Td(value) << bit;
490}
491
492template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800493inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100494 int least_significant_bit,
495 Ts mask,
496 Ts value) {
497 VIXL_ASSERT(least_significant_bit >= 0);
498 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
499 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
500 Td(mask));
501 VIXL_ASSERT((value & mask) == value);
502 dst &= ~(Td(mask) << least_significant_bit);
503 dst |= Td(value) << least_significant_bit;
504}
505
506class VFP {
507 public:
508 static uint32_t FP32ToImm8(float imm) {
509 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
510 uint32_t bits = FloatToRawbits(imm);
511 // bit7: a000.0000
512 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
513 // bit6: 0b00.0000
514 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
515 // bit5_to_0: 00cd.efgh
516 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
517 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
518 }
519 static uint32_t FP64ToImm8(double imm) {
520 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
521 // 0000.0000.0000.0000.0000.0000.0000.0000
522 uint64_t bits = DoubleToRawbits(imm);
523 // bit7: a000.0000
524 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
525 // bit6: 0b00.0000
526 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
527 // bit5_to_0: 00cd.efgh
528 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
529
530 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
531 }
532 static float Imm8ToFP32(uint32_t imm8) {
533 // Imm8: abcdefgh (8 bits)
534 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
535 // where B is b ^ 1
536 uint32_t bits = imm8;
537 uint32_t bit7 = (bits >> 7) & 0x1;
538 uint32_t bit6 = (bits >> 6) & 0x1;
539 uint32_t bit5_to_0 = bits & 0x3f;
540 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
541
542 return RawbitsToFloat(result);
543 }
544 static double Imm8ToFP64(uint32_t imm8) {
545 // Imm8: abcdefgh (8 bits)
546 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
547 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
548 // where B is b ^ 1
549 uint32_t bits = imm8;
550 uint64_t bit7 = (bits >> 7) & 0x1;
551 uint64_t bit6 = (bits >> 6) & 0x1;
552 uint64_t bit5_to_0 = bits & 0x3f;
553 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
554 return RawbitsToDouble(result);
555 }
556 static bool IsImmFP32(float imm) {
557 // Valid values will have the form:
558 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
559 uint32_t bits = FloatToRawbits(imm);
560 // bits[19..0] are cleared.
561 if ((bits & 0x7ffff) != 0) {
562 return false;
563 }
564
565
566 // bits[29..25] are all set or all cleared.
567 uint32_t b_pattern = (bits >> 16) & 0x3e00;
568 if (b_pattern != 0 && b_pattern != 0x3e00) {
569 return false;
570 }
571 // bit[30] and bit[29] are opposite.
572 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
573 return false;
574 }
575 return true;
576 }
577 static bool IsImmFP64(double imm) {
578 // Valid values will have the form:
579 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
580 // 0000.0000.0000.0000.0000.0000.0000.0000
581 uint64_t bits = DoubleToRawbits(imm);
582 // bits[47..0] are cleared.
583 if ((bits & 0x0000ffffffffffff) != 0) {
584 return false;
585 }
586 // bits[61..54] are all set or all cleared.
587 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
588 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
589 return false;
590 }
591 // bit[62] and bit[61] are opposite.
592 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
593 return false;
594 }
595 return true;
596 }
597};
598
599class BitField {
600 // ForEachBitHelper is a functor that will call
601 // bool ForEachBitHelper::execute(ElementType id) const
602 // and expects a boolean in return whether to continue (if true)
603 // or stop (if false)
604 // check_set will check if the bits are on (true) or off(false)
605 template <typename ForEachBitHelper, bool check_set>
606 bool ForEachBit(const ForEachBitHelper& helper) {
607 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
608 if (bitfield_[i] == check_set)
609 if (!helper.execute(i)) return false;
610 }
611 return true;
612 }
613
614 public:
615 explicit BitField(unsigned size) : bitfield_(size, 0) {}
616
617 void Set(int i) {
618 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
619 bitfield_[i] = true;
620 }
621
622 void Unset(int i) {
623 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
624 bitfield_[i] = true;
625 }
626
627 bool IsSet(int i) const { return bitfield_[i]; }
628
629 // For each bit not set in the bitfield call the execute functor
630 // execute.
631 // ForEachBitSetHelper::execute returns true if the iteration through
632 // the bits can continue, otherwise it will stop.
633 // struct ForEachBitSetHelper {
634 // bool execute(int /*id*/) { return false; }
635 // };
636 template <typename ForEachBitNotSetHelper>
637 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
638 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
639 }
640
641 // For each bit set in the bitfield call the execute functor
642 // execute.
643 template <typename ForEachBitSetHelper>
644 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
645 return ForEachBit<ForEachBitSetHelper, true>(helper);
646 }
647
648 private:
649 std::vector<bool> bitfield_;
650};
651
652typedef int64_t Int64;
653class Uint64;
654class Uint128;
655
656class Uint32 {
657 uint32_t data_;
658
659 public:
660 // Unlike uint32_t, Uint32 has a default constructor.
661 Uint32() { data_ = 0; }
662 explicit Uint32(uint32_t data) : data_(data) {}
663 inline explicit Uint32(Uint64 data);
664 uint32_t Get() const { return data_; }
665 template <int N>
666 int32_t GetSigned() const {
667 return ExtractSignedBitfield32(N - 1, 0, data_);
668 }
669 int32_t GetSigned() const { return data_; }
670 Uint32 operator~() const { return Uint32(~data_); }
671 Uint32 operator-() const { return Uint32(-data_); }
672 bool operator==(Uint32 value) const { return data_ == value.data_; }
673 bool operator!=(Uint32 value) const { return data_ != value.data_; }
674 bool operator>(Uint32 value) const { return data_ > value.data_; }
675 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
676 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
677 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
678 Uint32 operator&=(Uint32 value) {
679 data_ &= value.data_;
680 return *this;
681 }
682 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
683 Uint32 operator^=(Uint32 value) {
684 data_ ^= value.data_;
685 return *this;
686 }
687 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
688 Uint32 operator|=(Uint32 value) {
689 data_ |= value.data_;
690 return *this;
691 }
692 // Unlike uint32_t, the shift functions can accept negative shift and
693 // return 0 when the shift is too big.
694 Uint32 operator>>(int shift) const {
695 if (shift == 0) return *this;
696 if (shift < 0) {
697 int tmp = -shift;
698 if (tmp >= 32) return Uint32(0);
699 return Uint32(data_ << tmp);
700 }
701 int tmp = shift;
702 if (tmp >= 32) return Uint32(0);
703 return Uint32(data_ >> tmp);
704 }
705 Uint32 operator<<(int shift) const {
706 if (shift == 0) return *this;
707 if (shift < 0) {
708 int tmp = -shift;
709 if (tmp >= 32) return Uint32(0);
710 return Uint32(data_ >> tmp);
711 }
712 int tmp = shift;
713 if (tmp >= 32) return Uint32(0);
714 return Uint32(data_ << tmp);
715 }
716};
717
718class Uint64 {
719 uint64_t data_;
720
721 public:
722 // Unlike uint64_t, Uint64 has a default constructor.
723 Uint64() { data_ = 0; }
724 explicit Uint64(uint64_t data) : data_(data) {}
725 explicit Uint64(Uint32 data) : data_(data.Get()) {}
726 inline explicit Uint64(Uint128 data);
727 uint64_t Get() const { return data_; }
728 int64_t GetSigned(int N) const {
729 return ExtractSignedBitfield64(N - 1, 0, data_);
730 }
731 int64_t GetSigned() const { return data_; }
732 Uint32 ToUint32() const {
733 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100734 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100735 }
736 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100737 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100738 Uint64 operator~() const { return Uint64(~data_); }
739 Uint64 operator-() const { return Uint64(-data_); }
740 bool operator==(Uint64 value) const { return data_ == value.data_; }
741 bool operator!=(Uint64 value) const { return data_ != value.data_; }
742 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
743 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
744 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
745 Uint64 operator&=(Uint64 value) {
746 data_ &= value.data_;
747 return *this;
748 }
749 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
750 Uint64 operator^=(Uint64 value) {
751 data_ ^= value.data_;
752 return *this;
753 }
754 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
755 Uint64 operator|=(Uint64 value) {
756 data_ |= value.data_;
757 return *this;
758 }
759 // Unlike uint64_t, the shift functions can accept negative shift and
760 // return 0 when the shift is too big.
761 Uint64 operator>>(int shift) const {
762 if (shift == 0) return *this;
763 if (shift < 0) {
764 int tmp = -shift;
765 if (tmp >= 64) return Uint64(0);
766 return Uint64(data_ << tmp);
767 }
768 int tmp = shift;
769 if (tmp >= 64) return Uint64(0);
770 return Uint64(data_ >> tmp);
771 }
772 Uint64 operator<<(int shift) const {
773 if (shift == 0) return *this;
774 if (shift < 0) {
775 int tmp = -shift;
776 if (tmp >= 64) return Uint64(0);
777 return Uint64(data_ >> tmp);
778 }
779 int tmp = shift;
780 if (tmp >= 64) return Uint64(0);
781 return Uint64(data_ << tmp);
782 }
783};
784
785class Uint128 {
786 uint64_t data_high_;
787 uint64_t data_low_;
788
789 public:
790 Uint128() : data_high_(0), data_low_(0) {}
791 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
792 explicit Uint128(Uint64 data_low)
793 : data_high_(0), data_low_(data_low.Get()) {}
794 Uint128(uint64_t data_high, uint64_t data_low)
795 : data_high_(data_high), data_low_(data_low) {}
796 Uint64 ToUint64() const {
797 VIXL_ASSERT(data_high_ == 0);
798 return Uint64(data_low_);
799 }
800 Uint64 GetHigh64() const { return Uint64(data_high_); }
801 Uint64 GetLow64() const { return Uint64(data_low_); }
802 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
803 bool operator==(Uint128 value) const {
804 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
805 }
806 Uint128 operator&(Uint128 value) const {
807 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
808 }
809 Uint128 operator&=(Uint128 value) {
810 data_high_ &= value.data_high_;
811 data_low_ &= value.data_low_;
812 return *this;
813 }
814 Uint128 operator|=(Uint128 value) {
815 data_high_ |= value.data_high_;
816 data_low_ |= value.data_low_;
817 return *this;
818 }
819 Uint128 operator>>(int shift) const {
820 VIXL_ASSERT((shift >= 0) && (shift < 128));
821 if (shift == 0) return *this;
822 if (shift >= 64) {
823 return Uint128(0, data_high_ >> (shift - 64));
824 }
825 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
826 return Uint128(data_high_ >> shift, tmp);
827 }
828 Uint128 operator<<(int shift) const {
829 VIXL_ASSERT((shift >= 0) && (shift < 128));
830 if (shift == 0) return *this;
831 if (shift >= 64) {
832 return Uint128(data_low_ << (shift - 64), 0);
833 }
834 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
835 return Uint128(tmp, data_low_ << shift);
836 }
837};
838
839Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
840Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
841
842Int64 BitCount(Uint32 value);
843
armvixlad96eda2013-06-14 11:42:37 +0100844} // namespace vixl
845
846#endif // VIXL_UTILS_H