blob: b4b9adaafdbe93c610ccce096be3dffbeb26d8df [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
Pierre Langlois88c46b82016-06-02 18:15:32 +010086inline uint32_t TruncateToIntN(unsigned n, int64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000087 VIXL_ASSERT((0 < n) && (n < 64));
armvixldb644342015-07-21 11:37:10 +010088 return static_cast<uint32_t>(x & ((INT64_C(1) << n) - 1));
armvixlad96eda2013-06-14 11:42:37 +010089}
Pierre Langlois88c46b82016-06-02 18:15:32 +010090VIXL_DEPRECATED("TruncateToIntN",
91 inline uint32_t truncate_to_intn(unsigned n, int64_t x)) {
92 return TruncateToIntN(n, x);
93}
armvixlad96eda2013-06-14 11:42:37 +010094
armvixl0f35e362016-05-10 13:57:58 +010095// clang-format off
armvixlad96eda2013-06-14 11:42:37 +010096#define INT_1_TO_63_LIST(V) \
97V(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) \
100V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32) \
101V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
102V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
103V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
104V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100105
armvixl0f35e362016-05-10 13:57:58 +0100106// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100107
Pierre Langlois88c46b82016-06-02 18:15:32 +0100108#define DECLARE_IS_INT_N(N) \
109 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
110 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
111 return IsIntN(N, x); \
112 }
113
114#define DECLARE_IS_UINT_N(N) \
115 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
116 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
117 return IsUintN(N, x); \
118 }
119
120#define DECLARE_TRUNCATE_TO_INT_N(N) \
121 inline uint32_t TruncateToInt##N(int x) { return TruncateToIntN(N, x); } \
122 VIXL_DEPRECATED("TruncateToInt" #N, \
123 inline bool truncate_to_int##N(int64_t x)) { \
124 return TruncateToIntN(N, x); \
125 }
126
armvixlad96eda2013-06-14 11:42:37 +0100127INT_1_TO_63_LIST(DECLARE_IS_INT_N)
128INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
129INT_1_TO_63_LIST(DECLARE_TRUNCATE_TO_INT_N)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100130
armvixlad96eda2013-06-14 11:42:37 +0100131#undef DECLARE_IS_INT_N
132#undef DECLARE_IS_UINT_N
133#undef DECLARE_TRUNCATE_TO_INT_N
134
135// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100136inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint32_t x) {
armvixlad96eda2013-06-14 11:42:37 +0100137 return (x >> lsb) & ((1 << (1 + msb - lsb)) - 1);
138}
139
Pierre Langlois88c46b82016-06-02 18:15:32 +0100140inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
armvixl578645f2013-08-15 17:21:42 +0100141 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100142}
143
Pierre Langlois88c46b82016-06-02 18:15:32 +0100144inline int32_t ExtractSignedBitfield32(int msb, int lsb, int32_t x) {
armvixlad96eda2013-06-14 11:42:37 +0100145 return (x << (31 - msb)) >> (lsb + 31 - msb);
146}
147
Pierre Langlois88c46b82016-06-02 18:15:32 +0100148inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) {
armvixlad96eda2013-06-14 11:42:37 +0100149 return (x << (63 - msb)) >> (lsb + 63 - msb);
150}
151
armvixlf37fdc02014-02-05 13:22:16 +0000152// Floating point representation.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100153uint32_t FloatToRawbits(float value);
154VIXL_DEPRECATED("FloatToRawbits",
155 inline uint32_t float_to_rawbits(float value)) {
156 return FloatToRawbits(value);
157}
armvixlad96eda2013-06-14 11:42:37 +0100158
Pierre Langlois88c46b82016-06-02 18:15:32 +0100159uint64_t DoubleToRawbits(double value);
160VIXL_DEPRECATED("DoubleToRawbits",
161 inline uint64_t double_to_rawbits(double value)) {
162 return DoubleToRawbits(value);
163}
armvixl5289c592015-03-02 13:52:04 +0000164
Pierre Langlois88c46b82016-06-02 18:15:32 +0100165float RawbitsToFloat(uint32_t bits);
166VIXL_DEPRECATED("RawbitsToFloat",
167 inline float rawbits_to_float(uint32_t bits)) {
168 return RawbitsToFloat(bits);
169}
170
171double RawbitsToDouble(uint64_t bits);
172VIXL_DEPRECATED("RawbitsToDouble",
173 inline double rawbits_to_double(uint64_t bits)) {
174 return RawbitsToDouble(bits);
175}
176
177uint32_t FloatSign(float value);
178VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
179 return FloatSign(value);
180}
181
182uint32_t FloatExp(float value);
183VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
184 return FloatExp(value);
185}
186
187uint32_t FloatMantissa(float value);
188VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
189 return FloatMantissa(value);
190}
191
192uint32_t DoubleSign(double value);
193VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
194 return DoubleSign(value);
195}
196
197uint32_t DoubleExp(double value);
198VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
199 return DoubleExp(value);
200}
201
202uint64_t DoubleMantissa(double value);
203VIXL_DEPRECATED("DoubleMantissa",
204 inline uint64_t double_mantissa(double value)) {
205 return DoubleMantissa(value);
206}
207
208float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
209VIXL_DEPRECATED("FloatPack",
210 inline float float_pack(uint32_t sign,
211 uint32_t exp,
212 uint32_t mantissa)) {
213 return FloatPack(sign, exp, mantissa);
214}
215
216double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
217VIXL_DEPRECATED("DoublePack",
218 inline double double_pack(uint32_t sign,
219 uint32_t exp,
220 uint64_t mantissa)) {
221 return DoublePack(sign, exp, mantissa);
222}
armvixl5289c592015-03-02 13:52:04 +0000223
224// An fpclassify() function for 16-bit half-precision floats.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100225int Float16Classify(float16 value);
226VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) {
227 return Float16Classify(value);
228}
armvixlf37fdc02014-02-05 13:22:16 +0000229
230// NaN tests.
231inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100232 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100233 uint64_t raw = DoubleToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100234 if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000235 return true;
236 }
237 return false;
238}
239
240
241inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000242 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100243 uint32_t raw = FloatToRawbits(num);
armvixl6e2c8272015-03-31 11:04:14 +0100244 if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000245 return true;
246 }
247 return false;
248}
249
250
armvixl5289c592015-03-02 13:52:04 +0000251inline bool IsSignallingNaN(float16 num) {
252 const uint16_t kFP16QuietNaNMask = 0x0200;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100253 return (Float16Classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000254}
255
256
armvixlf37fdc02014-02-05 13:22:16 +0000257template <typename T>
258inline bool IsQuietNaN(T num) {
armvixl6e2c8272015-03-31 11:04:14 +0100259 return std::isnan(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000260}
261
262
armvixlb0c8ae22014-03-21 14:03:59 +0000263// Convert the NaN in 'num' to a quiet NaN.
264inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100265 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
armvixl6e2c8272015-03-31 11:04:14 +0100266 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100267 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000268}
269
270
271inline float ToQuietNaN(float num) {
272 const uint32_t kFP32QuietNaNMask = 0x00400000;
armvixl6e2c8272015-03-31 11:04:14 +0100273 VIXL_ASSERT(std::isnan(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100274 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000275}
276
277
278// Fused multiply-add.
279inline double FusedMultiplyAdd(double op1, double op2, double a) {
280 return fma(op1, op2, a);
281}
282
283
284inline float FusedMultiplyAdd(float op1, float op2, float a) {
285 return fmaf(op1, op2, a);
286}
287
288
armvixl0f35e362016-05-10 13:57:58 +0100289inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100290
291
armvixl0f35e362016-05-10 13:57:58 +0100292template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100293inline int HighestSetBitPosition(T value) {
294 VIXL_ASSERT(value != 0);
295 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
296}
297
298
armvixl0f35e362016-05-10 13:57:58 +0100299template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100300inline int WhichPowerOf2(V value) {
301 VIXL_ASSERT(IsPowerOf2(value));
302 return CountTrailingZeros(value);
303}
armvixlad96eda2013-06-14 11:42:37 +0100304
armvixldb644342015-07-21 11:37:10 +0100305
armvixl330dc712014-11-25 10:38:32 +0000306unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
307
armvixldb644342015-07-21 11:37:10 +0100308
Pierre Langlois88c46b82016-06-02 18:15:32 +0100309int BitCount(uint64_t value);
310
311
armvixldb644342015-07-21 11:37:10 +0100312template <typename T>
313T ReverseBits(T value) {
314 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
315 (sizeof(value) == 4) || (sizeof(value) == 8));
316 T result = 0;
317 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
318 result = (result << 1) | (value & 1);
319 value >>= 1;
320 }
321 return result;
322}
323
324
325template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100326inline T SignExtend(T val, int bitSize) {
327 VIXL_ASSERT(bitSize > 0);
328 T mask = (T(2) << (bitSize - 1)) - T(1);
329 val &= mask;
330 T sign = -(val >> (bitSize - 1));
331 val |= (sign << bitSize);
332 return val;
333}
334
335
336template <typename T>
armvixldb644342015-07-21 11:37:10 +0100337T ReverseBytes(T value, int block_bytes_log2) {
338 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
339 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
340 // Split the 64-bit value into an 8-bit array, where b[0] is the least
341 // significant byte, and b[7] is the most significant.
342 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000343 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100344 for (int i = 7; i >= 0; i--) {
345 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
346 mask >>= 8;
347 }
348
349 // Permutation tables for REV instructions.
350 // permute_table[0] is used by REV16_x, REV16_w
351 // permute_table[1] is used by REV32_x, REV_w
352 // permute_table[2] is used by REV_x
353 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100354 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
355 {4, 5, 6, 7, 0, 1, 2, 3},
356 {0, 1, 2, 3, 4, 5, 6, 7}};
armvixldb644342015-07-21 11:37:10 +0100357 T result = 0;
358 for (int i = 0; i < 8; i++) {
359 result <<= 8;
360 result |= bytes[permute_table[block_bytes_log2 - 1][i]];
361 }
362 return result;
363}
364
365
armvixlad96eda2013-06-14 11:42:37 +0100366// Pointer alignment
367// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100368template <unsigned ALIGN, typename T>
369inline bool IsAligned(T pointer) {
370 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
371 VIXL_ASSERT(IsPowerOf2(ALIGN));
372 // Use C-style casts to get static_cast behaviour for integral types (T), and
373 // reinterpret_cast behaviour for other types.
374 return ((intptr_t)(pointer) & (ALIGN - 1)) == 0;
375}
376
armvixl0f35e362016-05-10 13:57:58 +0100377template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100378bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100379 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100380}
381
armvixl330dc712014-11-25 10:38:32 +0000382// Increment a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100383template <class T>
armvixlad96eda2013-06-14 11:42:37 +0100384T AlignUp(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100385 // Use C-style casts to get static_cast behaviour for integral types (T), and
386 // reinterpret_cast behaviour for other types.
387
armvixl330dc712014-11-25 10:38:32 +0000388 uint64_t pointer_raw = (uint64_t)pointer;
389 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100390
armvixlad96eda2013-06-14 11:42:37 +0100391 size_t align_step = (alignment - pointer_raw) % alignment;
armvixlb0c8ae22014-03-21 14:03:59 +0000392 VIXL_ASSERT((pointer_raw + align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100393
394 return (T)(pointer_raw + align_step);
armvixlad96eda2013-06-14 11:42:37 +0100395}
396
armvixl330dc712014-11-25 10:38:32 +0000397// Decrement a pointer (up to 64 bits) until it has the specified alignment.
armvixl0f35e362016-05-10 13:57:58 +0100398template <class T>
armvixlb0c8ae22014-03-21 14:03:59 +0000399T AlignDown(T pointer, size_t alignment) {
armvixl4a102ba2014-07-14 09:02:40 +0100400 // Use C-style casts to get static_cast behaviour for integral types (T), and
401 // reinterpret_cast behaviour for other types.
402
armvixl330dc712014-11-25 10:38:32 +0000403 uint64_t pointer_raw = (uint64_t)pointer;
404 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100405
armvixlb0c8ae22014-03-21 14:03:59 +0000406 size_t align_step = pointer_raw % alignment;
407 VIXL_ASSERT((pointer_raw - align_step) % alignment == 0);
armvixl4a102ba2014-07-14 09:02:40 +0100408
409 return (T)(pointer_raw - align_step);
armvixlb0c8ae22014-03-21 14:03:59 +0000410}
411
Pierre Langlois88c46b82016-06-02 18:15:32 +0100412template <typename T>
413inline T ExtractBit(T value, unsigned bit) {
414 return (value >> bit) & T(1);
415}
416
417template <typename Ts, typename Td>
418inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
419 return Td((value >> least_significant_bit) & Ts(mask));
420}
421
422template <typename Ts, typename Td>
423inline void AssignBit(Td& dst, int bit, Ts value) { // NOLINT
424 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
425 VIXL_ASSERT(bit >= 0);
426 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
427 Td mask(1);
428 dst &= ~(mask << bit);
429 dst |= Td(value) << bit;
430}
431
432template <typename Td, typename Ts>
433inline void AssignBits(Td& dst, // NOLINT
434 int least_significant_bit,
435 Ts mask,
436 Ts value) {
437 VIXL_ASSERT(least_significant_bit >= 0);
438 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
439 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
440 Td(mask));
441 VIXL_ASSERT((value & mask) == value);
442 dst &= ~(Td(mask) << least_significant_bit);
443 dst |= Td(value) << least_significant_bit;
444}
445
446class VFP {
447 public:
448 static uint32_t FP32ToImm8(float imm) {
449 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
450 uint32_t bits = FloatToRawbits(imm);
451 // bit7: a000.0000
452 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
453 // bit6: 0b00.0000
454 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
455 // bit5_to_0: 00cd.efgh
456 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
457 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
458 }
459 static uint32_t FP64ToImm8(double imm) {
460 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
461 // 0000.0000.0000.0000.0000.0000.0000.0000
462 uint64_t bits = DoubleToRawbits(imm);
463 // bit7: a000.0000
464 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
465 // bit6: 0b00.0000
466 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
467 // bit5_to_0: 00cd.efgh
468 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
469
470 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
471 }
472 static float Imm8ToFP32(uint32_t imm8) {
473 // Imm8: abcdefgh (8 bits)
474 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
475 // where B is b ^ 1
476 uint32_t bits = imm8;
477 uint32_t bit7 = (bits >> 7) & 0x1;
478 uint32_t bit6 = (bits >> 6) & 0x1;
479 uint32_t bit5_to_0 = bits & 0x3f;
480 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
481
482 return RawbitsToFloat(result);
483 }
484 static double Imm8ToFP64(uint32_t imm8) {
485 // Imm8: abcdefgh (8 bits)
486 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
487 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
488 // where B is b ^ 1
489 uint32_t bits = imm8;
490 uint64_t bit7 = (bits >> 7) & 0x1;
491 uint64_t bit6 = (bits >> 6) & 0x1;
492 uint64_t bit5_to_0 = bits & 0x3f;
493 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
494 return RawbitsToDouble(result);
495 }
496 static bool IsImmFP32(float imm) {
497 // Valid values will have the form:
498 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
499 uint32_t bits = FloatToRawbits(imm);
500 // bits[19..0] are cleared.
501 if ((bits & 0x7ffff) != 0) {
502 return false;
503 }
504
505
506 // bits[29..25] are all set or all cleared.
507 uint32_t b_pattern = (bits >> 16) & 0x3e00;
508 if (b_pattern != 0 && b_pattern != 0x3e00) {
509 return false;
510 }
511 // bit[30] and bit[29] are opposite.
512 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
513 return false;
514 }
515 return true;
516 }
517 static bool IsImmFP64(double imm) {
518 // Valid values will have the form:
519 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
520 // 0000.0000.0000.0000.0000.0000.0000.0000
521 uint64_t bits = DoubleToRawbits(imm);
522 // bits[47..0] are cleared.
523 if ((bits & 0x0000ffffffffffff) != 0) {
524 return false;
525 }
526 // bits[61..54] are all set or all cleared.
527 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
528 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
529 return false;
530 }
531 // bit[62] and bit[61] are opposite.
532 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
533 return false;
534 }
535 return true;
536 }
537};
538
539class BitField {
540 // ForEachBitHelper is a functor that will call
541 // bool ForEachBitHelper::execute(ElementType id) const
542 // and expects a boolean in return whether to continue (if true)
543 // or stop (if false)
544 // check_set will check if the bits are on (true) or off(false)
545 template <typename ForEachBitHelper, bool check_set>
546 bool ForEachBit(const ForEachBitHelper& helper) {
547 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
548 if (bitfield_[i] == check_set)
549 if (!helper.execute(i)) return false;
550 }
551 return true;
552 }
553
554 public:
555 explicit BitField(unsigned size) : bitfield_(size, 0) {}
556
557 void Set(int i) {
558 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
559 bitfield_[i] = true;
560 }
561
562 void Unset(int i) {
563 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
564 bitfield_[i] = true;
565 }
566
567 bool IsSet(int i) const { return bitfield_[i]; }
568
569 // For each bit not set in the bitfield call the execute functor
570 // execute.
571 // ForEachBitSetHelper::execute returns true if the iteration through
572 // the bits can continue, otherwise it will stop.
573 // struct ForEachBitSetHelper {
574 // bool execute(int /*id*/) { return false; }
575 // };
576 template <typename ForEachBitNotSetHelper>
577 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
578 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
579 }
580
581 // For each bit set in the bitfield call the execute functor
582 // execute.
583 template <typename ForEachBitSetHelper>
584 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
585 return ForEachBit<ForEachBitSetHelper, true>(helper);
586 }
587
588 private:
589 std::vector<bool> bitfield_;
590};
591
592typedef int64_t Int64;
593class Uint64;
594class Uint128;
595
596class Uint32 {
597 uint32_t data_;
598
599 public:
600 // Unlike uint32_t, Uint32 has a default constructor.
601 Uint32() { data_ = 0; }
602 explicit Uint32(uint32_t data) : data_(data) {}
603 inline explicit Uint32(Uint64 data);
604 uint32_t Get() const { return data_; }
605 template <int N>
606 int32_t GetSigned() const {
607 return ExtractSignedBitfield32(N - 1, 0, data_);
608 }
609 int32_t GetSigned() const { return data_; }
610 Uint32 operator~() const { return Uint32(~data_); }
611 Uint32 operator-() const { return Uint32(-data_); }
612 bool operator==(Uint32 value) const { return data_ == value.data_; }
613 bool operator!=(Uint32 value) const { return data_ != value.data_; }
614 bool operator>(Uint32 value) const { return data_ > value.data_; }
615 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
616 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
617 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
618 Uint32 operator&=(Uint32 value) {
619 data_ &= value.data_;
620 return *this;
621 }
622 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
623 Uint32 operator^=(Uint32 value) {
624 data_ ^= value.data_;
625 return *this;
626 }
627 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
628 Uint32 operator|=(Uint32 value) {
629 data_ |= value.data_;
630 return *this;
631 }
632 // Unlike uint32_t, the shift functions can accept negative shift and
633 // return 0 when the shift is too big.
634 Uint32 operator>>(int shift) const {
635 if (shift == 0) return *this;
636 if (shift < 0) {
637 int tmp = -shift;
638 if (tmp >= 32) return Uint32(0);
639 return Uint32(data_ << tmp);
640 }
641 int tmp = shift;
642 if (tmp >= 32) return Uint32(0);
643 return Uint32(data_ >> tmp);
644 }
645 Uint32 operator<<(int shift) const {
646 if (shift == 0) return *this;
647 if (shift < 0) {
648 int tmp = -shift;
649 if (tmp >= 32) return Uint32(0);
650 return Uint32(data_ >> tmp);
651 }
652 int tmp = shift;
653 if (tmp >= 32) return Uint32(0);
654 return Uint32(data_ << tmp);
655 }
656};
657
658class Uint64 {
659 uint64_t data_;
660
661 public:
662 // Unlike uint64_t, Uint64 has a default constructor.
663 Uint64() { data_ = 0; }
664 explicit Uint64(uint64_t data) : data_(data) {}
665 explicit Uint64(Uint32 data) : data_(data.Get()) {}
666 inline explicit Uint64(Uint128 data);
667 uint64_t Get() const { return data_; }
668 int64_t GetSigned(int N) const {
669 return ExtractSignedBitfield64(N - 1, 0, data_);
670 }
671 int64_t GetSigned() const { return data_; }
672 Uint32 ToUint32() const {
673 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100674 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100675 }
676 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100677 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100678 Uint64 operator~() const { return Uint64(~data_); }
679 Uint64 operator-() const { return Uint64(-data_); }
680 bool operator==(Uint64 value) const { return data_ == value.data_; }
681 bool operator!=(Uint64 value) const { return data_ != value.data_; }
682 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
683 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
684 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
685 Uint64 operator&=(Uint64 value) {
686 data_ &= value.data_;
687 return *this;
688 }
689 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
690 Uint64 operator^=(Uint64 value) {
691 data_ ^= value.data_;
692 return *this;
693 }
694 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
695 Uint64 operator|=(Uint64 value) {
696 data_ |= value.data_;
697 return *this;
698 }
699 // Unlike uint64_t, the shift functions can accept negative shift and
700 // return 0 when the shift is too big.
701 Uint64 operator>>(int shift) const {
702 if (shift == 0) return *this;
703 if (shift < 0) {
704 int tmp = -shift;
705 if (tmp >= 64) return Uint64(0);
706 return Uint64(data_ << tmp);
707 }
708 int tmp = shift;
709 if (tmp >= 64) return Uint64(0);
710 return Uint64(data_ >> tmp);
711 }
712 Uint64 operator<<(int shift) const {
713 if (shift == 0) return *this;
714 if (shift < 0) {
715 int tmp = -shift;
716 if (tmp >= 64) return Uint64(0);
717 return Uint64(data_ >> tmp);
718 }
719 int tmp = shift;
720 if (tmp >= 64) return Uint64(0);
721 return Uint64(data_ << tmp);
722 }
723};
724
725class Uint128 {
726 uint64_t data_high_;
727 uint64_t data_low_;
728
729 public:
730 Uint128() : data_high_(0), data_low_(0) {}
731 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
732 explicit Uint128(Uint64 data_low)
733 : data_high_(0), data_low_(data_low.Get()) {}
734 Uint128(uint64_t data_high, uint64_t data_low)
735 : data_high_(data_high), data_low_(data_low) {}
736 Uint64 ToUint64() const {
737 VIXL_ASSERT(data_high_ == 0);
738 return Uint64(data_low_);
739 }
740 Uint64 GetHigh64() const { return Uint64(data_high_); }
741 Uint64 GetLow64() const { return Uint64(data_low_); }
742 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
743 bool operator==(Uint128 value) const {
744 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
745 }
746 Uint128 operator&(Uint128 value) const {
747 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
748 }
749 Uint128 operator&=(Uint128 value) {
750 data_high_ &= value.data_high_;
751 data_low_ &= value.data_low_;
752 return *this;
753 }
754 Uint128 operator|=(Uint128 value) {
755 data_high_ |= value.data_high_;
756 data_low_ |= value.data_low_;
757 return *this;
758 }
759 Uint128 operator>>(int shift) const {
760 VIXL_ASSERT((shift >= 0) && (shift < 128));
761 if (shift == 0) return *this;
762 if (shift >= 64) {
763 return Uint128(0, data_high_ >> (shift - 64));
764 }
765 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
766 return Uint128(data_high_ >> shift, tmp);
767 }
768 Uint128 operator<<(int shift) const {
769 VIXL_ASSERT((shift >= 0) && (shift < 128));
770 if (shift == 0) return *this;
771 if (shift >= 64) {
772 return Uint128(data_low_ << (shift - 64), 0);
773 }
774 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
775 return Uint128(tmp, data_low_ << shift);
776 }
777};
778
779Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
780Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
781
782Int64 BitCount(Uint32 value);
783
armvixlad96eda2013-06-14 11:42:37 +0100784} // namespace vixl
785
786#endif // VIXL_UTILS_H