blob: 0ae6dfc044cf6276c6d04a8aeb15dc267e5d6447 [file] [log] [blame]
Alexandre Ramesb78f1392016-07-01 14:22:22 +01001// Copyright 2015, VIXL authors
armvixlad96eda2013-06-14 11:42:37 +01002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// * Neither the name of ARM Limited nor the names of its contributors may be
13// used to endorse or promote products derived from this software without
14// specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef VIXL_UTILS_H
28#define VIXL_UTILS_H
29
armvixl6e2c8272015-03-31 11:04:14 +010030#include <cmath>
Pierre Langlois78973f22016-08-10 14:35:56 +010031#include <cstring>
Jacob Bramleyca789742018-09-13 14:25:46 +010032#include <limits>
Pierre Langlois88c46b82016-06-02 18:15:32 +010033#include <vector>
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010034
Alexandre Rames1f9074d2016-05-23 15:50:01 +010035#include "compiler-intrinsics-vixl.h"
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010036#include "globals-vixl.h"
armvixlad96eda2013-06-14 11:42:37 +010037
38namespace vixl {
39
armvixl4a102ba2014-07-14 09:02:40 +010040// Macros for compile-time format checking.
armvixl788c84f2015-12-08 17:05:23 +000041#if GCC_VERSION_OR_NEWER(4, 4, 0)
armvixl4a102ba2014-07-14 09:02:40 +010042#define PRINTF_CHECK(format_index, varargs_index) \
armvixl788c84f2015-12-08 17:05:23 +000043 __attribute__((format(gnu_printf, format_index, varargs_index)))
armvixl4a102ba2014-07-14 09:02:40 +010044#else
45#define PRINTF_CHECK(format_index, varargs_index)
46#endif
47
Pierre Langlois88c46b82016-06-02 18:15:32 +010048#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 Bramleyca789742018-09-13 14:25:46 +010069template <typename T, size_t n>
TatWai Chong2cb1b612020-03-04 23:51:21 -080070constexpr size_t ArrayLength(const T (&)[n]) {
Jacob Bramleyca789742018-09-13 14:25:46 +010071 return n;
72}
73
Jacob Bramley03c0b512019-02-22 16:42:06 +000074inline 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
TatWai Chong29a0c432019-11-06 22:20:44 -080080inline uint64_t GetSignMask(unsigned bits) {
81 VIXL_ASSERT(bits <= 64);
82 return UINT64_C(1) << (bits - 1);
83}
84
armvixlad96eda2013-06-14 11:42:37 +010085// Check number width.
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000086// TODO: Refactor these using templates.
87inline bool IsIntN(unsigned n, uint32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010088 VIXL_ASSERT((0 < n) && (n <= 32));
89 return x <= static_cast<uint32_t>(INT32_MAX >> (32 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000090}
91inline bool IsIntN(unsigned n, int32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010092 VIXL_ASSERT((0 < n) && (n <= 32));
93 if (n == 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000094 int32_t limit = INT32_C(1) << (n - 1);
95 return (-limit <= x) && (x < limit);
96}
97inline bool IsIntN(unsigned n, uint64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010098 VIXL_ASSERT((0 < n) && (n <= 64));
99 return x <= static_cast<uint64_t>(INT64_MAX >> (64 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000100}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100101inline bool IsIntN(unsigned n, int64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +0100102 VIXL_ASSERT((0 < n) && (n <= 64));
103 if (n == 64) return true;
armvixlb0c8ae22014-03-21 14:03:59 +0000104 int64_t limit = INT64_C(1) << (n - 1);
armvixlad96eda2013-06-14 11:42:37 +0100105 return (-limit <= x) && (x < limit);
106}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100107VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
108 return IsIntN(n, x);
109}
armvixlad96eda2013-06-14 11:42:37 +0100110
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000111inline bool IsUintN(unsigned n, uint32_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000112 VIXL_ASSERT((0 < n) && (n <= 32));
113 if (n >= 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000114 return !(x >> n);
115}
116inline bool IsUintN(unsigned n, int32_t x) {
117 VIXL_ASSERT((0 < n) && (n < 32));
118 // Convert to an unsigned integer to avoid implementation-defined behavior.
119 return !(static_cast<uint32_t>(x) >> n);
120}
121inline bool IsUintN(unsigned n, uint64_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000122 VIXL_ASSERT((0 < n) && (n <= 64));
123 if (n >= 64) return true;
armvixlad96eda2013-06-14 11:42:37 +0100124 return !(x >> n);
125}
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000126inline bool IsUintN(unsigned n, int64_t x) {
127 VIXL_ASSERT((0 < n) && (n < 64));
128 // Convert to an unsigned integer to avoid implementation-defined behavior.
129 return !(static_cast<uint64_t>(x) >> n);
130}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100131VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
132 return IsUintN(n, x);
133}
armvixlad96eda2013-06-14 11:42:37 +0100134
Jacob Bramley3976edb2016-10-18 10:51:43 +0100135inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000136 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +0100137 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100138}
Jacob Bramley3976edb2016-10-18 10:51:43 +0100139VIXL_DEPRECATED("TruncateToUintN",
140 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
141 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100142}
armvixlad96eda2013-06-14 11:42:37 +0100143
armvixl0f35e362016-05-10 13:57:58 +0100144// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +0100145#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100146V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
147V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
148V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100149V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
150
151#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100152V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
153V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
154V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
155V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100156
Jacob Bramley3976edb2016-10-18 10:51:43 +0100157#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
158
armvixl0f35e362016-05-10 13:57:58 +0100159// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100160
Pierre Langlois88c46b82016-06-02 18:15:32 +0100161#define DECLARE_IS_INT_N(N) \
162 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
163 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
164 return IsIntN(N, x); \
165 }
166
167#define DECLARE_IS_UINT_N(N) \
168 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
169 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
170 return IsUintN(N, x); \
171 }
172
Jacob Bramley3976edb2016-10-18 10:51:43 +0100173#define DECLARE_TRUNCATE_TO_UINT_32(N) \
174 inline uint32_t TruncateToUint##N(uint64_t x) { \
175 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
176 } \
177 VIXL_DEPRECATED("TruncateToUint" #N, \
178 inline uint32_t truncate_to_int##N(int64_t x)) { \
179 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100180 }
181
armvixlad96eda2013-06-14 11:42:37 +0100182INT_1_TO_63_LIST(DECLARE_IS_INT_N)
183INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100184INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100185
armvixlad96eda2013-06-14 11:42:37 +0100186#undef DECLARE_IS_INT_N
187#undef DECLARE_IS_UINT_N
188#undef DECLARE_TRUNCATE_TO_INT_N
189
190// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100191inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000192 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
193 (msb >= lsb));
194 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100195 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100196}
197
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000198
Jacob Bramley199339d2019-08-05 18:49:13 +0100199inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000200 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
201 (msb >= lsb));
202 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100203}
204
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000205
Jacob Bramley6069fd42019-06-24 10:20:45 +0100206inline int64_t ExtractSignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000207 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
208 (msb >= lsb));
209 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
210 // If the highest extracted bit is set, sign extend.
211 if ((temp >> (msb - lsb)) == 1) {
212 temp |= ~UINT64_C(0) << (msb - lsb);
213 }
214 int64_t result;
215 memcpy(&result, &temp, sizeof(result));
216 return result;
armvixlad96eda2013-06-14 11:42:37 +0100217}
218
Jacob Bramley199339d2019-08-05 18:49:13 +0100219inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000220 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
221 (msb >= lsb));
222 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
223 int32_t result;
224 memcpy(&result, &temp, sizeof(result));
225 return result;
226}
227
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000228inline uint64_t RotateRight(uint64_t value,
229 unsigned int rotate,
230 unsigned int width) {
231 VIXL_ASSERT((width > 0) && (width <= 64));
232 uint64_t width_mask = ~UINT64_C(0) >> (64 - width);
233 rotate &= 63;
234 if (rotate > 0) {
235 value &= width_mask;
236 value = (value << (width - rotate)) | (value >> rotate);
237 }
238 return value & width_mask;
239}
240
241
Jacob Bramleyca789742018-09-13 14:25:46 +0100242// Wrapper class for passing FP16 values through the assembler.
243// This is purely to aid with type checking/casting.
244class Float16 {
245 public:
246 explicit Float16(double dvalue);
247 Float16() : rawbits_(0x0) {}
248 friend uint16_t Float16ToRawbits(Float16 value);
249 friend Float16 RawbitsToFloat16(uint16_t bits);
250
251 protected:
252 uint16_t rawbits_;
253};
254
armvixlf37fdc02014-02-05 13:22:16 +0000255// Floating point representation.
Jacob Bramleyca789742018-09-13 14:25:46 +0100256uint16_t Float16ToRawbits(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100257
258
Pierre Langlois88c46b82016-06-02 18:15:32 +0100259uint32_t FloatToRawbits(float value);
260VIXL_DEPRECATED("FloatToRawbits",
261 inline uint32_t float_to_rawbits(float value)) {
262 return FloatToRawbits(value);
263}
armvixlad96eda2013-06-14 11:42:37 +0100264
Pierre Langlois88c46b82016-06-02 18:15:32 +0100265uint64_t DoubleToRawbits(double value);
266VIXL_DEPRECATED("DoubleToRawbits",
267 inline uint64_t double_to_rawbits(double value)) {
268 return DoubleToRawbits(value);
269}
armvixl5289c592015-03-02 13:52:04 +0000270
Jacob Bramleyca789742018-09-13 14:25:46 +0100271Float16 RawbitsToFloat16(uint16_t bits);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100272
Pierre Langlois88c46b82016-06-02 18:15:32 +0100273float RawbitsToFloat(uint32_t bits);
274VIXL_DEPRECATED("RawbitsToFloat",
275 inline float rawbits_to_float(uint32_t bits)) {
276 return RawbitsToFloat(bits);
277}
278
279double RawbitsToDouble(uint64_t bits);
280VIXL_DEPRECATED("RawbitsToDouble",
281 inline double rawbits_to_double(uint64_t bits)) {
282 return RawbitsToDouble(bits);
283}
284
Jacob Bramley03c0b512019-02-22 16:42:06 +0000285// Convert unsigned to signed numbers in a well-defined way (using two's
286// complement representations).
287inline int64_t RawbitsToInt64(uint64_t bits) {
288 return (bits >= UINT64_C(0x8000000000000000))
289 ? (-static_cast<int64_t>(-bits - 1) - 1)
290 : static_cast<int64_t>(bits);
291}
292
293inline int32_t RawbitsToInt32(uint32_t bits) {
294 return (bits >= UINT64_C(0x80000000)) ? (-static_cast<int32_t>(-bits - 1) - 1)
295 : static_cast<int32_t>(bits);
296}
297
Jacob Bramleyca789742018-09-13 14:25:46 +0100298namespace internal {
299
300// Internal simulation class used solely by the simulator to
301// provide an abstraction layer for any half-precision arithmetic.
302class SimFloat16 : public Float16 {
303 public:
304 // TODO: We should investigate making this constructor explicit.
305 // This is currently difficult to do due to a number of templated
306 // functions in the simulator which rely on returning double values.
307 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
308 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
309 this->rawbits_ = Float16ToRawbits(f);
310 }
311 SimFloat16() : Float16() {}
312 SimFloat16 operator-() const;
313 SimFloat16 operator+(SimFloat16 rhs) const;
314 SimFloat16 operator-(SimFloat16 rhs) const;
315 SimFloat16 operator*(SimFloat16 rhs) const;
316 SimFloat16 operator/(SimFloat16 rhs) const;
317 bool operator<(SimFloat16 rhs) const;
318 bool operator>(SimFloat16 rhs) const;
319 bool operator==(SimFloat16 rhs) const;
320 bool operator!=(SimFloat16 rhs) const;
321 // This is necessary for conversions peformed in (macro asm) Fmov.
322 bool operator==(double rhs) const;
323 operator double() const;
324};
325} // namespace internal
326
327uint32_t Float16Sign(internal::SimFloat16 value);
328
329uint32_t Float16Exp(internal::SimFloat16 value);
330
331uint32_t Float16Mantissa(internal::SimFloat16 value);
332
Pierre Langlois88c46b82016-06-02 18:15:32 +0100333uint32_t FloatSign(float value);
334VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
335 return FloatSign(value);
336}
337
338uint32_t FloatExp(float value);
339VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
340 return FloatExp(value);
341}
342
343uint32_t FloatMantissa(float value);
344VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
345 return FloatMantissa(value);
346}
347
348uint32_t DoubleSign(double value);
349VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
350 return DoubleSign(value);
351}
352
353uint32_t DoubleExp(double value);
354VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
355 return DoubleExp(value);
356}
357
358uint64_t DoubleMantissa(double value);
359VIXL_DEPRECATED("DoubleMantissa",
360 inline uint64_t double_mantissa(double value)) {
361 return DoubleMantissa(value);
362}
363
Jacob Bramleyca789742018-09-13 14:25:46 +0100364internal::SimFloat16 Float16Pack(uint16_t sign,
365 uint16_t exp,
366 uint16_t mantissa);
367
Pierre Langlois88c46b82016-06-02 18:15:32 +0100368float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
369VIXL_DEPRECATED("FloatPack",
370 inline float float_pack(uint32_t sign,
371 uint32_t exp,
372 uint32_t mantissa)) {
373 return FloatPack(sign, exp, mantissa);
374}
375
376double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
377VIXL_DEPRECATED("DoublePack",
378 inline double double_pack(uint32_t sign,
379 uint32_t exp,
380 uint64_t mantissa)) {
381 return DoublePack(sign, exp, mantissa);
382}
armvixl5289c592015-03-02 13:52:04 +0000383
384// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100385int Float16Classify(Float16 value);
386VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
387 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100388}
armvixlf37fdc02014-02-05 13:22:16 +0000389
Jacob Bramleyca789742018-09-13 14:25:46 +0100390bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100391
Jacob Bramleyf48172b2020-07-13 14:47:15 +0100392inline bool IsPositiveZero(double value) {
393 return (value == 0.0) && (copysign(1.0, value) > 0.0);
394}
395
Jacob Bramleyca789742018-09-13 14:25:46 +0100396inline bool IsNaN(float value) { return std::isnan(value); }
397
398inline bool IsNaN(double value) { return std::isnan(value); }
399
400inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
401
402inline bool IsInf(float value) { return std::isinf(value); }
403
404inline bool IsInf(double value) { return std::isinf(value); }
405
406inline bool IsInf(Float16 value) {
407 return Float16Classify(value) == FP_INFINITE;
408}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100409
410
armvixlf37fdc02014-02-05 13:22:16 +0000411// NaN tests.
412inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100413 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100414 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100415 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000416 return true;
417 }
418 return false;
419}
420
421
422inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000423 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100424 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100425 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000426 return true;
427 }
428 return false;
429}
430
431
Jacob Bramleyca789742018-09-13 14:25:46 +0100432inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000433 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100434 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000435}
436
437
armvixlf37fdc02014-02-05 13:22:16 +0000438template <typename T>
439inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100440 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000441}
442
443
armvixlb0c8ae22014-03-21 14:03:59 +0000444// Convert the NaN in 'num' to a quiet NaN.
445inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100446 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100447 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100448 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000449}
450
451
452inline float ToQuietNaN(float num) {
453 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100454 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100455 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000456}
457
458
Jacob Bramleyca789742018-09-13 14:25:46 +0100459inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
460 const uint16_t kFP16QuietNaNMask = 0x0200;
461 VIXL_ASSERT(IsNaN(num));
462 return internal::SimFloat16(
463 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
464}
465
466
armvixlb0c8ae22014-03-21 14:03:59 +0000467// Fused multiply-add.
468inline double FusedMultiplyAdd(double op1, double op2, double a) {
469 return fma(op1, op2, a);
470}
471
472
473inline float FusedMultiplyAdd(float op1, float op2, float a) {
474 return fmaf(op1, op2, a);
475}
476
477
armvixl0f35e362016-05-10 13:57:58 +0100478inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100479
480
armvixl0f35e362016-05-10 13:57:58 +0100481template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100482inline int HighestSetBitPosition(T value) {
483 VIXL_ASSERT(value != 0);
484 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
485}
486
487
armvixl0f35e362016-05-10 13:57:58 +0100488template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100489inline int WhichPowerOf2(V value) {
490 VIXL_ASSERT(IsPowerOf2(value));
491 return CountTrailingZeros(value);
492}
armvixlad96eda2013-06-14 11:42:37 +0100493
armvixldb644342015-07-21 11:37:10 +0100494
armvixl330dc712014-11-25 10:38:32 +0000495unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
496
armvixldb644342015-07-21 11:37:10 +0100497
Pierre Langlois88c46b82016-06-02 18:15:32 +0100498int BitCount(uint64_t value);
499
500
armvixldb644342015-07-21 11:37:10 +0100501template <typename T>
502T ReverseBits(T value) {
503 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
504 (sizeof(value) == 4) || (sizeof(value) == 8));
505 T result = 0;
506 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
507 result = (result << 1) | (value & 1);
508 value >>= 1;
509 }
510 return result;
511}
512
513
514template <typename T>
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000515inline T SignExtend(T val, int size_in_bits) {
516 VIXL_ASSERT(size_in_bits > 0);
517 T mask = (T(2) << (size_in_bits - 1)) - T(1);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100518 val &= mask;
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000519 T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700520 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100521 return val;
522}
523
524
525template <typename T>
armvixldb644342015-07-21 11:37:10 +0100526T ReverseBytes(T value, int block_bytes_log2) {
527 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
528 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
529 // Split the 64-bit value into an 8-bit array, where b[0] is the least
530 // significant byte, and b[7] is the most significant.
531 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000532 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100533 for (int i = 7; i >= 0; i--) {
534 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
535 mask >>= 8;
536 }
537
538 // Permutation tables for REV instructions.
539 // permute_table[0] is used by REV16_x, REV16_w
540 // permute_table[1] is used by REV32_x, REV_w
541 // permute_table[2] is used by REV_x
542 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100543 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
544 {4, 5, 6, 7, 0, 1, 2, 3},
545 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000546 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100547 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000548 temp <<= 8;
549 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100550 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000551
552 T result;
553 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
554 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100555 return result;
556}
557
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700558template <unsigned MULTIPLE, typename T>
559inline bool IsMultiple(T value) {
560 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
561 return (value & (MULTIPLE - 1)) == 0;
562}
armvixldb644342015-07-21 11:37:10 +0100563
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000564template <typename T>
565inline bool IsMultiple(T value, unsigned multiple) {
566 VIXL_ASSERT(IsPowerOf2(multiple));
567 return (value & (multiple - 1)) == 0;
568}
569
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000570template <typename T>
571inline bool IsAligned(T pointer, int alignment) {
572 VIXL_ASSERT(IsPowerOf2(alignment));
573 return (pointer & (alignment - 1)) == 0;
574}
575
armvixlad96eda2013-06-14 11:42:37 +0100576// Pointer alignment
577// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100578template <unsigned ALIGN, typename T>
579inline bool IsAligned(T pointer) {
580 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100581 // Use C-style casts to get static_cast behaviour for integral types (T), and
582 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000583 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100584}
585
armvixl0f35e362016-05-10 13:57:58 +0100586template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100587bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100588 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100589}
590
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100591// Increment a pointer until it has the specified alignment. The alignment must
592// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100593template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100594T AlignUp(T pointer,
595 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100596 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100597 // Use C-style casts to get static_cast behaviour for integral types (T), and
598 // reinterpret_cast behaviour for other types.
599
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100600 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000601 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000602 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100603
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100604 size_t mask = alignment - 1;
605 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000606 VIXL_ASSERT(result >= pointer);
607
608 return result;
armvixlad96eda2013-06-14 11:42:37 +0100609}
610
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100611// Decrement a pointer until it has the specified alignment. The alignment must
612// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100613template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100614T AlignDown(T pointer,
615 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100616 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100617 // Use C-style casts to get static_cast behaviour for integral types (T), and
618 // reinterpret_cast behaviour for other types.
619
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100620 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000621 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000622 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100623
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100624 size_t mask = alignment - 1;
625 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000626}
627
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100628
Pierre Langlois88c46b82016-06-02 18:15:32 +0100629template <typename T>
630inline T ExtractBit(T value, unsigned bit) {
631 return (value >> bit) & T(1);
632}
633
634template <typename Ts, typename Td>
635inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
636 return Td((value >> least_significant_bit) & Ts(mask));
637}
638
639template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800640inline void AssignBit(Td& dst, // NOLINT(runtime/references)
641 int bit,
642 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100643 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
644 VIXL_ASSERT(bit >= 0);
645 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
646 Td mask(1);
647 dst &= ~(mask << bit);
648 dst |= Td(value) << bit;
649}
650
651template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800652inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100653 int least_significant_bit,
654 Ts mask,
655 Ts value) {
656 VIXL_ASSERT(least_significant_bit >= 0);
657 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
658 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
659 Td(mask));
660 VIXL_ASSERT((value & mask) == value);
661 dst &= ~(Td(mask) << least_significant_bit);
662 dst |= Td(value) << least_significant_bit;
663}
664
665class VFP {
666 public:
667 static uint32_t FP32ToImm8(float imm) {
668 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
669 uint32_t bits = FloatToRawbits(imm);
670 // bit7: a000.0000
671 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
672 // bit6: 0b00.0000
673 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
674 // bit5_to_0: 00cd.efgh
675 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
676 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
677 }
678 static uint32_t FP64ToImm8(double imm) {
679 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
680 // 0000.0000.0000.0000.0000.0000.0000.0000
681 uint64_t bits = DoubleToRawbits(imm);
682 // bit7: a000.0000
683 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
684 // bit6: 0b00.0000
685 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
686 // bit5_to_0: 00cd.efgh
687 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
688
689 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
690 }
691 static float Imm8ToFP32(uint32_t imm8) {
692 // Imm8: abcdefgh (8 bits)
693 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
694 // where B is b ^ 1
695 uint32_t bits = imm8;
696 uint32_t bit7 = (bits >> 7) & 0x1;
697 uint32_t bit6 = (bits >> 6) & 0x1;
698 uint32_t bit5_to_0 = bits & 0x3f;
699 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
700
701 return RawbitsToFloat(result);
702 }
703 static double Imm8ToFP64(uint32_t imm8) {
704 // Imm8: abcdefgh (8 bits)
705 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
706 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
707 // where B is b ^ 1
708 uint32_t bits = imm8;
709 uint64_t bit7 = (bits >> 7) & 0x1;
710 uint64_t bit6 = (bits >> 6) & 0x1;
711 uint64_t bit5_to_0 = bits & 0x3f;
712 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
713 return RawbitsToDouble(result);
714 }
715 static bool IsImmFP32(float imm) {
716 // Valid values will have the form:
717 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
718 uint32_t bits = FloatToRawbits(imm);
719 // bits[19..0] are cleared.
720 if ((bits & 0x7ffff) != 0) {
721 return false;
722 }
723
724
725 // bits[29..25] are all set or all cleared.
726 uint32_t b_pattern = (bits >> 16) & 0x3e00;
727 if (b_pattern != 0 && b_pattern != 0x3e00) {
728 return false;
729 }
730 // bit[30] and bit[29] are opposite.
731 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
732 return false;
733 }
734 return true;
735 }
736 static bool IsImmFP64(double imm) {
737 // Valid values will have the form:
738 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
739 // 0000.0000.0000.0000.0000.0000.0000.0000
740 uint64_t bits = DoubleToRawbits(imm);
741 // bits[47..0] are cleared.
742 if ((bits & 0x0000ffffffffffff) != 0) {
743 return false;
744 }
745 // bits[61..54] are all set or all cleared.
746 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
747 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
748 return false;
749 }
750 // bit[62] and bit[61] are opposite.
751 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
752 return false;
753 }
754 return true;
755 }
756};
757
758class BitField {
759 // ForEachBitHelper is a functor that will call
760 // bool ForEachBitHelper::execute(ElementType id) const
761 // and expects a boolean in return whether to continue (if true)
762 // or stop (if false)
763 // check_set will check if the bits are on (true) or off(false)
764 template <typename ForEachBitHelper, bool check_set>
765 bool ForEachBit(const ForEachBitHelper& helper) {
766 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
767 if (bitfield_[i] == check_set)
768 if (!helper.execute(i)) return false;
769 }
770 return true;
771 }
772
773 public:
774 explicit BitField(unsigned size) : bitfield_(size, 0) {}
775
776 void Set(int i) {
777 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
778 bitfield_[i] = true;
779 }
780
781 void Unset(int i) {
782 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
783 bitfield_[i] = true;
784 }
785
786 bool IsSet(int i) const { return bitfield_[i]; }
787
788 // For each bit not set in the bitfield call the execute functor
789 // execute.
790 // ForEachBitSetHelper::execute returns true if the iteration through
791 // the bits can continue, otherwise it will stop.
792 // struct ForEachBitSetHelper {
793 // bool execute(int /*id*/) { return false; }
794 // };
795 template <typename ForEachBitNotSetHelper>
796 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
797 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
798 }
799
800 // For each bit set in the bitfield call the execute functor
801 // execute.
802 template <typename ForEachBitSetHelper>
803 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
804 return ForEachBit<ForEachBitSetHelper, true>(helper);
805 }
806
807 private:
808 std::vector<bool> bitfield_;
809};
810
Pierre Langloisd82faf62018-04-04 13:06:58 +0100811namespace internal {
812
Pierre Langlois88c46b82016-06-02 18:15:32 +0100813typedef int64_t Int64;
814class Uint64;
815class Uint128;
816
817class Uint32 {
818 uint32_t data_;
819
820 public:
821 // Unlike uint32_t, Uint32 has a default constructor.
822 Uint32() { data_ = 0; }
823 explicit Uint32(uint32_t data) : data_(data) {}
824 inline explicit Uint32(Uint64 data);
825 uint32_t Get() const { return data_; }
826 template <int N>
827 int32_t GetSigned() const {
828 return ExtractSignedBitfield32(N - 1, 0, data_);
829 }
830 int32_t GetSigned() const { return data_; }
831 Uint32 operator~() const { return Uint32(~data_); }
832 Uint32 operator-() const { return Uint32(-data_); }
833 bool operator==(Uint32 value) const { return data_ == value.data_; }
834 bool operator!=(Uint32 value) const { return data_ != value.data_; }
835 bool operator>(Uint32 value) const { return data_ > value.data_; }
836 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
837 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
838 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
839 Uint32 operator&=(Uint32 value) {
840 data_ &= value.data_;
841 return *this;
842 }
843 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
844 Uint32 operator^=(Uint32 value) {
845 data_ ^= value.data_;
846 return *this;
847 }
848 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
849 Uint32 operator|=(Uint32 value) {
850 data_ |= value.data_;
851 return *this;
852 }
853 // Unlike uint32_t, the shift functions can accept negative shift and
854 // return 0 when the shift is too big.
855 Uint32 operator>>(int shift) const {
856 if (shift == 0) return *this;
857 if (shift < 0) {
858 int tmp = -shift;
859 if (tmp >= 32) return Uint32(0);
860 return Uint32(data_ << tmp);
861 }
862 int tmp = shift;
863 if (tmp >= 32) return Uint32(0);
864 return Uint32(data_ >> tmp);
865 }
866 Uint32 operator<<(int shift) const {
867 if (shift == 0) return *this;
868 if (shift < 0) {
869 int tmp = -shift;
870 if (tmp >= 32) return Uint32(0);
871 return Uint32(data_ >> tmp);
872 }
873 int tmp = shift;
874 if (tmp >= 32) return Uint32(0);
875 return Uint32(data_ << tmp);
876 }
877};
878
879class Uint64 {
880 uint64_t data_;
881
882 public:
883 // Unlike uint64_t, Uint64 has a default constructor.
884 Uint64() { data_ = 0; }
885 explicit Uint64(uint64_t data) : data_(data) {}
886 explicit Uint64(Uint32 data) : data_(data.Get()) {}
887 inline explicit Uint64(Uint128 data);
888 uint64_t Get() const { return data_; }
889 int64_t GetSigned(int N) const {
890 return ExtractSignedBitfield64(N - 1, 0, data_);
891 }
892 int64_t GetSigned() const { return data_; }
893 Uint32 ToUint32() const {
894 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100895 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100896 }
897 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100898 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100899 Uint64 operator~() const { return Uint64(~data_); }
900 Uint64 operator-() const { return Uint64(-data_); }
901 bool operator==(Uint64 value) const { return data_ == value.data_; }
902 bool operator!=(Uint64 value) const { return data_ != value.data_; }
903 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
904 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
905 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
906 Uint64 operator&=(Uint64 value) {
907 data_ &= value.data_;
908 return *this;
909 }
910 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
911 Uint64 operator^=(Uint64 value) {
912 data_ ^= value.data_;
913 return *this;
914 }
915 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
916 Uint64 operator|=(Uint64 value) {
917 data_ |= value.data_;
918 return *this;
919 }
920 // Unlike uint64_t, the shift functions can accept negative shift and
921 // return 0 when the shift is too big.
922 Uint64 operator>>(int shift) const {
923 if (shift == 0) return *this;
924 if (shift < 0) {
925 int tmp = -shift;
926 if (tmp >= 64) return Uint64(0);
927 return Uint64(data_ << tmp);
928 }
929 int tmp = shift;
930 if (tmp >= 64) return Uint64(0);
931 return Uint64(data_ >> tmp);
932 }
933 Uint64 operator<<(int shift) const {
934 if (shift == 0) return *this;
935 if (shift < 0) {
936 int tmp = -shift;
937 if (tmp >= 64) return Uint64(0);
938 return Uint64(data_ >> tmp);
939 }
940 int tmp = shift;
941 if (tmp >= 64) return Uint64(0);
942 return Uint64(data_ << tmp);
943 }
944};
945
946class Uint128 {
947 uint64_t data_high_;
948 uint64_t data_low_;
949
950 public:
951 Uint128() : data_high_(0), data_low_(0) {}
952 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
953 explicit Uint128(Uint64 data_low)
954 : data_high_(0), data_low_(data_low.Get()) {}
955 Uint128(uint64_t data_high, uint64_t data_low)
956 : data_high_(data_high), data_low_(data_low) {}
957 Uint64 ToUint64() const {
958 VIXL_ASSERT(data_high_ == 0);
959 return Uint64(data_low_);
960 }
961 Uint64 GetHigh64() const { return Uint64(data_high_); }
962 Uint64 GetLow64() const { return Uint64(data_low_); }
963 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
964 bool operator==(Uint128 value) const {
965 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
966 }
967 Uint128 operator&(Uint128 value) const {
968 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
969 }
970 Uint128 operator&=(Uint128 value) {
971 data_high_ &= value.data_high_;
972 data_low_ &= value.data_low_;
973 return *this;
974 }
975 Uint128 operator|=(Uint128 value) {
976 data_high_ |= value.data_high_;
977 data_low_ |= value.data_low_;
978 return *this;
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(0, data_high_ >> (shift - 64));
985 }
986 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
987 return Uint128(data_high_ >> shift, tmp);
988 }
989 Uint128 operator<<(int shift) const {
990 VIXL_ASSERT((shift >= 0) && (shift < 128));
991 if (shift == 0) return *this;
992 if (shift >= 64) {
993 return Uint128(data_low_ << (shift - 64), 0);
994 }
995 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
996 return Uint128(tmp, data_low_ << shift);
997 }
998};
999
1000Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
1001Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1002
1003Int64 BitCount(Uint32 value);
1004
TatWai Chong13634762019-07-16 16:20:45 -07001005// The algorithm used is adapted from the one described in section 8.2 of
1006// Hacker's Delight, by Henry S. Warren, Jr.
1007template <unsigned N, typename T>
1008int64_t MultiplyHigh(T u, T v) {
1009 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1010 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1011 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1012 uint64_t sign_ext = 0;
1013 unsigned half_bits = N / 2;
1014 uint64_t half_mask = GetUintMask(half_bits);
1015 if (std::numeric_limits<T>::is_signed) {
1016 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1017 }
1018
1019 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1020 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1021
1022 u0 = u & half_mask;
1023 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1024 v0 = v & half_mask;
1025 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1026
1027 w0 = u0 * v0;
1028 t = u1 * v0 + (w0 >> half_bits);
1029
1030 w1 = t & half_mask;
1031 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1032 w1 = u0 * v1 + w1;
1033 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1034
1035 uint64_t value = u1 * v1 + w2 + w1;
1036 int64_t result;
1037 memcpy(&result, &value, sizeof(result));
1038 return result;
1039}
1040
Pierre Langloisd82faf62018-04-04 13:06:58 +01001041} // namespace internal
1042
Jacob Bramleyca789742018-09-13 14:25:46 +01001043// The default NaN values (for FPCR.DN=1).
1044extern const double kFP64DefaultNaN;
1045extern const float kFP32DefaultNaN;
1046extern const Float16 kFP16DefaultNaN;
1047
1048// Floating-point infinity values.
1049extern const Float16 kFP16PositiveInfinity;
1050extern const Float16 kFP16NegativeInfinity;
1051extern const float kFP32PositiveInfinity;
1052extern const float kFP32NegativeInfinity;
1053extern const double kFP64PositiveInfinity;
1054extern const double kFP64NegativeInfinity;
1055
1056// Floating-point zero values.
1057extern const Float16 kFP16PositiveZero;
1058extern const Float16 kFP16NegativeZero;
1059
1060// AArch64 floating-point specifics. These match IEEE-754.
1061const unsigned kDoubleMantissaBits = 52;
1062const unsigned kDoubleExponentBits = 11;
1063const unsigned kFloatMantissaBits = 23;
1064const unsigned kFloatExponentBits = 8;
1065const unsigned kFloat16MantissaBits = 10;
1066const unsigned kFloat16ExponentBits = 5;
1067
1068enum FPRounding {
1069 // The first four values are encodable directly by FPCR<RMode>.
1070 FPTieEven = 0x0,
1071 FPPositiveInfinity = 0x1,
1072 FPNegativeInfinity = 0x2,
1073 FPZero = 0x3,
1074
1075 // The final rounding modes are only available when explicitly specified by
1076 // the instruction (such as with fcvta). It cannot be set in FPCR.
1077 FPTieAway,
1078 FPRoundOdd
1079};
1080
1081enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1082
1083// Assemble the specified IEEE-754 components into the target type and apply
1084// appropriate rounding.
1085// sign: 0 = positive, 1 = negative
1086// exponent: Unbiased IEEE-754 exponent.
1087// mantissa: The mantissa of the input. The top bit (which is not encoded for
1088// normal IEEE-754 values) must not be omitted. This bit has the
1089// value 'pow(2, exponent)'.
1090//
1091// The input value is assumed to be a normalized value. That is, the input may
1092// not be infinity or NaN. If the source value is subnormal, it must be
1093// normalized before calling this function such that the highest set bit in the
1094// mantissa has the value 'pow(2, exponent)'.
1095//
1096// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1097// calling a templated FPRound.
1098template <class T, int ebits, int mbits>
1099T FPRound(int64_t sign,
1100 int64_t exponent,
1101 uint64_t mantissa,
1102 FPRounding round_mode) {
1103 VIXL_ASSERT((sign == 0) || (sign == 1));
1104
1105 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1106 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1107
1108 // Rounding can promote subnormals to normals, and normals to infinities. For
1109 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1110 // encodable as a float, but rounding based on the low-order mantissa bits
1111 // could make it overflow. With ties-to-even rounding, this value would become
1112 // an infinity.
1113
1114 // ---- Rounding Method ----
1115 //
1116 // The exponent is irrelevant in the rounding operation, so we treat the
1117 // lowest-order bit that will fit into the result ('onebit') as having
1118 // the value '1'. Similarly, the highest-order bit that won't fit into
1119 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1120 // 'onebit' and 'halfbit':
1121 //
1122 // These bits fit into the result.
1123 // |---------------------|
1124 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1125 // ||
1126 // / |
1127 // / halfbit
1128 // onebit
1129 //
1130 // For subnormal outputs, the range of representable bits is smaller and
1131 // the position of onebit and halfbit depends on the exponent of the
1132 // input, but the method is otherwise similar.
1133 //
1134 // onebit(frac)
1135 // |
1136 // | halfbit(frac) halfbit(adjusted)
1137 // | / /
1138 // | | |
1139 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1140 // 0b00.0... -> 0b00.0... -> 0b00
1141 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1142 // 0b00.1... -> 0b00.1... -> 0b01
1143 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1144 // 0b01.0... -> 0b01.0... -> 0b01
1145 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1146 // 0b01.1... -> 0b01.1... -> 0b10
1147 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1148 // 0b10.0... -> 0b10.0... -> 0b10
1149 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1150 // 0b10.1... -> 0b10.1... -> 0b11
1151 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1152 // ... / | / |
1153 // / | / |
1154 // / |
1155 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1156 //
1157 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1158
1159 static const int mantissa_offset = 0;
1160 static const int exponent_offset = mantissa_offset + mbits;
1161 static const int sign_offset = exponent_offset + ebits;
1162 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1163
1164 // Bail out early for zero inputs.
1165 if (mantissa == 0) {
1166 return static_cast<T>(sign << sign_offset);
1167 }
1168
1169 // If all bits in the exponent are set, the value is infinite or NaN.
1170 // This is true for all binary IEEE-754 formats.
1171 static const int infinite_exponent = (1 << ebits) - 1;
1172 static const int max_normal_exponent = infinite_exponent - 1;
1173
1174 // Apply the exponent bias to encode it for the result. Doing this early makes
1175 // it easy to detect values that will be infinite or subnormal.
1176 exponent += max_normal_exponent >> 1;
1177
1178 if (exponent > max_normal_exponent) {
1179 // Overflow: the input is too large for the result type to represent.
1180 if (round_mode == FPTieEven) {
1181 // FPTieEven rounding mode handles overflows using infinities.
1182 exponent = infinite_exponent;
1183 mantissa = 0;
1184 } else {
1185 VIXL_ASSERT(round_mode == FPRoundOdd);
1186 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1187 // normal number.
1188 exponent = max_normal_exponent;
1189 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1190 }
1191 return static_cast<T>((sign << sign_offset) |
1192 (exponent << exponent_offset) |
1193 (mantissa << mantissa_offset));
1194 }
1195
1196 // Calculate the shift required to move the top mantissa bit to the proper
1197 // place in the destination type.
1198 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1199 int shift = highest_significant_bit - mbits;
1200
1201 if (exponent <= 0) {
1202 // The output will be subnormal (before rounding).
1203 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1204 // is necessary because the exponent of a subnormal value (encoded as 0) is
1205 // the same as the exponent of the smallest normal value (encoded as 1).
1206 shift += -exponent + 1;
1207
1208 // Handle inputs that would produce a zero output.
1209 //
1210 // Shifts higher than highest_significant_bit+1 will always produce a zero
1211 // result. A shift of exactly highest_significant_bit+1 might produce a
1212 // non-zero result after rounding.
1213 if (shift > (highest_significant_bit + 1)) {
1214 if (round_mode == FPTieEven) {
1215 // The result will always be +/-0.0.
1216 return static_cast<T>(sign << sign_offset);
1217 } else {
1218 VIXL_ASSERT(round_mode == FPRoundOdd);
1219 VIXL_ASSERT(mantissa != 0);
1220 // For FPRoundOdd, if the mantissa is too small to represent and
1221 // non-zero return the next "odd" value.
1222 return static_cast<T>((sign << sign_offset) | 1);
1223 }
1224 }
1225
1226 // Properly encode the exponent for a subnormal output.
1227 exponent = 0;
1228 } else {
1229 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1230 // normal values.
1231 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1232 }
1233
1234 // The casts below are only well-defined for unsigned integers.
1235 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1236 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1237
1238 if (shift > 0) {
1239 if (round_mode == FPTieEven) {
1240 // We have to shift the mantissa to the right. Some precision is lost, so
1241 // we need to apply rounding.
1242 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1243 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1244 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1245 uint64_t adjusted = mantissa - adjustment;
1246 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1247
1248 T result =
1249 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1250 ((mantissa >> shift) << mantissa_offset));
1251
1252 // A very large mantissa can overflow during rounding. If this happens,
1253 // the exponent should be incremented and the mantissa set to 1.0
1254 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1255 // has the nice side-effect that this case is handled for free.
1256 //
1257 // This also handles cases where a very large finite value overflows to
1258 // infinity, or where a very large subnormal value overflows to become
1259 // normal.
1260 return result + halfbit_adjusted;
1261 } else {
1262 VIXL_ASSERT(round_mode == FPRoundOdd);
1263 // If any bits at position halfbit or below are set, onebit (ie. the
1264 // bottom bit of the resulting mantissa) must be set.
1265 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1266 if (fractional_bits != 0) {
1267 mantissa |= UINT64_C(1) << shift;
1268 }
1269
1270 return static_cast<T>((sign << sign_offset) |
1271 (exponent << exponent_offset) |
1272 ((mantissa >> shift) << mantissa_offset));
1273 }
1274 } else {
1275 // We have to shift the mantissa to the left (or not at all). The input
1276 // mantissa is exactly representable in the output mantissa, so apply no
1277 // rounding correction.
1278 return static_cast<T>((sign << sign_offset) |
1279 (exponent << exponent_offset) |
1280 ((mantissa << -shift) << mantissa_offset));
1281 }
1282}
1283
1284
1285// See FPRound for a description of this function.
1286inline double FPRoundToDouble(int64_t sign,
1287 int64_t exponent,
1288 uint64_t mantissa,
1289 FPRounding round_mode) {
1290 uint64_t bits =
1291 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1292 exponent,
1293 mantissa,
1294 round_mode);
1295 return RawbitsToDouble(bits);
1296}
1297
1298
1299// See FPRound for a description of this function.
1300inline Float16 FPRoundToFloat16(int64_t sign,
1301 int64_t exponent,
1302 uint64_t mantissa,
1303 FPRounding round_mode) {
1304 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001305 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1306 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001307}
1308
1309
1310// See FPRound for a description of this function.
1311static inline float FPRoundToFloat(int64_t sign,
1312 int64_t exponent,
1313 uint64_t mantissa,
1314 FPRounding round_mode) {
1315 uint32_t bits =
1316 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1317 exponent,
1318 mantissa,
1319 round_mode);
1320 return RawbitsToFloat(bits);
1321}
1322
1323
1324float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1325float FPToFloat(double value,
1326 FPRounding round_mode,
1327 UseDefaultNaN DN,
1328 bool* exception = NULL);
1329
1330double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1331double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1332
1333Float16 FPToFloat16(float value,
1334 FPRounding round_mode,
1335 UseDefaultNaN DN,
1336 bool* exception = NULL);
1337
1338Float16 FPToFloat16(double value,
1339 FPRounding round_mode,
1340 UseDefaultNaN DN,
1341 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001342
1343// Like static_cast<T>(value), but with specialisations for the Float16 type.
1344template <typename T, typename F>
1345T StaticCastFPTo(F value) {
1346 return static_cast<T>(value);
1347}
1348
1349template <>
1350inline float StaticCastFPTo<float, Float16>(Float16 value) {
1351 return FPToFloat(value, kIgnoreDefaultNaN);
1352}
1353
1354template <>
1355inline double StaticCastFPTo<double, Float16>(Float16 value) {
1356 return FPToDouble(value, kIgnoreDefaultNaN);
1357}
1358
1359template <>
1360inline Float16 StaticCastFPTo<Float16, float>(float value) {
1361 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1362}
1363
1364template <>
1365inline Float16 StaticCastFPTo<Float16, double>(double value) {
1366 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1367}
1368
1369template <typename T>
1370uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1371 switch (size_in_bits) {
1372 case 16:
1373 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1374 case 32:
1375 return FloatToRawbits(StaticCastFPTo<float>(value));
1376 case 64:
1377 return DoubleToRawbits(StaticCastFPTo<double>(value));
1378 }
1379 VIXL_UNREACHABLE();
1380 return 0;
1381}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001382
1383template <typename T>
1384T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1385 VIXL_ASSERT(IsUintN(size_in_bits, value));
1386 switch (size_in_bits) {
1387 case 16:
1388 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1389 case 32:
1390 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1391 case 64:
1392 return StaticCastFPTo<T>(RawbitsToDouble(value));
1393 }
1394 VIXL_UNREACHABLE();
1395 return 0;
1396}
1397
armvixlad96eda2013-06-14 11:42:37 +01001398} // namespace vixl
1399
1400#endif // VIXL_UTILS_H