blob: cd50a2a5de078c8f2a1a00f19c478b963262c65a [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 Bramley5523f6c2019-06-28 11:37:26 +010074inline 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) {
83 VIXL_ASSERT((0 < n) && (n < 32));
84 uint32_t limit = UINT32_C(1) << (n - 1);
85 return x < limit;
86}
87inline bool IsIntN(unsigned n, int32_t x) {
88 VIXL_ASSERT((0 < n) && (n < 32));
89 int32_t limit = INT32_C(1) << (n - 1);
90 return (-limit <= x) && (x < limit);
91}
92inline bool IsIntN(unsigned n, uint64_t x) {
93 VIXL_ASSERT((0 < n) && (n < 64));
94 uint64_t limit = UINT64_C(1) << (n - 1);
95 return x < limit;
96}
Pierre Langlois88c46b82016-06-02 18:15:32 +010097inline bool IsIntN(unsigned n, int64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +000098 VIXL_ASSERT((0 < n) && (n < 64));
99 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) {
107 VIXL_ASSERT((0 < n) && (n < 32));
108 return !(x >> n);
109}
110inline bool IsUintN(unsigned n, int32_t x) {
111 VIXL_ASSERT((0 < n) && (n < 32));
112 // Convert to an unsigned integer to avoid implementation-defined behavior.
113 return !(static_cast<uint32_t>(x) >> n);
114}
115inline bool IsUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000116 VIXL_ASSERT((0 < n) && (n < 64));
armvixlad96eda2013-06-14 11:42:37 +0100117 return !(x >> n);
118}
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000119inline bool IsUintN(unsigned n, int64_t x) {
120 VIXL_ASSERT((0 < n) && (n < 64));
121 // Convert to an unsigned integer to avoid implementation-defined behavior.
122 return !(static_cast<uint64_t>(x) >> n);
123}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100124VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
125 return IsUintN(n, x);
126}
armvixlad96eda2013-06-14 11:42:37 +0100127
Jacob Bramley3976edb2016-10-18 10:51:43 +0100128inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000129 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +0100130 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100131}
Jacob Bramley3976edb2016-10-18 10:51:43 +0100132VIXL_DEPRECATED("TruncateToUintN",
133 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
134 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100135}
armvixlad96eda2013-06-14 11:42:37 +0100136
armvixl0f35e362016-05-10 13:57:58 +0100137// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +0100138#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100139V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
140V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
141V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100142V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
143
144#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100145V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
146V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
147V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
148V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100149
Jacob Bramley3976edb2016-10-18 10:51:43 +0100150#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
151
armvixl0f35e362016-05-10 13:57:58 +0100152// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100153
Pierre Langlois88c46b82016-06-02 18:15:32 +0100154#define DECLARE_IS_INT_N(N) \
155 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
156 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
157 return IsIntN(N, x); \
158 }
159
160#define DECLARE_IS_UINT_N(N) \
161 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
162 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
163 return IsUintN(N, x); \
164 }
165
Jacob Bramley3976edb2016-10-18 10:51:43 +0100166#define DECLARE_TRUNCATE_TO_UINT_32(N) \
167 inline uint32_t TruncateToUint##N(uint64_t x) { \
168 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
169 } \
170 VIXL_DEPRECATED("TruncateToUint" #N, \
171 inline uint32_t truncate_to_int##N(int64_t x)) { \
172 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100173 }
174
armvixlad96eda2013-06-14 11:42:37 +0100175INT_1_TO_63_LIST(DECLARE_IS_INT_N)
176INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100177INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100178
armvixlad96eda2013-06-14 11:42:37 +0100179#undef DECLARE_IS_INT_N
180#undef DECLARE_IS_UINT_N
181#undef DECLARE_TRUNCATE_TO_INT_N
182
183// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100184inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000185 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
186 (msb >= lsb));
187 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100188 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100189}
190
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000191
192inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint32_t x) {
193 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
194 (msb >= lsb));
195 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100196}
197
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000198
Jacob Bramley5523f6c2019-06-28 11:37:26 +0100199inline int64_t ExtractSignedBitfield64(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 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
203 // If the highest extracted bit is set, sign extend.
204 if ((temp >> (msb - lsb)) == 1) {
205 temp |= ~UINT64_C(0) << (msb - lsb);
206 }
207 int64_t result;
208 memcpy(&result, &temp, sizeof(result));
209 return result;
armvixlad96eda2013-06-14 11:42:37 +0100210}
211
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000212
Jacob Bramley5523f6c2019-06-28 11:37:26 +0100213inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint32_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000214 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
215 (msb >= lsb));
216 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
217 int32_t result;
218 memcpy(&result, &temp, sizeof(result));
219 return result;
220}
221
222
223inline 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 Bramleyca789742018-09-13 14:25:46 +0100280namespace internal {
281
282// Internal simulation class used solely by the simulator to
283// provide an abstraction layer for any half-precision arithmetic.
284class SimFloat16 : public Float16 {
285 public:
286 // TODO: We should investigate making this constructor explicit.
287 // This is currently difficult to do due to a number of templated
288 // functions in the simulator which rely on returning double values.
289 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
290 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
291 this->rawbits_ = Float16ToRawbits(f);
292 }
293 SimFloat16() : Float16() {}
294 SimFloat16 operator-() const;
295 SimFloat16 operator+(SimFloat16 rhs) const;
296 SimFloat16 operator-(SimFloat16 rhs) const;
297 SimFloat16 operator*(SimFloat16 rhs) const;
298 SimFloat16 operator/(SimFloat16 rhs) const;
299 bool operator<(SimFloat16 rhs) const;
300 bool operator>(SimFloat16 rhs) const;
301 bool operator==(SimFloat16 rhs) const;
302 bool operator!=(SimFloat16 rhs) const;
303 // This is necessary for conversions peformed in (macro asm) Fmov.
304 bool operator==(double rhs) const;
305 operator double() const;
306};
307} // namespace internal
308
309uint32_t Float16Sign(internal::SimFloat16 value);
310
311uint32_t Float16Exp(internal::SimFloat16 value);
312
313uint32_t Float16Mantissa(internal::SimFloat16 value);
314
Pierre Langlois88c46b82016-06-02 18:15:32 +0100315uint32_t FloatSign(float value);
316VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
317 return FloatSign(value);
318}
319
320uint32_t FloatExp(float value);
321VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
322 return FloatExp(value);
323}
324
325uint32_t FloatMantissa(float value);
326VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
327 return FloatMantissa(value);
328}
329
330uint32_t DoubleSign(double value);
331VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
332 return DoubleSign(value);
333}
334
335uint32_t DoubleExp(double value);
336VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
337 return DoubleExp(value);
338}
339
340uint64_t DoubleMantissa(double value);
341VIXL_DEPRECATED("DoubleMantissa",
342 inline uint64_t double_mantissa(double value)) {
343 return DoubleMantissa(value);
344}
345
Jacob Bramleyca789742018-09-13 14:25:46 +0100346internal::SimFloat16 Float16Pack(uint16_t sign,
347 uint16_t exp,
348 uint16_t mantissa);
349
Pierre Langlois88c46b82016-06-02 18:15:32 +0100350float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
351VIXL_DEPRECATED("FloatPack",
352 inline float float_pack(uint32_t sign,
353 uint32_t exp,
354 uint32_t mantissa)) {
355 return FloatPack(sign, exp, mantissa);
356}
357
358double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
359VIXL_DEPRECATED("DoublePack",
360 inline double double_pack(uint32_t sign,
361 uint32_t exp,
362 uint64_t mantissa)) {
363 return DoublePack(sign, exp, mantissa);
364}
armvixl5289c592015-03-02 13:52:04 +0000365
366// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100367int Float16Classify(Float16 value);
368VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
369 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100370}
armvixlf37fdc02014-02-05 13:22:16 +0000371
Jacob Bramleyca789742018-09-13 14:25:46 +0100372bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100373
Jacob Bramleyca789742018-09-13 14:25:46 +0100374inline bool IsNaN(float value) { return std::isnan(value); }
375
376inline bool IsNaN(double value) { return std::isnan(value); }
377
378inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
379
380inline bool IsInf(float value) { return std::isinf(value); }
381
382inline bool IsInf(double value) { return std::isinf(value); }
383
384inline bool IsInf(Float16 value) {
385 return Float16Classify(value) == FP_INFINITE;
386}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100387
388
armvixlf37fdc02014-02-05 13:22:16 +0000389// NaN tests.
390inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100391 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100392 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100393 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000394 return true;
395 }
396 return false;
397}
398
399
400inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000401 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100402 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100403 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000404 return true;
405 }
406 return false;
407}
408
409
Jacob Bramleyca789742018-09-13 14:25:46 +0100410inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000411 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100412 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000413}
414
415
armvixlf37fdc02014-02-05 13:22:16 +0000416template <typename T>
417inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100418 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000419}
420
421
armvixlb0c8ae22014-03-21 14:03:59 +0000422// Convert the NaN in 'num' to a quiet NaN.
423inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100424 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100425 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100426 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000427}
428
429
430inline float ToQuietNaN(float num) {
431 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100432 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100433 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000434}
435
436
Jacob Bramleyca789742018-09-13 14:25:46 +0100437inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
438 const uint16_t kFP16QuietNaNMask = 0x0200;
439 VIXL_ASSERT(IsNaN(num));
440 return internal::SimFloat16(
441 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
442}
443
444
armvixlb0c8ae22014-03-21 14:03:59 +0000445// Fused multiply-add.
446inline double FusedMultiplyAdd(double op1, double op2, double a) {
447 return fma(op1, op2, a);
448}
449
450
451inline float FusedMultiplyAdd(float op1, float op2, float a) {
452 return fmaf(op1, op2, a);
453}
454
455
armvixl0f35e362016-05-10 13:57:58 +0100456inline uint64_t LowestSetBit(uint64_t value) { return value & -value; }
armvixl6e2c8272015-03-31 11:04:14 +0100457
458
armvixl0f35e362016-05-10 13:57:58 +0100459template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100460inline int HighestSetBitPosition(T value) {
461 VIXL_ASSERT(value != 0);
462 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
463}
464
465
armvixl0f35e362016-05-10 13:57:58 +0100466template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100467inline int WhichPowerOf2(V value) {
468 VIXL_ASSERT(IsPowerOf2(value));
469 return CountTrailingZeros(value);
470}
armvixlad96eda2013-06-14 11:42:37 +0100471
armvixldb644342015-07-21 11:37:10 +0100472
armvixl330dc712014-11-25 10:38:32 +0000473unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
474
armvixldb644342015-07-21 11:37:10 +0100475
Pierre Langlois88c46b82016-06-02 18:15:32 +0100476int BitCount(uint64_t value);
477
478
armvixldb644342015-07-21 11:37:10 +0100479template <typename T>
480T ReverseBits(T value) {
481 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
482 (sizeof(value) == 4) || (sizeof(value) == 8));
483 T result = 0;
484 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
485 result = (result << 1) | (value & 1);
486 value >>= 1;
487 }
488 return result;
489}
490
491
492template <typename T>
Pierre Langlois88c46b82016-06-02 18:15:32 +0100493inline T SignExtend(T val, int bitSize) {
494 VIXL_ASSERT(bitSize > 0);
495 T mask = (T(2) << (bitSize - 1)) - T(1);
496 val &= mask;
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700497 T sign_bits = -((val >> (bitSize - 1)) << bitSize);
498 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100499 return val;
500}
501
502
503template <typename T>
armvixldb644342015-07-21 11:37:10 +0100504T ReverseBytes(T value, int block_bytes_log2) {
505 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
506 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
507 // Split the 64-bit value into an 8-bit array, where b[0] is the least
508 // significant byte, and b[7] is the most significant.
509 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000510 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100511 for (int i = 7; i >= 0; i--) {
512 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
513 mask >>= 8;
514 }
515
516 // Permutation tables for REV instructions.
517 // permute_table[0] is used by REV16_x, REV16_w
518 // permute_table[1] is used by REV32_x, REV_w
519 // permute_table[2] is used by REV_x
520 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100521 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
522 {4, 5, 6, 7, 0, 1, 2, 3},
523 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000524 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100525 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000526 temp <<= 8;
527 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100528 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000529
530 T result;
531 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
532 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100533 return result;
534}
535
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700536template <unsigned MULTIPLE, typename T>
537inline bool IsMultiple(T value) {
538 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
539 return (value & (MULTIPLE - 1)) == 0;
540}
armvixldb644342015-07-21 11:37:10 +0100541
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000542template <typename T>
543inline bool IsMultiple(T value, unsigned multiple) {
544 VIXL_ASSERT(IsPowerOf2(multiple));
545 return (value & (multiple - 1)) == 0;
546}
547
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000548template <typename T>
549inline bool IsAligned(T pointer, int alignment) {
550 VIXL_ASSERT(IsPowerOf2(alignment));
551 return (pointer & (alignment - 1)) == 0;
552}
553
armvixlad96eda2013-06-14 11:42:37 +0100554// Pointer alignment
555// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100556template <unsigned ALIGN, typename T>
557inline bool IsAligned(T pointer) {
558 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100559 // Use C-style casts to get static_cast behaviour for integral types (T), and
560 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000561 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100562}
563
armvixl0f35e362016-05-10 13:57:58 +0100564template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100565bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100566 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100567}
568
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100569// Increment a pointer until it has the specified alignment. The alignment must
570// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100571template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100572T AlignUp(T pointer,
573 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100574 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100575 // Use C-style casts to get static_cast behaviour for integral types (T), and
576 // reinterpret_cast behaviour for other types.
577
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100578 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000579 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000580 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100581
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100582 size_t mask = alignment - 1;
583 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000584 VIXL_ASSERT(result >= pointer);
585
586 return result;
armvixlad96eda2013-06-14 11:42:37 +0100587}
588
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100589// Decrement a pointer until it has the specified alignment. The alignment must
590// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100591template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100592T AlignDown(T pointer,
593 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100594 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100595 // Use C-style casts to get static_cast behaviour for integral types (T), and
596 // reinterpret_cast behaviour for other types.
597
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100598 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000599 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000600 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100601
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100602 size_t mask = alignment - 1;
603 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000604}
605
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100606
Pierre Langlois88c46b82016-06-02 18:15:32 +0100607template <typename T>
608inline T ExtractBit(T value, unsigned bit) {
609 return (value >> bit) & T(1);
610}
611
612template <typename Ts, typename Td>
613inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
614 return Td((value >> least_significant_bit) & Ts(mask));
615}
616
617template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800618inline void AssignBit(Td& dst, // NOLINT(runtime/references)
619 int bit,
620 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100621 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
622 VIXL_ASSERT(bit >= 0);
623 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
624 Td mask(1);
625 dst &= ~(mask << bit);
626 dst |= Td(value) << bit;
627}
628
629template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800630inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100631 int least_significant_bit,
632 Ts mask,
633 Ts value) {
634 VIXL_ASSERT(least_significant_bit >= 0);
635 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
636 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
637 Td(mask));
638 VIXL_ASSERT((value & mask) == value);
639 dst &= ~(Td(mask) << least_significant_bit);
640 dst |= Td(value) << least_significant_bit;
641}
642
643class VFP {
644 public:
645 static uint32_t FP32ToImm8(float imm) {
646 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
647 uint32_t bits = FloatToRawbits(imm);
648 // bit7: a000.0000
649 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
650 // bit6: 0b00.0000
651 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
652 // bit5_to_0: 00cd.efgh
653 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
654 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
655 }
656 static uint32_t FP64ToImm8(double imm) {
657 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
658 // 0000.0000.0000.0000.0000.0000.0000.0000
659 uint64_t bits = DoubleToRawbits(imm);
660 // bit7: a000.0000
661 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
662 // bit6: 0b00.0000
663 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
664 // bit5_to_0: 00cd.efgh
665 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
666
667 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
668 }
669 static float Imm8ToFP32(uint32_t imm8) {
670 // Imm8: abcdefgh (8 bits)
671 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
672 // where B is b ^ 1
673 uint32_t bits = imm8;
674 uint32_t bit7 = (bits >> 7) & 0x1;
675 uint32_t bit6 = (bits >> 6) & 0x1;
676 uint32_t bit5_to_0 = bits & 0x3f;
677 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
678
679 return RawbitsToFloat(result);
680 }
681 static double Imm8ToFP64(uint32_t imm8) {
682 // Imm8: abcdefgh (8 bits)
683 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
684 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
685 // where B is b ^ 1
686 uint32_t bits = imm8;
687 uint64_t bit7 = (bits >> 7) & 0x1;
688 uint64_t bit6 = (bits >> 6) & 0x1;
689 uint64_t bit5_to_0 = bits & 0x3f;
690 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
691 return RawbitsToDouble(result);
692 }
693 static bool IsImmFP32(float imm) {
694 // Valid values will have the form:
695 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
696 uint32_t bits = FloatToRawbits(imm);
697 // bits[19..0] are cleared.
698 if ((bits & 0x7ffff) != 0) {
699 return false;
700 }
701
702
703 // bits[29..25] are all set or all cleared.
704 uint32_t b_pattern = (bits >> 16) & 0x3e00;
705 if (b_pattern != 0 && b_pattern != 0x3e00) {
706 return false;
707 }
708 // bit[30] and bit[29] are opposite.
709 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
710 return false;
711 }
712 return true;
713 }
714 static bool IsImmFP64(double imm) {
715 // Valid values will have the form:
716 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
717 // 0000.0000.0000.0000.0000.0000.0000.0000
718 uint64_t bits = DoubleToRawbits(imm);
719 // bits[47..0] are cleared.
720 if ((bits & 0x0000ffffffffffff) != 0) {
721 return false;
722 }
723 // bits[61..54] are all set or all cleared.
724 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
725 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
726 return false;
727 }
728 // bit[62] and bit[61] are opposite.
729 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
730 return false;
731 }
732 return true;
733 }
734};
735
736class BitField {
737 // ForEachBitHelper is a functor that will call
738 // bool ForEachBitHelper::execute(ElementType id) const
739 // and expects a boolean in return whether to continue (if true)
740 // or stop (if false)
741 // check_set will check if the bits are on (true) or off(false)
742 template <typename ForEachBitHelper, bool check_set>
743 bool ForEachBit(const ForEachBitHelper& helper) {
744 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
745 if (bitfield_[i] == check_set)
746 if (!helper.execute(i)) return false;
747 }
748 return true;
749 }
750
751 public:
752 explicit BitField(unsigned size) : bitfield_(size, 0) {}
753
754 void Set(int i) {
755 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
756 bitfield_[i] = true;
757 }
758
759 void Unset(int i) {
760 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
761 bitfield_[i] = true;
762 }
763
764 bool IsSet(int i) const { return bitfield_[i]; }
765
766 // For each bit not set in the bitfield call the execute functor
767 // execute.
768 // ForEachBitSetHelper::execute returns true if the iteration through
769 // the bits can continue, otherwise it will stop.
770 // struct ForEachBitSetHelper {
771 // bool execute(int /*id*/) { return false; }
772 // };
773 template <typename ForEachBitNotSetHelper>
774 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
775 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
776 }
777
778 // For each bit set in the bitfield call the execute functor
779 // execute.
780 template <typename ForEachBitSetHelper>
781 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
782 return ForEachBit<ForEachBitSetHelper, true>(helper);
783 }
784
785 private:
786 std::vector<bool> bitfield_;
787};
788
Pierre Langloisd82faf62018-04-04 13:06:58 +0100789namespace internal {
790
Pierre Langlois88c46b82016-06-02 18:15:32 +0100791typedef int64_t Int64;
792class Uint64;
793class Uint128;
794
795class Uint32 {
796 uint32_t data_;
797
798 public:
799 // Unlike uint32_t, Uint32 has a default constructor.
800 Uint32() { data_ = 0; }
801 explicit Uint32(uint32_t data) : data_(data) {}
802 inline explicit Uint32(Uint64 data);
803 uint32_t Get() const { return data_; }
804 template <int N>
805 int32_t GetSigned() const {
806 return ExtractSignedBitfield32(N - 1, 0, data_);
807 }
808 int32_t GetSigned() const { return data_; }
809 Uint32 operator~() const { return Uint32(~data_); }
810 Uint32 operator-() const { return Uint32(-data_); }
811 bool operator==(Uint32 value) const { return data_ == value.data_; }
812 bool operator!=(Uint32 value) const { return data_ != value.data_; }
813 bool operator>(Uint32 value) const { return data_ > value.data_; }
814 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
815 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
816 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
817 Uint32 operator&=(Uint32 value) {
818 data_ &= value.data_;
819 return *this;
820 }
821 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
822 Uint32 operator^=(Uint32 value) {
823 data_ ^= value.data_;
824 return *this;
825 }
826 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
827 Uint32 operator|=(Uint32 value) {
828 data_ |= value.data_;
829 return *this;
830 }
831 // Unlike uint32_t, the shift functions can accept negative shift and
832 // return 0 when the shift is too big.
833 Uint32 operator>>(int shift) const {
834 if (shift == 0) return *this;
835 if (shift < 0) {
836 int tmp = -shift;
837 if (tmp >= 32) return Uint32(0);
838 return Uint32(data_ << tmp);
839 }
840 int tmp = shift;
841 if (tmp >= 32) return Uint32(0);
842 return Uint32(data_ >> tmp);
843 }
844 Uint32 operator<<(int shift) const {
845 if (shift == 0) return *this;
846 if (shift < 0) {
847 int tmp = -shift;
848 if (tmp >= 32) return Uint32(0);
849 return Uint32(data_ >> tmp);
850 }
851 int tmp = shift;
852 if (tmp >= 32) return Uint32(0);
853 return Uint32(data_ << tmp);
854 }
855};
856
857class Uint64 {
858 uint64_t data_;
859
860 public:
861 // Unlike uint64_t, Uint64 has a default constructor.
862 Uint64() { data_ = 0; }
863 explicit Uint64(uint64_t data) : data_(data) {}
864 explicit Uint64(Uint32 data) : data_(data.Get()) {}
865 inline explicit Uint64(Uint128 data);
866 uint64_t Get() const { return data_; }
867 int64_t GetSigned(int N) const {
868 return ExtractSignedBitfield64(N - 1, 0, data_);
869 }
870 int64_t GetSigned() const { return data_; }
871 Uint32 ToUint32() const {
872 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100873 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100874 }
875 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100876 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100877 Uint64 operator~() const { return Uint64(~data_); }
878 Uint64 operator-() const { return Uint64(-data_); }
879 bool operator==(Uint64 value) const { return data_ == value.data_; }
880 bool operator!=(Uint64 value) const { return data_ != value.data_; }
881 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
882 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
883 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
884 Uint64 operator&=(Uint64 value) {
885 data_ &= value.data_;
886 return *this;
887 }
888 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
889 Uint64 operator^=(Uint64 value) {
890 data_ ^= value.data_;
891 return *this;
892 }
893 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
894 Uint64 operator|=(Uint64 value) {
895 data_ |= value.data_;
896 return *this;
897 }
898 // Unlike uint64_t, the shift functions can accept negative shift and
899 // return 0 when the shift is too big.
900 Uint64 operator>>(int shift) const {
901 if (shift == 0) return *this;
902 if (shift < 0) {
903 int tmp = -shift;
904 if (tmp >= 64) return Uint64(0);
905 return Uint64(data_ << tmp);
906 }
907 int tmp = shift;
908 if (tmp >= 64) return Uint64(0);
909 return Uint64(data_ >> tmp);
910 }
911 Uint64 operator<<(int shift) const {
912 if (shift == 0) return *this;
913 if (shift < 0) {
914 int tmp = -shift;
915 if (tmp >= 64) return Uint64(0);
916 return Uint64(data_ >> tmp);
917 }
918 int tmp = shift;
919 if (tmp >= 64) return Uint64(0);
920 return Uint64(data_ << tmp);
921 }
922};
923
924class Uint128 {
925 uint64_t data_high_;
926 uint64_t data_low_;
927
928 public:
929 Uint128() : data_high_(0), data_low_(0) {}
930 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
931 explicit Uint128(Uint64 data_low)
932 : data_high_(0), data_low_(data_low.Get()) {}
933 Uint128(uint64_t data_high, uint64_t data_low)
934 : data_high_(data_high), data_low_(data_low) {}
935 Uint64 ToUint64() const {
936 VIXL_ASSERT(data_high_ == 0);
937 return Uint64(data_low_);
938 }
939 Uint64 GetHigh64() const { return Uint64(data_high_); }
940 Uint64 GetLow64() const { return Uint64(data_low_); }
941 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
942 bool operator==(Uint128 value) const {
943 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
944 }
945 Uint128 operator&(Uint128 value) const {
946 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
947 }
948 Uint128 operator&=(Uint128 value) {
949 data_high_ &= value.data_high_;
950 data_low_ &= value.data_low_;
951 return *this;
952 }
953 Uint128 operator|=(Uint128 value) {
954 data_high_ |= value.data_high_;
955 data_low_ |= value.data_low_;
956 return *this;
957 }
958 Uint128 operator>>(int shift) const {
959 VIXL_ASSERT((shift >= 0) && (shift < 128));
960 if (shift == 0) return *this;
961 if (shift >= 64) {
962 return Uint128(0, data_high_ >> (shift - 64));
963 }
964 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
965 return Uint128(data_high_ >> shift, tmp);
966 }
967 Uint128 operator<<(int shift) const {
968 VIXL_ASSERT((shift >= 0) && (shift < 128));
969 if (shift == 0) return *this;
970 if (shift >= 64) {
971 return Uint128(data_low_ << (shift - 64), 0);
972 }
973 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
974 return Uint128(tmp, data_low_ << shift);
975 }
976};
977
978Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
979Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
980
981Int64 BitCount(Uint32 value);
982
Pierre Langloisd82faf62018-04-04 13:06:58 +0100983} // namespace internal
984
Jacob Bramleyca789742018-09-13 14:25:46 +0100985// The default NaN values (for FPCR.DN=1).
986extern const double kFP64DefaultNaN;
987extern const float kFP32DefaultNaN;
988extern const Float16 kFP16DefaultNaN;
989
990// Floating-point infinity values.
991extern const Float16 kFP16PositiveInfinity;
992extern const Float16 kFP16NegativeInfinity;
993extern const float kFP32PositiveInfinity;
994extern const float kFP32NegativeInfinity;
995extern const double kFP64PositiveInfinity;
996extern const double kFP64NegativeInfinity;
997
998// Floating-point zero values.
999extern const Float16 kFP16PositiveZero;
1000extern const Float16 kFP16NegativeZero;
1001
1002// AArch64 floating-point specifics. These match IEEE-754.
1003const unsigned kDoubleMantissaBits = 52;
1004const unsigned kDoubleExponentBits = 11;
1005const unsigned kFloatMantissaBits = 23;
1006const unsigned kFloatExponentBits = 8;
1007const unsigned kFloat16MantissaBits = 10;
1008const unsigned kFloat16ExponentBits = 5;
1009
1010enum FPRounding {
1011 // The first four values are encodable directly by FPCR<RMode>.
1012 FPTieEven = 0x0,
1013 FPPositiveInfinity = 0x1,
1014 FPNegativeInfinity = 0x2,
1015 FPZero = 0x3,
1016
1017 // The final rounding modes are only available when explicitly specified by
1018 // the instruction (such as with fcvta). It cannot be set in FPCR.
1019 FPTieAway,
1020 FPRoundOdd
1021};
1022
1023enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1024
1025// Assemble the specified IEEE-754 components into the target type and apply
1026// appropriate rounding.
1027// sign: 0 = positive, 1 = negative
1028// exponent: Unbiased IEEE-754 exponent.
1029// mantissa: The mantissa of the input. The top bit (which is not encoded for
1030// normal IEEE-754 values) must not be omitted. This bit has the
1031// value 'pow(2, exponent)'.
1032//
1033// The input value is assumed to be a normalized value. That is, the input may
1034// not be infinity or NaN. If the source value is subnormal, it must be
1035// normalized before calling this function such that the highest set bit in the
1036// mantissa has the value 'pow(2, exponent)'.
1037//
1038// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1039// calling a templated FPRound.
1040template <class T, int ebits, int mbits>
1041T FPRound(int64_t sign,
1042 int64_t exponent,
1043 uint64_t mantissa,
1044 FPRounding round_mode) {
1045 VIXL_ASSERT((sign == 0) || (sign == 1));
1046
1047 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1048 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1049
1050 // Rounding can promote subnormals to normals, and normals to infinities. For
1051 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1052 // encodable as a float, but rounding based on the low-order mantissa bits
1053 // could make it overflow. With ties-to-even rounding, this value would become
1054 // an infinity.
1055
1056 // ---- Rounding Method ----
1057 //
1058 // The exponent is irrelevant in the rounding operation, so we treat the
1059 // lowest-order bit that will fit into the result ('onebit') as having
1060 // the value '1'. Similarly, the highest-order bit that won't fit into
1061 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1062 // 'onebit' and 'halfbit':
1063 //
1064 // These bits fit into the result.
1065 // |---------------------|
1066 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1067 // ||
1068 // / |
1069 // / halfbit
1070 // onebit
1071 //
1072 // For subnormal outputs, the range of representable bits is smaller and
1073 // the position of onebit and halfbit depends on the exponent of the
1074 // input, but the method is otherwise similar.
1075 //
1076 // onebit(frac)
1077 // |
1078 // | halfbit(frac) halfbit(adjusted)
1079 // | / /
1080 // | | |
1081 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1082 // 0b00.0... -> 0b00.0... -> 0b00
1083 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1084 // 0b00.1... -> 0b00.1... -> 0b01
1085 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1086 // 0b01.0... -> 0b01.0... -> 0b01
1087 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1088 // 0b01.1... -> 0b01.1... -> 0b10
1089 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1090 // 0b10.0... -> 0b10.0... -> 0b10
1091 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1092 // 0b10.1... -> 0b10.1... -> 0b11
1093 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1094 // ... / | / |
1095 // / | / |
1096 // / |
1097 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1098 //
1099 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1100
1101 static const int mantissa_offset = 0;
1102 static const int exponent_offset = mantissa_offset + mbits;
1103 static const int sign_offset = exponent_offset + ebits;
1104 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1105
1106 // Bail out early for zero inputs.
1107 if (mantissa == 0) {
1108 return static_cast<T>(sign << sign_offset);
1109 }
1110
1111 // If all bits in the exponent are set, the value is infinite or NaN.
1112 // This is true for all binary IEEE-754 formats.
1113 static const int infinite_exponent = (1 << ebits) - 1;
1114 static const int max_normal_exponent = infinite_exponent - 1;
1115
1116 // Apply the exponent bias to encode it for the result. Doing this early makes
1117 // it easy to detect values that will be infinite or subnormal.
1118 exponent += max_normal_exponent >> 1;
1119
1120 if (exponent > max_normal_exponent) {
1121 // Overflow: the input is too large for the result type to represent.
1122 if (round_mode == FPTieEven) {
1123 // FPTieEven rounding mode handles overflows using infinities.
1124 exponent = infinite_exponent;
1125 mantissa = 0;
1126 } else {
1127 VIXL_ASSERT(round_mode == FPRoundOdd);
1128 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1129 // normal number.
1130 exponent = max_normal_exponent;
1131 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1132 }
1133 return static_cast<T>((sign << sign_offset) |
1134 (exponent << exponent_offset) |
1135 (mantissa << mantissa_offset));
1136 }
1137
1138 // Calculate the shift required to move the top mantissa bit to the proper
1139 // place in the destination type.
1140 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1141 int shift = highest_significant_bit - mbits;
1142
1143 if (exponent <= 0) {
1144 // The output will be subnormal (before rounding).
1145 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1146 // is necessary because the exponent of a subnormal value (encoded as 0) is
1147 // the same as the exponent of the smallest normal value (encoded as 1).
1148 shift += -exponent + 1;
1149
1150 // Handle inputs that would produce a zero output.
1151 //
1152 // Shifts higher than highest_significant_bit+1 will always produce a zero
1153 // result. A shift of exactly highest_significant_bit+1 might produce a
1154 // non-zero result after rounding.
1155 if (shift > (highest_significant_bit + 1)) {
1156 if (round_mode == FPTieEven) {
1157 // The result will always be +/-0.0.
1158 return static_cast<T>(sign << sign_offset);
1159 } else {
1160 VIXL_ASSERT(round_mode == FPRoundOdd);
1161 VIXL_ASSERT(mantissa != 0);
1162 // For FPRoundOdd, if the mantissa is too small to represent and
1163 // non-zero return the next "odd" value.
1164 return static_cast<T>((sign << sign_offset) | 1);
1165 }
1166 }
1167
1168 // Properly encode the exponent for a subnormal output.
1169 exponent = 0;
1170 } else {
1171 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1172 // normal values.
1173 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1174 }
1175
1176 // The casts below are only well-defined for unsigned integers.
1177 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1178 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1179
1180 if (shift > 0) {
1181 if (round_mode == FPTieEven) {
1182 // We have to shift the mantissa to the right. Some precision is lost, so
1183 // we need to apply rounding.
1184 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1185 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1186 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1187 uint64_t adjusted = mantissa - adjustment;
1188 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1189
1190 T result =
1191 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1192 ((mantissa >> shift) << mantissa_offset));
1193
1194 // A very large mantissa can overflow during rounding. If this happens,
1195 // the exponent should be incremented and the mantissa set to 1.0
1196 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1197 // has the nice side-effect that this case is handled for free.
1198 //
1199 // This also handles cases where a very large finite value overflows to
1200 // infinity, or where a very large subnormal value overflows to become
1201 // normal.
1202 return result + halfbit_adjusted;
1203 } else {
1204 VIXL_ASSERT(round_mode == FPRoundOdd);
1205 // If any bits at position halfbit or below are set, onebit (ie. the
1206 // bottom bit of the resulting mantissa) must be set.
1207 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1208 if (fractional_bits != 0) {
1209 mantissa |= UINT64_C(1) << shift;
1210 }
1211
1212 return static_cast<T>((sign << sign_offset) |
1213 (exponent << exponent_offset) |
1214 ((mantissa >> shift) << mantissa_offset));
1215 }
1216 } else {
1217 // We have to shift the mantissa to the left (or not at all). The input
1218 // mantissa is exactly representable in the output mantissa, so apply no
1219 // rounding correction.
1220 return static_cast<T>((sign << sign_offset) |
1221 (exponent << exponent_offset) |
1222 ((mantissa << -shift) << mantissa_offset));
1223 }
1224}
1225
1226
1227// See FPRound for a description of this function.
1228inline double FPRoundToDouble(int64_t sign,
1229 int64_t exponent,
1230 uint64_t mantissa,
1231 FPRounding round_mode) {
1232 uint64_t bits =
1233 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1234 exponent,
1235 mantissa,
1236 round_mode);
1237 return RawbitsToDouble(bits);
1238}
1239
1240
1241// See FPRound for a description of this function.
1242inline Float16 FPRoundToFloat16(int64_t sign,
1243 int64_t exponent,
1244 uint64_t mantissa,
1245 FPRounding round_mode) {
1246 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001247 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1248 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001249}
1250
1251
1252// See FPRound for a description of this function.
1253static inline float FPRoundToFloat(int64_t sign,
1254 int64_t exponent,
1255 uint64_t mantissa,
1256 FPRounding round_mode) {
1257 uint32_t bits =
1258 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1259 exponent,
1260 mantissa,
1261 round_mode);
1262 return RawbitsToFloat(bits);
1263}
1264
1265
1266float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1267float FPToFloat(double value,
1268 FPRounding round_mode,
1269 UseDefaultNaN DN,
1270 bool* exception = NULL);
1271
1272double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1273double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1274
1275Float16 FPToFloat16(float value,
1276 FPRounding round_mode,
1277 UseDefaultNaN DN,
1278 bool* exception = NULL);
1279
1280Float16 FPToFloat16(double value,
1281 FPRounding round_mode,
1282 UseDefaultNaN DN,
1283 bool* exception = NULL);
armvixlad96eda2013-06-14 11:42:37 +01001284} // namespace vixl
1285
1286#endif // VIXL_UTILS_H