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> |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 32 | #include <limits> |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 33 | #include <vector> |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 34 | |
Alexandre Rames | 1f9074d | 2016-05-23 15:50:01 +0100 | [diff] [blame] | 35 | #include "compiler-intrinsics-vixl.h" |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 36 | #include "globals-vixl.h" |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 37 | |
| 38 | namespace vixl { |
| 39 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 40 | // Macros for compile-time format checking. |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 41 | #if GCC_VERSION_OR_NEWER(4, 4, 0) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 42 | #define PRINTF_CHECK(format_index, varargs_index) \ |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 43 | __attribute__((format(gnu_printf, format_index, varargs_index))) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 44 | #else |
| 45 | #define PRINTF_CHECK(format_index, varargs_index) |
| 46 | #endif |
| 47 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 48 | #ifdef __GNUC__ |
| 49 | #define VIXL_HAS_DEPRECATED_WITH_MSG |
| 50 | #elif defined(__clang__) |
| 51 | #ifdef __has_extension(attribute_deprecated_with_message) |
| 52 | #define VIXL_HAS_DEPRECATED_WITH_MSG |
| 53 | #endif |
| 54 | #endif |
| 55 | |
| 56 | #ifdef VIXL_HAS_DEPRECATED_WITH_MSG |
| 57 | #define VIXL_DEPRECATED(replaced_by, declarator) \ |
| 58 | __attribute__((deprecated("Use \"" replaced_by "\" instead"))) declarator |
| 59 | #else |
| 60 | #define VIXL_DEPRECATED(replaced_by, declarator) declarator |
| 61 | #endif |
| 62 | |
| 63 | #ifdef VIXL_DEBUG |
| 64 | #define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_UNREACHABLE() |
| 65 | #else |
| 66 | #define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_FALLTHROUGH() |
| 67 | #endif |
| 68 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 69 | template <typename T, size_t n> |
| 70 | size_t ArrayLength(const T (&)[n]) { |
| 71 | return n; |
| 72 | } |
| 73 | |
Jacob Bramley | 03c0b51 | 2019-02-22 16:42:06 +0000 | [diff] [blame] | 74 | inline uint64_t GetUintMask(unsigned bits) { |
| 75 | VIXL_ASSERT(bits <= 64); |
| 76 | uint64_t base = (bits >= 64) ? 0 : (UINT64_C(1) << bits); |
| 77 | return base - 1; |
| 78 | } |
| 79 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 80 | // Check number width. |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 81 | // TODO: Refactor these using templates. |
| 82 | inline bool IsIntN(unsigned n, uint32_t x) { |
Jacob Bramley | 6069fd4 | 2019-06-24 10:20:45 +0100 | [diff] [blame] | 83 | VIXL_ASSERT((0 < n) && (n <= 32)); |
| 84 | return x <= static_cast<uint32_t>(INT32_MAX >> (32 - n)); |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 85 | } |
| 86 | inline bool IsIntN(unsigned n, int32_t x) { |
Jacob Bramley | 6069fd4 | 2019-06-24 10:20:45 +0100 | [diff] [blame] | 87 | VIXL_ASSERT((0 < n) && (n <= 32)); |
| 88 | if (n == 32) return true; |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 89 | int32_t limit = INT32_C(1) << (n - 1); |
| 90 | return (-limit <= x) && (x < limit); |
| 91 | } |
| 92 | inline bool IsIntN(unsigned n, uint64_t x) { |
Jacob Bramley | 6069fd4 | 2019-06-24 10:20:45 +0100 | [diff] [blame] | 93 | VIXL_ASSERT((0 < n) && (n <= 64)); |
| 94 | return x <= static_cast<uint64_t>(INT64_MAX >> (64 - n)); |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 95 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 96 | inline bool IsIntN(unsigned n, int64_t x) { |
Jacob Bramley | 6069fd4 | 2019-06-24 10:20:45 +0100 | [diff] [blame] | 97 | VIXL_ASSERT((0 < n) && (n <= 64)); |
| 98 | if (n == 64) return true; |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 99 | int64_t limit = INT64_C(1) << (n - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 100 | return (-limit <= x) && (x < limit); |
| 101 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 102 | VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) { |
| 103 | return IsIntN(n, x); |
| 104 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 105 | |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 106 | inline bool IsUintN(unsigned n, uint32_t x) { |
Jacob Bramley | 03c0b51 | 2019-02-22 16:42:06 +0000 | [diff] [blame] | 107 | VIXL_ASSERT((0 < n) && (n <= 32)); |
| 108 | if (n >= 32) return true; |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 109 | return !(x >> n); |
| 110 | } |
| 111 | inline bool IsUintN(unsigned n, int32_t x) { |
| 112 | VIXL_ASSERT((0 < n) && (n < 32)); |
| 113 | // Convert to an unsigned integer to avoid implementation-defined behavior. |
| 114 | return !(static_cast<uint32_t>(x) >> n); |
| 115 | } |
| 116 | inline bool IsUintN(unsigned n, uint64_t x) { |
Jacob Bramley | 03c0b51 | 2019-02-22 16:42:06 +0000 | [diff] [blame] | 117 | VIXL_ASSERT((0 < n) && (n <= 64)); |
| 118 | if (n >= 64) return true; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 119 | return !(x >> n); |
| 120 | } |
Pierre Langlois | efe0c1f | 2016-11-24 11:54:47 +0000 | [diff] [blame] | 121 | inline bool IsUintN(unsigned n, int64_t x) { |
| 122 | VIXL_ASSERT((0 < n) && (n < 64)); |
| 123 | // Convert to an unsigned integer to avoid implementation-defined behavior. |
| 124 | return !(static_cast<uint64_t>(x) >> n); |
| 125 | } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 126 | VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) { |
| 127 | return IsUintN(n, x); |
| 128 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 129 | |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 130 | inline uint64_t TruncateToUintN(unsigned n, uint64_t x) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 131 | VIXL_ASSERT((0 < n) && (n < 64)); |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 132 | return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 133 | } |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 134 | VIXL_DEPRECATED("TruncateToUintN", |
| 135 | inline uint64_t truncate_to_intn(unsigned n, int64_t x)) { |
| 136 | return TruncateToUintN(n, x); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 137 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 138 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 139 | // clang-format off |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 140 | #define INT_1_TO_32_LIST(V) \ |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 141 | V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \ |
| 142 | V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \ |
| 143 | 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] | 144 | V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32) |
| 145 | |
| 146 | #define INT_33_TO_63_LIST(V) \ |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 147 | V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \ |
| 148 | V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \ |
| 149 | V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \ |
| 150 | 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] | 151 | |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 152 | #define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V) |
| 153 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 154 | // clang-format on |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 155 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 156 | #define DECLARE_IS_INT_N(N) \ |
| 157 | inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \ |
| 158 | VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \ |
| 159 | return IsIntN(N, x); \ |
| 160 | } |
| 161 | |
| 162 | #define DECLARE_IS_UINT_N(N) \ |
| 163 | inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \ |
| 164 | VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \ |
| 165 | return IsUintN(N, x); \ |
| 166 | } |
| 167 | |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 168 | #define DECLARE_TRUNCATE_TO_UINT_32(N) \ |
| 169 | inline uint32_t TruncateToUint##N(uint64_t x) { \ |
| 170 | return static_cast<uint32_t>(TruncateToUintN(N, x)); \ |
| 171 | } \ |
| 172 | VIXL_DEPRECATED("TruncateToUint" #N, \ |
| 173 | inline uint32_t truncate_to_int##N(int64_t x)) { \ |
| 174 | return TruncateToUint##N(x); \ |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 175 | } |
| 176 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 177 | INT_1_TO_63_LIST(DECLARE_IS_INT_N) |
| 178 | INT_1_TO_63_LIST(DECLARE_IS_UINT_N) |
Jacob Bramley | 3976edb | 2016-10-18 10:51:43 +0100 | [diff] [blame] | 179 | INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 180 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 181 | #undef DECLARE_IS_INT_N |
| 182 | #undef DECLARE_IS_UINT_N |
| 183 | #undef DECLARE_TRUNCATE_TO_INT_N |
| 184 | |
| 185 | // Bit field extraction. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 186 | inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 187 | VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) && |
| 188 | (msb >= lsb)); |
| 189 | if ((msb == 63) && (lsb == 0)) return x; |
armvixl | 578645f | 2013-08-15 17:21:42 +0100 | [diff] [blame] | 190 | return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 193 | |
Jacob Bramley | 199339d | 2019-08-05 18:49:13 +0100 | [diff] [blame] | 194 | inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 195 | VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) && |
| 196 | (msb >= lsb)); |
| 197 | return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x)); |
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 | |
Jacob Bramley | 6069fd4 | 2019-06-24 10:20:45 +0100 | [diff] [blame] | 201 | inline int64_t ExtractSignedBitfield64(int msb, int lsb, uint64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 202 | VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) && |
| 203 | (msb >= lsb)); |
| 204 | uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x); |
| 205 | // If the highest extracted bit is set, sign extend. |
| 206 | if ((temp >> (msb - lsb)) == 1) { |
| 207 | temp |= ~UINT64_C(0) << (msb - lsb); |
| 208 | } |
| 209 | int64_t result; |
| 210 | memcpy(&result, &temp, sizeof(result)); |
| 211 | return result; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Jacob Bramley | 199339d | 2019-08-05 18:49:13 +0100 | [diff] [blame] | 214 | inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint64_t x) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 215 | VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) && |
| 216 | (msb >= lsb)); |
| 217 | uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x)); |
| 218 | int32_t result; |
| 219 | memcpy(&result, &temp, sizeof(result)); |
| 220 | return result; |
| 221 | } |
| 222 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 223 | inline uint64_t RotateRight(uint64_t value, |
| 224 | unsigned int rotate, |
| 225 | unsigned int width) { |
| 226 | VIXL_ASSERT((width > 0) && (width <= 64)); |
| 227 | uint64_t width_mask = ~UINT64_C(0) >> (64 - width); |
| 228 | rotate &= 63; |
| 229 | if (rotate > 0) { |
| 230 | value &= width_mask; |
| 231 | value = (value << (width - rotate)) | (value >> rotate); |
| 232 | } |
| 233 | return value & width_mask; |
| 234 | } |
| 235 | |
| 236 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 237 | // Wrapper class for passing FP16 values through the assembler. |
| 238 | // This is purely to aid with type checking/casting. |
| 239 | class Float16 { |
| 240 | public: |
| 241 | explicit Float16(double dvalue); |
| 242 | Float16() : rawbits_(0x0) {} |
| 243 | friend uint16_t Float16ToRawbits(Float16 value); |
| 244 | friend Float16 RawbitsToFloat16(uint16_t bits); |
| 245 | |
| 246 | protected: |
| 247 | uint16_t rawbits_; |
| 248 | }; |
| 249 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 250 | // Floating point representation. |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 251 | uint16_t Float16ToRawbits(Float16 value); |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame] | 252 | |
| 253 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 254 | uint32_t FloatToRawbits(float value); |
| 255 | VIXL_DEPRECATED("FloatToRawbits", |
| 256 | inline uint32_t float_to_rawbits(float value)) { |
| 257 | return FloatToRawbits(value); |
| 258 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 259 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 260 | uint64_t DoubleToRawbits(double value); |
| 261 | VIXL_DEPRECATED("DoubleToRawbits", |
| 262 | inline uint64_t double_to_rawbits(double value)) { |
| 263 | return DoubleToRawbits(value); |
| 264 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 265 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 266 | Float16 RawbitsToFloat16(uint16_t bits); |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame] | 267 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 268 | float RawbitsToFloat(uint32_t bits); |
| 269 | VIXL_DEPRECATED("RawbitsToFloat", |
| 270 | inline float rawbits_to_float(uint32_t bits)) { |
| 271 | return RawbitsToFloat(bits); |
| 272 | } |
| 273 | |
| 274 | double RawbitsToDouble(uint64_t bits); |
| 275 | VIXL_DEPRECATED("RawbitsToDouble", |
| 276 | inline double rawbits_to_double(uint64_t bits)) { |
| 277 | return RawbitsToDouble(bits); |
| 278 | } |
| 279 | |
Jacob Bramley | 03c0b51 | 2019-02-22 16:42:06 +0000 | [diff] [blame] | 280 | // Convert unsigned to signed numbers in a well-defined way (using two's |
| 281 | // complement representations). |
| 282 | inline int64_t RawbitsToInt64(uint64_t bits) { |
| 283 | return (bits >= UINT64_C(0x8000000000000000)) |
| 284 | ? (-static_cast<int64_t>(-bits - 1) - 1) |
| 285 | : static_cast<int64_t>(bits); |
| 286 | } |
| 287 | |
| 288 | inline int32_t RawbitsToInt32(uint32_t bits) { |
| 289 | return (bits >= UINT64_C(0x80000000)) ? (-static_cast<int32_t>(-bits - 1) - 1) |
| 290 | : static_cast<int32_t>(bits); |
| 291 | } |
| 292 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 293 | namespace internal { |
| 294 | |
| 295 | // Internal simulation class used solely by the simulator to |
| 296 | // provide an abstraction layer for any half-precision arithmetic. |
| 297 | class SimFloat16 : public Float16 { |
| 298 | public: |
| 299 | // TODO: We should investigate making this constructor explicit. |
| 300 | // This is currently difficult to do due to a number of templated |
| 301 | // functions in the simulator which rely on returning double values. |
| 302 | SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit) |
| 303 | SimFloat16(Float16 f) { // NOLINT(runtime/explicit) |
| 304 | this->rawbits_ = Float16ToRawbits(f); |
| 305 | } |
| 306 | SimFloat16() : Float16() {} |
| 307 | SimFloat16 operator-() const; |
| 308 | SimFloat16 operator+(SimFloat16 rhs) const; |
| 309 | SimFloat16 operator-(SimFloat16 rhs) const; |
| 310 | SimFloat16 operator*(SimFloat16 rhs) const; |
| 311 | SimFloat16 operator/(SimFloat16 rhs) const; |
| 312 | bool operator<(SimFloat16 rhs) const; |
| 313 | bool operator>(SimFloat16 rhs) const; |
| 314 | bool operator==(SimFloat16 rhs) const; |
| 315 | bool operator!=(SimFloat16 rhs) const; |
| 316 | // This is necessary for conversions peformed in (macro asm) Fmov. |
| 317 | bool operator==(double rhs) const; |
| 318 | operator double() const; |
| 319 | }; |
| 320 | } // namespace internal |
| 321 | |
| 322 | uint32_t Float16Sign(internal::SimFloat16 value); |
| 323 | |
| 324 | uint32_t Float16Exp(internal::SimFloat16 value); |
| 325 | |
| 326 | uint32_t Float16Mantissa(internal::SimFloat16 value); |
| 327 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 328 | uint32_t FloatSign(float value); |
| 329 | VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) { |
| 330 | return FloatSign(value); |
| 331 | } |
| 332 | |
| 333 | uint32_t FloatExp(float value); |
| 334 | VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) { |
| 335 | return FloatExp(value); |
| 336 | } |
| 337 | |
| 338 | uint32_t FloatMantissa(float value); |
| 339 | VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) { |
| 340 | return FloatMantissa(value); |
| 341 | } |
| 342 | |
| 343 | uint32_t DoubleSign(double value); |
| 344 | VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) { |
| 345 | return DoubleSign(value); |
| 346 | } |
| 347 | |
| 348 | uint32_t DoubleExp(double value); |
| 349 | VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) { |
| 350 | return DoubleExp(value); |
| 351 | } |
| 352 | |
| 353 | uint64_t DoubleMantissa(double value); |
| 354 | VIXL_DEPRECATED("DoubleMantissa", |
| 355 | inline uint64_t double_mantissa(double value)) { |
| 356 | return DoubleMantissa(value); |
| 357 | } |
| 358 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 359 | internal::SimFloat16 Float16Pack(uint16_t sign, |
| 360 | uint16_t exp, |
| 361 | uint16_t mantissa); |
| 362 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 363 | float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa); |
| 364 | VIXL_DEPRECATED("FloatPack", |
| 365 | inline float float_pack(uint32_t sign, |
| 366 | uint32_t exp, |
| 367 | uint32_t mantissa)) { |
| 368 | return FloatPack(sign, exp, mantissa); |
| 369 | } |
| 370 | |
| 371 | double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa); |
| 372 | VIXL_DEPRECATED("DoublePack", |
| 373 | inline double double_pack(uint32_t sign, |
| 374 | uint32_t exp, |
| 375 | uint64_t mantissa)) { |
| 376 | return DoublePack(sign, exp, mantissa); |
| 377 | } |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 378 | |
| 379 | // An fpclassify() function for 16-bit half-precision floats. |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 380 | int Float16Classify(Float16 value); |
| 381 | VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) { |
| 382 | return Float16Classify(RawbitsToFloat16(value)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 383 | } |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 384 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 385 | bool IsZero(Float16 value); |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame] | 386 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 387 | inline bool IsNaN(float value) { return std::isnan(value); } |
| 388 | |
| 389 | inline bool IsNaN(double value) { return std::isnan(value); } |
| 390 | |
| 391 | inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; } |
| 392 | |
| 393 | inline bool IsInf(float value) { return std::isinf(value); } |
| 394 | |
| 395 | inline bool IsInf(double value) { return std::isinf(value); } |
| 396 | |
| 397 | inline bool IsInf(Float16 value) { |
| 398 | return Float16Classify(value) == FP_INFINITE; |
| 399 | } |
Carey Williams | d8bb357 | 2018-04-10 11:58:07 +0100 | [diff] [blame] | 400 | |
| 401 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 402 | // NaN tests. |
| 403 | inline bool IsSignallingNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 404 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 405 | uint64_t raw = DoubleToRawbits(num); |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 406 | if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | inline bool IsSignallingNaN(float num) { |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 414 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 415 | uint32_t raw = FloatToRawbits(num); |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 416 | if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) { |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 423 | inline bool IsSignallingNaN(Float16 num) { |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 424 | const uint16_t kFP16QuietNaNMask = 0x0200; |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 425 | return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0); |
armvixl | 5289c59 | 2015-03-02 13:52:04 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 429 | template <typename T> |
| 430 | inline bool IsQuietNaN(T num) { |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 431 | return IsNaN(num) && !IsSignallingNaN(num); |
armvixl | f37fdc0 | 2014-02-05 13:22:16 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 435 | // Convert the NaN in 'num' to a quiet NaN. |
| 436 | inline double ToQuietNaN(double num) { |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 437 | const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000); |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 438 | VIXL_ASSERT(IsNaN(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 439 | return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | inline float ToQuietNaN(float num) { |
| 444 | const uint32_t kFP32QuietNaNMask = 0x00400000; |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 445 | VIXL_ASSERT(IsNaN(num)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 446 | return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 450 | inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) { |
| 451 | const uint16_t kFP16QuietNaNMask = 0x0200; |
| 452 | VIXL_ASSERT(IsNaN(num)); |
| 453 | return internal::SimFloat16( |
| 454 | RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask)); |
| 455 | } |
| 456 | |
| 457 | |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 458 | // Fused multiply-add. |
| 459 | inline double FusedMultiplyAdd(double op1, double op2, double a) { |
| 460 | return fma(op1, op2, a); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | inline float FusedMultiplyAdd(float op1, float op2, float a) { |
| 465 | return fmaf(op1, op2, a); |
| 466 | } |
| 467 | |
| 468 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 469 | inline uint64_t LowestSetBit(uint64_t value) { return value & -value; } |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 470 | |
| 471 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 472 | template <typename T> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 473 | inline int HighestSetBitPosition(T value) { |
| 474 | VIXL_ASSERT(value != 0); |
| 475 | return (sizeof(value) * 8 - 1) - CountLeadingZeros(value); |
| 476 | } |
| 477 | |
| 478 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 479 | template <typename V> |
armvixl | 6e2c827 | 2015-03-31 11:04:14 +0100 | [diff] [blame] | 480 | inline int WhichPowerOf2(V value) { |
| 481 | VIXL_ASSERT(IsPowerOf2(value)); |
| 482 | return CountTrailingZeros(value); |
| 483 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 484 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 485 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 486 | unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size); |
| 487 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 488 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 489 | int BitCount(uint64_t value); |
| 490 | |
| 491 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 492 | template <typename T> |
| 493 | T ReverseBits(T value) { |
| 494 | VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) || |
| 495 | (sizeof(value) == 4) || (sizeof(value) == 8)); |
| 496 | T result = 0; |
| 497 | for (unsigned i = 0; i < (sizeof(value) * 8); i++) { |
| 498 | result = (result << 1) | (value & 1); |
| 499 | value >>= 1; |
| 500 | } |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | template <typename T> |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 506 | inline T SignExtend(T val, int bitSize) { |
| 507 | VIXL_ASSERT(bitSize > 0); |
| 508 | T mask = (T(2) << (bitSize - 1)) - T(1); |
| 509 | val &= mask; |
Vincent Belliard | 4e52d4d | 2018-04-03 13:34:44 -0700 | [diff] [blame] | 510 | T sign_bits = -((val >> (bitSize - 1)) << bitSize); |
| 511 | val |= sign_bits; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 512 | return val; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | template <typename T> |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 517 | T ReverseBytes(T value, int block_bytes_log2) { |
| 518 | VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8)); |
| 519 | VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value)); |
| 520 | // Split the 64-bit value into an 8-bit array, where b[0] is the least |
| 521 | // significant byte, and b[7] is the most significant. |
| 522 | uint8_t bytes[8]; |
armvixl | 788c84f | 2015-12-08 17:05:23 +0000 | [diff] [blame] | 523 | uint64_t mask = UINT64_C(0xff00000000000000); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 524 | for (int i = 7; i >= 0; i--) { |
| 525 | bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8); |
| 526 | mask >>= 8; |
| 527 | } |
| 528 | |
| 529 | // Permutation tables for REV instructions. |
| 530 | // permute_table[0] is used by REV16_x, REV16_w |
| 531 | // permute_table[1] is used by REV32_x, REV_w |
| 532 | // permute_table[2] is used by REV_x |
| 533 | VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4)); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 534 | static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1}, |
| 535 | {4, 5, 6, 7, 0, 1, 2, 3}, |
| 536 | {0, 1, 2, 3, 4, 5, 6, 7}}; |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 537 | uint64_t temp = 0; |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 538 | for (int i = 0; i < 8; i++) { |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 539 | temp <<= 8; |
| 540 | temp |= bytes[permute_table[block_bytes_log2 - 1][i]]; |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 541 | } |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 542 | |
| 543 | T result; |
| 544 | VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp)); |
| 545 | memcpy(&result, &temp, sizeof(result)); |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 546 | return result; |
| 547 | } |
| 548 | |
Vincent Belliard | 96ff2a4 | 2016-10-27 08:51:55 -0700 | [diff] [blame] | 549 | template <unsigned MULTIPLE, typename T> |
| 550 | inline bool IsMultiple(T value) { |
| 551 | VIXL_ASSERT(IsPowerOf2(MULTIPLE)); |
| 552 | return (value & (MULTIPLE - 1)) == 0; |
| 553 | } |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 554 | |
Martyn Capewell | fb8e3df | 2016-11-03 15:50:19 +0000 | [diff] [blame] | 555 | template <typename T> |
| 556 | inline bool IsMultiple(T value, unsigned multiple) { |
| 557 | VIXL_ASSERT(IsPowerOf2(multiple)); |
| 558 | return (value & (multiple - 1)) == 0; |
| 559 | } |
| 560 | |
Georgia Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 561 | template <typename T> |
| 562 | inline bool IsAligned(T pointer, int alignment) { |
| 563 | VIXL_ASSERT(IsPowerOf2(alignment)); |
| 564 | return (pointer & (alignment - 1)) == 0; |
| 565 | } |
| 566 | |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 567 | // Pointer alignment |
| 568 | // TODO: rename/refactor to make it specific to instructions. |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 569 | template <unsigned ALIGN, typename T> |
| 570 | inline bool IsAligned(T pointer) { |
| 571 | VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 572 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 573 | // reinterpret_cast behaviour for other types. |
Georgia Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 574 | return IsAligned((intptr_t)(pointer), ALIGN); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 575 | } |
| 576 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 577 | template <typename T> |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 578 | bool IsWordAligned(T pointer) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 579 | return IsAligned<4>(pointer); |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 582 | // Increment a pointer until it has the specified alignment. The alignment must |
| 583 | // be a power of two. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 584 | template <class T> |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 585 | T AlignUp(T pointer, |
| 586 | typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) { |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 587 | VIXL_ASSERT(IsPowerOf2(alignment)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 588 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 589 | // reinterpret_cast behaviour for other types. |
| 590 | |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 591 | typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw = |
| 592 | (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 593 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 594 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 595 | size_t mask = alignment - 1; |
| 596 | T result = (T)((pointer_raw + mask) & ~mask); |
Alexandre Rames | 47ed265 | 2016-11-09 14:44:06 +0000 | [diff] [blame] | 597 | VIXL_ASSERT(result >= pointer); |
| 598 | |
| 599 | return result; |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 602 | // Decrement a pointer until it has the specified alignment. The alignment must |
| 603 | // be a power of two. |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 604 | template <class T> |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 605 | T AlignDown(T pointer, |
| 606 | typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) { |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 607 | VIXL_ASSERT(IsPowerOf2(alignment)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 608 | // Use C-style casts to get static_cast behaviour for integral types (T), and |
| 609 | // reinterpret_cast behaviour for other types. |
| 610 | |
Pierre Langlois | 0ad8a6f | 2017-05-16 08:58:34 +0100 | [diff] [blame] | 611 | typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw = |
| 612 | (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 613 | VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw)); |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 614 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 615 | size_t mask = alignment - 1; |
| 616 | return (T)(pointer_raw & ~mask); |
armvixl | b0c8ae2 | 2014-03-21 14:03:59 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Georgia Kouveli | bbd09d6 | 2017-05-03 17:23:47 +0100 | [diff] [blame] | 619 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 620 | template <typename T> |
| 621 | inline T ExtractBit(T value, unsigned bit) { |
| 622 | return (value >> bit) & T(1); |
| 623 | } |
| 624 | |
| 625 | template <typename Ts, typename Td> |
| 626 | inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) { |
| 627 | return Td((value >> least_significant_bit) & Ts(mask)); |
| 628 | } |
| 629 | |
| 630 | template <typename Ts, typename Td> |
Vincent Belliard | 60241a5 | 2016-11-10 12:41:11 -0800 | [diff] [blame] | 631 | inline void AssignBit(Td& dst, // NOLINT(runtime/references) |
| 632 | int bit, |
| 633 | Ts value) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 634 | VIXL_ASSERT((value == Ts(0)) || (value == Ts(1))); |
| 635 | VIXL_ASSERT(bit >= 0); |
| 636 | VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8)); |
| 637 | Td mask(1); |
| 638 | dst &= ~(mask << bit); |
| 639 | dst |= Td(value) << bit; |
| 640 | } |
| 641 | |
| 642 | template <typename Td, typename Ts> |
Vincent Belliard | 60241a5 | 2016-11-10 12:41:11 -0800 | [diff] [blame] | 643 | inline void AssignBits(Td& dst, // NOLINT(runtime/references) |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 644 | int least_significant_bit, |
| 645 | Ts mask, |
| 646 | Ts value) { |
| 647 | VIXL_ASSERT(least_significant_bit >= 0); |
| 648 | VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8)); |
| 649 | VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) == |
| 650 | Td(mask)); |
| 651 | VIXL_ASSERT((value & mask) == value); |
| 652 | dst &= ~(Td(mask) << least_significant_bit); |
| 653 | dst |= Td(value) << least_significant_bit; |
| 654 | } |
| 655 | |
| 656 | class VFP { |
| 657 | public: |
| 658 | static uint32_t FP32ToImm8(float imm) { |
| 659 | // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000 |
| 660 | uint32_t bits = FloatToRawbits(imm); |
| 661 | // bit7: a000.0000 |
| 662 | uint32_t bit7 = ((bits >> 31) & 0x1) << 7; |
| 663 | // bit6: 0b00.0000 |
| 664 | uint32_t bit6 = ((bits >> 29) & 0x1) << 6; |
| 665 | // bit5_to_0: 00cd.efgh |
| 666 | uint32_t bit5_to_0 = (bits >> 19) & 0x3f; |
| 667 | return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0); |
| 668 | } |
| 669 | static uint32_t FP64ToImm8(double imm) { |
| 670 | // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000 |
| 671 | // 0000.0000.0000.0000.0000.0000.0000.0000 |
| 672 | uint64_t bits = DoubleToRawbits(imm); |
| 673 | // bit7: a000.0000 |
| 674 | uint64_t bit7 = ((bits >> 63) & 0x1) << 7; |
| 675 | // bit6: 0b00.0000 |
| 676 | uint64_t bit6 = ((bits >> 61) & 0x1) << 6; |
| 677 | // bit5_to_0: 00cd.efgh |
| 678 | uint64_t bit5_to_0 = (bits >> 48) & 0x3f; |
| 679 | |
| 680 | return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0); |
| 681 | } |
| 682 | static float Imm8ToFP32(uint32_t imm8) { |
| 683 | // Imm8: abcdefgh (8 bits) |
| 684 | // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits) |
| 685 | // where B is b ^ 1 |
| 686 | uint32_t bits = imm8; |
| 687 | uint32_t bit7 = (bits >> 7) & 0x1; |
| 688 | uint32_t bit6 = (bits >> 6) & 0x1; |
| 689 | uint32_t bit5_to_0 = bits & 0x3f; |
| 690 | uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19); |
| 691 | |
| 692 | return RawbitsToFloat(result); |
| 693 | } |
| 694 | static double Imm8ToFP64(uint32_t imm8) { |
| 695 | // Imm8: abcdefgh (8 bits) |
| 696 | // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000 |
| 697 | // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits) |
| 698 | // where B is b ^ 1 |
| 699 | uint32_t bits = imm8; |
| 700 | uint64_t bit7 = (bits >> 7) & 0x1; |
| 701 | uint64_t bit6 = (bits >> 6) & 0x1; |
| 702 | uint64_t bit5_to_0 = bits & 0x3f; |
| 703 | uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48); |
| 704 | return RawbitsToDouble(result); |
| 705 | } |
| 706 | static bool IsImmFP32(float imm) { |
| 707 | // Valid values will have the form: |
| 708 | // aBbb.bbbc.defg.h000.0000.0000.0000.0000 |
| 709 | uint32_t bits = FloatToRawbits(imm); |
| 710 | // bits[19..0] are cleared. |
| 711 | if ((bits & 0x7ffff) != 0) { |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | |
| 716 | // bits[29..25] are all set or all cleared. |
| 717 | uint32_t b_pattern = (bits >> 16) & 0x3e00; |
| 718 | if (b_pattern != 0 && b_pattern != 0x3e00) { |
| 719 | return false; |
| 720 | } |
| 721 | // bit[30] and bit[29] are opposite. |
| 722 | if (((bits ^ (bits << 1)) & 0x40000000) == 0) { |
| 723 | return false; |
| 724 | } |
| 725 | return true; |
| 726 | } |
| 727 | static bool IsImmFP64(double imm) { |
| 728 | // Valid values will have the form: |
| 729 | // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000 |
| 730 | // 0000.0000.0000.0000.0000.0000.0000.0000 |
| 731 | uint64_t bits = DoubleToRawbits(imm); |
| 732 | // bits[47..0] are cleared. |
| 733 | if ((bits & 0x0000ffffffffffff) != 0) { |
| 734 | return false; |
| 735 | } |
| 736 | // bits[61..54] are all set or all cleared. |
| 737 | uint32_t b_pattern = (bits >> 48) & 0x3fc0; |
| 738 | if ((b_pattern != 0) && (b_pattern != 0x3fc0)) { |
| 739 | return false; |
| 740 | } |
| 741 | // bit[62] and bit[61] are opposite. |
| 742 | if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) { |
| 743 | return false; |
| 744 | } |
| 745 | return true; |
| 746 | } |
| 747 | }; |
| 748 | |
| 749 | class BitField { |
| 750 | // ForEachBitHelper is a functor that will call |
| 751 | // bool ForEachBitHelper::execute(ElementType id) const |
| 752 | // and expects a boolean in return whether to continue (if true) |
| 753 | // or stop (if false) |
| 754 | // check_set will check if the bits are on (true) or off(false) |
| 755 | template <typename ForEachBitHelper, bool check_set> |
| 756 | bool ForEachBit(const ForEachBitHelper& helper) { |
| 757 | for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) { |
| 758 | if (bitfield_[i] == check_set) |
| 759 | if (!helper.execute(i)) return false; |
| 760 | } |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | public: |
| 765 | explicit BitField(unsigned size) : bitfield_(size, 0) {} |
| 766 | |
| 767 | void Set(int i) { |
| 768 | VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size())); |
| 769 | bitfield_[i] = true; |
| 770 | } |
| 771 | |
| 772 | void Unset(int i) { |
| 773 | VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size())); |
| 774 | bitfield_[i] = true; |
| 775 | } |
| 776 | |
| 777 | bool IsSet(int i) const { return bitfield_[i]; } |
| 778 | |
| 779 | // For each bit not set in the bitfield call the execute functor |
| 780 | // execute. |
| 781 | // ForEachBitSetHelper::execute returns true if the iteration through |
| 782 | // the bits can continue, otherwise it will stop. |
| 783 | // struct ForEachBitSetHelper { |
| 784 | // bool execute(int /*id*/) { return false; } |
| 785 | // }; |
| 786 | template <typename ForEachBitNotSetHelper> |
| 787 | bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) { |
| 788 | return ForEachBit<ForEachBitNotSetHelper, false>(helper); |
| 789 | } |
| 790 | |
| 791 | // For each bit set in the bitfield call the execute functor |
| 792 | // execute. |
| 793 | template <typename ForEachBitSetHelper> |
| 794 | bool ForEachBitSet(const ForEachBitSetHelper& helper) { |
| 795 | return ForEachBit<ForEachBitSetHelper, true>(helper); |
| 796 | } |
| 797 | |
| 798 | private: |
| 799 | std::vector<bool> bitfield_; |
| 800 | }; |
| 801 | |
Pierre Langlois | d82faf6 | 2018-04-04 13:06:58 +0100 | [diff] [blame] | 802 | namespace internal { |
| 803 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 804 | typedef int64_t Int64; |
| 805 | class Uint64; |
| 806 | class Uint128; |
| 807 | |
| 808 | class Uint32 { |
| 809 | uint32_t data_; |
| 810 | |
| 811 | public: |
| 812 | // Unlike uint32_t, Uint32 has a default constructor. |
| 813 | Uint32() { data_ = 0; } |
| 814 | explicit Uint32(uint32_t data) : data_(data) {} |
| 815 | inline explicit Uint32(Uint64 data); |
| 816 | uint32_t Get() const { return data_; } |
| 817 | template <int N> |
| 818 | int32_t GetSigned() const { |
| 819 | return ExtractSignedBitfield32(N - 1, 0, data_); |
| 820 | } |
| 821 | int32_t GetSigned() const { return data_; } |
| 822 | Uint32 operator~() const { return Uint32(~data_); } |
| 823 | Uint32 operator-() const { return Uint32(-data_); } |
| 824 | bool operator==(Uint32 value) const { return data_ == value.data_; } |
| 825 | bool operator!=(Uint32 value) const { return data_ != value.data_; } |
| 826 | bool operator>(Uint32 value) const { return data_ > value.data_; } |
| 827 | Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); } |
| 828 | Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); } |
| 829 | Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); } |
| 830 | Uint32 operator&=(Uint32 value) { |
| 831 | data_ &= value.data_; |
| 832 | return *this; |
| 833 | } |
| 834 | Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); } |
| 835 | Uint32 operator^=(Uint32 value) { |
| 836 | data_ ^= value.data_; |
| 837 | return *this; |
| 838 | } |
| 839 | Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); } |
| 840 | Uint32 operator|=(Uint32 value) { |
| 841 | data_ |= value.data_; |
| 842 | return *this; |
| 843 | } |
| 844 | // Unlike uint32_t, the shift functions can accept negative shift and |
| 845 | // return 0 when the shift is too big. |
| 846 | Uint32 operator>>(int shift) const { |
| 847 | if (shift == 0) return *this; |
| 848 | if (shift < 0) { |
| 849 | int tmp = -shift; |
| 850 | if (tmp >= 32) return Uint32(0); |
| 851 | return Uint32(data_ << tmp); |
| 852 | } |
| 853 | int tmp = shift; |
| 854 | if (tmp >= 32) return Uint32(0); |
| 855 | return Uint32(data_ >> tmp); |
| 856 | } |
| 857 | Uint32 operator<<(int shift) const { |
| 858 | if (shift == 0) return *this; |
| 859 | if (shift < 0) { |
| 860 | int tmp = -shift; |
| 861 | if (tmp >= 32) return Uint32(0); |
| 862 | return Uint32(data_ >> tmp); |
| 863 | } |
| 864 | int tmp = shift; |
| 865 | if (tmp >= 32) return Uint32(0); |
| 866 | return Uint32(data_ << tmp); |
| 867 | } |
| 868 | }; |
| 869 | |
| 870 | class Uint64 { |
| 871 | uint64_t data_; |
| 872 | |
| 873 | public: |
| 874 | // Unlike uint64_t, Uint64 has a default constructor. |
| 875 | Uint64() { data_ = 0; } |
| 876 | explicit Uint64(uint64_t data) : data_(data) {} |
| 877 | explicit Uint64(Uint32 data) : data_(data.Get()) {} |
| 878 | inline explicit Uint64(Uint128 data); |
| 879 | uint64_t Get() const { return data_; } |
| 880 | int64_t GetSigned(int N) const { |
| 881 | return ExtractSignedBitfield64(N - 1, 0, data_); |
| 882 | } |
| 883 | int64_t GetSigned() const { return data_; } |
| 884 | Uint32 ToUint32() const { |
| 885 | VIXL_ASSERT((data_ >> 32) == 0); |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 886 | return Uint32(static_cast<uint32_t>(data_)); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 887 | } |
| 888 | Uint32 GetHigh32() const { return Uint32(data_ >> 32); } |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 889 | Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); } |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 890 | Uint64 operator~() const { return Uint64(~data_); } |
| 891 | Uint64 operator-() const { return Uint64(-data_); } |
| 892 | bool operator==(Uint64 value) const { return data_ == value.data_; } |
| 893 | bool operator!=(Uint64 value) const { return data_ != value.data_; } |
| 894 | Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); } |
| 895 | Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); } |
| 896 | Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); } |
| 897 | Uint64 operator&=(Uint64 value) { |
| 898 | data_ &= value.data_; |
| 899 | return *this; |
| 900 | } |
| 901 | Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); } |
| 902 | Uint64 operator^=(Uint64 value) { |
| 903 | data_ ^= value.data_; |
| 904 | return *this; |
| 905 | } |
| 906 | Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); } |
| 907 | Uint64 operator|=(Uint64 value) { |
| 908 | data_ |= value.data_; |
| 909 | return *this; |
| 910 | } |
| 911 | // Unlike uint64_t, the shift functions can accept negative shift and |
| 912 | // return 0 when the shift is too big. |
| 913 | Uint64 operator>>(int shift) const { |
| 914 | if (shift == 0) return *this; |
| 915 | if (shift < 0) { |
| 916 | int tmp = -shift; |
| 917 | if (tmp >= 64) return Uint64(0); |
| 918 | return Uint64(data_ << tmp); |
| 919 | } |
| 920 | int tmp = shift; |
| 921 | if (tmp >= 64) return Uint64(0); |
| 922 | return Uint64(data_ >> tmp); |
| 923 | } |
| 924 | Uint64 operator<<(int shift) const { |
| 925 | if (shift == 0) return *this; |
| 926 | if (shift < 0) { |
| 927 | int tmp = -shift; |
| 928 | if (tmp >= 64) return Uint64(0); |
| 929 | return Uint64(data_ >> tmp); |
| 930 | } |
| 931 | int tmp = shift; |
| 932 | if (tmp >= 64) return Uint64(0); |
| 933 | return Uint64(data_ << tmp); |
| 934 | } |
| 935 | }; |
| 936 | |
| 937 | class Uint128 { |
| 938 | uint64_t data_high_; |
| 939 | uint64_t data_low_; |
| 940 | |
| 941 | public: |
| 942 | Uint128() : data_high_(0), data_low_(0) {} |
| 943 | explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {} |
| 944 | explicit Uint128(Uint64 data_low) |
| 945 | : data_high_(0), data_low_(data_low.Get()) {} |
| 946 | Uint128(uint64_t data_high, uint64_t data_low) |
| 947 | : data_high_(data_high), data_low_(data_low) {} |
| 948 | Uint64 ToUint64() const { |
| 949 | VIXL_ASSERT(data_high_ == 0); |
| 950 | return Uint64(data_low_); |
| 951 | } |
| 952 | Uint64 GetHigh64() const { return Uint64(data_high_); } |
| 953 | Uint64 GetLow64() const { return Uint64(data_low_); } |
| 954 | Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); } |
| 955 | bool operator==(Uint128 value) const { |
| 956 | return (data_high_ == value.data_high_) && (data_low_ == value.data_low_); |
| 957 | } |
| 958 | Uint128 operator&(Uint128 value) const { |
| 959 | return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_); |
| 960 | } |
| 961 | Uint128 operator&=(Uint128 value) { |
| 962 | data_high_ &= value.data_high_; |
| 963 | data_low_ &= value.data_low_; |
| 964 | return *this; |
| 965 | } |
| 966 | Uint128 operator|=(Uint128 value) { |
| 967 | data_high_ |= value.data_high_; |
| 968 | data_low_ |= value.data_low_; |
| 969 | return *this; |
| 970 | } |
| 971 | Uint128 operator>>(int shift) const { |
| 972 | VIXL_ASSERT((shift >= 0) && (shift < 128)); |
| 973 | if (shift == 0) return *this; |
| 974 | if (shift >= 64) { |
| 975 | return Uint128(0, data_high_ >> (shift - 64)); |
| 976 | } |
| 977 | uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift); |
| 978 | return Uint128(data_high_ >> shift, tmp); |
| 979 | } |
| 980 | Uint128 operator<<(int shift) const { |
| 981 | VIXL_ASSERT((shift >= 0) && (shift < 128)); |
| 982 | if (shift == 0) return *this; |
| 983 | if (shift >= 64) { |
| 984 | return Uint128(data_low_ << (shift - 64), 0); |
| 985 | } |
| 986 | uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift)); |
| 987 | return Uint128(tmp, data_low_ << shift); |
| 988 | } |
| 989 | }; |
| 990 | |
| 991 | Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {} |
| 992 | Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {} |
| 993 | |
| 994 | Int64 BitCount(Uint32 value); |
| 995 | |
TatWai Chong | 1363476 | 2019-07-16 16:20:45 -0700 | [diff] [blame] | 996 | // The algorithm used is adapted from the one described in section 8.2 of |
| 997 | // Hacker's Delight, by Henry S. Warren, Jr. |
| 998 | template <unsigned N, typename T> |
| 999 | int64_t MultiplyHigh(T u, T v) { |
| 1000 | uint64_t u0, v0, w0, u1, v1, w1, w2, t; |
| 1001 | VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64)); |
| 1002 | uint64_t sign_mask = UINT64_C(1) << (N - 1); |
| 1003 | uint64_t sign_ext = 0; |
| 1004 | unsigned half_bits = N / 2; |
| 1005 | uint64_t half_mask = GetUintMask(half_bits); |
| 1006 | if (std::numeric_limits<T>::is_signed) { |
| 1007 | sign_ext = UINT64_C(0xffffffffffffffff) << half_bits; |
| 1008 | } |
| 1009 | |
| 1010 | VIXL_ASSERT(sizeof(u) == sizeof(uint64_t)); |
| 1011 | VIXL_ASSERT(sizeof(u) == sizeof(u0)); |
| 1012 | |
| 1013 | u0 = u & half_mask; |
| 1014 | u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0); |
| 1015 | v0 = v & half_mask; |
| 1016 | v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0); |
| 1017 | |
| 1018 | w0 = u0 * v0; |
| 1019 | t = u1 * v0 + (w0 >> half_bits); |
| 1020 | |
| 1021 | w1 = t & half_mask; |
| 1022 | w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0); |
| 1023 | w1 = u0 * v1 + w1; |
| 1024 | w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0); |
| 1025 | |
| 1026 | uint64_t value = u1 * v1 + w2 + w1; |
| 1027 | int64_t result; |
| 1028 | memcpy(&result, &value, sizeof(result)); |
| 1029 | return result; |
| 1030 | } |
| 1031 | |
Pierre Langlois | d82faf6 | 2018-04-04 13:06:58 +0100 | [diff] [blame] | 1032 | } // namespace internal |
| 1033 | |
Jacob Bramley | ca78974 | 2018-09-13 14:25:46 +0100 | [diff] [blame] | 1034 | // The default NaN values (for FPCR.DN=1). |
| 1035 | extern const double kFP64DefaultNaN; |
| 1036 | extern const float kFP32DefaultNaN; |
| 1037 | extern const Float16 kFP16DefaultNaN; |
| 1038 | |
| 1039 | // Floating-point infinity values. |
| 1040 | extern const Float16 kFP16PositiveInfinity; |
| 1041 | extern const Float16 kFP16NegativeInfinity; |
| 1042 | extern const float kFP32PositiveInfinity; |
| 1043 | extern const float kFP32NegativeInfinity; |
| 1044 | extern const double kFP64PositiveInfinity; |
| 1045 | extern const double kFP64NegativeInfinity; |
| 1046 | |
| 1047 | // Floating-point zero values. |
| 1048 | extern const Float16 kFP16PositiveZero; |
| 1049 | extern const Float16 kFP16NegativeZero; |
| 1050 | |
| 1051 | // AArch64 floating-point specifics. These match IEEE-754. |
| 1052 | const unsigned kDoubleMantissaBits = 52; |
| 1053 | const unsigned kDoubleExponentBits = 11; |
| 1054 | const unsigned kFloatMantissaBits = 23; |
| 1055 | const unsigned kFloatExponentBits = 8; |
| 1056 | const unsigned kFloat16MantissaBits = 10; |
| 1057 | const unsigned kFloat16ExponentBits = 5; |
| 1058 | |
| 1059 | enum FPRounding { |
| 1060 | // The first four values are encodable directly by FPCR<RMode>. |
| 1061 | FPTieEven = 0x0, |
| 1062 | FPPositiveInfinity = 0x1, |
| 1063 | FPNegativeInfinity = 0x2, |
| 1064 | FPZero = 0x3, |
| 1065 | |
| 1066 | // The final rounding modes are only available when explicitly specified by |
| 1067 | // the instruction (such as with fcvta). It cannot be set in FPCR. |
| 1068 | FPTieAway, |
| 1069 | FPRoundOdd |
| 1070 | }; |
| 1071 | |
| 1072 | enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN }; |
| 1073 | |
| 1074 | // Assemble the specified IEEE-754 components into the target type and apply |
| 1075 | // appropriate rounding. |
| 1076 | // sign: 0 = positive, 1 = negative |
| 1077 | // exponent: Unbiased IEEE-754 exponent. |
| 1078 | // mantissa: The mantissa of the input. The top bit (which is not encoded for |
| 1079 | // normal IEEE-754 values) must not be omitted. This bit has the |
| 1080 | // value 'pow(2, exponent)'. |
| 1081 | // |
| 1082 | // The input value is assumed to be a normalized value. That is, the input may |
| 1083 | // not be infinity or NaN. If the source value is subnormal, it must be |
| 1084 | // normalized before calling this function such that the highest set bit in the |
| 1085 | // mantissa has the value 'pow(2, exponent)'. |
| 1086 | // |
| 1087 | // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than |
| 1088 | // calling a templated FPRound. |
| 1089 | template <class T, int ebits, int mbits> |
| 1090 | T FPRound(int64_t sign, |
| 1091 | int64_t exponent, |
| 1092 | uint64_t mantissa, |
| 1093 | FPRounding round_mode) { |
| 1094 | VIXL_ASSERT((sign == 0) || (sign == 1)); |
| 1095 | |
| 1096 | // Only FPTieEven and FPRoundOdd rounding modes are implemented. |
| 1097 | VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd)); |
| 1098 | |
| 1099 | // Rounding can promote subnormals to normals, and normals to infinities. For |
| 1100 | // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be |
| 1101 | // encodable as a float, but rounding based on the low-order mantissa bits |
| 1102 | // could make it overflow. With ties-to-even rounding, this value would become |
| 1103 | // an infinity. |
| 1104 | |
| 1105 | // ---- Rounding Method ---- |
| 1106 | // |
| 1107 | // The exponent is irrelevant in the rounding operation, so we treat the |
| 1108 | // lowest-order bit that will fit into the result ('onebit') as having |
| 1109 | // the value '1'. Similarly, the highest-order bit that won't fit into |
| 1110 | // the result ('halfbit') has the value '0.5'. The 'point' sits between |
| 1111 | // 'onebit' and 'halfbit': |
| 1112 | // |
| 1113 | // These bits fit into the result. |
| 1114 | // |---------------------| |
| 1115 | // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 1116 | // || |
| 1117 | // / | |
| 1118 | // / halfbit |
| 1119 | // onebit |
| 1120 | // |
| 1121 | // For subnormal outputs, the range of representable bits is smaller and |
| 1122 | // the position of onebit and halfbit depends on the exponent of the |
| 1123 | // input, but the method is otherwise similar. |
| 1124 | // |
| 1125 | // onebit(frac) |
| 1126 | // | |
| 1127 | // | halfbit(frac) halfbit(adjusted) |
| 1128 | // | / / |
| 1129 | // | | | |
| 1130 | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 |
| 1131 | // 0b00.0... -> 0b00.0... -> 0b00 |
| 1132 | // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 |
| 1133 | // 0b00.1... -> 0b00.1... -> 0b01 |
| 1134 | // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 |
| 1135 | // 0b01.0... -> 0b01.0... -> 0b01 |
| 1136 | // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 |
| 1137 | // 0b01.1... -> 0b01.1... -> 0b10 |
| 1138 | // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 |
| 1139 | // 0b10.0... -> 0b10.0... -> 0b10 |
| 1140 | // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 |
| 1141 | // 0b10.1... -> 0b10.1... -> 0b11 |
| 1142 | // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 |
| 1143 | // ... / | / | |
| 1144 | // / | / | |
| 1145 | // / | |
| 1146 | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | |
| 1147 | // |
| 1148 | // mantissa = (mantissa >> shift) + halfbit(adjusted); |
| 1149 | |
| 1150 | static const int mantissa_offset = 0; |
| 1151 | static const int exponent_offset = mantissa_offset + mbits; |
| 1152 | static const int sign_offset = exponent_offset + ebits; |
| 1153 | VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1)); |
| 1154 | |
| 1155 | // Bail out early for zero inputs. |
| 1156 | if (mantissa == 0) { |
| 1157 | return static_cast<T>(sign << sign_offset); |
| 1158 | } |
| 1159 | |
| 1160 | // If all bits in the exponent are set, the value is infinite or NaN. |
| 1161 | // This is true for all binary IEEE-754 formats. |
| 1162 | static const int infinite_exponent = (1 << ebits) - 1; |
| 1163 | static const int max_normal_exponent = infinite_exponent - 1; |
| 1164 | |
| 1165 | // Apply the exponent bias to encode it for the result. Doing this early makes |
| 1166 | // it easy to detect values that will be infinite or subnormal. |
| 1167 | exponent += max_normal_exponent >> 1; |
| 1168 | |
| 1169 | if (exponent > max_normal_exponent) { |
| 1170 | // Overflow: the input is too large for the result type to represent. |
| 1171 | if (round_mode == FPTieEven) { |
| 1172 | // FPTieEven rounding mode handles overflows using infinities. |
| 1173 | exponent = infinite_exponent; |
| 1174 | mantissa = 0; |
| 1175 | } else { |
| 1176 | VIXL_ASSERT(round_mode == FPRoundOdd); |
| 1177 | // FPRoundOdd rounding mode handles overflows using the largest magnitude |
| 1178 | // normal number. |
| 1179 | exponent = max_normal_exponent; |
| 1180 | mantissa = (UINT64_C(1) << exponent_offset) - 1; |
| 1181 | } |
| 1182 | return static_cast<T>((sign << sign_offset) | |
| 1183 | (exponent << exponent_offset) | |
| 1184 | (mantissa << mantissa_offset)); |
| 1185 | } |
| 1186 | |
| 1187 | // Calculate the shift required to move the top mantissa bit to the proper |
| 1188 | // place in the destination type. |
| 1189 | const int highest_significant_bit = 63 - CountLeadingZeros(mantissa); |
| 1190 | int shift = highest_significant_bit - mbits; |
| 1191 | |
| 1192 | if (exponent <= 0) { |
| 1193 | // The output will be subnormal (before rounding). |
| 1194 | // For subnormal outputs, the shift must be adjusted by the exponent. The +1 |
| 1195 | // is necessary because the exponent of a subnormal value (encoded as 0) is |
| 1196 | // the same as the exponent of the smallest normal value (encoded as 1). |
| 1197 | shift += -exponent + 1; |
| 1198 | |
| 1199 | // Handle inputs that would produce a zero output. |
| 1200 | // |
| 1201 | // Shifts higher than highest_significant_bit+1 will always produce a zero |
| 1202 | // result. A shift of exactly highest_significant_bit+1 might produce a |
| 1203 | // non-zero result after rounding. |
| 1204 | if (shift > (highest_significant_bit + 1)) { |
| 1205 | if (round_mode == FPTieEven) { |
| 1206 | // The result will always be +/-0.0. |
| 1207 | return static_cast<T>(sign << sign_offset); |
| 1208 | } else { |
| 1209 | VIXL_ASSERT(round_mode == FPRoundOdd); |
| 1210 | VIXL_ASSERT(mantissa != 0); |
| 1211 | // For FPRoundOdd, if the mantissa is too small to represent and |
| 1212 | // non-zero return the next "odd" value. |
| 1213 | return static_cast<T>((sign << sign_offset) | 1); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // Properly encode the exponent for a subnormal output. |
| 1218 | exponent = 0; |
| 1219 | } else { |
| 1220 | // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 |
| 1221 | // normal values. |
| 1222 | mantissa &= ~(UINT64_C(1) << highest_significant_bit); |
| 1223 | } |
| 1224 | |
| 1225 | // The casts below are only well-defined for unsigned integers. |
| 1226 | VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer); |
| 1227 | VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed); |
| 1228 | |
| 1229 | if (shift > 0) { |
| 1230 | if (round_mode == FPTieEven) { |
| 1231 | // We have to shift the mantissa to the right. Some precision is lost, so |
| 1232 | // we need to apply rounding. |
| 1233 | uint64_t onebit_mantissa = (mantissa >> (shift)) & 1; |
| 1234 | uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1; |
| 1235 | uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa); |
| 1236 | uint64_t adjusted = mantissa - adjustment; |
| 1237 | T halfbit_adjusted = (adjusted >> (shift - 1)) & 1; |
| 1238 | |
| 1239 | T result = |
| 1240 | static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) | |
| 1241 | ((mantissa >> shift) << mantissa_offset)); |
| 1242 | |
| 1243 | // A very large mantissa can overflow during rounding. If this happens, |
| 1244 | // the exponent should be incremented and the mantissa set to 1.0 |
| 1245 | // (encoded as 0). Applying halfbit_adjusted after assembling the float |
| 1246 | // has the nice side-effect that this case is handled for free. |
| 1247 | // |
| 1248 | // This also handles cases where a very large finite value overflows to |
| 1249 | // infinity, or where a very large subnormal value overflows to become |
| 1250 | // normal. |
| 1251 | return result + halfbit_adjusted; |
| 1252 | } else { |
| 1253 | VIXL_ASSERT(round_mode == FPRoundOdd); |
| 1254 | // If any bits at position halfbit or below are set, onebit (ie. the |
| 1255 | // bottom bit of the resulting mantissa) must be set. |
| 1256 | uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1); |
| 1257 | if (fractional_bits != 0) { |
| 1258 | mantissa |= UINT64_C(1) << shift; |
| 1259 | } |
| 1260 | |
| 1261 | return static_cast<T>((sign << sign_offset) | |
| 1262 | (exponent << exponent_offset) | |
| 1263 | ((mantissa >> shift) << mantissa_offset)); |
| 1264 | } |
| 1265 | } else { |
| 1266 | // We have to shift the mantissa to the left (or not at all). The input |
| 1267 | // mantissa is exactly representable in the output mantissa, so apply no |
| 1268 | // rounding correction. |
| 1269 | return static_cast<T>((sign << sign_offset) | |
| 1270 | (exponent << exponent_offset) | |
| 1271 | ((mantissa << -shift) << mantissa_offset)); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | // See FPRound for a description of this function. |
| 1277 | inline double FPRoundToDouble(int64_t sign, |
| 1278 | int64_t exponent, |
| 1279 | uint64_t mantissa, |
| 1280 | FPRounding round_mode) { |
| 1281 | uint64_t bits = |
| 1282 | FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign, |
| 1283 | exponent, |
| 1284 | mantissa, |
| 1285 | round_mode); |
| 1286 | return RawbitsToDouble(bits); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | // See FPRound for a description of this function. |
| 1291 | inline Float16 FPRoundToFloat16(int64_t sign, |
| 1292 | int64_t exponent, |
| 1293 | uint64_t mantissa, |
| 1294 | FPRounding round_mode) { |
| 1295 | return RawbitsToFloat16( |
| 1296 | FPRound<uint16_t, |
| 1297 | kFloat16ExponentBits, |
| 1298 | kFloat16MantissaBits>(sign, exponent, mantissa, round_mode)); |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | // See FPRound for a description of this function. |
| 1303 | static inline float FPRoundToFloat(int64_t sign, |
| 1304 | int64_t exponent, |
| 1305 | uint64_t mantissa, |
| 1306 | FPRounding round_mode) { |
| 1307 | uint32_t bits = |
| 1308 | FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign, |
| 1309 | exponent, |
| 1310 | mantissa, |
| 1311 | round_mode); |
| 1312 | return RawbitsToFloat(bits); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL); |
| 1317 | float FPToFloat(double value, |
| 1318 | FPRounding round_mode, |
| 1319 | UseDefaultNaN DN, |
| 1320 | bool* exception = NULL); |
| 1321 | |
| 1322 | double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL); |
| 1323 | double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL); |
| 1324 | |
| 1325 | Float16 FPToFloat16(float value, |
| 1326 | FPRounding round_mode, |
| 1327 | UseDefaultNaN DN, |
| 1328 | bool* exception = NULL); |
| 1329 | |
| 1330 | Float16 FPToFloat16(double value, |
| 1331 | FPRounding round_mode, |
| 1332 | UseDefaultNaN DN, |
| 1333 | bool* exception = NULL); |
Jacob Bramley | 0f62eab | 2019-10-23 17:07:47 +0100 | [diff] [blame^] | 1334 | |
| 1335 | // Like static_cast<T>(value), but with specialisations for the Float16 type. |
| 1336 | template <typename T, typename F> |
| 1337 | T StaticCastFPTo(F value) { |
| 1338 | return static_cast<T>(value); |
| 1339 | } |
| 1340 | |
| 1341 | template <> |
| 1342 | inline float StaticCastFPTo<float, Float16>(Float16 value) { |
| 1343 | return FPToFloat(value, kIgnoreDefaultNaN); |
| 1344 | } |
| 1345 | |
| 1346 | template <> |
| 1347 | inline double StaticCastFPTo<double, Float16>(Float16 value) { |
| 1348 | return FPToDouble(value, kIgnoreDefaultNaN); |
| 1349 | } |
| 1350 | |
| 1351 | template <> |
| 1352 | inline Float16 StaticCastFPTo<Float16, float>(float value) { |
| 1353 | return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN); |
| 1354 | } |
| 1355 | |
| 1356 | template <> |
| 1357 | inline Float16 StaticCastFPTo<Float16, double>(double value) { |
| 1358 | return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN); |
| 1359 | } |
| 1360 | |
| 1361 | template <typename T> |
| 1362 | uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) { |
| 1363 | switch (size_in_bits) { |
| 1364 | case 16: |
| 1365 | return Float16ToRawbits(StaticCastFPTo<Float16>(value)); |
| 1366 | case 32: |
| 1367 | return FloatToRawbits(StaticCastFPTo<float>(value)); |
| 1368 | case 64: |
| 1369 | return DoubleToRawbits(StaticCastFPTo<double>(value)); |
| 1370 | } |
| 1371 | VIXL_UNREACHABLE(); |
| 1372 | return 0; |
| 1373 | } |
armvixl | ad96eda | 2013-06-14 11:42:37 +0100 | [diff] [blame] | 1374 | } // namespace vixl |
| 1375 | |
| 1376 | #endif // VIXL_UTILS_H |