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 | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 69 | inline bool IsIntN(unsigned n, int64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 70 | VIXL_ASSERT((0 < n) && (n < 64)); |
| 71 | int64_t limit = INT64_C(1) << (n - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 72 | return (-limit <= x) && (x < limit); |
| 73 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 74 | VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) { |
| 75 | return IsIntN(n, x); |
| 76 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 77 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 78 | inline bool IsUintN(unsigned n, int64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 79 | VIXL_ASSERT((0 < n) && (n < 64)); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 80 | return !(x >> n); |
| 81 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 82 | VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) { |
| 83 | return IsUintN(n, x); |
| 84 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 85 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 86 | inline uint32_t TruncateToIntN(unsigned n, int64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 87 | VIXL_ASSERT((0 < n) && (n < 64)); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 88 | return static_cast<uint32_t>(x & ((INT64_C(1) << n) - 1)); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 89 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 90 | VIXL_DEPRECATED("TruncateToIntN", |
| 91 | inline uint32_t truncate_to_intn(unsigned n, int64_t x)) { |
| 92 | return TruncateToIntN(n, x); |
| 93 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 94 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 95 | // clang-format off |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 96 | #define INT_1_TO_63_LIST(V) \ |
| 97 | V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \ |
| 98 | V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \ |
| 99 | V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \ |
| 100 | V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32) \ |
| 101 | V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \ |
| 102 | V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \ |
| 103 | V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \ |
| 104 | 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] | 105 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 106 | // clang-format on |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 107 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 108 | #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 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 127 | INT_1_TO_63_LIST(DECLARE_IS_INT_N) |
| 128 | INT_1_TO_63_LIST(DECLARE_IS_UINT_N) |
| 129 | INT_1_TO_63_LIST(DECLARE_TRUNCATE_TO_INT_N) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 130 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 131 | #undef DECLARE_IS_INT_N |
| 132 | #undef DECLARE_IS_UINT_N |
| 133 | #undef DECLARE_TRUNCATE_TO_INT_N |
| 134 | |
| 135 | // Bit field extraction. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 136 | inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint32_t x) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 137 | return (x >> lsb) & ((1 << (1 + msb - lsb)) - 1); |
| 138 | } |
| 139 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 140 | inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) { |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 141 | return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 144 | inline int32_t ExtractSignedBitfield32(int msb, int lsb, int32_t x) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 145 | return (x << (31 - msb)) >> (lsb + 31 - msb); |
| 146 | } |
| 147 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 148 | inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) { |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 149 | return (x << (63 - msb)) >> (lsb + 63 - msb); |
| 150 | } |
| 151 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 152 | // Floating point representation. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 153 | uint32_t FloatToRawbits(float value); |
| 154 | VIXL_DEPRECATED("FloatToRawbits", |
| 155 | inline uint32_t float_to_rawbits(float value)) { |
| 156 | return FloatToRawbits(value); |
| 157 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 158 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 159 | uint64_t DoubleToRawbits(double value); |
| 160 | VIXL_DEPRECATED("DoubleToRawbits", |
| 161 | inline uint64_t double_to_rawbits(double value)) { |
| 162 | return DoubleToRawbits(value); |
| 163 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 164 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 165 | float RawbitsToFloat(uint32_t bits); |
| 166 | VIXL_DEPRECATED("RawbitsToFloat", |
| 167 | inline float rawbits_to_float(uint32_t bits)) { |
| 168 | return RawbitsToFloat(bits); |
| 169 | } |
| 170 | |
| 171 | double RawbitsToDouble(uint64_t bits); |
| 172 | VIXL_DEPRECATED("RawbitsToDouble", |
| 173 | inline double rawbits_to_double(uint64_t bits)) { |
| 174 | return RawbitsToDouble(bits); |
| 175 | } |
| 176 | |
| 177 | uint32_t FloatSign(float value); |
| 178 | VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) { |
| 179 | return FloatSign(value); |
| 180 | } |
| 181 | |
| 182 | uint32_t FloatExp(float value); |
| 183 | VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) { |
| 184 | return FloatExp(value); |
| 185 | } |
| 186 | |
| 187 | uint32_t FloatMantissa(float value); |
| 188 | VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) { |
| 189 | return FloatMantissa(value); |
| 190 | } |
| 191 | |
| 192 | uint32_t DoubleSign(double value); |
| 193 | VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) { |
| 194 | return DoubleSign(value); |
| 195 | } |
| 196 | |
| 197 | uint32_t DoubleExp(double value); |
| 198 | VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) { |
| 199 | return DoubleExp(value); |
| 200 | } |
| 201 | |
| 202 | uint64_t DoubleMantissa(double value); |
| 203 | VIXL_DEPRECATED("DoubleMantissa", |
| 204 | inline uint64_t double_mantissa(double value)) { |
| 205 | return DoubleMantissa(value); |
| 206 | } |
| 207 | |
| 208 | float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa); |
| 209 | VIXL_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 | |
| 216 | double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa); |
| 217 | VIXL_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 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 223 | |
| 224 | // An fpclassify() function for 16-bit half-precision floats. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 225 | int Float16Classify(float16 value); |
| 226 | VIXL_DEPRECATED("Float16Classify", inline int float16classify(float16 value)) { |
| 227 | return Float16Classify(value); |
| 228 | } |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 229 | |
| 230 | // NaN tests. |
| 231 | inline bool IsSignallingNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 232 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 233 | uint64_t raw = DoubleToRawbits(num); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 234 | if (std::isnan(num) && ((raw & kFP64QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | inline bool IsSignallingNaN(float num) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 242 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 243 | uint32_t raw = FloatToRawbits(num); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 244 | if (std::isnan(num) && ((raw & kFP32QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 251 | inline bool IsSignallingNaN(float16 num) { |
| 252 | const uint16_t kFP16QuietNaNMask = 0x0200; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 253 | return (Float16Classify(num) == FP_NAN) && ((num & kFP16QuietNaNMask) == 0); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 257 | template <typename T> |
| 258 | inline bool IsQuietNaN(T num) { |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 259 | return std::isnan(num) && !IsSignallingNaN(num); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 263 | // Convert the NaN in 'num' to a quiet NaN. |
| 264 | inline double ToQuietNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 265 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 266 | VIXL_ASSERT(std::isnan(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 267 | return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | inline float ToQuietNaN(float num) { |
| 272 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 273 | VIXL_ASSERT(std::isnan(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 274 | return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
| 278 | // Fused multiply-add. |
| 279 | inline double FusedMultiplyAdd(double op1, double op2, double a) { |
| 280 | return fma(op1, op2, a); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | inline float FusedMultiplyAdd(float op1, float op2, float a) { |
| 285 | return fmaf(op1, op2, a); |
| 286 | } |
| 287 | |
| 288 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 289 | inline uint64_t LowestSetBit(uint64_t value) { return value & -value; } |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 290 | |
| 291 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 292 | template <typename T> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 293 | inline int HighestSetBitPosition(T value) { |
| 294 | VIXL_ASSERT(value != 0); |
| 295 | return (sizeof(value) * 8 - 1) - CountLeadingZeros(value); |
| 296 | } |
| 297 | |
| 298 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 299 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 300 | inline int WhichPowerOf2(V value) { |
| 301 | VIXL_ASSERT(IsPowerOf2(value)); |
| 302 | return CountTrailingZeros(value); |
| 303 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 304 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 305 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 306 | unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size); |
| 307 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 308 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 309 | int BitCount(uint64_t value); |
| 310 | |
| 311 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 312 | template <typename T> |
| 313 | T 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 | |
| 325 | template <typename T> |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 326 | inline 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 | |
| 336 | template <typename T> |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 337 | T 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]; |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 343 | uint64_t mask = UINT64_C(0xff00000000000000); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 344 | 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)); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 354 | 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}}; |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 357 | 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 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 366 | // Pointer alignment |
| 367 | // TODO: rename/refactor to make it specific to instructions. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 368 | template <unsigned ALIGN, typename T> |
| 369 | inline 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 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 377 | template <typename T> |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 378 | bool IsWordAligned(T pointer) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 379 | return IsAligned<4>(pointer); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 380 | } |
| 381 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 382 | // Increment a pointer (up to 64 bits) until it has the specified alignment. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 383 | template <class T> |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 384 | T AlignUp(T pointer, size_t alignment) { |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 385 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 386 | // reinterpret_cast behaviour for other types. |
| 387 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 388 | uint64_t pointer_raw = (uint64_t)pointer; |
| 389 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 390 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 391 | size_t align_step = (alignment - pointer_raw) % alignment; |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 392 | VIXL_ASSERT((pointer_raw + align_step) % alignment == 0); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 393 | |
| 394 | return (T)(pointer_raw + align_step); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 395 | } |
| 396 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 397 | // Decrement a pointer (up to 64 bits) until it has the specified alignment. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 398 | template <class T> |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 399 | T AlignDown(T pointer, size_t alignment) { |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 400 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 401 | // reinterpret_cast behaviour for other types. |
| 402 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 403 | uint64_t pointer_raw = (uint64_t)pointer; |
| 404 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 405 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 406 | size_t align_step = pointer_raw % alignment; |
| 407 | VIXL_ASSERT((pointer_raw - align_step) % alignment == 0); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 408 | |
| 409 | return (T)(pointer_raw - align_step); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 412 | template <typename T> |
| 413 | inline T ExtractBit(T value, unsigned bit) { |
| 414 | return (value >> bit) & T(1); |
| 415 | } |
| 416 | |
| 417 | template <typename Ts, typename Td> |
| 418 | inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) { |
| 419 | return Td((value >> least_significant_bit) & Ts(mask)); |
| 420 | } |
| 421 | |
| 422 | template <typename Ts, typename Td> |
| 423 | inline 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 | |
| 432 | template <typename Td, typename Ts> |
| 433 | inline 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 | |
| 446 | class 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 | |
| 539 | class 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 | |
| 592 | typedef int64_t Int64; |
| 593 | class Uint64; |
| 594 | class Uint128; |
| 595 | |
| 596 | class 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 | |
| 658 | class 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 Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame^] | 674 | return Uint32(static_cast<uint32_t>(data_)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 675 | } |
| 676 | Uint32 GetHigh32() const { return Uint32(data_ >> 32); } |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame^] | 677 | Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 678 | 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 | |
| 725 | class 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 | |
| 779 | Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {} |
| 780 | Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {} |
| 781 | |
| 782 | Int64 BitCount(Uint32 value); |
| 783 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 784 | } // namespace vixl |
| 785 | |
| 786 | #endif // VIXL_UTILS_H |