blob: 947e09c85679972f58855fa569a3fb50c3e64f64 [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 Langloisefe0c1f2016-11-24 11:54:47 +000069// TODO: Refactor these using templates.
70inline bool IsIntN(unsigned n, uint32_t x) {
71 VIXL_ASSERT((0 < n) && (n < 32));
72 uint32_t limit = UINT32_C(1) << (n - 1);
73 return x < limit;
74}
75inline bool IsIntN(unsigned n, int32_t x) {
76 VIXL_ASSERT((0 < n) && (n < 32));
77 int32_t limit = INT32_C(1) << (n - 1);
78 return (-limit <= x) && (x < limit);
79}
80inline bool IsIntN(unsigned n, uint64_t x) {
81 VIXL_ASSERT((0 < n) && (n < 64));
82 uint64_t limit = UINT64_C(1) << (n - 1);
83 return x < limit;
84}
Pierre Langlois88c46b82016-06-02 18:15:32 +010085inline bool IsIntN(unsigned n, int64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000086 VIXL_ASSERT((0 < n) && (n < 64));
87 int64_t limit = INT64_C(1) << (n - 1);
armvixlad96eda2013-06-14 11:42:37 +010088 return (-limit <= x) && (x < limit);
89}
Pierre Langlois88c46b82016-06-02 18:15:32 +010090VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
91 return IsIntN(n, x);
92}
armvixlad96eda2013-06-14 11:42:37 +010093
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000094inline bool IsUintN(unsigned n, uint32_t x) {
95 VIXL_ASSERT((0 < n) && (n < 32));
96 return !(x >> n);
97}
98inline bool IsUintN(unsigned n, int32_t x) {
99 VIXL_ASSERT((0 < n) && (n < 32));
100 // Convert to an unsigned integer to avoid implementation-defined behavior.
101 return !(static_cast<uint32_t>(x) >> n);
102}
103inline bool IsUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000104 VIXL_ASSERT((0 < n) && (n < 64));
armvixlad96eda2013-06-14 11:42:37 +0100105 return !(x >> n);
106}
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000107inline bool IsUintN(unsigned n, int64_t x) {
108 VIXL_ASSERT((0 < n) && (n < 64));
109 // Convert to an unsigned integer to avoid implementation-defined behavior.
110 return !(static_cast<uint64_t>(x) >> n);
111}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100112VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
113 return IsUintN(n, x);
114}
armvixlad96eda2013-06-14 11:42:37 +0100115
Jacob Bramley3976edb2016-10-18 10:51:43 +0100116inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000117 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +0100118 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100119}
Jacob Bramley3976edb2016-10-18 10:51:43 +0100120VIXL_DEPRECATED("TruncateToUintN",
121 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
122 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100123}
armvixlad96eda2013-06-14 11:42:37 +0100124
armvixl0f35e362016-05-10 13:57:58 +0100125// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +0100126#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100127V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
128V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
129V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100130V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
131
132#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100133V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
134V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
135V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
136V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100137
Jacob Bramley3976edb2016-10-18 10:51:43 +0100138#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
139
armvixl0f35e362016-05-10 13:57:58 +0100140// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100141
Pierre Langlois88c46b82016-06-02 18:15:32 +0100142#define DECLARE_IS_INT_N(N) \
143 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
144 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
145 return IsIntN(N, x); \
146 }
147
148#define DECLARE_IS_UINT_N(N) \
149 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
150 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
151 return IsUintN(N, x); \
152 }
153
Jacob Bramley3976edb2016-10-18 10:51:43 +0100154#define DECLARE_TRUNCATE_TO_UINT_32(N) \
155 inline uint32_t TruncateToUint##N(uint64_t x) { \
156 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
157 } \
158 VIXL_DEPRECATED("TruncateToUint" #N, \
159 inline uint32_t truncate_to_int##N(int64_t x)) { \
160 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100161 }
162
armvixlad96eda2013-06-14 11:42:37 +0100163INT_1_TO_63_LIST(DECLARE_IS_INT_N)
164INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100165INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100166
armvixlad96eda2013-06-14 11:42:37 +0100167#undef DECLARE_IS_INT_N
168#undef DECLARE_IS_UINT_N
169#undef DECLARE_TRUNCATE_TO_INT_N
170
171// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100172inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000173 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
174 (msb >= lsb));
175 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100176 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100177}
178
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000179
180inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint32_t x) {
181 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
182 (msb >= lsb));
183 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100184}
185
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000186
Pierre Langlois88c46b82016-06-02 18:15:32 +0100187inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000188 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
189 (msb >= lsb));
190 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
191 // If the highest extracted bit is set, sign extend.
192 if ((temp >> (msb - lsb)) == 1) {
193 temp |= ~UINT64_C(0) << (msb - lsb);
194 }
195 int64_t result;
196 memcpy(&result, &temp, sizeof(result));
197 return result;
armvixlad96eda2013-06-14 11:42:37 +0100198}
199
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000200
201inline int32_t ExtractSignedBitfield32(int msb, int lsb, int32_t x) {
202 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
203 (msb >= lsb));
204 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
205 int32_t result;
206 memcpy(&result, &temp, sizeof(result));
207 return result;
208}
209
210
211inline uint64_t RotateRight(uint64_t value,
212 unsigned int rotate,
213 unsigned int width) {
214 VIXL_ASSERT((width > 0) && (width <= 64));
215 uint64_t width_mask = ~UINT64_C(0) >> (64 - width);
216 rotate &= 63;
217 if (rotate > 0) {
218 value &= width_mask;
219 value = (value << (width - rotate)) | (value >> rotate);
220 }
221 return value & width_mask;
222}
223
224
armvixlf37fdc02014-02-05 13:22:16 +0000225// Floating point representation.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100226uint32_t FloatToRawbits(float value);
227VIXL_DEPRECATED("FloatToRawbits",
228 inline uint32_t float_to_rawbits(float value)) {
229 return FloatToRawbits(value);
230}
armvixlad96eda2013-06-14 11:42:37 +0100231
Pierre Langlois88c46b82016-06-02 18:15:32 +0100232uint64_t DoubleToRawbits(double value);
233VIXL_DEPRECATED("DoubleToRawbits",
234 inline uint64_t double_to_rawbits(double value)) {
235 return DoubleToRawbits(value);
236}
armvixl5289c592015-03-02 13:52:04 +0000237
Pierre Langlois88c46b82016-06-02 18:15:32 +0100238float RawbitsToFloat(uint32_t bits);
239VIXL_DEPRECATED("RawbitsToFloat",
240 inline float rawbits_to_float(uint32_t bits)) {
241 return RawbitsToFloat(bits);
242}
243
244double RawbitsToDouble(uint64_t bits);
245VIXL_DEPRECATED("RawbitsToDouble",
246 inline double rawbits_to_double(uint64_t bits)) {
247 return RawbitsToDouble(bits);
248}
249
250uint32_t FloatSign(float value);
251VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
252 return FloatSign(value);
253}
254
255uint32_t FloatExp(float value);
256VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
257 return FloatExp(value);
258}
259
260uint32_t FloatMantissa(float value);
261VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
262 return FloatMantissa(value);
263}
264
265uint32_t DoubleSign(double value);
266VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
267 return DoubleSign(value);
268}
269
270uint32_t DoubleExp(double value);
271VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
272 return DoubleExp(value);
273}
274
275uint64_t DoubleMantissa(double value);
276VIXL_DEPRECATED("DoubleMantissa",
277 inline uint64_t double_mantissa(double value)) {
278 return DoubleMantissa(value);
279}
280
281float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
282VIXL_DEPRECATED("FloatPack",
283 inline float float_pack(uint32_t sign,
284 uint32_t exp,
285 uint32_t mantissa)) {
286 return FloatPack(sign, exp, mantissa);
287}
288
289double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
290VIXL_DEPRECATED("DoublePack",
291 inline double double_pack(uint32_t sign,
292 uint32_t exp,
293 uint64_t mantissa)) {
294 return DoublePack(sign, exp, mantissa);
295}
armvixl5289c592015-03-02 13:52:04 +0000296
297// An fpclassify() function for 16-bit half-precision floats.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100298int Float16Classify(float16 value);
299VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) {
300 return Float16Classify(value);
301}
armvixlf37fdc02014-02-05 13:22:16 +0000302
303// NaN tests.
304inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100305 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100306 uint64_t raw = DoubleToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100307 if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000308 return true;
309 }
310 return false;
311}
312
313
314inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000315 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100316 uint32_t raw = FloatToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100317 if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000318 return true;
319 }
320 return false;
321}
322
323
armvixl5289c592015-03-02 13:52:04 +0000324inline bool IsSignallingNaN(float16 num) {
325 const uint16_t kFP16QuietNaNMask = 0x0200;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100326 return (Float16Classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000327}
328
329
armvixlf37fdc02014-02-05 13:22:16 +0000330template <typename T>
331inline bool IsQuietNaN(T num) {
armvixl6e2c8272015-03-31 11:04:14 +0100332 return std::isnan(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000333}
334
335
armvixlb0c8ae22014-03-21 14:03:59 +0000336// Convert the NaN in 'num' to a quiet NaN.
337inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100338 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
armvixl6e2c8272015-03-31 11:04:14 +0100339 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100340 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000341}
342
343
344inline float ToQuietNaN(float num) {
345 const uint32_t kFP32QuietNaNMask = 0x00400000;
armvixl6e2c8272015-03-31 11:04:14 +0100346 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100347 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000348}
349
350
351// Fused multiply-add.
352inline double FusedMultiplyAdd(double op1, double op2, double a) {
353 return fma(op1, op2, a);
354}
355
356
357inline float FusedMultiplyAdd(float op1, float op2, float a) {
358 return fmaf(op1, op2, a);
359}
360
361
armvixl0f35e362016-05-10 13:57:58 +0100362inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100363
364
armvixl0f35e362016-05-10 13:57:58 +0100365template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100366inline int HighestSetBitPosition(T value) {
367 VIXL_ASSERT(value != 0);
368 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
369}
370
371
armvixl0f35e362016-05-10 13:57:58 +0100372template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100373inline int WhichPowerOf2(V value) {
374 VIXL_ASSERT(IsPowerOf2(value));
375 return CountTrailingZeros(value);
376}
armvixlad96eda2013-06-14 11:42:37 +0100377
armvixldb644342015-07-21 11:37:10 +0100378
armvixl330dc712014-11-25 10:38:32 +0000379unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
380
armvixldb644342015-07-21 11:37:10 +0100381
Pierre Langlois88c46b82016-06-02 18:15:32 +0100382int BitCount(uint64_t value);
383
384
armvixldb644342015-07-21 11:37:10 +0100385template <typename T>
386T ReverseBits(T value) {
387 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
388 (sizeof(value) == 4) || (sizeof(value) == 8));
389 T result = 0;
390 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
391 result = (result << 1) | (value & 1);
392 value >>= 1;
393 }
394 return result;
395}
396
397
398template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100399inline T SignExtend(T val, int bitSize) {
400 VIXL_ASSERT(bitSize > 0);
401 T mask = (T(2) << (bitSize - 1)) - T(1);
402 val &= mask;
403 T sign = -(val >> (bitSize - 1));
404 val |= (sign << bitSize);
405 return val;
406}
407
408
409template <typename T>
armvixldb644342015-07-21 11:37:10 +0100410T ReverseBytes(T value, int block_bytes_log2) {
411 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
412 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
413 // Split the 64-bit value into an 8-bit array, where b[0] is the least
414 // significant byte, and b[7] is the most significant.
415 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000416 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100417 for (int i = 7; i >= 0; i--) {
418 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
419 mask >>= 8;
420 }
421
422 // Permutation tables for REV instructions.
423 // permute_table[0] is used by REV16_x, REV16_w
424 // permute_table[1] is used by REV32_x, REV_w
425 // permute_table[2] is used by REV_x
426 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100427 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
428 {4, 5, 6, 7, 0, 1, 2, 3},
429 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000430 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100431 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000432 temp <<= 8;
433 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100434 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000435
436 T result;
437 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
438 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100439 return result;
440}
441
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700442template <unsigned MULTIPLE, typename T>
443inline bool IsMultiple(T value) {
444 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
445 return (value & (MULTIPLE - 1)) == 0;
446}
armvixldb644342015-07-21 11:37:10 +0100447
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000448template <typename T>
449inline bool IsMultiple(T value, unsigned multiple) {
450 VIXL_ASSERT(IsPowerOf2(multiple));
451 return (value & (multiple - 1)) == 0;
452}
453
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000454template <typename T>
455inline bool IsAligned(T pointer, int alignment) {
456 VIXL_ASSERT(IsPowerOf2(alignment));
457 return (pointer & (alignment - 1)) == 0;
458}
459
armvixlad96eda2013-06-14 11:42:37 +0100460// Pointer alignment
461// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100462template <unsigned ALIGN, typename T>
463inline bool IsAligned(T pointer) {
464 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100465 // Use C-style casts to get static_cast behaviour for integral types (T), and
466 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000467 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100468}
469
armvixl0f35e362016-05-10 13:57:58 +0100470template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100471bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100472 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100473}
474
armvixl330dc712014-11-25 10:38:32 +0000475// Increment a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100476template <class T>
armvixlad96eda2013-06-14 11:42:37 +0100477T AlignUp(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100478 // Use C-style casts to get static_cast behaviour for integral types (T), and
479 // reinterpret_cast behaviour for other types.
480
armvixl330dc712014-11-25 10:38:32 +0000481 uint64_t pointer_raw = (uint64_t)pointer;
482 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100483
armvixlad96eda2013-06-14 11:42:37 +0100484 size_t align_step = (alignment - pointer_raw) % alignment;
armvixlb0c8ae22014-03-21 14:03:59 +0000485 VIXL_ASSERT((pointer_raw + align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100486
Alexandre Rames47ed2652016-11-09 14:44:06 +0000487 T result = (T)(pointer_raw + align_step);
488
489 VIXL_ASSERT(result >= pointer);
490
491 return result;
armvixlad96eda2013-06-14 11:42:37 +0100492}
493
armvixl330dc712014-11-25 10:38:32 +0000494// Decrement a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100495template <class T>
armvixlb0c8ae22014-03-21 14:03:59 +0000496T AlignDown(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100497 // Use C-style casts to get static_cast behaviour for integral types (T), and
498 // reinterpret_cast behaviour for other types.
499
armvixl330dc712014-11-25 10:38:32 +0000500 uint64_t pointer_raw = (uint64_t)pointer;
501 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100502
armvixlb0c8ae22014-03-21 14:03:59 +0000503 size_t align_step = pointer_raw % alignment;
504 VIXL_ASSERT((pointer_raw - align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100505
506 return (T)(pointer_raw - align_step);
armvixlb0c8ae22014-03-21 14:03:59 +0000507}
508
Pierre Langlois88c46b82016-06-02 18:15:32 +0100509template <typename T>
510inline T ExtractBit(T value, unsigned bit) {
511 return (value >> bit) & T(1);
512}
513
514template <typename Ts, typename Td>
515inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
516 return Td((value >> least_significant_bit) & Ts(mask));
517}
518
519template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800520inline void AssignBit(Td& dst, // NOLINT(runtime/references)
521 int bit,
522 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100523 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
524 VIXL_ASSERT(bit >= 0);
525 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
526 Td mask(1);
527 dst &= ~(mask << bit);
528 dst |= Td(value) << bit;
529}
530
531template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800532inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100533 int least_significant_bit,
534 Ts mask,
535 Ts value) {
536 VIXL_ASSERT(least_significant_bit >= 0);
537 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
538 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
539 Td(mask));
540 VIXL_ASSERT((value & mask) == value);
541 dst &= ~(Td(mask) << least_significant_bit);
542 dst |= Td(value) << least_significant_bit;
543}
544
545class VFP {
546 public:
547 static uint32_t FP32ToImm8(float imm) {
548 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
549 uint32_t bits = FloatToRawbits(imm);
550 // bit7: a000.0000
551 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
552 // bit6: 0b00.0000
553 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
554 // bit5_to_0: 00cd.efgh
555 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
556 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
557 }
558 static uint32_t FP64ToImm8(double imm) {
559 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
560 // 0000.0000.0000.0000.0000.0000.0000.0000
561 uint64_t bits = DoubleToRawbits(imm);
562 // bit7: a000.0000
563 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
564 // bit6: 0b00.0000
565 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
566 // bit5_to_0: 00cd.efgh
567 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
568
569 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
570 }
571 static float Imm8ToFP32(uint32_t imm8) {
572 // Imm8: abcdefgh (8 bits)
573 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
574 // where B is b ^ 1
575 uint32_t bits = imm8;
576 uint32_t bit7 = (bits >> 7) & 0x1;
577 uint32_t bit6 = (bits >> 6) & 0x1;
578 uint32_t bit5_to_0 = bits & 0x3f;
579 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
580
581 return RawbitsToFloat(result);
582 }
583 static double Imm8ToFP64(uint32_t imm8) {
584 // Imm8: abcdefgh (8 bits)
585 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
586 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
587 // where B is b ^ 1
588 uint32_t bits = imm8;
589 uint64_t bit7 = (bits >> 7) & 0x1;
590 uint64_t bit6 = (bits >> 6) & 0x1;
591 uint64_t bit5_to_0 = bits & 0x3f;
592 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
593 return RawbitsToDouble(result);
594 }
595 static bool IsImmFP32(float imm) {
596 // Valid values will have the form:
597 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
598 uint32_t bits = FloatToRawbits(imm);
599 // bits[19..0] are cleared.
600 if ((bits & 0x7ffff) != 0) {
601 return false;
602 }
603
604
605 // bits[29..25] are all set or all cleared.
606 uint32_t b_pattern = (bits >> 16) & 0x3e00;
607 if (b_pattern != 0 && b_pattern != 0x3e00) {
608 return false;
609 }
610 // bit[30] and bit[29] are opposite.
611 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
612 return false;
613 }
614 return true;
615 }
616 static bool IsImmFP64(double imm) {
617 // Valid values will have the form:
618 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
619 // 0000.0000.0000.0000.0000.0000.0000.0000
620 uint64_t bits = DoubleToRawbits(imm);
621 // bits[47..0] are cleared.
622 if ((bits & 0x0000ffffffffffff) != 0) {
623 return false;
624 }
625 // bits[61..54] are all set or all cleared.
626 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
627 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
628 return false;
629 }
630 // bit[62] and bit[61] are opposite.
631 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
632 return false;
633 }
634 return true;
635 }
636};
637
638class BitField {
639 // ForEachBitHelper is a functor that will call
640 // bool ForEachBitHelper::execute(ElementType id) const
641 // and expects a boolean in return whether to continue (if true)
642 // or stop (if false)
643 // check_set will check if the bits are on (true) or off(false)
644 template <typename ForEachBitHelper, bool check_set>
645 bool ForEachBit(const ForEachBitHelper& helper) {
646 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
647 if (bitfield_[i] == check_set)
648 if (!helper.execute(i)) return false;
649 }
650 return true;
651 }
652
653 public:
654 explicit BitField(unsigned size) : bitfield_(size, 0) {}
655
656 void Set(int i) {
657 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
658 bitfield_[i] = true;
659 }
660
661 void Unset(int i) {
662 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
663 bitfield_[i] = true;
664 }
665
666 bool IsSet(int i) const { return bitfield_[i]; }
667
668 // For each bit not set in the bitfield call the execute functor
669 // execute.
670 // ForEachBitSetHelper::execute returns true if the iteration through
671 // the bits can continue, otherwise it will stop.
672 // struct ForEachBitSetHelper {
673 // bool execute(int /*id*/) { return false; }
674 // };
675 template <typename ForEachBitNotSetHelper>
676 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
677 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
678 }
679
680 // For each bit set in the bitfield call the execute functor
681 // execute.
682 template <typename ForEachBitSetHelper>
683 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
684 return ForEachBit<ForEachBitSetHelper, true>(helper);
685 }
686
687 private:
688 std::vector<bool> bitfield_;
689};
690
691typedef int64_t Int64;
692class Uint64;
693class Uint128;
694
695class Uint32 {
696 uint32_t data_;
697
698 public:
699 // Unlike uint32_t, Uint32 has a default constructor.
700 Uint32() { data_ = 0; }
701 explicit Uint32(uint32_t data) : data_(data) {}
702 inline explicit Uint32(Uint64 data);
703 uint32_t Get() const { return data_; }
704 template <int N>
705 int32_t GetSigned() const {
706 return ExtractSignedBitfield32(N - 1, 0, data_);
707 }
708 int32_t GetSigned() const { return data_; }
709 Uint32 operator~() const { return Uint32(~data_); }
710 Uint32 operator-() const { return Uint32(-data_); }
711 bool operator==(Uint32 value) const { return data_ == value.data_; }
712 bool operator!=(Uint32 value) const { return data_ != value.data_; }
713 bool operator>(Uint32 value) const { return data_ > value.data_; }
714 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
715 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
716 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
717 Uint32 operator&=(Uint32 value) {
718 data_ &= value.data_;
719 return *this;
720 }
721 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
722 Uint32 operator^=(Uint32 value) {
723 data_ ^= value.data_;
724 return *this;
725 }
726 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
727 Uint32 operator|=(Uint32 value) {
728 data_ |= value.data_;
729 return *this;
730 }
731 // Unlike uint32_t, the shift functions can accept negative shift and
732 // return 0 when the shift is too big.
733 Uint32 operator>>(int shift) const {
734 if (shift == 0) return *this;
735 if (shift < 0) {
736 int tmp = -shift;
737 if (tmp >= 32) return Uint32(0);
738 return Uint32(data_ << tmp);
739 }
740 int tmp = shift;
741 if (tmp >= 32) return Uint32(0);
742 return Uint32(data_ >> tmp);
743 }
744 Uint32 operator<<(int shift) const {
745 if (shift == 0) return *this;
746 if (shift < 0) {
747 int tmp = -shift;
748 if (tmp >= 32) return Uint32(0);
749 return Uint32(data_ >> tmp);
750 }
751 int tmp = shift;
752 if (tmp >= 32) return Uint32(0);
753 return Uint32(data_ << tmp);
754 }
755};
756
757class Uint64 {
758 uint64_t data_;
759
760 public:
761 // Unlike uint64_t, Uint64 has a default constructor.
762 Uint64() { data_ = 0; }
763 explicit Uint64(uint64_t data) : data_(data) {}
764 explicit Uint64(Uint32 data) : data_(data.Get()) {}
765 inline explicit Uint64(Uint128 data);
766 uint64_t Get() const { return data_; }
767 int64_t GetSigned(int N) const {
768 return ExtractSignedBitfield64(N - 1, 0, data_);
769 }
770 int64_t GetSigned() const { return data_; }
771 Uint32 ToUint32() const {
772 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100773 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100774 }
775 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100776 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100777 Uint64 operator~() const { return Uint64(~data_); }
778 Uint64 operator-() const { return Uint64(-data_); }
779 bool operator==(Uint64 value) const { return data_ == value.data_; }
780 bool operator!=(Uint64 value) const { return data_ != value.data_; }
781 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
782 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
783 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
784 Uint64 operator&=(Uint64 value) {
785 data_ &= value.data_;
786 return *this;
787 }
788 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
789 Uint64 operator^=(Uint64 value) {
790 data_ ^= value.data_;
791 return *this;
792 }
793 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
794 Uint64 operator|=(Uint64 value) {
795 data_ |= value.data_;
796 return *this;
797 }
798 // Unlike uint64_t, the shift functions can accept negative shift and
799 // return 0 when the shift is too big.
800 Uint64 operator>>(int shift) const {
801 if (shift == 0) return *this;
802 if (shift < 0) {
803 int tmp = -shift;
804 if (tmp >= 64) return Uint64(0);
805 return Uint64(data_ << tmp);
806 }
807 int tmp = shift;
808 if (tmp >= 64) return Uint64(0);
809 return Uint64(data_ >> tmp);
810 }
811 Uint64 operator<<(int shift) const {
812 if (shift == 0) return *this;
813 if (shift < 0) {
814 int tmp = -shift;
815 if (tmp >= 64) return Uint64(0);
816 return Uint64(data_ >> tmp);
817 }
818 int tmp = shift;
819 if (tmp >= 64) return Uint64(0);
820 return Uint64(data_ << tmp);
821 }
822};
823
824class Uint128 {
825 uint64_t data_high_;
826 uint64_t data_low_;
827
828 public:
829 Uint128() : data_high_(0), data_low_(0) {}
830 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
831 explicit Uint128(Uint64 data_low)
832 : data_high_(0), data_low_(data_low.Get()) {}
833 Uint128(uint64_t data_high, uint64_t data_low)
834 : data_high_(data_high), data_low_(data_low) {}
835 Uint64 ToUint64() const {
836 VIXL_ASSERT(data_high_ == 0);
837 return Uint64(data_low_);
838 }
839 Uint64 GetHigh64() const { return Uint64(data_high_); }
840 Uint64 GetLow64() const { return Uint64(data_low_); }
841 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
842 bool operator==(Uint128 value) const {
843 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
844 }
845 Uint128 operator&(Uint128 value) const {
846 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
847 }
848 Uint128 operator&=(Uint128 value) {
849 data_high_ &= value.data_high_;
850 data_low_ &= value.data_low_;
851 return *this;
852 }
853 Uint128 operator|=(Uint128 value) {
854 data_high_ |= value.data_high_;
855 data_low_ |= value.data_low_;
856 return *this;
857 }
858 Uint128 operator>>(int shift) const {
859 VIXL_ASSERT((shift >= 0) && (shift < 128));
860 if (shift == 0) return *this;
861 if (shift >= 64) {
862 return Uint128(0, data_high_ >> (shift - 64));
863 }
864 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
865 return Uint128(data_high_ >> shift, tmp);
866 }
867 Uint128 operator<<(int shift) const {
868 VIXL_ASSERT((shift >= 0) && (shift < 128));
869 if (shift == 0) return *this;
870 if (shift >= 64) {
871 return Uint128(data_low_ << (shift - 64), 0);
872 }
873 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
874 return Uint128(tmp, data_low_ << shift);
875 }
876};
877
878Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
879Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
880
881Int64 BitCount(Uint32 value);
882
armvixlad96eda2013-06-14 11:42:37 +0100883} // namespace vixl
884
885#endif // VIXL_UTILS_H