blob: 1e86e9592bd46eaf2e5af90000a628e836b5c88f [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
armvixlad96eda2013-06-14 11:42:37 +010080// Check number width.
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000081// TODO: Refactor these using templates.
82inline bool IsIntN(unsigned n, uint32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010083 VIXL_ASSERT((0 < n) && (n <= 32));
84 return x <= static_cast<uint32_t>(INT32_MAX >> (32 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000085}
86inline bool IsIntN(unsigned n, int32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010087 VIXL_ASSERT((0 < n) && (n <= 32));
88 if (n == 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000089 int32_t limit = INT32_C(1) << (n - 1);
90 return (-limit <= x) && (x < limit);
91}
92inline bool IsIntN(unsigned n, uint64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010093 VIXL_ASSERT((0 < n) && (n <= 64));
94 return x <= static_cast<uint64_t>(INT64_MAX >> (64 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000095}
Pierre Langlois88c46b82016-06-02 18:15:32 +010096inline bool IsIntN(unsigned n, int64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010097 VIXL_ASSERT((0 < n) && (n <= 64));
98 if (n == 64) return true;
armvixlb0c8ae22014-03-21 14:03:59 +000099 int64_t limit = INT64_C(1) << (n - 1);
armvixlad96eda2013-06-14 11:42:37 +0100100 return (-limit <= x) && (x < limit);
101}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100102VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
103 return IsIntN(n, x);
104}
armvixlad96eda2013-06-14 11:42:37 +0100105
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000106inline bool IsUintN(unsigned n, uint32_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000107 VIXL_ASSERT((0 < n) && (n <= 32));
108 if (n >= 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000109 return !(x >> n);
110}
111inline bool IsUintN(unsigned n, int32_t x) {
112 VIXL_ASSERT((0 < n) && (n < 32));
113 // Convert to an unsigned integer to avoid implementation-defined behavior.
114 return !(static_cast<uint32_t>(x) >> n);
115}
116inline bool IsUintN(unsigned n, uint64_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000117 VIXL_ASSERT((0 < n) && (n <= 64));
118 if (n >= 64) return true;
armvixlad96eda2013-06-14 11:42:37 +0100119 return !(x >> n);
120}
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000121inline bool IsUintN(unsigned n, int64_t x) {
122 VIXL_ASSERT((0 < n) && (n < 64));
123 // Convert to an unsigned integer to avoid implementation-defined behavior.
124 return !(static_cast<uint64_t>(x) >> n);
125}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100126VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
127 return IsUintN(n, x);
128}
armvixlad96eda2013-06-14 11:42:37 +0100129
Jacob Bramley3976edb2016-10-18 10:51:43 +0100130inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000131 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +0100132 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100133}
Jacob Bramley3976edb2016-10-18 10:51:43 +0100134VIXL_DEPRECATED("TruncateToUintN",
135 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
136 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100137}
armvixlad96eda2013-06-14 11:42:37 +0100138
armvixl0f35e362016-05-10 13:57:58 +0100139// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +0100140#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100141V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
142V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
143V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100144V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
145
146#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100147V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
148V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
149V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
150V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100151
Jacob Bramley3976edb2016-10-18 10:51:43 +0100152#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
153
armvixl0f35e362016-05-10 13:57:58 +0100154// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100155
Pierre Langlois88c46b82016-06-02 18:15:32 +0100156#define DECLARE_IS_INT_N(N) \
157 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
158 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
159 return IsIntN(N, x); \
160 }
161
162#define DECLARE_IS_UINT_N(N) \
163 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
164 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
165 return IsUintN(N, x); \
166 }
167
Jacob Bramley3976edb2016-10-18 10:51:43 +0100168#define DECLARE_TRUNCATE_TO_UINT_32(N) \
169 inline uint32_t TruncateToUint##N(uint64_t x) { \
170 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
171 } \
172 VIXL_DEPRECATED("TruncateToUint" #N, \
173 inline uint32_t truncate_to_int##N(int64_t x)) { \
174 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100175 }
176
armvixlad96eda2013-06-14 11:42:37 +0100177INT_1_TO_63_LIST(DECLARE_IS_INT_N)
178INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100179INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100180
armvixlad96eda2013-06-14 11:42:37 +0100181#undef DECLARE_IS_INT_N
182#undef DECLARE_IS_UINT_N
183#undef DECLARE_TRUNCATE_TO_INT_N
184
185// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100186inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000187 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
188 (msb >= lsb));
189 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100190 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100191}
192
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000193
Jacob Bramley199339d2019-08-05 18:49:13 +0100194inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000195 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
196 (msb >= lsb));
197 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100198}
199
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000200
Jacob Bramley6069fd42019-06-24 10:20:45 +0100201inline int64_t ExtractSignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000202 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
203 (msb >= lsb));
204 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
205 // If the highest extracted bit is set, sign extend.
206 if ((temp >> (msb - lsb)) == 1) {
207 temp |= ~UINT64_C(0) << (msb - lsb);
208 }
209 int64_t result;
210 memcpy(&result, &temp, sizeof(result));
211 return result;
armvixlad96eda2013-06-14 11:42:37 +0100212}
213
Jacob Bramley199339d2019-08-05 18:49:13 +0100214inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000215 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
216 (msb >= lsb));
217 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
218 int32_t result;
219 memcpy(&result, &temp, sizeof(result));
220 return result;
221}
222
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000223inline uint64_t RotateRight(uint64_t value,
224 unsigned int rotate,
225 unsigned int width) {
226 VIXL_ASSERT((width > 0) && (width <= 64));
227 uint64_t width_mask = ~UINT64_C(0) >> (64 - width);
228 rotate &= 63;
229 if (rotate > 0) {
230 value &= width_mask;
231 value = (value << (width - rotate)) | (value >> rotate);
232 }
233 return value & width_mask;
234}
235
236
Jacob Bramleyca789742018-09-13 14:25:46 +0100237// Wrapper class for passing FP16 values through the assembler.
238// This is purely to aid with type checking/casting.
239class Float16 {
240 public:
241 explicit Float16(double dvalue);
242 Float16() : rawbits_(0x0) {}
243 friend uint16_t Float16ToRawbits(Float16 value);
244 friend Float16 RawbitsToFloat16(uint16_t bits);
245
246 protected:
247 uint16_t rawbits_;
248};
249
armvixlf37fdc02014-02-05 13:22:16 +0000250// Floating point representation.
Jacob Bramleyca789742018-09-13 14:25:46 +0100251uint16_t Float16ToRawbits(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100252
253
Pierre Langlois88c46b82016-06-02 18:15:32 +0100254uint32_t FloatToRawbits(float value);
255VIXL_DEPRECATED("FloatToRawbits",
256 inline uint32_t float_to_rawbits(float value)) {
257 return FloatToRawbits(value);
258}
armvixlad96eda2013-06-14 11:42:37 +0100259
Pierre Langlois88c46b82016-06-02 18:15:32 +0100260uint64_t DoubleToRawbits(double value);
261VIXL_DEPRECATED("DoubleToRawbits",
262 inline uint64_t double_to_rawbits(double value)) {
263 return DoubleToRawbits(value);
264}
armvixl5289c592015-03-02 13:52:04 +0000265
Jacob Bramleyca789742018-09-13 14:25:46 +0100266Float16 RawbitsToFloat16(uint16_t bits);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100267
Pierre Langlois88c46b82016-06-02 18:15:32 +0100268float RawbitsToFloat(uint32_t bits);
269VIXL_DEPRECATED("RawbitsToFloat",
270 inline float rawbits_to_float(uint32_t bits)) {
271 return RawbitsToFloat(bits);
272}
273
274double RawbitsToDouble(uint64_t bits);
275VIXL_DEPRECATED("RawbitsToDouble",
276 inline double rawbits_to_double(uint64_t bits)) {
277 return RawbitsToDouble(bits);
278}
279
Jacob Bramley03c0b512019-02-22 16:42:06 +0000280// Convert unsigned to signed numbers in a well-defined way (using two's
281// complement representations).
282inline int64_t RawbitsToInt64(uint64_t bits) {
283 return (bits >= UINT64_C(0x8000000000000000))
284 ? (-static_cast<int64_t>(-bits - 1) - 1)
285 : static_cast<int64_t>(bits);
286}
287
288inline int32_t RawbitsToInt32(uint32_t bits) {
289 return (bits >= UINT64_C(0x80000000)) ? (-static_cast<int32_t>(-bits - 1) - 1)
290 : static_cast<int32_t>(bits);
291}
292
Jacob Bramleyca789742018-09-13 14:25:46 +0100293namespace internal {
294
295// Internal simulation class used solely by the simulator to
296// provide an abstraction layer for any half-precision arithmetic.
297class SimFloat16 : public Float16 {
298 public:
299 // TODO: We should investigate making this constructor explicit.
300 // This is currently difficult to do due to a number of templated
301 // functions in the simulator which rely on returning double values.
302 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
303 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
304 this->rawbits_ = Float16ToRawbits(f);
305 }
306 SimFloat16() : Float16() {}
307 SimFloat16 operator-() const;
308 SimFloat16 operator+(SimFloat16 rhs) const;
309 SimFloat16 operator-(SimFloat16 rhs) const;
310 SimFloat16 operator*(SimFloat16 rhs) const;
311 SimFloat16 operator/(SimFloat16 rhs) const;
312 bool operator<(SimFloat16 rhs) const;
313 bool operator>(SimFloat16 rhs) const;
314 bool operator==(SimFloat16 rhs) const;
315 bool operator!=(SimFloat16 rhs) const;
316 // This is necessary for conversions peformed in (macro asm) Fmov.
317 bool operator==(double rhs) const;
318 operator double() const;
319};
320} // namespace internal
321
322uint32_t Float16Sign(internal::SimFloat16 value);
323
324uint32_t Float16Exp(internal::SimFloat16 value);
325
326uint32_t Float16Mantissa(internal::SimFloat16 value);
327
Pierre Langlois88c46b82016-06-02 18:15:32 +0100328uint32_t FloatSign(float value);
329VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
330 return FloatSign(value);
331}
332
333uint32_t FloatExp(float value);
334VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
335 return FloatExp(value);
336}
337
338uint32_t FloatMantissa(float value);
339VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
340 return FloatMantissa(value);
341}
342
343uint32_t DoubleSign(double value);
344VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
345 return DoubleSign(value);
346}
347
348uint32_t DoubleExp(double value);
349VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
350 return DoubleExp(value);
351}
352
353uint64_t DoubleMantissa(double value);
354VIXL_DEPRECATED("DoubleMantissa",
355 inline uint64_t double_mantissa(double value)) {
356 return DoubleMantissa(value);
357}
358
Jacob Bramleyca789742018-09-13 14:25:46 +0100359internal::SimFloat16 Float16Pack(uint16_t sign,
360 uint16_t exp,
361 uint16_t mantissa);
362
Pierre Langlois88c46b82016-06-02 18:15:32 +0100363float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
364VIXL_DEPRECATED("FloatPack",
365 inline float float_pack(uint32_t sign,
366 uint32_t exp,
367 uint32_t mantissa)) {
368 return FloatPack(sign, exp, mantissa);
369}
370
371double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
372VIXL_DEPRECATED("DoublePack",
373 inline double double_pack(uint32_t sign,
374 uint32_t exp,
375 uint64_t mantissa)) {
376 return DoublePack(sign, exp, mantissa);
377}
armvixl5289c592015-03-02 13:52:04 +0000378
379// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100380int Float16Classify(Float16 value);
381VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
382 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100383}
armvixlf37fdc02014-02-05 13:22:16 +0000384
Jacob Bramleyca789742018-09-13 14:25:46 +0100385bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100386
Jacob Bramleyca789742018-09-13 14:25:46 +0100387inline bool IsNaN(float value) { return std::isnan(value); }
388
389inline bool IsNaN(double value) { return std::isnan(value); }
390
391inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
392
393inline bool IsInf(float value) { return std::isinf(value); }
394
395inline bool IsInf(double value) { return std::isinf(value); }
396
397inline bool IsInf(Float16 value) {
398 return Float16Classify(value) == FP_INFINITE;
399}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100400
401
armvixlf37fdc02014-02-05 13:22:16 +0000402// NaN tests.
403inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100404 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100405 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100406 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000407 return true;
408 }
409 return false;
410}
411
412
413inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000414 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100415 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100416 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000417 return true;
418 }
419 return false;
420}
421
422
Jacob Bramleyca789742018-09-13 14:25:46 +0100423inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000424 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100425 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000426}
427
428
armvixlf37fdc02014-02-05 13:22:16 +0000429template <typename T>
430inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100431 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000432}
433
434
armvixlb0c8ae22014-03-21 14:03:59 +0000435// Convert the NaN in 'num' to a quiet NaN.
436inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100437 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100438 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100439 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000440}
441
442
443inline float ToQuietNaN(float num) {
444 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100445 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100446 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000447}
448
449
Jacob Bramleyca789742018-09-13 14:25:46 +0100450inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
451 const uint16_t kFP16QuietNaNMask = 0x0200;
452 VIXL_ASSERT(IsNaN(num));
453 return internal::SimFloat16(
454 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
455}
456
457
armvixlb0c8ae22014-03-21 14:03:59 +0000458// Fused multiply-add.
459inline double FusedMultiplyAdd(double op1, double op2, double a) {
460 return fma(op1, op2, a);
461}
462
463
464inline float FusedMultiplyAdd(float op1, float op2, float a) {
465 return fmaf(op1, op2, a);
466}
467
468
armvixl0f35e362016-05-10 13:57:58 +0100469inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100470
471
armvixl0f35e362016-05-10 13:57:58 +0100472template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100473inline int HighestSetBitPosition(T value) {
474 VIXL_ASSERT(value != 0);
475 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
476}
477
478
armvixl0f35e362016-05-10 13:57:58 +0100479template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100480inline int WhichPowerOf2(V value) {
481 VIXL_ASSERT(IsPowerOf2(value));
482 return CountTrailingZeros(value);
483}
armvixlad96eda2013-06-14 11:42:37 +0100484
armvixldb644342015-07-21 11:37:10 +0100485
armvixl330dc712014-11-25 10:38:32 +0000486unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
487
armvixldb644342015-07-21 11:37:10 +0100488
Pierre Langlois88c46b82016-06-02 18:15:32 +0100489int BitCount(uint64_t value);
490
491
armvixldb644342015-07-21 11:37:10 +0100492template <typename T>
493T ReverseBits(T value) {
494 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
495 (sizeof(value) == 4) || (sizeof(value) == 8));
496 T result = 0;
497 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
498 result = (result << 1) | (value & 1);
499 value >>= 1;
500 }
501 return result;
502}
503
504
505template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100506inline T SignExtend(T val, int bitSize) {
507 VIXL_ASSERT(bitSize > 0);
508 T mask = (T(2) << (bitSize - 1)) - T(1);
509 val &= mask;
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700510 T sign_bits = -((val >> (bitSize - 1)) << bitSize);
511 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100512 return val;
513}
514
515
516template <typename T>
armvixldb644342015-07-21 11:37:10 +0100517T ReverseBytes(T value, int block_bytes_log2) {
518 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
519 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
520 // Split the 64-bit value into an 8-bit array, where b[0] is the least
521 // significant byte, and b[7] is the most significant.
522 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000523 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100524 for (int i = 7; i >= 0; i--) {
525 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
526 mask >>= 8;
527 }
528
529 // Permutation tables for REV instructions.
530 // permute_table[0] is used by REV16_x, REV16_w
531 // permute_table[1] is used by REV32_x, REV_w
532 // permute_table[2] is used by REV_x
533 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100534 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
535 {4, 5, 6, 7, 0, 1, 2, 3},
536 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000537 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100538 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000539 temp <<= 8;
540 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100541 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000542
543 T result;
544 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
545 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100546 return result;
547}
548
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700549template <unsigned MULTIPLE, typename T>
550inline bool IsMultiple(T value) {
551 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
552 return (value & (MULTIPLE - 1)) == 0;
553}
armvixldb644342015-07-21 11:37:10 +0100554
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000555template <typename T>
556inline bool IsMultiple(T value, unsigned multiple) {
557 VIXL_ASSERT(IsPowerOf2(multiple));
558 return (value & (multiple - 1)) == 0;
559}
560
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000561template <typename T>
562inline bool IsAligned(T pointer, int alignment) {
563 VIXL_ASSERT(IsPowerOf2(alignment));
564 return (pointer & (alignment - 1)) == 0;
565}
566
armvixlad96eda2013-06-14 11:42:37 +0100567// Pointer alignment
568// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100569template <unsigned ALIGN, typename T>
570inline bool IsAligned(T pointer) {
571 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100572 // Use C-style casts to get static_cast behaviour for integral types (T), and
573 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000574 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100575}
576
armvixl0f35e362016-05-10 13:57:58 +0100577template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100578bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100579 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100580}
581
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100582// Increment a pointer until it has the specified alignment. The alignment must
583// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100584template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100585T AlignUp(T pointer,
586 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100587 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100588 // Use C-style casts to get static_cast behaviour for integral types (T), and
589 // reinterpret_cast behaviour for other types.
590
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100591 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
592 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000593 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100594
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100595 size_t mask = alignment - 1;
596 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000597 VIXL_ASSERT(result >= pointer);
598
599 return result;
armvixlad96eda2013-06-14 11:42:37 +0100600}
601
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100602// Decrement a pointer until it has the specified alignment. The alignment must
603// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100604template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100605T AlignDown(T pointer,
606 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100607 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100608 // Use C-style casts to get static_cast behaviour for integral types (T), and
609 // reinterpret_cast behaviour for other types.
610
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100611 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
612 (typename Unsigned<sizeof(T) * kBitsPerByte>::type)pointer;
armvixl330dc712014-11-25 10:38:32 +0000613 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100614
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100615 size_t mask = alignment - 1;
616 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000617}
618
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100619
Pierre Langlois88c46b82016-06-02 18:15:32 +0100620template <typename T>
621inline T ExtractBit(T value, unsigned bit) {
622 return (value >> bit) & T(1);
623}
624
625template <typename Ts, typename Td>
626inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
627 return Td((value >> least_significant_bit) & Ts(mask));
628}
629
630template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800631inline void AssignBit(Td& dst, // NOLINT(runtime/references)
632 int bit,
633 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100634 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
635 VIXL_ASSERT(bit >= 0);
636 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
637 Td mask(1);
638 dst &= ~(mask << bit);
639 dst |= Td(value) << bit;
640}
641
642template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800643inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100644 int least_significant_bit,
645 Ts mask,
646 Ts value) {
647 VIXL_ASSERT(least_significant_bit >= 0);
648 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
649 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
650 Td(mask));
651 VIXL_ASSERT((value & mask) == value);
652 dst &= ~(Td(mask) << least_significant_bit);
653 dst |= Td(value) << least_significant_bit;
654}
655
656class VFP {
657 public:
658 static uint32_t FP32ToImm8(float imm) {
659 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
660 uint32_t bits = FloatToRawbits(imm);
661 // bit7: a000.0000
662 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
663 // bit6: 0b00.0000
664 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
665 // bit5_to_0: 00cd.efgh
666 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
667 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
668 }
669 static uint32_t FP64ToImm8(double imm) {
670 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
671 // 0000.0000.0000.0000.0000.0000.0000.0000
672 uint64_t bits = DoubleToRawbits(imm);
673 // bit7: a000.0000
674 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
675 // bit6: 0b00.0000
676 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
677 // bit5_to_0: 00cd.efgh
678 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
679
680 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
681 }
682 static float Imm8ToFP32(uint32_t imm8) {
683 // Imm8: abcdefgh (8 bits)
684 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
685 // where B is b ^ 1
686 uint32_t bits = imm8;
687 uint32_t bit7 = (bits >> 7) & 0x1;
688 uint32_t bit6 = (bits >> 6) & 0x1;
689 uint32_t bit5_to_0 = bits & 0x3f;
690 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
691
692 return RawbitsToFloat(result);
693 }
694 static double Imm8ToFP64(uint32_t imm8) {
695 // Imm8: abcdefgh (8 bits)
696 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
697 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
698 // where B is b ^ 1
699 uint32_t bits = imm8;
700 uint64_t bit7 = (bits >> 7) & 0x1;
701 uint64_t bit6 = (bits >> 6) & 0x1;
702 uint64_t bit5_to_0 = bits & 0x3f;
703 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
704 return RawbitsToDouble(result);
705 }
706 static bool IsImmFP32(float imm) {
707 // Valid values will have the form:
708 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
709 uint32_t bits = FloatToRawbits(imm);
710 // bits[19..0] are cleared.
711 if ((bits & 0x7ffff) != 0) {
712 return false;
713 }
714
715
716 // bits[29..25] are all set or all cleared.
717 uint32_t b_pattern = (bits >> 16) & 0x3e00;
718 if (b_pattern != 0 && b_pattern != 0x3e00) {
719 return false;
720 }
721 // bit[30] and bit[29] are opposite.
722 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
723 return false;
724 }
725 return true;
726 }
727 static bool IsImmFP64(double imm) {
728 // Valid values will have the form:
729 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
730 // 0000.0000.0000.0000.0000.0000.0000.0000
731 uint64_t bits = DoubleToRawbits(imm);
732 // bits[47..0] are cleared.
733 if ((bits & 0x0000ffffffffffff) != 0) {
734 return false;
735 }
736 // bits[61..54] are all set or all cleared.
737 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
738 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
739 return false;
740 }
741 // bit[62] and bit[61] are opposite.
742 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
743 return false;
744 }
745 return true;
746 }
747};
748
749class BitField {
750 // ForEachBitHelper is a functor that will call
751 // bool ForEachBitHelper::execute(ElementType id) const
752 // and expects a boolean in return whether to continue (if true)
753 // or stop (if false)
754 // check_set will check if the bits are on (true) or off(false)
755 template <typename ForEachBitHelper, bool check_set>
756 bool ForEachBit(const ForEachBitHelper& helper) {
757 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
758 if (bitfield_[i] == check_set)
759 if (!helper.execute(i)) return false;
760 }
761 return true;
762 }
763
764 public:
765 explicit BitField(unsigned size) : bitfield_(size, 0) {}
766
767 void Set(int i) {
768 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
769 bitfield_[i] = true;
770 }
771
772 void Unset(int i) {
773 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
774 bitfield_[i] = true;
775 }
776
777 bool IsSet(int i) const { return bitfield_[i]; }
778
779 // For each bit not set in the bitfield call the execute functor
780 // execute.
781 // ForEachBitSetHelper::execute returns true if the iteration through
782 // the bits can continue, otherwise it will stop.
783 // struct ForEachBitSetHelper {
784 // bool execute(int /*id*/) { return false; }
785 // };
786 template <typename ForEachBitNotSetHelper>
787 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
788 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
789 }
790
791 // For each bit set in the bitfield call the execute functor
792 // execute.
793 template <typename ForEachBitSetHelper>
794 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
795 return ForEachBit<ForEachBitSetHelper, true>(helper);
796 }
797
798 private:
799 std::vector<bool> bitfield_;
800};
801
Pierre Langloisd82faf62018-04-04 13:06:58 +0100802namespace internal {
803
Pierre Langlois88c46b82016-06-02 18:15:32 +0100804typedef int64_t Int64;
805class Uint64;
806class Uint128;
807
808class Uint32 {
809 uint32_t data_;
810
811 public:
812 // Unlike uint32_t, Uint32 has a default constructor.
813 Uint32() { data_ = 0; }
814 explicit Uint32(uint32_t data) : data_(data) {}
815 inline explicit Uint32(Uint64 data);
816 uint32_t Get() const { return data_; }
817 template <int N>
818 int32_t GetSigned() const {
819 return ExtractSignedBitfield32(N - 1, 0, data_);
820 }
821 int32_t GetSigned() const { return data_; }
822 Uint32 operator~() const { return Uint32(~data_); }
823 Uint32 operator-() const { return Uint32(-data_); }
824 bool operator==(Uint32 value) const { return data_ == value.data_; }
825 bool operator!=(Uint32 value) const { return data_ != value.data_; }
826 bool operator>(Uint32 value) const { return data_ > value.data_; }
827 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
828 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
829 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
830 Uint32 operator&=(Uint32 value) {
831 data_ &= value.data_;
832 return *this;
833 }
834 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
835 Uint32 operator^=(Uint32 value) {
836 data_ ^= value.data_;
837 return *this;
838 }
839 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
840 Uint32 operator|=(Uint32 value) {
841 data_ |= value.data_;
842 return *this;
843 }
844 // Unlike uint32_t, the shift functions can accept negative shift and
845 // return 0 when the shift is too big.
846 Uint32 operator>>(int shift) const {
847 if (shift == 0) return *this;
848 if (shift < 0) {
849 int tmp = -shift;
850 if (tmp >= 32) return Uint32(0);
851 return Uint32(data_ << tmp);
852 }
853 int tmp = shift;
854 if (tmp >= 32) return Uint32(0);
855 return Uint32(data_ >> tmp);
856 }
857 Uint32 operator<<(int shift) const {
858 if (shift == 0) return *this;
859 if (shift < 0) {
860 int tmp = -shift;
861 if (tmp >= 32) return Uint32(0);
862 return Uint32(data_ >> tmp);
863 }
864 int tmp = shift;
865 if (tmp >= 32) return Uint32(0);
866 return Uint32(data_ << tmp);
867 }
868};
869
870class Uint64 {
871 uint64_t data_;
872
873 public:
874 // Unlike uint64_t, Uint64 has a default constructor.
875 Uint64() { data_ = 0; }
876 explicit Uint64(uint64_t data) : data_(data) {}
877 explicit Uint64(Uint32 data) : data_(data.Get()) {}
878 inline explicit Uint64(Uint128 data);
879 uint64_t Get() const { return data_; }
880 int64_t GetSigned(int N) const {
881 return ExtractSignedBitfield64(N - 1, 0, data_);
882 }
883 int64_t GetSigned() const { return data_; }
884 Uint32 ToUint32() const {
885 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100886 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100887 }
888 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100889 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100890 Uint64 operator~() const { return Uint64(~data_); }
891 Uint64 operator-() const { return Uint64(-data_); }
892 bool operator==(Uint64 value) const { return data_ == value.data_; }
893 bool operator!=(Uint64 value) const { return data_ != value.data_; }
894 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
895 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
896 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
897 Uint64 operator&=(Uint64 value) {
898 data_ &= value.data_;
899 return *this;
900 }
901 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
902 Uint64 operator^=(Uint64 value) {
903 data_ ^= value.data_;
904 return *this;
905 }
906 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
907 Uint64 operator|=(Uint64 value) {
908 data_ |= value.data_;
909 return *this;
910 }
911 // Unlike uint64_t, the shift functions can accept negative shift and
912 // return 0 when the shift is too big.
913 Uint64 operator>>(int shift) const {
914 if (shift == 0) return *this;
915 if (shift < 0) {
916 int tmp = -shift;
917 if (tmp >= 64) return Uint64(0);
918 return Uint64(data_ << tmp);
919 }
920 int tmp = shift;
921 if (tmp >= 64) return Uint64(0);
922 return Uint64(data_ >> tmp);
923 }
924 Uint64 operator<<(int shift) const {
925 if (shift == 0) return *this;
926 if (shift < 0) {
927 int tmp = -shift;
928 if (tmp >= 64) return Uint64(0);
929 return Uint64(data_ >> tmp);
930 }
931 int tmp = shift;
932 if (tmp >= 64) return Uint64(0);
933 return Uint64(data_ << tmp);
934 }
935};
936
937class Uint128 {
938 uint64_t data_high_;
939 uint64_t data_low_;
940
941 public:
942 Uint128() : data_high_(0), data_low_(0) {}
943 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
944 explicit Uint128(Uint64 data_low)
945 : data_high_(0), data_low_(data_low.Get()) {}
946 Uint128(uint64_t data_high, uint64_t data_low)
947 : data_high_(data_high), data_low_(data_low) {}
948 Uint64 ToUint64() const {
949 VIXL_ASSERT(data_high_ == 0);
950 return Uint64(data_low_);
951 }
952 Uint64 GetHigh64() const { return Uint64(data_high_); }
953 Uint64 GetLow64() const { return Uint64(data_low_); }
954 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
955 bool operator==(Uint128 value) const {
956 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
957 }
958 Uint128 operator&(Uint128 value) const {
959 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
960 }
961 Uint128 operator&=(Uint128 value) {
962 data_high_ &= value.data_high_;
963 data_low_ &= value.data_low_;
964 return *this;
965 }
966 Uint128 operator|=(Uint128 value) {
967 data_high_ |= value.data_high_;
968 data_low_ |= value.data_low_;
969 return *this;
970 }
971 Uint128 operator>>(int shift) const {
972 VIXL_ASSERT((shift >= 0) && (shift < 128));
973 if (shift == 0) return *this;
974 if (shift >= 64) {
975 return Uint128(0, data_high_ >> (shift - 64));
976 }
977 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
978 return Uint128(data_high_ >> shift, tmp);
979 }
980 Uint128 operator<<(int shift) const {
981 VIXL_ASSERT((shift >= 0) && (shift < 128));
982 if (shift == 0) return *this;
983 if (shift >= 64) {
984 return Uint128(data_low_ << (shift - 64), 0);
985 }
986 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
987 return Uint128(tmp, data_low_ << shift);
988 }
989};
990
991Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
992Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
993
994Int64 BitCount(Uint32 value);
995
TatWai Chong13634762019-07-16 16:20:45 -0700996// The algorithm used is adapted from the one described in section 8.2 of
997// Hacker's Delight, by Henry S. Warren, Jr.
998template <unsigned N, typename T>
999int64_t MultiplyHigh(T u, T v) {
1000 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1001 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1002 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1003 uint64_t sign_ext = 0;
1004 unsigned half_bits = N / 2;
1005 uint64_t half_mask = GetUintMask(half_bits);
1006 if (std::numeric_limits<T>::is_signed) {
1007 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1008 }
1009
1010 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1011 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1012
1013 u0 = u & half_mask;
1014 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1015 v0 = v & half_mask;
1016 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1017
1018 w0 = u0 * v0;
1019 t = u1 * v0 + (w0 >> half_bits);
1020
1021 w1 = t & half_mask;
1022 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1023 w1 = u0 * v1 + w1;
1024 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1025
1026 uint64_t value = u1 * v1 + w2 + w1;
1027 int64_t result;
1028 memcpy(&result, &value, sizeof(result));
1029 return result;
1030}
1031
Pierre Langloisd82faf62018-04-04 13:06:58 +01001032} // namespace internal
1033
Jacob Bramleyca789742018-09-13 14:25:46 +01001034// The default NaN values (for FPCR.DN=1).
1035extern const double kFP64DefaultNaN;
1036extern const float kFP32DefaultNaN;
1037extern const Float16 kFP16DefaultNaN;
1038
1039// Floating-point infinity values.
1040extern const Float16 kFP16PositiveInfinity;
1041extern const Float16 kFP16NegativeInfinity;
1042extern const float kFP32PositiveInfinity;
1043extern const float kFP32NegativeInfinity;
1044extern const double kFP64PositiveInfinity;
1045extern const double kFP64NegativeInfinity;
1046
1047// Floating-point zero values.
1048extern const Float16 kFP16PositiveZero;
1049extern const Float16 kFP16NegativeZero;
1050
1051// AArch64 floating-point specifics. These match IEEE-754.
1052const unsigned kDoubleMantissaBits = 52;
1053const unsigned kDoubleExponentBits = 11;
1054const unsigned kFloatMantissaBits = 23;
1055const unsigned kFloatExponentBits = 8;
1056const unsigned kFloat16MantissaBits = 10;
1057const unsigned kFloat16ExponentBits = 5;
1058
1059enum FPRounding {
1060 // The first four values are encodable directly by FPCR<RMode>.
1061 FPTieEven = 0x0,
1062 FPPositiveInfinity = 0x1,
1063 FPNegativeInfinity = 0x2,
1064 FPZero = 0x3,
1065
1066 // The final rounding modes are only available when explicitly specified by
1067 // the instruction (such as with fcvta). It cannot be set in FPCR.
1068 FPTieAway,
1069 FPRoundOdd
1070};
1071
1072enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1073
1074// Assemble the specified IEEE-754 components into the target type and apply
1075// appropriate rounding.
1076// sign: 0 = positive, 1 = negative
1077// exponent: Unbiased IEEE-754 exponent.
1078// mantissa: The mantissa of the input. The top bit (which is not encoded for
1079// normal IEEE-754 values) must not be omitted. This bit has the
1080// value 'pow(2, exponent)'.
1081//
1082// The input value is assumed to be a normalized value. That is, the input may
1083// not be infinity or NaN. If the source value is subnormal, it must be
1084// normalized before calling this function such that the highest set bit in the
1085// mantissa has the value 'pow(2, exponent)'.
1086//
1087// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1088// calling a templated FPRound.
1089template <class T, int ebits, int mbits>
1090T FPRound(int64_t sign,
1091 int64_t exponent,
1092 uint64_t mantissa,
1093 FPRounding round_mode) {
1094 VIXL_ASSERT((sign == 0) || (sign == 1));
1095
1096 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1097 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1098
1099 // Rounding can promote subnormals to normals, and normals to infinities. For
1100 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1101 // encodable as a float, but rounding based on the low-order mantissa bits
1102 // could make it overflow. With ties-to-even rounding, this value would become
1103 // an infinity.
1104
1105 // ---- Rounding Method ----
1106 //
1107 // The exponent is irrelevant in the rounding operation, so we treat the
1108 // lowest-order bit that will fit into the result ('onebit') as having
1109 // the value '1'. Similarly, the highest-order bit that won't fit into
1110 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1111 // 'onebit' and 'halfbit':
1112 //
1113 // These bits fit into the result.
1114 // |---------------------|
1115 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1116 // ||
1117 // / |
1118 // / halfbit
1119 // onebit
1120 //
1121 // For subnormal outputs, the range of representable bits is smaller and
1122 // the position of onebit and halfbit depends on the exponent of the
1123 // input, but the method is otherwise similar.
1124 //
1125 // onebit(frac)
1126 // |
1127 // | halfbit(frac) halfbit(adjusted)
1128 // | / /
1129 // | | |
1130 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1131 // 0b00.0... -> 0b00.0... -> 0b00
1132 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1133 // 0b00.1... -> 0b00.1... -> 0b01
1134 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1135 // 0b01.0... -> 0b01.0... -> 0b01
1136 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1137 // 0b01.1... -> 0b01.1... -> 0b10
1138 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1139 // 0b10.0... -> 0b10.0... -> 0b10
1140 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1141 // 0b10.1... -> 0b10.1... -> 0b11
1142 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1143 // ... / | / |
1144 // / | / |
1145 // / |
1146 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1147 //
1148 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1149
1150 static const int mantissa_offset = 0;
1151 static const int exponent_offset = mantissa_offset + mbits;
1152 static const int sign_offset = exponent_offset + ebits;
1153 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1154
1155 // Bail out early for zero inputs.
1156 if (mantissa == 0) {
1157 return static_cast<T>(sign << sign_offset);
1158 }
1159
1160 // If all bits in the exponent are set, the value is infinite or NaN.
1161 // This is true for all binary IEEE-754 formats.
1162 static const int infinite_exponent = (1 << ebits) - 1;
1163 static const int max_normal_exponent = infinite_exponent - 1;
1164
1165 // Apply the exponent bias to encode it for the result. Doing this early makes
1166 // it easy to detect values that will be infinite or subnormal.
1167 exponent += max_normal_exponent >> 1;
1168
1169 if (exponent > max_normal_exponent) {
1170 // Overflow: the input is too large for the result type to represent.
1171 if (round_mode == FPTieEven) {
1172 // FPTieEven rounding mode handles overflows using infinities.
1173 exponent = infinite_exponent;
1174 mantissa = 0;
1175 } else {
1176 VIXL_ASSERT(round_mode == FPRoundOdd);
1177 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1178 // normal number.
1179 exponent = max_normal_exponent;
1180 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1181 }
1182 return static_cast<T>((sign << sign_offset) |
1183 (exponent << exponent_offset) |
1184 (mantissa << mantissa_offset));
1185 }
1186
1187 // Calculate the shift required to move the top mantissa bit to the proper
1188 // place in the destination type.
1189 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1190 int shift = highest_significant_bit - mbits;
1191
1192 if (exponent <= 0) {
1193 // The output will be subnormal (before rounding).
1194 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1195 // is necessary because the exponent of a subnormal value (encoded as 0) is
1196 // the same as the exponent of the smallest normal value (encoded as 1).
1197 shift += -exponent + 1;
1198
1199 // Handle inputs that would produce a zero output.
1200 //
1201 // Shifts higher than highest_significant_bit+1 will always produce a zero
1202 // result. A shift of exactly highest_significant_bit+1 might produce a
1203 // non-zero result after rounding.
1204 if (shift > (highest_significant_bit + 1)) {
1205 if (round_mode == FPTieEven) {
1206 // The result will always be +/-0.0.
1207 return static_cast<T>(sign << sign_offset);
1208 } else {
1209 VIXL_ASSERT(round_mode == FPRoundOdd);
1210 VIXL_ASSERT(mantissa != 0);
1211 // For FPRoundOdd, if the mantissa is too small to represent and
1212 // non-zero return the next "odd" value.
1213 return static_cast<T>((sign << sign_offset) | 1);
1214 }
1215 }
1216
1217 // Properly encode the exponent for a subnormal output.
1218 exponent = 0;
1219 } else {
1220 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1221 // normal values.
1222 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1223 }
1224
1225 // The casts below are only well-defined for unsigned integers.
1226 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1227 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1228
1229 if (shift > 0) {
1230 if (round_mode == FPTieEven) {
1231 // We have to shift the mantissa to the right. Some precision is lost, so
1232 // we need to apply rounding.
1233 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1234 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1235 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1236 uint64_t adjusted = mantissa - adjustment;
1237 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1238
1239 T result =
1240 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1241 ((mantissa >> shift) << mantissa_offset));
1242
1243 // A very large mantissa can overflow during rounding. If this happens,
1244 // the exponent should be incremented and the mantissa set to 1.0
1245 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1246 // has the nice side-effect that this case is handled for free.
1247 //
1248 // This also handles cases where a very large finite value overflows to
1249 // infinity, or where a very large subnormal value overflows to become
1250 // normal.
1251 return result + halfbit_adjusted;
1252 } else {
1253 VIXL_ASSERT(round_mode == FPRoundOdd);
1254 // If any bits at position halfbit or below are set, onebit (ie. the
1255 // bottom bit of the resulting mantissa) must be set.
1256 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1257 if (fractional_bits != 0) {
1258 mantissa |= UINT64_C(1) << shift;
1259 }
1260
1261 return static_cast<T>((sign << sign_offset) |
1262 (exponent << exponent_offset) |
1263 ((mantissa >> shift) << mantissa_offset));
1264 }
1265 } else {
1266 // We have to shift the mantissa to the left (or not at all). The input
1267 // mantissa is exactly representable in the output mantissa, so apply no
1268 // rounding correction.
1269 return static_cast<T>((sign << sign_offset) |
1270 (exponent << exponent_offset) |
1271 ((mantissa << -shift) << mantissa_offset));
1272 }
1273}
1274
1275
1276// See FPRound for a description of this function.
1277inline double FPRoundToDouble(int64_t sign,
1278 int64_t exponent,
1279 uint64_t mantissa,
1280 FPRounding round_mode) {
1281 uint64_t bits =
1282 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1283 exponent,
1284 mantissa,
1285 round_mode);
1286 return RawbitsToDouble(bits);
1287}
1288
1289
1290// See FPRound for a description of this function.
1291inline Float16 FPRoundToFloat16(int64_t sign,
1292 int64_t exponent,
1293 uint64_t mantissa,
1294 FPRounding round_mode) {
1295 return RawbitsToFloat16(
1296 FPRound<uint16_t,
1297 kFloat16ExponentBits,
1298 kFloat16MantissaBits>(sign, exponent, mantissa, round_mode));
1299}
1300
1301
1302// See FPRound for a description of this function.
1303static inline float FPRoundToFloat(int64_t sign,
1304 int64_t exponent,
1305 uint64_t mantissa,
1306 FPRounding round_mode) {
1307 uint32_t bits =
1308 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1309 exponent,
1310 mantissa,
1311 round_mode);
1312 return RawbitsToFloat(bits);
1313}
1314
1315
1316float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1317float FPToFloat(double value,
1318 FPRounding round_mode,
1319 UseDefaultNaN DN,
1320 bool* exception = NULL);
1321
1322double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1323double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1324
1325Float16 FPToFloat16(float value,
1326 FPRounding round_mode,
1327 UseDefaultNaN DN,
1328 bool* exception = NULL);
1329
1330Float16 FPToFloat16(double value,
1331 FPRounding round_mode,
1332 UseDefaultNaN DN,
1333 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001334
1335// Like static_cast<T>(value), but with specialisations for the Float16 type.
1336template <typename T, typename F>
1337T StaticCastFPTo(F value) {
1338 return static_cast<T>(value);
1339}
1340
1341template <>
1342inline float StaticCastFPTo<float, Float16>(Float16 value) {
1343 return FPToFloat(value, kIgnoreDefaultNaN);
1344}
1345
1346template <>
1347inline double StaticCastFPTo<double, Float16>(Float16 value) {
1348 return FPToDouble(value, kIgnoreDefaultNaN);
1349}
1350
1351template <>
1352inline Float16 StaticCastFPTo<Float16, float>(float value) {
1353 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1354}
1355
1356template <>
1357inline Float16 StaticCastFPTo<Float16, double>(double value) {
1358 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1359}
1360
1361template <typename T>
1362uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1363 switch (size_in_bits) {
1364 case 16:
1365 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1366 case 32:
1367 return FloatToRawbits(StaticCastFPTo<float>(value));
1368 case 64:
1369 return DoubleToRawbits(StaticCastFPTo<double>(value));
1370 }
1371 VIXL_UNREACHABLE();
1372 return 0;
1373}
armvixlad96eda2013-06-14 11:42:37 +01001374} // namespace vixl
1375
1376#endif // VIXL_UTILS_H