Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | // Copyright 2015, VIXL authors |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 2 | // 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 | |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 30 | #include <cmath> |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 31 | #include <cstring> |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 32 | #include <vector> |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 33 | |
Alexandre Rames | 1f9074d | 2016-05-23 15:50:01 +0100 | [diff] [blame] | 34 | #include "compiler-intrinsics-vixl.h" |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 35 | #include "globals-vixl.h" |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 36 | |
| 37 | namespace vixl { |
| 38 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 39 | // Macros for compile-time format checking. |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 40 | #if GCC_VERSION_OR_NEWER(4, 4, 0) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 41 | #define PRINTF_CHECK(format_index, varargs_index) \ |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 42 | __attribute__((format(gnu_printf, format_index, varargs_index))) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 43 | #else |
| 44 | #define PRINTF_CHECK(format_index, varargs_index) |
| 45 | #endif |
| 46 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 47 | #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 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 68 | // Check number width. |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 69 | // TODO: Refactor these using templates. |
| 70 | inline 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 | } |
| 75 | inline 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 | } |
| 80 | inline 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 Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 85 | inline bool IsIntN(unsigned n, int64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 86 | VIXL_ASSERT((0 < n) && (n < 64)); |
| 87 | int64_t limit = INT64_C(1) << (n - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 88 | return (-limit <= x) && (x < limit); |
| 89 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 90 | VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) { |
| 91 | return IsIntN(n, x); |
| 92 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 93 | |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 94 | inline bool IsUintN(unsigned n, uint32_t x) { |
| 95 | VIXL_ASSERT((0 < n) && (n < 32)); |
| 96 | return !(x >> n); |
| 97 | } |
| 98 | inline 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 | } |
| 103 | inline bool IsUintN(unsigned n, uint64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 104 | VIXL_ASSERT((0 < n) && (n < 64)); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 105 | return !(x >> n); |
| 106 | } |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 107 | inline 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 Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 112 | VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) { |
| 113 | return IsUintN(n, x); |
| 114 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 115 | |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 116 | inline uint64_t TruncateToUintN(unsigned n, uint64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 117 | VIXL_ASSERT((0 < n) && (n < 64)); |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 118 | return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 119 | } |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 120 | VIXL_DEPRECATED("TruncateToUintN", |
| 121 | inline uint64_t truncate_to_intn(unsigned n, int64_t x)) { |
| 122 | return TruncateToUintN(n, x); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 123 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 124 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 125 | // clang-format off |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 126 | #define INT_1_TO_32_LIST(V) \ |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 127 | V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \ |
| 128 | V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \ |
| 129 | V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \ |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 130 | V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32) |
| 131 | |
| 132 | #define INT_33_TO_63_LIST(V) \ |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 133 | V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \ |
| 134 | V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \ |
| 135 | V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \ |
| 136 | V(57) V(58) V(59) V(60) V(61) V(62) V(63) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 137 | |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 138 | #define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V) |
| 139 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 140 | // clang-format on |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 141 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 142 | #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 Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 154 | #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 Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 161 | } |
| 162 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 163 | INT_1_TO_63_LIST(DECLARE_IS_INT_N) |
| 164 | INT_1_TO_63_LIST(DECLARE_IS_UINT_N) |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 165 | INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 166 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 167 | #undef DECLARE_IS_INT_N |
| 168 | #undef DECLARE_IS_UINT_N |
| 169 | #undef DECLARE_TRUNCATE_TO_INT_N |
| 170 | |
| 171 | // Bit field extraction. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 172 | inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 173 | VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) && |
| 174 | (msb >= lsb)); |
| 175 | if ((msb == 63) && (lsb == 0)) return x; |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 176 | return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 179 | |
| 180 | inline 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)); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 186 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 187 | inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 188 | 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; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 200 | |
| 201 | inline 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 | |
| 211 | inline 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 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 225 | // Floating point representation. |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame^] | 226 | uint16_t Float16ToRawbits(float16 value); |
| 227 | |
| 228 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 229 | uint32_t FloatToRawbits(float value); |
| 230 | VIXL_DEPRECATED("FloatToRawbits", |
| 231 | inline uint32_t float_to_rawbits(float value)) { |
| 232 | return FloatToRawbits(value); |
| 233 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 234 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 235 | uint64_t DoubleToRawbits(double value); |
| 236 | VIXL_DEPRECATED("DoubleToRawbits", |
| 237 | inline uint64_t double_to_rawbits(double value)) { |
| 238 | return DoubleToRawbits(value); |
| 239 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 240 | |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame^] | 241 | float16 RawbitsToFloat16(uint16_t bits); |
| 242 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 243 | float RawbitsToFloat(uint32_t bits); |
| 244 | VIXL_DEPRECATED("RawbitsToFloat", |
| 245 | inline float rawbits_to_float(uint32_t bits)) { |
| 246 | return RawbitsToFloat(bits); |
| 247 | } |
| 248 | |
| 249 | double RawbitsToDouble(uint64_t bits); |
| 250 | VIXL_DEPRECATED("RawbitsToDouble", |
| 251 | inline double rawbits_to_double(uint64_t bits)) { |
| 252 | return RawbitsToDouble(bits); |
| 253 | } |
| 254 | |
| 255 | uint32_t FloatSign(float value); |
| 256 | VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) { |
| 257 | return FloatSign(value); |
| 258 | } |
| 259 | |
| 260 | uint32_t FloatExp(float value); |
| 261 | VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) { |
| 262 | return FloatExp(value); |
| 263 | } |
| 264 | |
| 265 | uint32_t FloatMantissa(float value); |
| 266 | VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) { |
| 267 | return FloatMantissa(value); |
| 268 | } |
| 269 | |
| 270 | uint32_t DoubleSign(double value); |
| 271 | VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) { |
| 272 | return DoubleSign(value); |
| 273 | } |
| 274 | |
| 275 | uint32_t DoubleExp(double value); |
| 276 | VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) { |
| 277 | return DoubleExp(value); |
| 278 | } |
| 279 | |
| 280 | uint64_t DoubleMantissa(double value); |
| 281 | VIXL_DEPRECATED("DoubleMantissa", |
| 282 | inline uint64_t double_mantissa(double value)) { |
| 283 | return DoubleMantissa(value); |
| 284 | } |
| 285 | |
| 286 | float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa); |
| 287 | VIXL_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 | |
| 294 | double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa); |
| 295 | VIXL_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 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 301 | |
| 302 | // An fpclassify() function for 16-bit half-precision floats. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 303 | int Float16Classify(float16 value); |
| 304 | VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) { |
| 305 | return Float16Classify(value); |
| 306 | } |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 307 | |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame^] | 308 | |
| 309 | // Check for float16 (uint16_t) NaNs. |
| 310 | inline bool IsNaN(float16 value) { return Float16Classify(value) == FP_NAN; } |
| 311 | |
| 312 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 313 | // NaN tests. |
| 314 | inline bool IsSignallingNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 315 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 316 | uint64_t raw = DoubleToRawbits(num); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 317 | if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | inline bool IsSignallingNaN(float num) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 325 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 326 | uint32_t raw = FloatToRawbits(num); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 327 | if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 334 | inline bool IsSignallingNaN(float16 num) { |
| 335 | const uint16_t kFP16QuietNaNMask = 0x0200; |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame^] | 336 | return IsNaN(num) && ((num & kFP16QuietNaNMask) == 0); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 340 | template <typename T> |
| 341 | inline bool IsQuietNaN(T num) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 342 | return std::isnan(num) && !IsSignallingNaN(num); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 346 | // Convert the NaN in 'num' to a quiet NaN. |
| 347 | inline double ToQuietNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 348 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 349 | VIXL_ASSERT(std::isnan(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 350 | return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | inline float ToQuietNaN(float num) { |
| 355 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 356 | VIXL_ASSERT(std::isnan(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 357 | return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
| 361 | // Fused multiply-add. |
| 362 | inline double FusedMultiplyAdd(double op1, double op2, double a) { |
| 363 | return fma(op1, op2, a); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | inline float FusedMultiplyAdd(float op1, float op2, float a) { |
| 368 | return fmaf(op1, op2, a); |
| 369 | } |
| 370 | |
| 371 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 372 | inline uint64_t LowestSetBit(uint64_t value) { return value & -value; } |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 373 | |
| 374 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 375 | template <typename T> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 376 | inline int HighestSetBitPosition(T value) { |
| 377 | VIXL_ASSERT(value != 0); |
| 378 | return (sizeof(value) * 8 - 1) - CountLeadingZeros(value); |
| 379 | } |
| 380 | |
| 381 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 382 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 383 | inline int WhichPowerOf2(V value) { |
| 384 | VIXL_ASSERT(IsPowerOf2(value)); |
| 385 | return CountTrailingZeros(value); |
| 386 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 387 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 388 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 389 | unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size); |
| 390 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 391 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 392 | int BitCount(uint64_t value); |
| 393 | |
| 394 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 395 | template <typename T> |
| 396 | T 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 | |
| 408 | template <typename T> |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 409 | inline T SignExtend(T val, int bitSize) { |
| 410 | VIXL_ASSERT(bitSize > 0); |
| 411 | T mask = (T(2) << (bitSize - 1)) - T(1); |
| 412 | val &= mask; |
Vincent Belliard | 4e52d4d | 2018-04-03 13:34:44 -0700 | [diff] [blame] | 413 | T sign_bits = -((val >> (bitSize - 1)) << bitSize); |
| 414 | val |= sign_bits; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 415 | return val; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | template <typename T> |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 420 | T 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]; |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 426 | uint64_t mask = UINT64_C(0xff00000000000000); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 427 | 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)); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 437 | 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 Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 440 | uint64_t temp = 0; |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 441 | for (int i = 0; i < 8; i++) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 442 | temp <<= 8; |
| 443 | temp |= bytes[permute_table[block_bytes_log2 - 1][i]]; |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 444 | } |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 445 | |
| 446 | T result; |
| 447 | VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp)); |
| 448 | memcpy(&result, &temp, sizeof(result)); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 449 | return result; |
| 450 | } |
| 451 | |
Vincent Belliard | 96ff2a4 | 2016-10-27 08:51:55 -0700 | [diff] [blame] | 452 | template <unsigned MULTIPLE, typename T> |
| 453 | inline bool IsMultiple(T value) { |
| 454 | VIXL_ASSERT(IsPowerOf2(MULTIPLE)); |
| 455 | return (value & (MULTIPLE - 1)) == 0; |
| 456 | } |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 457 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 458 | template <typename T> |
| 459 | inline bool IsMultiple(T value, unsigned multiple) { |
| 460 | VIXL_ASSERT(IsPowerOf2(multiple)); |
| 461 | return (value & (multiple - 1)) == 0; |
| 462 | } |
| 463 | |
Georgia Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 464 | template <typename T> |
| 465 | inline bool IsAligned(T pointer, int alignment) { |
| 466 | VIXL_ASSERT(IsPowerOf2(alignment)); |
| 467 | return (pointer & (alignment - 1)) == 0; |
| 468 | } |
| 469 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 470 | // Pointer alignment |
| 471 | // TODO: rename/refactor to make it specific to instructions. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 472 | template <unsigned ALIGN, typename T> |
| 473 | inline bool IsAligned(T pointer) { |
| 474 | VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 475 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 476 | // reinterpret_cast behaviour for other types. |
Georgia Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 477 | return IsAligned((intptr_t)(pointer), ALIGN); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 478 | } |
| 479 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 480 | template <typename T> |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 481 | bool IsWordAligned(T pointer) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 482 | return IsAligned<4>(pointer); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 485 | // Increment a pointer until it has the specified alignment. The alignment must |
| 486 | // be a power of two. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 487 | template <class T> |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 488 | T AlignUp(T pointer, |
| 489 | typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) { |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 490 | VIXL_ASSERT(IsPowerOf2(alignment)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 491 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 492 | // reinterpret_cast behaviour for other types. |
| 493 | |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 494 | typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw = |
| 495 | (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 496 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 497 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 498 | size_t mask = alignment - 1; |
| 499 | T result = (T)((pointer_raw + mask) & ~mask); |
Alexandre Rames | 47ed265 | 2016-11-09 14:44:06 +0000 | [diff] [blame] | 500 | VIXL_ASSERT(result >= pointer); |
| 501 | |
| 502 | return result; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 505 | // Decrement a pointer until it has the specified alignment. The alignment must |
| 506 | // be a power of two. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 507 | template <class T> |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 508 | T AlignDown(T pointer, |
| 509 | typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) { |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 510 | VIXL_ASSERT(IsPowerOf2(alignment)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 511 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 512 | // reinterpret_cast behaviour for other types. |
| 513 | |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 514 | typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw = |
| 515 | (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 516 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 517 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 518 | size_t mask = alignment - 1; |
| 519 | return (T)(pointer_raw & ~mask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 522 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 523 | template <typename T> |
| 524 | inline T ExtractBit(T value, unsigned bit) { |
| 525 | return (value >> bit) & T(1); |
| 526 | } |
| 527 | |
| 528 | template <typename Ts, typename Td> |
| 529 | inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) { |
| 530 | return Td((value >> least_significant_bit) & Ts(mask)); |
| 531 | } |
| 532 | |
| 533 | template <typename Ts, typename Td> |
Vincent Belliard | 60241a5 | 2016-11-10 12:41:11 -0800 | [diff] [blame] | 534 | inline void AssignBit(Td& dst, // NOLINT(runtime/references) |
| 535 | int bit, |
| 536 | Ts value) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 537 | 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 | |
| 545 | template <typename Td, typename Ts> |
Vincent Belliard | 60241a5 | 2016-11-10 12:41:11 -0800 | [diff] [blame] | 546 | inline void AssignBits(Td& dst, // NOLINT(runtime/references) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 547 | 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 | |
| 559 | class 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 | |
| 652 | class 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 Langlois | d82faf6 | 2018-04-04 13:06:58 +0100 | [diff] [blame] | 705 | namespace internal { |
| 706 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 707 | typedef int64_t Int64; |
| 708 | class Uint64; |
| 709 | class Uint128; |
| 710 | |
| 711 | class 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 | |
| 773 | class 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 Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 789 | return Uint32(static_cast<uint32_t>(data_)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 790 | } |
| 791 | Uint32 GetHigh32() const { return Uint32(data_ >> 32); } |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 792 | Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 793 | 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 | |
| 840 | class 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 | |
| 894 | Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {} |
| 895 | Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {} |
| 896 | |
| 897 | Int64 BitCount(Uint32 value); |
| 898 | |
Pierre Langlois | d82faf6 | 2018-04-04 13:06:58 +0100 | [diff] [blame] | 899 | } // namespace internal |
| 900 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 901 | } // namespace vixl |
| 902 | |
| 903 | #endif // VIXL_UTILS_H |