blob: e57cbeecdda8cf6e99e88c7613ea5999fbaad5a7 [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>
70size_t ArrayLength(const T (&)[n]) {
71 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 Bramleyca789742018-09-13 14:25:46 +0100392inline bool IsNaN(float value) { return std::isnan(value); }
393
394inline bool IsNaN(double value) { return std::isnan(value); }
395
396inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
397
398inline bool IsInf(float value) { return std::isinf(value); }
399
400inline bool IsInf(double value) { return std::isinf(value); }
401
402inline bool IsInf(Float16 value) {
403 return Float16Classify(value) == FP_INFINITE;
404}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100405
406
armvixlf37fdc02014-02-05 13:22:16 +0000407// NaN tests.
408inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100409 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100410 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100411 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000412 return true;
413 }
414 return false;
415}
416
417
418inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000419 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100420 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100421 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000422 return true;
423 }
424 return false;
425}
426
427
Jacob Bramleyca789742018-09-13 14:25:46 +0100428inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000429 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100430 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000431}
432
433
armvixlf37fdc02014-02-05 13:22:16 +0000434template <typename T>
435inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100436 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000437}
438
439
armvixlb0c8ae22014-03-21 14:03:59 +0000440// Convert the NaN in 'num' to a quiet NaN.
441inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100442 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100443 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100444 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000445}
446
447
448inline float ToQuietNaN(float num) {
449 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100450 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100451 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000452}
453
454
Jacob Bramleyca789742018-09-13 14:25:46 +0100455inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
456 const uint16_t kFP16QuietNaNMask = 0x0200;
457 VIXL_ASSERT(IsNaN(num));
458 return internal::SimFloat16(
459 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
460}
461
462
armvixlb0c8ae22014-03-21 14:03:59 +0000463// Fused multiply-add.
464inline double FusedMultiplyAdd(double op1, double op2, double a) {
465 return fma(op1, op2, a);
466}
467
468
469inline float FusedMultiplyAdd(float op1, float op2, float a) {
470 return fmaf(op1, op2, a);
471}
472
473
armvixl0f35e362016-05-10 13:57:58 +0100474inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100475
476
armvixl0f35e362016-05-10 13:57:58 +0100477template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100478inline int HighestSetBitPosition(T value) {
479 VIXL_ASSERT(value != 0);
480 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
481}
482
483
armvixl0f35e362016-05-10 13:57:58 +0100484template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100485inline int WhichPowerOf2(V value) {
486 VIXL_ASSERT(IsPowerOf2(value));
487 return CountTrailingZeros(value);
488}
armvixlad96eda2013-06-14 11:42:37 +0100489
armvixldb644342015-07-21 11:37:10 +0100490
armvixl330dc712014-11-25 10:38:32 +0000491unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
492
armvixldb644342015-07-21 11:37:10 +0100493
Pierre Langlois88c46b82016-06-02 18:15:32 +0100494int BitCount(uint64_t value);
495
496
armvixldb644342015-07-21 11:37:10 +0100497template <typename T>
498T ReverseBits(T value) {
499 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
500 (sizeof(value) == 4) || (sizeof(value) == 8));
501 T result = 0;
502 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
503 result = (result << 1) | (value & 1);
504 value >>= 1;
505 }
506 return result;
507}
508
509
510template <typename T>
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000511inline T SignExtend(T val, int size_in_bits) {
512 VIXL_ASSERT(size_in_bits > 0);
513 T mask = (T(2) << (size_in_bits - 1)) - T(1);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100514 val &= mask;
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000515 T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700516 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100517 return val;
518}
519
520
521template <typename T>
armvixldb644342015-07-21 11:37:10 +0100522T ReverseBytes(T value, int block_bytes_log2) {
523 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
524 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
525 // Split the 64-bit value into an 8-bit array, where b[0] is the least
526 // significant byte, and b[7] is the most significant.
527 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000528 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100529 for (int i = 7; i >= 0; i--) {
530 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
531 mask >>= 8;
532 }
533
534 // Permutation tables for REV instructions.
535 // permute_table[0] is used by REV16_x, REV16_w
536 // permute_table[1] is used by REV32_x, REV_w
537 // permute_table[2] is used by REV_x
538 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100539 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
540 {4, 5, 6, 7, 0, 1, 2, 3},
541 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000542 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100543 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000544 temp <<= 8;
545 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100546 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000547
548 T result;
549 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
550 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100551 return result;
552}
553
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700554template <unsigned MULTIPLE, typename T>
555inline bool IsMultiple(T value) {
556 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
557 return (value & (MULTIPLE - 1)) == 0;
558}
armvixldb644342015-07-21 11:37:10 +0100559
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000560template <typename T>
561inline bool IsMultiple(T value, unsigned multiple) {
562 VIXL_ASSERT(IsPowerOf2(multiple));
563 return (value & (multiple - 1)) == 0;
564}
565
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000566template <typename T>
567inline bool IsAligned(T pointer, int alignment) {
568 VIXL_ASSERT(IsPowerOf2(alignment));
569 return (pointer & (alignment - 1)) == 0;
570}
571
armvixlad96eda2013-06-14 11:42:37 +0100572// Pointer alignment
573// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100574template <unsigned ALIGN, typename T>
575inline bool IsAligned(T pointer) {
576 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100577 // Use C-style casts to get static_cast behaviour for integral types (T), and
578 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000579 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100580}
581
armvixl0f35e362016-05-10 13:57:58 +0100582template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100583bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100584 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100585}
586
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100587// Increment a pointer until it has the specified alignment. The alignment must
588// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100589template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100590T AlignUp(T pointer,
591 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100592 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100593 // Use C-style casts to get static_cast behaviour for integral types (T), and
594 // reinterpret_cast behaviour for other types.
595
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100596 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
597 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000598 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100599
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100600 size_t mask = alignment - 1;
601 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000602 VIXL_ASSERT(result >= pointer);
603
604 return result;
armvixlad96eda2013-06-14 11:42:37 +0100605}
606
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100607// Decrement a pointer until it has the specified alignment. The alignment must
608// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100609template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100610T AlignDown(T pointer,
611 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100612 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100613 // Use C-style casts to get static_cast behaviour for integral types (T), and
614 // reinterpret_cast behaviour for other types.
615
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100616 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
617 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000618 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100619
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100620 size_t mask = alignment - 1;
621 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000622}
623
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100624
Pierre Langlois88c46b82016-06-02 18:15:32 +0100625template <typename T>
626inline T ExtractBit(T value, unsigned bit) {
627 return (value >> bit) & T(1);
628}
629
630template <typename Ts, typename Td>
631inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
632 return Td((value >> least_significant_bit) & Ts(mask));
633}
634
635template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800636inline void AssignBit(Td& dst, // NOLINT(runtime/references)
637 int bit,
638 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100639 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
640 VIXL_ASSERT(bit >= 0);
641 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
642 Td mask(1);
643 dst &= ~(mask << bit);
644 dst |= Td(value) << bit;
645}
646
647template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800648inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100649 int least_significant_bit,
650 Ts mask,
651 Ts value) {
652 VIXL_ASSERT(least_significant_bit >= 0);
653 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
654 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
655 Td(mask));
656 VIXL_ASSERT((value & mask) == value);
657 dst &= ~(Td(mask) << least_significant_bit);
658 dst |= Td(value) << least_significant_bit;
659}
660
661class VFP {
662 public:
663 static uint32_t FP32ToImm8(float imm) {
664 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
665 uint32_t bits = FloatToRawbits(imm);
666 // bit7: a000.0000
667 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
668 // bit6: 0b00.0000
669 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
670 // bit5_to_0: 00cd.efgh
671 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
672 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
673 }
674 static uint32_t FP64ToImm8(double imm) {
675 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
676 // 0000.0000.0000.0000.0000.0000.0000.0000
677 uint64_t bits = DoubleToRawbits(imm);
678 // bit7: a000.0000
679 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
680 // bit6: 0b00.0000
681 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
682 // bit5_to_0: 00cd.efgh
683 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
684
685 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
686 }
687 static float Imm8ToFP32(uint32_t imm8) {
688 // Imm8: abcdefgh (8 bits)
689 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
690 // where B is b ^ 1
691 uint32_t bits = imm8;
692 uint32_t bit7 = (bits >> 7) & 0x1;
693 uint32_t bit6 = (bits >> 6) & 0x1;
694 uint32_t bit5_to_0 = bits & 0x3f;
695 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
696
697 return RawbitsToFloat(result);
698 }
699 static double Imm8ToFP64(uint32_t imm8) {
700 // Imm8: abcdefgh (8 bits)
701 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
702 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
703 // where B is b ^ 1
704 uint32_t bits = imm8;
705 uint64_t bit7 = (bits >> 7) & 0x1;
706 uint64_t bit6 = (bits >> 6) & 0x1;
707 uint64_t bit5_to_0 = bits & 0x3f;
708 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
709 return RawbitsToDouble(result);
710 }
711 static bool IsImmFP32(float imm) {
712 // Valid values will have the form:
713 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
714 uint32_t bits = FloatToRawbits(imm);
715 // bits[19..0] are cleared.
716 if ((bits & 0x7ffff) != 0) {
717 return false;
718 }
719
720
721 // bits[29..25] are all set or all cleared.
722 uint32_t b_pattern = (bits >> 16) & 0x3e00;
723 if (b_pattern != 0 && b_pattern != 0x3e00) {
724 return false;
725 }
726 // bit[30] and bit[29] are opposite.
727 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
728 return false;
729 }
730 return true;
731 }
732 static bool IsImmFP64(double imm) {
733 // Valid values will have the form:
734 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
735 // 0000.0000.0000.0000.0000.0000.0000.0000
736 uint64_t bits = DoubleToRawbits(imm);
737 // bits[47..0] are cleared.
738 if ((bits & 0x0000ffffffffffff) != 0) {
739 return false;
740 }
741 // bits[61..54] are all set or all cleared.
742 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
743 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
744 return false;
745 }
746 // bit[62] and bit[61] are opposite.
747 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
748 return false;
749 }
750 return true;
751 }
752};
753
754class BitField {
755 // ForEachBitHelper is a functor that will call
756 // bool ForEachBitHelper::execute(ElementType id) const
757 // and expects a boolean in return whether to continue (if true)
758 // or stop (if false)
759 // check_set will check if the bits are on (true) or off(false)
760 template <typename ForEachBitHelper, bool check_set>
761 bool ForEachBit(const ForEachBitHelper& helper) {
762 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
763 if (bitfield_[i] == check_set)
764 if (!helper.execute(i)) return false;
765 }
766 return true;
767 }
768
769 public:
770 explicit BitField(unsigned size) : bitfield_(size, 0) {}
771
772 void Set(int i) {
773 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
774 bitfield_[i] = true;
775 }
776
777 void Unset(int i) {
778 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
779 bitfield_[i] = true;
780 }
781
782 bool IsSet(int i) const { return bitfield_[i]; }
783
784 // For each bit not set in the bitfield call the execute functor
785 // execute.
786 // ForEachBitSetHelper::execute returns true if the iteration through
787 // the bits can continue, otherwise it will stop.
788 // struct ForEachBitSetHelper {
789 // bool execute(int /*id*/) { return false; }
790 // };
791 template <typename ForEachBitNotSetHelper>
792 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
793 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
794 }
795
796 // For each bit set in the bitfield call the execute functor
797 // execute.
798 template <typename ForEachBitSetHelper>
799 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
800 return ForEachBit<ForEachBitSetHelper, true>(helper);
801 }
802
803 private:
804 std::vector<bool> bitfield_;
805};
806
Pierre Langloisd82faf62018-04-04 13:06:58 +0100807namespace internal {
808
Pierre Langlois88c46b82016-06-02 18:15:32 +0100809typedef int64_t Int64;
810class Uint64;
811class Uint128;
812
813class Uint32 {
814 uint32_t data_;
815
816 public:
817 // Unlike uint32_t, Uint32 has a default constructor.
818 Uint32() { data_ = 0; }
819 explicit Uint32(uint32_t data) : data_(data) {}
820 inline explicit Uint32(Uint64 data);
821 uint32_t Get() const { return data_; }
822 template <int N>
823 int32_t GetSigned() const {
824 return ExtractSignedBitfield32(N - 1, 0, data_);
825 }
826 int32_t GetSigned() const { return data_; }
827 Uint32 operator~() const { return Uint32(~data_); }
828 Uint32 operator-() const { return Uint32(-data_); }
829 bool operator==(Uint32 value) const { return data_ == value.data_; }
830 bool operator!=(Uint32 value) const { return data_ != value.data_; }
831 bool operator>(Uint32 value) const { return data_ > value.data_; }
832 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
833 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
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 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
845 Uint32 operator|=(Uint32 value) {
846 data_ |= value.data_;
847 return *this;
848 }
849 // Unlike uint32_t, the shift functions can accept negative shift and
850 // return 0 when the shift is too big.
851 Uint32 operator>>(int shift) const {
852 if (shift == 0) return *this;
853 if (shift < 0) {
854 int tmp = -shift;
855 if (tmp >= 32) return Uint32(0);
856 return Uint32(data_ << tmp);
857 }
858 int tmp = shift;
859 if (tmp >= 32) return Uint32(0);
860 return Uint32(data_ >> tmp);
861 }
862 Uint32 operator<<(int shift) const {
863 if (shift == 0) return *this;
864 if (shift < 0) {
865 int tmp = -shift;
866 if (tmp >= 32) return Uint32(0);
867 return Uint32(data_ >> tmp);
868 }
869 int tmp = shift;
870 if (tmp >= 32) return Uint32(0);
871 return Uint32(data_ << tmp);
872 }
873};
874
875class Uint64 {
876 uint64_t data_;
877
878 public:
879 // Unlike uint64_t, Uint64 has a default constructor.
880 Uint64() { data_ = 0; }
881 explicit Uint64(uint64_t data) : data_(data) {}
882 explicit Uint64(Uint32 data) : data_(data.Get()) {}
883 inline explicit Uint64(Uint128 data);
884 uint64_t Get() const { return data_; }
885 int64_t GetSigned(int N) const {
886 return ExtractSignedBitfield64(N - 1, 0, data_);
887 }
888 int64_t GetSigned() const { return data_; }
889 Uint32 ToUint32() const {
890 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100891 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100892 }
893 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100894 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100895 Uint64 operator~() const { return Uint64(~data_); }
896 Uint64 operator-() const { return Uint64(-data_); }
897 bool operator==(Uint64 value) const { return data_ == value.data_; }
898 bool operator!=(Uint64 value) const { return data_ != value.data_; }
899 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
900 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
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 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
912 Uint64 operator|=(Uint64 value) {
913 data_ |= value.data_;
914 return *this;
915 }
916 // Unlike uint64_t, the shift functions can accept negative shift and
917 // return 0 when the shift is too big.
918 Uint64 operator>>(int shift) const {
919 if (shift == 0) return *this;
920 if (shift < 0) {
921 int tmp = -shift;
922 if (tmp >= 64) return Uint64(0);
923 return Uint64(data_ << tmp);
924 }
925 int tmp = shift;
926 if (tmp >= 64) return Uint64(0);
927 return Uint64(data_ >> tmp);
928 }
929 Uint64 operator<<(int shift) const {
930 if (shift == 0) return *this;
931 if (shift < 0) {
932 int tmp = -shift;
933 if (tmp >= 64) return Uint64(0);
934 return Uint64(data_ >> tmp);
935 }
936 int tmp = shift;
937 if (tmp >= 64) return Uint64(0);
938 return Uint64(data_ << tmp);
939 }
940};
941
942class Uint128 {
943 uint64_t data_high_;
944 uint64_t data_low_;
945
946 public:
947 Uint128() : data_high_(0), data_low_(0) {}
948 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
949 explicit Uint128(Uint64 data_low)
950 : data_high_(0), data_low_(data_low.Get()) {}
951 Uint128(uint64_t data_high, uint64_t data_low)
952 : data_high_(data_high), data_low_(data_low) {}
953 Uint64 ToUint64() const {
954 VIXL_ASSERT(data_high_ == 0);
955 return Uint64(data_low_);
956 }
957 Uint64 GetHigh64() const { return Uint64(data_high_); }
958 Uint64 GetLow64() const { return Uint64(data_low_); }
959 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
960 bool operator==(Uint128 value) const {
961 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
962 }
963 Uint128 operator&(Uint128 value) const {
964 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
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|=(Uint128 value) {
972 data_high_ |= value.data_high_;
973 data_low_ |= value.data_low_;
974 return *this;
975 }
976 Uint128 operator>>(int shift) const {
977 VIXL_ASSERT((shift >= 0) && (shift < 128));
978 if (shift == 0) return *this;
979 if (shift >= 64) {
980 return Uint128(0, data_high_ >> (shift - 64));
981 }
982 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
983 return Uint128(data_high_ >> shift, tmp);
984 }
985 Uint128 operator<<(int shift) const {
986 VIXL_ASSERT((shift >= 0) && (shift < 128));
987 if (shift == 0) return *this;
988 if (shift >= 64) {
989 return Uint128(data_low_ << (shift - 64), 0);
990 }
991 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
992 return Uint128(tmp, data_low_ << shift);
993 }
994};
995
996Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
997Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
998
999Int64 BitCount(Uint32 value);
1000
TatWai Chong13634762019-07-16 16:20:45 -07001001// The algorithm used is adapted from the one described in section 8.2 of
1002// Hacker's Delight, by Henry S. Warren, Jr.
1003template <unsigned N, typename T>
1004int64_t MultiplyHigh(T u, T v) {
1005 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1006 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1007 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1008 uint64_t sign_ext = 0;
1009 unsigned half_bits = N / 2;
1010 uint64_t half_mask = GetUintMask(half_bits);
1011 if (std::numeric_limits<T>::is_signed) {
1012 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1013 }
1014
1015 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1016 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1017
1018 u0 = u & half_mask;
1019 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1020 v0 = v & half_mask;
1021 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1022
1023 w0 = u0 * v0;
1024 t = u1 * v0 + (w0 >> half_bits);
1025
1026 w1 = t & half_mask;
1027 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1028 w1 = u0 * v1 + w1;
1029 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1030
1031 uint64_t value = u1 * v1 + w2 + w1;
1032 int64_t result;
1033 memcpy(&result, &value, sizeof(result));
1034 return result;
1035}
1036
Pierre Langloisd82faf62018-04-04 13:06:58 +01001037} // namespace internal
1038
Jacob Bramleyca789742018-09-13 14:25:46 +01001039// The default NaN values (for FPCR.DN=1).
1040extern const double kFP64DefaultNaN;
1041extern const float kFP32DefaultNaN;
1042extern const Float16 kFP16DefaultNaN;
1043
1044// Floating-point infinity values.
1045extern const Float16 kFP16PositiveInfinity;
1046extern const Float16 kFP16NegativeInfinity;
1047extern const float kFP32PositiveInfinity;
1048extern const float kFP32NegativeInfinity;
1049extern const double kFP64PositiveInfinity;
1050extern const double kFP64NegativeInfinity;
1051
1052// Floating-point zero values.
1053extern const Float16 kFP16PositiveZero;
1054extern const Float16 kFP16NegativeZero;
1055
1056// AArch64 floating-point specifics. These match IEEE-754.
1057const unsigned kDoubleMantissaBits = 52;
1058const unsigned kDoubleExponentBits = 11;
1059const unsigned kFloatMantissaBits = 23;
1060const unsigned kFloatExponentBits = 8;
1061const unsigned kFloat16MantissaBits = 10;
1062const unsigned kFloat16ExponentBits = 5;
1063
1064enum FPRounding {
1065 // The first four values are encodable directly by FPCR<RMode>.
1066 FPTieEven = 0x0,
1067 FPPositiveInfinity = 0x1,
1068 FPNegativeInfinity = 0x2,
1069 FPZero = 0x3,
1070
1071 // The final rounding modes are only available when explicitly specified by
1072 // the instruction (such as with fcvta). It cannot be set in FPCR.
1073 FPTieAway,
1074 FPRoundOdd
1075};
1076
1077enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1078
1079// Assemble the specified IEEE-754 components into the target type and apply
1080// appropriate rounding.
1081// sign: 0 = positive, 1 = negative
1082// exponent: Unbiased IEEE-754 exponent.
1083// mantissa: The mantissa of the input. The top bit (which is not encoded for
1084// normal IEEE-754 values) must not be omitted. This bit has the
1085// value 'pow(2, exponent)'.
1086//
1087// The input value is assumed to be a normalized value. That is, the input may
1088// not be infinity or NaN. If the source value is subnormal, it must be
1089// normalized before calling this function such that the highest set bit in the
1090// mantissa has the value 'pow(2, exponent)'.
1091//
1092// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1093// calling a templated FPRound.
1094template <class T, int ebits, int mbits>
1095T FPRound(int64_t sign,
1096 int64_t exponent,
1097 uint64_t mantissa,
1098 FPRounding round_mode) {
1099 VIXL_ASSERT((sign == 0) || (sign == 1));
1100
1101 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1102 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1103
1104 // Rounding can promote subnormals to normals, and normals to infinities. For
1105 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1106 // encodable as a float, but rounding based on the low-order mantissa bits
1107 // could make it overflow. With ties-to-even rounding, this value would become
1108 // an infinity.
1109
1110 // ---- Rounding Method ----
1111 //
1112 // The exponent is irrelevant in the rounding operation, so we treat the
1113 // lowest-order bit that will fit into the result ('onebit') as having
1114 // the value '1'. Similarly, the highest-order bit that won't fit into
1115 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1116 // 'onebit' and 'halfbit':
1117 //
1118 // These bits fit into the result.
1119 // |---------------------|
1120 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1121 // ||
1122 // / |
1123 // / halfbit
1124 // onebit
1125 //
1126 // For subnormal outputs, the range of representable bits is smaller and
1127 // the position of onebit and halfbit depends on the exponent of the
1128 // input, but the method is otherwise similar.
1129 //
1130 // onebit(frac)
1131 // |
1132 // | halfbit(frac) halfbit(adjusted)
1133 // | / /
1134 // | | |
1135 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1136 // 0b00.0... -> 0b00.0... -> 0b00
1137 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1138 // 0b00.1... -> 0b00.1... -> 0b01
1139 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1140 // 0b01.0... -> 0b01.0... -> 0b01
1141 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1142 // 0b01.1... -> 0b01.1... -> 0b10
1143 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1144 // 0b10.0... -> 0b10.0... -> 0b10
1145 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1146 // 0b10.1... -> 0b10.1... -> 0b11
1147 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1148 // ... / | / |
1149 // / | / |
1150 // / |
1151 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1152 //
1153 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1154
1155 static const int mantissa_offset = 0;
1156 static const int exponent_offset = mantissa_offset + mbits;
1157 static const int sign_offset = exponent_offset + ebits;
1158 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1159
1160 // Bail out early for zero inputs.
1161 if (mantissa == 0) {
1162 return static_cast<T>(sign << sign_offset);
1163 }
1164
1165 // If all bits in the exponent are set, the value is infinite or NaN.
1166 // This is true for all binary IEEE-754 formats.
1167 static const int infinite_exponent = (1 << ebits) - 1;
1168 static const int max_normal_exponent = infinite_exponent - 1;
1169
1170 // Apply the exponent bias to encode it for the result. Doing this early makes
1171 // it easy to detect values that will be infinite or subnormal.
1172 exponent += max_normal_exponent >> 1;
1173
1174 if (exponent > max_normal_exponent) {
1175 // Overflow: the input is too large for the result type to represent.
1176 if (round_mode == FPTieEven) {
1177 // FPTieEven rounding mode handles overflows using infinities.
1178 exponent = infinite_exponent;
1179 mantissa = 0;
1180 } else {
1181 VIXL_ASSERT(round_mode == FPRoundOdd);
1182 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1183 // normal number.
1184 exponent = max_normal_exponent;
1185 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1186 }
1187 return static_cast<T>((sign << sign_offset) |
1188 (exponent << exponent_offset) |
1189 (mantissa << mantissa_offset));
1190 }
1191
1192 // Calculate the shift required to move the top mantissa bit to the proper
1193 // place in the destination type.
1194 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1195 int shift = highest_significant_bit - mbits;
1196
1197 if (exponent <= 0) {
1198 // The output will be subnormal (before rounding).
1199 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1200 // is necessary because the exponent of a subnormal value (encoded as 0) is
1201 // the same as the exponent of the smallest normal value (encoded as 1).
1202 shift += -exponent + 1;
1203
1204 // Handle inputs that would produce a zero output.
1205 //
1206 // Shifts higher than highest_significant_bit+1 will always produce a zero
1207 // result. A shift of exactly highest_significant_bit+1 might produce a
1208 // non-zero result after rounding.
1209 if (shift > (highest_significant_bit + 1)) {
1210 if (round_mode == FPTieEven) {
1211 // The result will always be +/-0.0.
1212 return static_cast<T>(sign << sign_offset);
1213 } else {
1214 VIXL_ASSERT(round_mode == FPRoundOdd);
1215 VIXL_ASSERT(mantissa != 0);
1216 // For FPRoundOdd, if the mantissa is too small to represent and
1217 // non-zero return the next "odd" value.
1218 return static_cast<T>((sign << sign_offset) | 1);
1219 }
1220 }
1221
1222 // Properly encode the exponent for a subnormal output.
1223 exponent = 0;
1224 } else {
1225 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1226 // normal values.
1227 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1228 }
1229
1230 // The casts below are only well-defined for unsigned integers.
1231 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1232 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1233
1234 if (shift > 0) {
1235 if (round_mode == FPTieEven) {
1236 // We have to shift the mantissa to the right. Some precision is lost, so
1237 // we need to apply rounding.
1238 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1239 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1240 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1241 uint64_t adjusted = mantissa - adjustment;
1242 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1243
1244 T result =
1245 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1246 ((mantissa >> shift) << mantissa_offset));
1247
1248 // A very large mantissa can overflow during rounding. If this happens,
1249 // the exponent should be incremented and the mantissa set to 1.0
1250 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1251 // has the nice side-effect that this case is handled for free.
1252 //
1253 // This also handles cases where a very large finite value overflows to
1254 // infinity, or where a very large subnormal value overflows to become
1255 // normal.
1256 return result + halfbit_adjusted;
1257 } else {
1258 VIXL_ASSERT(round_mode == FPRoundOdd);
1259 // If any bits at position halfbit or below are set, onebit (ie. the
1260 // bottom bit of the resulting mantissa) must be set.
1261 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1262 if (fractional_bits != 0) {
1263 mantissa |= UINT64_C(1) << shift;
1264 }
1265
1266 return static_cast<T>((sign << sign_offset) |
1267 (exponent << exponent_offset) |
1268 ((mantissa >> shift) << mantissa_offset));
1269 }
1270 } else {
1271 // We have to shift the mantissa to the left (or not at all). The input
1272 // mantissa is exactly representable in the output mantissa, so apply no
1273 // rounding correction.
1274 return static_cast<T>((sign << sign_offset) |
1275 (exponent << exponent_offset) |
1276 ((mantissa << -shift) << mantissa_offset));
1277 }
1278}
1279
1280
1281// See FPRound for a description of this function.
1282inline double FPRoundToDouble(int64_t sign,
1283 int64_t exponent,
1284 uint64_t mantissa,
1285 FPRounding round_mode) {
1286 uint64_t bits =
1287 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1288 exponent,
1289 mantissa,
1290 round_mode);
1291 return RawbitsToDouble(bits);
1292}
1293
1294
1295// See FPRound for a description of this function.
1296inline Float16 FPRoundToFloat16(int64_t sign,
1297 int64_t exponent,
1298 uint64_t mantissa,
1299 FPRounding round_mode) {
1300 return RawbitsToFloat16(
1301 FPRound<uint16_t,
1302 kFloat16ExponentBits,
1303 kFloat16MantissaBits>(sign, exponent, mantissa, round_mode));
1304}
1305
1306
1307// See FPRound for a description of this function.
1308static inline float FPRoundToFloat(int64_t sign,
1309 int64_t exponent,
1310 uint64_t mantissa,
1311 FPRounding round_mode) {
1312 uint32_t bits =
1313 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1314 exponent,
1315 mantissa,
1316 round_mode);
1317 return RawbitsToFloat(bits);
1318}
1319
1320
1321float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1322float FPToFloat(double value,
1323 FPRounding round_mode,
1324 UseDefaultNaN DN,
1325 bool* exception = NULL);
1326
1327double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1328double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1329
1330Float16 FPToFloat16(float value,
1331 FPRounding round_mode,
1332 UseDefaultNaN DN,
1333 bool* exception = NULL);
1334
1335Float16 FPToFloat16(double value,
1336 FPRounding round_mode,
1337 UseDefaultNaN DN,
1338 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001339
1340// Like static_cast<T>(value), but with specialisations for the Float16 type.
1341template <typename T, typename F>
1342T StaticCastFPTo(F value) {
1343 return static_cast<T>(value);
1344}
1345
1346template <>
1347inline float StaticCastFPTo<float, Float16>(Float16 value) {
1348 return FPToFloat(value, kIgnoreDefaultNaN);
1349}
1350
1351template <>
1352inline double StaticCastFPTo<double, Float16>(Float16 value) {
1353 return FPToDouble(value, kIgnoreDefaultNaN);
1354}
1355
1356template <>
1357inline Float16 StaticCastFPTo<Float16, float>(float value) {
1358 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1359}
1360
1361template <>
1362inline Float16 StaticCastFPTo<Float16, double>(double value) {
1363 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1364}
1365
1366template <typename T>
1367uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1368 switch (size_in_bits) {
1369 case 16:
1370 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1371 case 32:
1372 return FloatToRawbits(StaticCastFPTo<float>(value));
1373 case 64:
1374 return DoubleToRawbits(StaticCastFPTo<double>(value));
1375 }
1376 VIXL_UNREACHABLE();
1377 return 0;
1378}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001379
1380template <typename T>
1381T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1382 VIXL_ASSERT(IsUintN(size_in_bits, value));
1383 switch (size_in_bits) {
1384 case 16:
1385 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1386 case 32:
1387 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1388 case 64:
1389 return StaticCastFPTo<T>(RawbitsToDouble(value));
1390 }
1391 VIXL_UNREACHABLE();
1392 return 0;
1393}
1394
armvixlad96eda2013-06-14 11:42:37 +01001395} // namespace vixl
1396
1397#endif // VIXL_UTILS_H