blob: c4ba800364e9dc0e69d78590d84974ec0b4fe684 [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.
Carey Williamsd8bb3572018-04-10 11:58:07 +0100226uint16_t Float16ToRawbits(float16 value);
227
228
Pierre Langlois88c46b82016-06-02 18:15:32 +0100229uint32_t FloatToRawbits(float value);
230VIXL_DEPRECATED("FloatToRawbits",
231 inline uint32_t float_to_rawbits(float value)) {
232 return FloatToRawbits(value);
233}
armvixlad96eda2013-06-14 11:42:37 +0100234
Pierre Langlois88c46b82016-06-02 18:15:32 +0100235uint64_t DoubleToRawbits(double value);
236VIXL_DEPRECATED("DoubleToRawbits",
237 inline uint64_t double_to_rawbits(double value)) {
238 return DoubleToRawbits(value);
239}
armvixl5289c592015-03-02 13:52:04 +0000240
Carey Williamsd8bb3572018-04-10 11:58:07 +0100241float16 RawbitsToFloat16(uint16_t bits);
242
Pierre Langlois88c46b82016-06-02 18:15:32 +0100243float RawbitsToFloat(uint32_t bits);
244VIXL_DEPRECATED("RawbitsToFloat",
245 inline float rawbits_to_float(uint32_t bits)) {
246 return RawbitsToFloat(bits);
247}
248
249double RawbitsToDouble(uint64_t bits);
250VIXL_DEPRECATED("RawbitsToDouble",
251 inline double rawbits_to_double(uint64_t bits)) {
252 return RawbitsToDouble(bits);
253}
254
255uint32_t FloatSign(float value);
256VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
257 return FloatSign(value);
258}
259
260uint32_t FloatExp(float value);
261VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
262 return FloatExp(value);
263}
264
265uint32_t FloatMantissa(float value);
266VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
267 return FloatMantissa(value);
268}
269
270uint32_t DoubleSign(double value);
271VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
272 return DoubleSign(value);
273}
274
275uint32_t DoubleExp(double value);
276VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
277 return DoubleExp(value);
278}
279
280uint64_t DoubleMantissa(double value);
281VIXL_DEPRECATED("DoubleMantissa",
282 inline uint64_t double_mantissa(double value)) {
283 return DoubleMantissa(value);
284}
285
286float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
287VIXL_DEPRECATED("FloatPack",
288 inline float float_pack(uint32_t sign,
289 uint32_t exp,
290 uint32_t mantissa)) {
291 return FloatPack(sign, exp, mantissa);
292}
293
294double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
295VIXL_DEPRECATED("DoublePack",
296 inline double double_pack(uint32_t sign,
297 uint32_t exp,
298 uint64_t mantissa)) {
299 return DoublePack(sign, exp, mantissa);
300}
armvixl5289c592015-03-02 13:52:04 +0000301
302// An fpclassify() function for 16-bit half-precision floats.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100303int Float16Classify(float16 value);
304VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) {
305 return Float16Classify(value);
306}
armvixlf37fdc02014-02-05 13:22:16 +0000307
Carey Williamsd8bb3572018-04-10 11:58:07 +0100308
309// Check for float16 (uint16_t) NaNs.
310inline bool IsNaN(float16 value) { return Float16Classify(value) == FP_NAN; }
311
312
armvixlf37fdc02014-02-05 13:22:16 +0000313// NaN tests.
314inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100315 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100316 uint64_t raw = DoubleToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100317 if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000318 return true;
319 }
320 return false;
321}
322
323
324inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000325 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100326 uint32_t raw = FloatToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100327 if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000328 return true;
329 }
330 return false;
331}
332
333
armvixl5289c592015-03-02 13:52:04 +0000334inline bool IsSignallingNaN(float16 num) {
335 const uint16_t kFP16QuietNaNMask = 0x0200;
Carey Williamsd8bb3572018-04-10 11:58:07 +0100336 return IsNaN(num) && ((num & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000337}
338
339
armvixlf37fdc02014-02-05 13:22:16 +0000340template <typename T>
341inline bool IsQuietNaN(T num) {
armvixl6e2c8272015-03-31 11:04:14 +0100342 return std::isnan(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000343}
344
345
armvixlb0c8ae22014-03-21 14:03:59 +0000346// Convert the NaN in 'num' to a quiet NaN.
347inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100348 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
armvixl6e2c8272015-03-31 11:04:14 +0100349 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100350 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000351}
352
353
354inline float ToQuietNaN(float num) {
355 const uint32_t kFP32QuietNaNMask = 0x00400000;
armvixl6e2c8272015-03-31 11:04:14 +0100356 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100357 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000358}
359
360
361// Fused multiply-add.
362inline double FusedMultiplyAdd(double op1, double op2, double a) {
363 return fma(op1, op2, a);
364}
365
366
367inline float FusedMultiplyAdd(float op1, float op2, float a) {
368 return fmaf(op1, op2, a);
369}
370
371
armvixl0f35e362016-05-10 13:57:58 +0100372inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100373
374
armvixl0f35e362016-05-10 13:57:58 +0100375template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100376inline int HighestSetBitPosition(T value) {
377 VIXL_ASSERT(value != 0);
378 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
379}
380
381
armvixl0f35e362016-05-10 13:57:58 +0100382template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100383inline int WhichPowerOf2(V value) {
384 VIXL_ASSERT(IsPowerOf2(value));
385 return CountTrailingZeros(value);
386}
armvixlad96eda2013-06-14 11:42:37 +0100387
armvixldb644342015-07-21 11:37:10 +0100388
armvixl330dc712014-11-25 10:38:32 +0000389unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
390
armvixldb644342015-07-21 11:37:10 +0100391
Pierre Langlois88c46b82016-06-02 18:15:32 +0100392int BitCount(uint64_t value);
393
394
armvixldb644342015-07-21 11:37:10 +0100395template <typename T>
396T ReverseBits(T value) {
397 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
398 (sizeof(value) == 4) || (sizeof(value) == 8));
399 T result = 0;
400 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
401 result = (result << 1) | (value & 1);
402 value >>= 1;
403 }
404 return result;
405}
406
407
408template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100409inline T SignExtend(T val, int bitSize) {
410 VIXL_ASSERT(bitSize > 0);
411 T mask = (T(2) << (bitSize - 1)) - T(1);
412 val &= mask;
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700413 T sign_bits = -((val >> (bitSize - 1)) << bitSize);
414 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100415 return val;
416}
417
418
419template <typename T>
armvixldb644342015-07-21 11:37:10 +0100420T ReverseBytes(T value, int block_bytes_log2) {
421 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
422 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
423 // Split the 64-bit value into an 8-bit array, where b[0] is the least
424 // significant byte, and b[7] is the most significant.
425 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000426 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100427 for (int i = 7; i >= 0; i--) {
428 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
429 mask >>= 8;
430 }
431
432 // Permutation tables for REV instructions.
433 // permute_table[0] is used by REV16_x, REV16_w
434 // permute_table[1] is used by REV32_x, REV_w
435 // permute_table[2] is used by REV_x
436 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100437 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
438 {4, 5, 6, 7, 0, 1, 2, 3},
439 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000440 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100441 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000442 temp <<= 8;
443 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100444 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000445
446 T result;
447 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
448 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100449 return result;
450}
451
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700452template <unsigned MULTIPLE, typename T>
453inline bool IsMultiple(T value) {
454 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
455 return (value & (MULTIPLE - 1)) == 0;
456}
armvixldb644342015-07-21 11:37:10 +0100457
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000458template <typename T>
459inline bool IsMultiple(T value, unsigned multiple) {
460 VIXL_ASSERT(IsPowerOf2(multiple));
461 return (value & (multiple - 1)) == 0;
462}
463
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000464template <typename T>
465inline bool IsAligned(T pointer, int alignment) {
466 VIXL_ASSERT(IsPowerOf2(alignment));
467 return (pointer & (alignment - 1)) == 0;
468}
469
armvixlad96eda2013-06-14 11:42:37 +0100470// Pointer alignment
471// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100472template <unsigned ALIGN, typename T>
473inline bool IsAligned(T pointer) {
474 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100475 // Use C-style casts to get static_cast behaviour for integral types (T), and
476 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000477 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100478}
479
armvixl0f35e362016-05-10 13:57:58 +0100480template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100481bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100482 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100483}
484
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100485// Increment a pointer until it has the specified alignment. The alignment must
486// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100487template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100488T AlignUp(T pointer,
489 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100490 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100491 // Use C-style casts to get static_cast behaviour for integral types (T), and
492 // reinterpret_cast behaviour for other types.
493
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100494 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
495 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000496 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100497
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100498 size_t mask = alignment - 1;
499 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000500 VIXL_ASSERT(result >= pointer);
501
502 return result;
armvixlad96eda2013-06-14 11:42:37 +0100503}
504
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100505// Decrement a pointer until it has the specified alignment. The alignment must
506// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100507template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100508T AlignDown(T pointer,
509 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100510 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100511 // Use C-style casts to get static_cast behaviour for integral types (T), and
512 // reinterpret_cast behaviour for other types.
513
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100514 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
515 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000516 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100517
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100518 size_t mask = alignment - 1;
519 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000520}
521
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100522
Pierre Langlois88c46b82016-06-02 18:15:32 +0100523template <typename T>
524inline T ExtractBit(T value, unsigned bit) {
525 return (value >> bit) & T(1);
526}
527
528template <typename Ts, typename Td>
529inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
530 return Td((value >> least_significant_bit) & Ts(mask));
531}
532
533template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800534inline void AssignBit(Td& dst, // NOLINT(runtime/references)
535 int bit,
536 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100537 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
538 VIXL_ASSERT(bit >= 0);
539 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
540 Td mask(1);
541 dst &= ~(mask << bit);
542 dst |= Td(value) << bit;
543}
544
545template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800546inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100547 int least_significant_bit,
548 Ts mask,
549 Ts value) {
550 VIXL_ASSERT(least_significant_bit >= 0);
551 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
552 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
553 Td(mask));
554 VIXL_ASSERT((value & mask) == value);
555 dst &= ~(Td(mask) << least_significant_bit);
556 dst |= Td(value) << least_significant_bit;
557}
558
559class VFP {
560 public:
561 static uint32_t FP32ToImm8(float imm) {
562 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
563 uint32_t bits = FloatToRawbits(imm);
564 // bit7: a000.0000
565 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
566 // bit6: 0b00.0000
567 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
568 // bit5_to_0: 00cd.efgh
569 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
570 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
571 }
572 static uint32_t FP64ToImm8(double imm) {
573 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
574 // 0000.0000.0000.0000.0000.0000.0000.0000
575 uint64_t bits = DoubleToRawbits(imm);
576 // bit7: a000.0000
577 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
578 // bit6: 0b00.0000
579 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
580 // bit5_to_0: 00cd.efgh
581 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
582
583 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
584 }
585 static float Imm8ToFP32(uint32_t imm8) {
586 // Imm8: abcdefgh (8 bits)
587 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
588 // where B is b ^ 1
589 uint32_t bits = imm8;
590 uint32_t bit7 = (bits >> 7) & 0x1;
591 uint32_t bit6 = (bits >> 6) & 0x1;
592 uint32_t bit5_to_0 = bits & 0x3f;
593 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
594
595 return RawbitsToFloat(result);
596 }
597 static double Imm8ToFP64(uint32_t imm8) {
598 // Imm8: abcdefgh (8 bits)
599 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
600 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
601 // where B is b ^ 1
602 uint32_t bits = imm8;
603 uint64_t bit7 = (bits >> 7) & 0x1;
604 uint64_t bit6 = (bits >> 6) & 0x1;
605 uint64_t bit5_to_0 = bits & 0x3f;
606 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
607 return RawbitsToDouble(result);
608 }
609 static bool IsImmFP32(float imm) {
610 // Valid values will have the form:
611 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
612 uint32_t bits = FloatToRawbits(imm);
613 // bits[19..0] are cleared.
614 if ((bits & 0x7ffff) != 0) {
615 return false;
616 }
617
618
619 // bits[29..25] are all set or all cleared.
620 uint32_t b_pattern = (bits >> 16) & 0x3e00;
621 if (b_pattern != 0 && b_pattern != 0x3e00) {
622 return false;
623 }
624 // bit[30] and bit[29] are opposite.
625 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
626 return false;
627 }
628 return true;
629 }
630 static bool IsImmFP64(double imm) {
631 // Valid values will have the form:
632 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
633 // 0000.0000.0000.0000.0000.0000.0000.0000
634 uint64_t bits = DoubleToRawbits(imm);
635 // bits[47..0] are cleared.
636 if ((bits & 0x0000ffffffffffff) != 0) {
637 return false;
638 }
639 // bits[61..54] are all set or all cleared.
640 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
641 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
642 return false;
643 }
644 // bit[62] and bit[61] are opposite.
645 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
646 return false;
647 }
648 return true;
649 }
650};
651
652class BitField {
653 // ForEachBitHelper is a functor that will call
654 // bool ForEachBitHelper::execute(ElementType id) const
655 // and expects a boolean in return whether to continue (if true)
656 // or stop (if false)
657 // check_set will check if the bits are on (true) or off(false)
658 template <typename ForEachBitHelper, bool check_set>
659 bool ForEachBit(const ForEachBitHelper& helper) {
660 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
661 if (bitfield_[i] == check_set)
662 if (!helper.execute(i)) return false;
663 }
664 return true;
665 }
666
667 public:
668 explicit BitField(unsigned size) : bitfield_(size, 0) {}
669
670 void Set(int i) {
671 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
672 bitfield_[i] = true;
673 }
674
675 void Unset(int i) {
676 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
677 bitfield_[i] = true;
678 }
679
680 bool IsSet(int i) const { return bitfield_[i]; }
681
682 // For each bit not set in the bitfield call the execute functor
683 // execute.
684 // ForEachBitSetHelper::execute returns true if the iteration through
685 // the bits can continue, otherwise it will stop.
686 // struct ForEachBitSetHelper {
687 // bool execute(int /*id*/) { return false; }
688 // };
689 template <typename ForEachBitNotSetHelper>
690 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
691 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
692 }
693
694 // For each bit set in the bitfield call the execute functor
695 // execute.
696 template <typename ForEachBitSetHelper>
697 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
698 return ForEachBit<ForEachBitSetHelper, true>(helper);
699 }
700
701 private:
702 std::vector<bool> bitfield_;
703};
704
Pierre Langloisd82faf62018-04-04 13:06:58 +0100705namespace internal {
706
Pierre Langlois88c46b82016-06-02 18:15:32 +0100707typedef int64_t Int64;
708class Uint64;
709class Uint128;
710
711class Uint32 {
712 uint32_t data_;
713
714 public:
715 // Unlike uint32_t, Uint32 has a default constructor.
716 Uint32() { data_ = 0; }
717 explicit Uint32(uint32_t data) : data_(data) {}
718 inline explicit Uint32(Uint64 data);
719 uint32_t Get() const { return data_; }
720 template <int N>
721 int32_t GetSigned() const {
722 return ExtractSignedBitfield32(N - 1, 0, data_);
723 }
724 int32_t GetSigned() const { return data_; }
725 Uint32 operator~() const { return Uint32(~data_); }
726 Uint32 operator-() const { return Uint32(-data_); }
727 bool operator==(Uint32 value) const { return data_ == value.data_; }
728 bool operator!=(Uint32 value) const { return data_ != value.data_; }
729 bool operator>(Uint32 value) const { return data_ > value.data_; }
730 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
731 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
732 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
733 Uint32 operator&=(Uint32 value) {
734 data_ &= value.data_;
735 return *this;
736 }
737 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
738 Uint32 operator^=(Uint32 value) {
739 data_ ^= value.data_;
740 return *this;
741 }
742 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
743 Uint32 operator|=(Uint32 value) {
744 data_ |= value.data_;
745 return *this;
746 }
747 // Unlike uint32_t, the shift functions can accept negative shift and
748 // return 0 when the shift is too big.
749 Uint32 operator>>(int shift) const {
750 if (shift == 0) return *this;
751 if (shift < 0) {
752 int tmp = -shift;
753 if (tmp >= 32) return Uint32(0);
754 return Uint32(data_ << tmp);
755 }
756 int tmp = shift;
757 if (tmp >= 32) return Uint32(0);
758 return Uint32(data_ >> tmp);
759 }
760 Uint32 operator<<(int shift) const {
761 if (shift == 0) return *this;
762 if (shift < 0) {
763 int tmp = -shift;
764 if (tmp >= 32) return Uint32(0);
765 return Uint32(data_ >> tmp);
766 }
767 int tmp = shift;
768 if (tmp >= 32) return Uint32(0);
769 return Uint32(data_ << tmp);
770 }
771};
772
773class Uint64 {
774 uint64_t data_;
775
776 public:
777 // Unlike uint64_t, Uint64 has a default constructor.
778 Uint64() { data_ = 0; }
779 explicit Uint64(uint64_t data) : data_(data) {}
780 explicit Uint64(Uint32 data) : data_(data.Get()) {}
781 inline explicit Uint64(Uint128 data);
782 uint64_t Get() const { return data_; }
783 int64_t GetSigned(int N) const {
784 return ExtractSignedBitfield64(N - 1, 0, data_);
785 }
786 int64_t GetSigned() const { return data_; }
787 Uint32 ToUint32() const {
788 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100789 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100790 }
791 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100792 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100793 Uint64 operator~() const { return Uint64(~data_); }
794 Uint64 operator-() const { return Uint64(-data_); }
795 bool operator==(Uint64 value) const { return data_ == value.data_; }
796 bool operator!=(Uint64 value) const { return data_ != value.data_; }
797 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
798 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
799 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
800 Uint64 operator&=(Uint64 value) {
801 data_ &= value.data_;
802 return *this;
803 }
804 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
805 Uint64 operator^=(Uint64 value) {
806 data_ ^= value.data_;
807 return *this;
808 }
809 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
810 Uint64 operator|=(Uint64 value) {
811 data_ |= value.data_;
812 return *this;
813 }
814 // Unlike uint64_t, the shift functions can accept negative shift and
815 // return 0 when the shift is too big.
816 Uint64 operator>>(int shift) const {
817 if (shift == 0) return *this;
818 if (shift < 0) {
819 int tmp = -shift;
820 if (tmp >= 64) return Uint64(0);
821 return Uint64(data_ << tmp);
822 }
823 int tmp = shift;
824 if (tmp >= 64) return Uint64(0);
825 return Uint64(data_ >> tmp);
826 }
827 Uint64 operator<<(int shift) const {
828 if (shift == 0) return *this;
829 if (shift < 0) {
830 int tmp = -shift;
831 if (tmp >= 64) return Uint64(0);
832 return Uint64(data_ >> tmp);
833 }
834 int tmp = shift;
835 if (tmp >= 64) return Uint64(0);
836 return Uint64(data_ << tmp);
837 }
838};
839
840class Uint128 {
841 uint64_t data_high_;
842 uint64_t data_low_;
843
844 public:
845 Uint128() : data_high_(0), data_low_(0) {}
846 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
847 explicit Uint128(Uint64 data_low)
848 : data_high_(0), data_low_(data_low.Get()) {}
849 Uint128(uint64_t data_high, uint64_t data_low)
850 : data_high_(data_high), data_low_(data_low) {}
851 Uint64 ToUint64() const {
852 VIXL_ASSERT(data_high_ == 0);
853 return Uint64(data_low_);
854 }
855 Uint64 GetHigh64() const { return Uint64(data_high_); }
856 Uint64 GetLow64() const { return Uint64(data_low_); }
857 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
858 bool operator==(Uint128 value) const {
859 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
860 }
861 Uint128 operator&(Uint128 value) const {
862 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
863 }
864 Uint128 operator&=(Uint128 value) {
865 data_high_ &= value.data_high_;
866 data_low_ &= value.data_low_;
867 return *this;
868 }
869 Uint128 operator|=(Uint128 value) {
870 data_high_ |= value.data_high_;
871 data_low_ |= value.data_low_;
872 return *this;
873 }
874 Uint128 operator>>(int shift) const {
875 VIXL_ASSERT((shift >= 0) && (shift < 128));
876 if (shift == 0) return *this;
877 if (shift >= 64) {
878 return Uint128(0, data_high_ >> (shift - 64));
879 }
880 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
881 return Uint128(data_high_ >> shift, tmp);
882 }
883 Uint128 operator<<(int shift) const {
884 VIXL_ASSERT((shift >= 0) && (shift < 128));
885 if (shift == 0) return *this;
886 if (shift >= 64) {
887 return Uint128(data_low_ << (shift - 64), 0);
888 }
889 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
890 return Uint128(tmp, data_low_ << shift);
891 }
892};
893
894Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
895Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
896
897Int64 BitCount(Uint32 value);
898
Pierre Langloisd82faf62018-04-04 13:06:58 +0100899} // namespace internal
900
armvixlad96eda2013-06-14 11:42:37 +0100901} // namespace vixl
902
903#endif // VIXL_UTILS_H