blob: b6c8455cfc322ee7ea334a4bfdeb33d5f012c423 [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>
Anton Kirilov088b01f2022-09-27 14:27:38 +010033#include <type_traits>
Pierre Langlois88c46b82016-06-02 18:15:32 +010034#include <vector>
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010035
Alexandre Rames1f9074d2016-05-23 15:50:01 +010036#include "compiler-intrinsics-vixl.h"
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010037#include "globals-vixl.h"
armvixlad96eda2013-06-14 11:42:37 +010038
39namespace vixl {
40
armvixl4a102ba2014-07-14 09:02:40 +010041// Macros for compile-time format checking.
armvixl788c84f2015-12-08 17:05:23 +000042#if GCC_VERSION_OR_NEWER(4, 4, 0)
armvixl4a102ba2014-07-14 09:02:40 +010043#define PRINTF_CHECK(format_index, varargs_index) \
armvixl788c84f2015-12-08 17:05:23 +000044 __attribute__((format(gnu_printf, format_index, varargs_index)))
armvixl4a102ba2014-07-14 09:02:40 +010045#else
46#define PRINTF_CHECK(format_index, varargs_index)
47#endif
48
Pierre Langlois88c46b82016-06-02 18:15:32 +010049#ifdef __GNUC__
50#define VIXL_HAS_DEPRECATED_WITH_MSG
51#elif defined(__clang__)
52#ifdef __has_extension(attribute_deprecated_with_message)
53#define VIXL_HAS_DEPRECATED_WITH_MSG
54#endif
55#endif
56
57#ifdef VIXL_HAS_DEPRECATED_WITH_MSG
58#define VIXL_DEPRECATED(replaced_by, declarator) \
59 __attribute__((deprecated("Use \"" replaced_by "\" instead"))) declarator
60#else
61#define VIXL_DEPRECATED(replaced_by, declarator) declarator
62#endif
63
64#ifdef VIXL_DEBUG
65#define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_UNREACHABLE()
66#else
67#define VIXL_UNREACHABLE_OR_FALLTHROUGH() VIXL_FALLTHROUGH()
68#endif
69
Jacob Bramleyca789742018-09-13 14:25:46 +010070template <typename T, size_t n>
TatWai Chong2cb1b612020-03-04 23:51:21 -080071constexpr size_t ArrayLength(const T (&)[n]) {
Jacob Bramleyca789742018-09-13 14:25:46 +010072 return n;
73}
74
Jacob Bramley03c0b512019-02-22 16:42:06 +000075inline uint64_t GetUintMask(unsigned bits) {
76 VIXL_ASSERT(bits <= 64);
77 uint64_t base = (bits >= 64) ? 0 : (UINT64_C(1) << bits);
78 return base - 1;
79}
80
TatWai Chong29a0c432019-11-06 22:20:44 -080081inline uint64_t GetSignMask(unsigned bits) {
82 VIXL_ASSERT(bits <= 64);
83 return UINT64_C(1) << (bits - 1);
84}
85
armvixlad96eda2013-06-14 11:42:37 +010086// Check number width.
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000087// TODO: Refactor these using templates.
88inline bool IsIntN(unsigned n, uint32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010089 VIXL_ASSERT((0 < n) && (n <= 32));
90 return x <= static_cast<uint32_t>(INT32_MAX >> (32 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000091}
92inline bool IsIntN(unsigned n, int32_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010093 VIXL_ASSERT((0 < n) && (n <= 32));
94 if (n == 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +000095 int32_t limit = INT32_C(1) << (n - 1);
96 return (-limit <= x) && (x < limit);
97}
98inline bool IsIntN(unsigned n, uint64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +010099 VIXL_ASSERT((0 < n) && (n <= 64));
100 return x <= static_cast<uint64_t>(INT64_MAX >> (64 - n));
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000101}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100102inline bool IsIntN(unsigned n, int64_t x) {
Jacob Bramley6069fd42019-06-24 10:20:45 +0100103 VIXL_ASSERT((0 < n) && (n <= 64));
104 if (n == 64) return true;
armvixlb0c8ae22014-03-21 14:03:59 +0000105 int64_t limit = INT64_C(1) << (n - 1);
armvixlad96eda2013-06-14 11:42:37 +0100106 return (-limit <= x) && (x < limit);
107}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100108VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
109 return IsIntN(n, x);
110}
armvixlad96eda2013-06-14 11:42:37 +0100111
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000112inline bool IsUintN(unsigned n, uint32_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000113 VIXL_ASSERT((0 < n) && (n <= 32));
114 if (n >= 32) return true;
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000115 return !(x >> n);
116}
117inline bool IsUintN(unsigned n, int32_t x) {
118 VIXL_ASSERT((0 < n) && (n < 32));
119 // Convert to an unsigned integer to avoid implementation-defined behavior.
120 return !(static_cast<uint32_t>(x) >> n);
121}
122inline bool IsUintN(unsigned n, uint64_t x) {
Jacob Bramley03c0b512019-02-22 16:42:06 +0000123 VIXL_ASSERT((0 < n) && (n <= 64));
124 if (n >= 64) return true;
armvixlad96eda2013-06-14 11:42:37 +0100125 return !(x >> n);
126}
Pierre Langloisefe0c1f2016-11-24 11:54:47 +0000127inline bool IsUintN(unsigned n, int64_t x) {
128 VIXL_ASSERT((0 < n) && (n < 64));
129 // Convert to an unsigned integer to avoid implementation-defined behavior.
130 return !(static_cast<uint64_t>(x) >> n);
131}
Pierre Langlois88c46b82016-06-02 18:15:32 +0100132VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
133 return IsUintN(n, x);
134}
armvixlad96eda2013-06-14 11:42:37 +0100135
Jacob Bramley3976edb2016-10-18 10:51:43 +0100136inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
armvixlb0c8ae22014-03-21 14:03:59 +0000137 VIXL_ASSERT((0 < n) && (n < 64));
Jacob Bramley3976edb2016-10-18 10:51:43 +0100138 return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100139}
Jacob Bramley3976edb2016-10-18 10:51:43 +0100140VIXL_DEPRECATED("TruncateToUintN",
141 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
142 return TruncateToUintN(n, x);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100143}
armvixlad96eda2013-06-14 11:42:37 +0100144
armvixl0f35e362016-05-10 13:57:58 +0100145// clang-format off
Jacob Bramley3976edb2016-10-18 10:51:43 +0100146#define INT_1_TO_32_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100147V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
148V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
149V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
Jacob Bramley3976edb2016-10-18 10:51:43 +0100150V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
151
152#define INT_33_TO_63_LIST(V) \
armvixlad96eda2013-06-14 11:42:37 +0100153V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
154V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
155V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
156V(57) V(58) V(59) V(60) V(61) V(62) V(63)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100157
Jacob Bramley3976edb2016-10-18 10:51:43 +0100158#define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
159
armvixl0f35e362016-05-10 13:57:58 +0100160// clang-format on
armvixlad96eda2013-06-14 11:42:37 +0100161
Pierre Langlois88c46b82016-06-02 18:15:32 +0100162#define DECLARE_IS_INT_N(N) \
163 inline bool IsInt##N(int64_t x) { return IsIntN(N, x); } \
164 VIXL_DEPRECATED("IsInt" #N, inline bool is_int##N(int64_t x)) { \
165 return IsIntN(N, x); \
166 }
167
168#define DECLARE_IS_UINT_N(N) \
169 inline bool IsUint##N(int64_t x) { return IsUintN(N, x); } \
170 VIXL_DEPRECATED("IsUint" #N, inline bool is_uint##N(int64_t x)) { \
171 return IsUintN(N, x); \
172 }
173
Jacob Bramley3976edb2016-10-18 10:51:43 +0100174#define DECLARE_TRUNCATE_TO_UINT_32(N) \
175 inline uint32_t TruncateToUint##N(uint64_t x) { \
176 return static_cast<uint32_t>(TruncateToUintN(N, x)); \
177 } \
178 VIXL_DEPRECATED("TruncateToUint" #N, \
179 inline uint32_t truncate_to_int##N(int64_t x)) { \
180 return TruncateToUint##N(x); \
Pierre Langlois88c46b82016-06-02 18:15:32 +0100181 }
182
armvixlad96eda2013-06-14 11:42:37 +0100183INT_1_TO_63_LIST(DECLARE_IS_INT_N)
184INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
Jacob Bramley3976edb2016-10-18 10:51:43 +0100185INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100186
armvixlad96eda2013-06-14 11:42:37 +0100187#undef DECLARE_IS_INT_N
188#undef DECLARE_IS_UINT_N
189#undef DECLARE_TRUNCATE_TO_INT_N
190
191// Bit field extraction.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100192inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000193 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
194 (msb >= lsb));
195 if ((msb == 63) && (lsb == 0)) return x;
armvixl578645f2013-08-15 17:21:42 +0100196 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
armvixlad96eda2013-06-14 11:42:37 +0100197}
198
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000199
Jacob Bramley199339d2019-08-05 18:49:13 +0100200inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000201 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
202 (msb >= lsb));
203 return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
armvixlad96eda2013-06-14 11:42:37 +0100204}
205
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000206
Jacob Bramley6069fd42019-06-24 10:20:45 +0100207inline int64_t ExtractSignedBitfield64(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000208 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
209 (msb >= lsb));
210 uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
211 // If the highest extracted bit is set, sign extend.
212 if ((temp >> (msb - lsb)) == 1) {
213 temp |= ~UINT64_C(0) << (msb - lsb);
214 }
215 int64_t result;
216 memcpy(&result, &temp, sizeof(result));
217 return result;
armvixlad96eda2013-06-14 11:42:37 +0100218}
219
Jacob Bramley199339d2019-08-05 18:49:13 +0100220inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint64_t x) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000221 VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
222 (msb >= lsb));
223 uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
224 int32_t result;
225 memcpy(&result, &temp, sizeof(result));
226 return result;
227}
228
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000229inline uint64_t RotateRight(uint64_t value,
230 unsigned int rotate,
231 unsigned int width) {
232 VIXL_ASSERT((width > 0) && (width <= 64));
233 uint64_t width_mask = ~UINT64_C(0) >> (64 - width);
234 rotate &= 63;
235 if (rotate > 0) {
236 value &= width_mask;
237 value = (value << (width - rotate)) | (value >> rotate);
238 }
239 return value & width_mask;
240}
241
242
Jacob Bramleyca789742018-09-13 14:25:46 +0100243// Wrapper class for passing FP16 values through the assembler.
244// This is purely to aid with type checking/casting.
245class Float16 {
246 public:
247 explicit Float16(double dvalue);
248 Float16() : rawbits_(0x0) {}
249 friend uint16_t Float16ToRawbits(Float16 value);
250 friend Float16 RawbitsToFloat16(uint16_t bits);
251
252 protected:
253 uint16_t rawbits_;
254};
255
armvixlf37fdc02014-02-05 13:22:16 +0000256// Floating point representation.
Jacob Bramleyca789742018-09-13 14:25:46 +0100257uint16_t Float16ToRawbits(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100258
259
Pierre Langlois88c46b82016-06-02 18:15:32 +0100260uint32_t FloatToRawbits(float value);
261VIXL_DEPRECATED("FloatToRawbits",
262 inline uint32_t float_to_rawbits(float value)) {
263 return FloatToRawbits(value);
264}
armvixlad96eda2013-06-14 11:42:37 +0100265
Pierre Langlois88c46b82016-06-02 18:15:32 +0100266uint64_t DoubleToRawbits(double value);
267VIXL_DEPRECATED("DoubleToRawbits",
268 inline uint64_t double_to_rawbits(double value)) {
269 return DoubleToRawbits(value);
270}
armvixl5289c592015-03-02 13:52:04 +0000271
Jacob Bramleyca789742018-09-13 14:25:46 +0100272Float16 RawbitsToFloat16(uint16_t bits);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100273
Pierre Langlois88c46b82016-06-02 18:15:32 +0100274float RawbitsToFloat(uint32_t bits);
275VIXL_DEPRECATED("RawbitsToFloat",
276 inline float rawbits_to_float(uint32_t bits)) {
277 return RawbitsToFloat(bits);
278}
279
280double RawbitsToDouble(uint64_t bits);
281VIXL_DEPRECATED("RawbitsToDouble",
282 inline double rawbits_to_double(uint64_t bits)) {
283 return RawbitsToDouble(bits);
284}
285
Anton Kirilov088b01f2022-09-27 14:27:38 +0100286// Some compilers dislike negating unsigned integers,
287// so we provide an equivalent.
288template <typename T>
289T UnsignedNegate(T value) {
290 VIXL_STATIC_ASSERT(std::is_unsigned<T>::value);
291 return ~value + 1;
292}
293
Jacob Bramley03c0b512019-02-22 16:42:06 +0000294// Convert unsigned to signed numbers in a well-defined way (using two's
295// complement representations).
296inline int64_t RawbitsToInt64(uint64_t bits) {
297 return (bits >= UINT64_C(0x8000000000000000))
Anton Kirilov088b01f2022-09-27 14:27:38 +0100298 ? (-static_cast<int64_t>(UnsignedNegate(bits) - 1) - 1)
Jacob Bramley03c0b512019-02-22 16:42:06 +0000299 : static_cast<int64_t>(bits);
300}
301
302inline int32_t RawbitsToInt32(uint32_t bits) {
Anton Kirilov088b01f2022-09-27 14:27:38 +0100303 return (bits >= UINT64_C(0x80000000))
304 ? (-static_cast<int32_t>(UnsignedNegate(bits) - 1) - 1)
305 : static_cast<int32_t>(bits);
Jacob Bramley03c0b512019-02-22 16:42:06 +0000306}
307
Jacob Bramleyca789742018-09-13 14:25:46 +0100308namespace internal {
309
310// Internal simulation class used solely by the simulator to
311// provide an abstraction layer for any half-precision arithmetic.
312class SimFloat16 : public Float16 {
313 public:
314 // TODO: We should investigate making this constructor explicit.
315 // This is currently difficult to do due to a number of templated
316 // functions in the simulator which rely on returning double values.
317 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
318 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
319 this->rawbits_ = Float16ToRawbits(f);
320 }
321 SimFloat16() : Float16() {}
322 SimFloat16 operator-() const;
323 SimFloat16 operator+(SimFloat16 rhs) const;
324 SimFloat16 operator-(SimFloat16 rhs) const;
325 SimFloat16 operator*(SimFloat16 rhs) const;
326 SimFloat16 operator/(SimFloat16 rhs) const;
327 bool operator<(SimFloat16 rhs) const;
328 bool operator>(SimFloat16 rhs) const;
329 bool operator==(SimFloat16 rhs) const;
330 bool operator!=(SimFloat16 rhs) const;
Josh Sorefb43d6ef2022-08-03 12:47:14 -0400331 // This is necessary for conversions performed in (macro asm) Fmov.
Jacob Bramleyca789742018-09-13 14:25:46 +0100332 bool operator==(double rhs) const;
333 operator double() const;
334};
335} // namespace internal
336
337uint32_t Float16Sign(internal::SimFloat16 value);
338
339uint32_t Float16Exp(internal::SimFloat16 value);
340
341uint32_t Float16Mantissa(internal::SimFloat16 value);
342
Pierre Langlois88c46b82016-06-02 18:15:32 +0100343uint32_t FloatSign(float value);
344VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
345 return FloatSign(value);
346}
347
348uint32_t FloatExp(float value);
349VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
350 return FloatExp(value);
351}
352
353uint32_t FloatMantissa(float value);
354VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
355 return FloatMantissa(value);
356}
357
358uint32_t DoubleSign(double value);
359VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
360 return DoubleSign(value);
361}
362
363uint32_t DoubleExp(double value);
364VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
365 return DoubleExp(value);
366}
367
368uint64_t DoubleMantissa(double value);
369VIXL_DEPRECATED("DoubleMantissa",
370 inline uint64_t double_mantissa(double value)) {
371 return DoubleMantissa(value);
372}
373
Jacob Bramleyca789742018-09-13 14:25:46 +0100374internal::SimFloat16 Float16Pack(uint16_t sign,
375 uint16_t exp,
376 uint16_t mantissa);
377
Pierre Langlois88c46b82016-06-02 18:15:32 +0100378float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
379VIXL_DEPRECATED("FloatPack",
380 inline float float_pack(uint32_t sign,
381 uint32_t exp,
382 uint32_t mantissa)) {
383 return FloatPack(sign, exp, mantissa);
384}
385
386double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
387VIXL_DEPRECATED("DoublePack",
388 inline double double_pack(uint32_t sign,
389 uint32_t exp,
390 uint64_t mantissa)) {
391 return DoublePack(sign, exp, mantissa);
392}
armvixl5289c592015-03-02 13:52:04 +0000393
394// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100395int Float16Classify(Float16 value);
396VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
397 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100398}
armvixlf37fdc02014-02-05 13:22:16 +0000399
Jacob Bramleyca789742018-09-13 14:25:46 +0100400bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100401
Jacob Bramleyf48172b2020-07-13 14:47:15 +0100402inline bool IsPositiveZero(double value) {
403 return (value == 0.0) && (copysign(1.0, value) > 0.0);
404}
405
Jacob Bramleyca789742018-09-13 14:25:46 +0100406inline bool IsNaN(float value) { return std::isnan(value); }
407
408inline bool IsNaN(double value) { return std::isnan(value); }
409
410inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
411
412inline bool IsInf(float value) { return std::isinf(value); }
413
414inline bool IsInf(double value) { return std::isinf(value); }
415
416inline bool IsInf(Float16 value) {
417 return Float16Classify(value) == FP_INFINITE;
418}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100419
420
armvixlf37fdc02014-02-05 13:22:16 +0000421// NaN tests.
422inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100423 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100424 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100425 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000426 return true;
427 }
428 return false;
429}
430
431
432inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000433 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100434 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100435 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000436 return true;
437 }
438 return false;
439}
440
441
Jacob Bramleyca789742018-09-13 14:25:46 +0100442inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000443 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100444 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000445}
446
447
armvixlf37fdc02014-02-05 13:22:16 +0000448template <typename T>
449inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100450 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000451}
452
453
armvixlb0c8ae22014-03-21 14:03:59 +0000454// Convert the NaN in 'num' to a quiet NaN.
455inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100456 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100457 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100458 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000459}
460
461
462inline float ToQuietNaN(float num) {
463 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100464 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100465 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000466}
467
468
Jacob Bramleyca789742018-09-13 14:25:46 +0100469inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
470 const uint16_t kFP16QuietNaNMask = 0x0200;
471 VIXL_ASSERT(IsNaN(num));
472 return internal::SimFloat16(
473 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
474}
475
476
armvixlb0c8ae22014-03-21 14:03:59 +0000477// Fused multiply-add.
478inline double FusedMultiplyAdd(double op1, double op2, double a) {
479 return fma(op1, op2, a);
480}
481
482
483inline float FusedMultiplyAdd(float op1, float op2, float a) {
484 return fmaf(op1, op2, a);
485}
486
487
Anton Kirilov088b01f2022-09-27 14:27:38 +0100488inline uint64_t LowestSetBit(uint64_t value) {
489 return value & UnsignedNegate(value);
490}
armvixl6e2c8272015-03-31 11:04:14 +0100491
492
armvixl0f35e362016-05-10 13:57:58 +0100493template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100494inline int HighestSetBitPosition(T value) {
495 VIXL_ASSERT(value != 0);
496 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
497}
498
499
armvixl0f35e362016-05-10 13:57:58 +0100500template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100501inline int WhichPowerOf2(V value) {
502 VIXL_ASSERT(IsPowerOf2(value));
503 return CountTrailingZeros(value);
504}
armvixlad96eda2013-06-14 11:42:37 +0100505
armvixldb644342015-07-21 11:37:10 +0100506
armvixl330dc712014-11-25 10:38:32 +0000507unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
508
armvixldb644342015-07-21 11:37:10 +0100509
Pierre Langlois88c46b82016-06-02 18:15:32 +0100510int BitCount(uint64_t value);
511
512
armvixldb644342015-07-21 11:37:10 +0100513template <typename T>
514T ReverseBits(T value) {
515 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
516 (sizeof(value) == 4) || (sizeof(value) == 8));
517 T result = 0;
518 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
519 result = (result << 1) | (value & 1);
520 value >>= 1;
521 }
522 return result;
523}
524
525
526template <typename T>
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000527inline T SignExtend(T val, int size_in_bits) {
528 VIXL_ASSERT(size_in_bits > 0);
529 T mask = (T(2) << (size_in_bits - 1)) - T(1);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100530 val &= mask;
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000531 T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700532 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100533 return val;
534}
535
536
537template <typename T>
armvixldb644342015-07-21 11:37:10 +0100538T ReverseBytes(T value, int block_bytes_log2) {
539 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
540 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
541 // Split the 64-bit value into an 8-bit array, where b[0] is the least
542 // significant byte, and b[7] is the most significant.
543 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000544 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100545 for (int i = 7; i >= 0; i--) {
546 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
547 mask >>= 8;
548 }
549
550 // Permutation tables for REV instructions.
551 // permute_table[0] is used by REV16_x, REV16_w
552 // permute_table[1] is used by REV32_x, REV_w
553 // permute_table[2] is used by REV_x
554 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100555 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
556 {4, 5, 6, 7, 0, 1, 2, 3},
557 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000558 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100559 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000560 temp <<= 8;
561 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100562 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000563
564 T result;
565 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
566 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100567 return result;
568}
569
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700570template <unsigned MULTIPLE, typename T>
571inline bool IsMultiple(T value) {
572 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
573 return (value & (MULTIPLE - 1)) == 0;
574}
armvixldb644342015-07-21 11:37:10 +0100575
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000576template <typename T>
577inline bool IsMultiple(T value, unsigned multiple) {
578 VIXL_ASSERT(IsPowerOf2(multiple));
579 return (value & (multiple - 1)) == 0;
580}
581
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000582template <typename T>
583inline bool IsAligned(T pointer, int alignment) {
584 VIXL_ASSERT(IsPowerOf2(alignment));
585 return (pointer & (alignment - 1)) == 0;
586}
587
armvixlad96eda2013-06-14 11:42:37 +0100588// Pointer alignment
589// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100590template <unsigned ALIGN, typename T>
591inline bool IsAligned(T pointer) {
592 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100593 // Use C-style casts to get static_cast behaviour for integral types (T), and
594 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000595 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100596}
597
armvixl0f35e362016-05-10 13:57:58 +0100598template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100599bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100600 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100601}
602
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100603// Increment a pointer until it has the specified alignment. The alignment must
604// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100605template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100606T AlignUp(T pointer,
607 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100608 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100609 // Use C-style casts to get static_cast behaviour for integral types (T), and
610 // reinterpret_cast behaviour for other types.
611
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100612 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000613 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000614 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100615
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100616 size_t mask = alignment - 1;
617 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000618 VIXL_ASSERT(result >= pointer);
619
620 return result;
armvixlad96eda2013-06-14 11:42:37 +0100621}
622
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100623// Decrement a pointer until it has the specified alignment. The alignment must
624// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100625template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100626T AlignDown(T pointer,
627 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100628 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100629 // Use C-style casts to get static_cast behaviour for integral types (T), and
630 // reinterpret_cast behaviour for other types.
631
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100632 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000633 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000634 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100635
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100636 size_t mask = alignment - 1;
637 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000638}
639
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100640
Pierre Langlois88c46b82016-06-02 18:15:32 +0100641template <typename T>
642inline T ExtractBit(T value, unsigned bit) {
643 return (value >> bit) & T(1);
644}
645
646template <typename Ts, typename Td>
647inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
648 return Td((value >> least_significant_bit) & Ts(mask));
649}
650
651template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800652inline void AssignBit(Td& dst, // NOLINT(runtime/references)
653 int bit,
654 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100655 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
656 VIXL_ASSERT(bit >= 0);
657 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
658 Td mask(1);
659 dst &= ~(mask << bit);
660 dst |= Td(value) << bit;
661}
662
663template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800664inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100665 int least_significant_bit,
666 Ts mask,
667 Ts value) {
668 VIXL_ASSERT(least_significant_bit >= 0);
669 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
670 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
671 Td(mask));
672 VIXL_ASSERT((value & mask) == value);
673 dst &= ~(Td(mask) << least_significant_bit);
674 dst |= Td(value) << least_significant_bit;
675}
676
677class VFP {
678 public:
679 static uint32_t FP32ToImm8(float imm) {
680 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
681 uint32_t bits = FloatToRawbits(imm);
682 // bit7: a000.0000
683 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
684 // bit6: 0b00.0000
685 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
686 // bit5_to_0: 00cd.efgh
687 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
688 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
689 }
690 static uint32_t FP64ToImm8(double imm) {
691 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
692 // 0000.0000.0000.0000.0000.0000.0000.0000
693 uint64_t bits = DoubleToRawbits(imm);
694 // bit7: a000.0000
695 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
696 // bit6: 0b00.0000
697 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
698 // bit5_to_0: 00cd.efgh
699 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
700
701 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
702 }
703 static float Imm8ToFP32(uint32_t imm8) {
704 // Imm8: abcdefgh (8 bits)
705 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
706 // where B is b ^ 1
707 uint32_t bits = imm8;
708 uint32_t bit7 = (bits >> 7) & 0x1;
709 uint32_t bit6 = (bits >> 6) & 0x1;
710 uint32_t bit5_to_0 = bits & 0x3f;
711 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
712
713 return RawbitsToFloat(result);
714 }
715 static double Imm8ToFP64(uint32_t imm8) {
716 // Imm8: abcdefgh (8 bits)
717 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
718 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
719 // where B is b ^ 1
720 uint32_t bits = imm8;
721 uint64_t bit7 = (bits >> 7) & 0x1;
722 uint64_t bit6 = (bits >> 6) & 0x1;
723 uint64_t bit5_to_0 = bits & 0x3f;
724 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
725 return RawbitsToDouble(result);
726 }
727 static bool IsImmFP32(float imm) {
728 // Valid values will have the form:
729 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
730 uint32_t bits = FloatToRawbits(imm);
731 // bits[19..0] are cleared.
732 if ((bits & 0x7ffff) != 0) {
733 return false;
734 }
735
736
737 // bits[29..25] are all set or all cleared.
738 uint32_t b_pattern = (bits >> 16) & 0x3e00;
739 if (b_pattern != 0 && b_pattern != 0x3e00) {
740 return false;
741 }
742 // bit[30] and bit[29] are opposite.
743 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
744 return false;
745 }
746 return true;
747 }
748 static bool IsImmFP64(double imm) {
749 // Valid values will have the form:
750 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
751 // 0000.0000.0000.0000.0000.0000.0000.0000
752 uint64_t bits = DoubleToRawbits(imm);
753 // bits[47..0] are cleared.
754 if ((bits & 0x0000ffffffffffff) != 0) {
755 return false;
756 }
757 // bits[61..54] are all set or all cleared.
758 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
759 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
760 return false;
761 }
762 // bit[62] and bit[61] are opposite.
763 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
764 return false;
765 }
766 return true;
767 }
768};
769
770class BitField {
771 // ForEachBitHelper is a functor that will call
772 // bool ForEachBitHelper::execute(ElementType id) const
773 // and expects a boolean in return whether to continue (if true)
774 // or stop (if false)
775 // check_set will check if the bits are on (true) or off(false)
776 template <typename ForEachBitHelper, bool check_set>
777 bool ForEachBit(const ForEachBitHelper& helper) {
778 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
779 if (bitfield_[i] == check_set)
780 if (!helper.execute(i)) return false;
781 }
782 return true;
783 }
784
785 public:
786 explicit BitField(unsigned size) : bitfield_(size, 0) {}
787
788 void Set(int i) {
789 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
790 bitfield_[i] = true;
791 }
792
793 void Unset(int i) {
794 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
795 bitfield_[i] = true;
796 }
797
798 bool IsSet(int i) const { return bitfield_[i]; }
799
800 // For each bit not set in the bitfield call the execute functor
801 // execute.
802 // ForEachBitSetHelper::execute returns true if the iteration through
803 // the bits can continue, otherwise it will stop.
804 // struct ForEachBitSetHelper {
805 // bool execute(int /*id*/) { return false; }
806 // };
807 template <typename ForEachBitNotSetHelper>
808 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
809 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
810 }
811
812 // For each bit set in the bitfield call the execute functor
813 // execute.
814 template <typename ForEachBitSetHelper>
815 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
816 return ForEachBit<ForEachBitSetHelper, true>(helper);
817 }
818
819 private:
820 std::vector<bool> bitfield_;
821};
822
Pierre Langloisd82faf62018-04-04 13:06:58 +0100823namespace internal {
824
Pierre Langlois88c46b82016-06-02 18:15:32 +0100825typedef int64_t Int64;
826class Uint64;
827class Uint128;
828
829class Uint32 {
830 uint32_t data_;
831
832 public:
833 // Unlike uint32_t, Uint32 has a default constructor.
834 Uint32() { data_ = 0; }
835 explicit Uint32(uint32_t data) : data_(data) {}
836 inline explicit Uint32(Uint64 data);
837 uint32_t Get() const { return data_; }
838 template <int N>
839 int32_t GetSigned() const {
840 return ExtractSignedBitfield32(N - 1, 0, data_);
841 }
842 int32_t GetSigned() const { return data_; }
843 Uint32 operator~() const { return Uint32(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100844 Uint32 operator-() const { return Uint32(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100845 bool operator==(Uint32 value) const { return data_ == value.data_; }
846 bool operator!=(Uint32 value) const { return data_ != value.data_; }
847 bool operator>(Uint32 value) const { return data_ > value.data_; }
848 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
849 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
850 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
851 Uint32 operator&=(Uint32 value) {
852 data_ &= value.data_;
853 return *this;
854 }
855 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
856 Uint32 operator^=(Uint32 value) {
857 data_ ^= value.data_;
858 return *this;
859 }
860 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
861 Uint32 operator|=(Uint32 value) {
862 data_ |= value.data_;
863 return *this;
864 }
865 // Unlike uint32_t, the shift functions can accept negative shift and
866 // return 0 when the shift is too big.
867 Uint32 operator>>(int shift) const {
868 if (shift == 0) return *this;
869 if (shift < 0) {
870 int tmp = -shift;
871 if (tmp >= 32) return Uint32(0);
872 return Uint32(data_ << tmp);
873 }
874 int tmp = shift;
875 if (tmp >= 32) return Uint32(0);
876 return Uint32(data_ >> tmp);
877 }
878 Uint32 operator<<(int shift) const {
879 if (shift == 0) return *this;
880 if (shift < 0) {
881 int tmp = -shift;
882 if (tmp >= 32) return Uint32(0);
883 return Uint32(data_ >> tmp);
884 }
885 int tmp = shift;
886 if (tmp >= 32) return Uint32(0);
887 return Uint32(data_ << tmp);
888 }
889};
890
891class Uint64 {
892 uint64_t data_;
893
894 public:
895 // Unlike uint64_t, Uint64 has a default constructor.
896 Uint64() { data_ = 0; }
897 explicit Uint64(uint64_t data) : data_(data) {}
898 explicit Uint64(Uint32 data) : data_(data.Get()) {}
899 inline explicit Uint64(Uint128 data);
900 uint64_t Get() const { return data_; }
901 int64_t GetSigned(int N) const {
902 return ExtractSignedBitfield64(N - 1, 0, data_);
903 }
904 int64_t GetSigned() const { return data_; }
905 Uint32 ToUint32() const {
906 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100907 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100908 }
909 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100910 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100911 Uint64 operator~() const { return Uint64(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100912 Uint64 operator-() const { return Uint64(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100913 bool operator==(Uint64 value) const { return data_ == value.data_; }
914 bool operator!=(Uint64 value) const { return data_ != value.data_; }
915 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
916 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
917 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
918 Uint64 operator&=(Uint64 value) {
919 data_ &= value.data_;
920 return *this;
921 }
922 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
923 Uint64 operator^=(Uint64 value) {
924 data_ ^= value.data_;
925 return *this;
926 }
927 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
928 Uint64 operator|=(Uint64 value) {
929 data_ |= value.data_;
930 return *this;
931 }
932 // Unlike uint64_t, the shift functions can accept negative shift and
933 // return 0 when the shift is too big.
934 Uint64 operator>>(int shift) const {
935 if (shift == 0) return *this;
936 if (shift < 0) {
937 int tmp = -shift;
938 if (tmp >= 64) return Uint64(0);
939 return Uint64(data_ << tmp);
940 }
941 int tmp = shift;
942 if (tmp >= 64) return Uint64(0);
943 return Uint64(data_ >> tmp);
944 }
945 Uint64 operator<<(int shift) const {
946 if (shift == 0) return *this;
947 if (shift < 0) {
948 int tmp = -shift;
949 if (tmp >= 64) return Uint64(0);
950 return Uint64(data_ >> tmp);
951 }
952 int tmp = shift;
953 if (tmp >= 64) return Uint64(0);
954 return Uint64(data_ << tmp);
955 }
956};
957
958class Uint128 {
959 uint64_t data_high_;
960 uint64_t data_low_;
961
962 public:
963 Uint128() : data_high_(0), data_low_(0) {}
964 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
965 explicit Uint128(Uint64 data_low)
966 : data_high_(0), data_low_(data_low.Get()) {}
967 Uint128(uint64_t data_high, uint64_t data_low)
968 : data_high_(data_high), data_low_(data_low) {}
969 Uint64 ToUint64() const {
970 VIXL_ASSERT(data_high_ == 0);
971 return Uint64(data_low_);
972 }
973 Uint64 GetHigh64() const { return Uint64(data_high_); }
974 Uint64 GetLow64() const { return Uint64(data_low_); }
975 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
976 bool operator==(Uint128 value) const {
977 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
978 }
979 Uint128 operator&(Uint128 value) const {
980 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
981 }
982 Uint128 operator&=(Uint128 value) {
983 data_high_ &= value.data_high_;
984 data_low_ &= value.data_low_;
985 return *this;
986 }
987 Uint128 operator|=(Uint128 value) {
988 data_high_ |= value.data_high_;
989 data_low_ |= value.data_low_;
990 return *this;
991 }
992 Uint128 operator>>(int shift) const {
993 VIXL_ASSERT((shift >= 0) && (shift < 128));
994 if (shift == 0) return *this;
995 if (shift >= 64) {
996 return Uint128(0, data_high_ >> (shift - 64));
997 }
998 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
999 return Uint128(data_high_ >> shift, tmp);
1000 }
1001 Uint128 operator<<(int shift) const {
1002 VIXL_ASSERT((shift >= 0) && (shift < 128));
1003 if (shift == 0) return *this;
1004 if (shift >= 64) {
1005 return Uint128(data_low_ << (shift - 64), 0);
1006 }
1007 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
1008 return Uint128(tmp, data_low_ << shift);
1009 }
1010};
1011
1012Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
1013Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1014
1015Int64 BitCount(Uint32 value);
1016
TatWai Chong13634762019-07-16 16:20:45 -07001017// The algorithm used is adapted from the one described in section 8.2 of
1018// Hacker's Delight, by Henry S. Warren, Jr.
1019template <unsigned N, typename T>
1020int64_t MultiplyHigh(T u, T v) {
1021 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1022 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1023 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1024 uint64_t sign_ext = 0;
1025 unsigned half_bits = N / 2;
1026 uint64_t half_mask = GetUintMask(half_bits);
1027 if (std::numeric_limits<T>::is_signed) {
1028 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1029 }
1030
1031 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1032 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1033
1034 u0 = u & half_mask;
1035 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1036 v0 = v & half_mask;
1037 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1038
1039 w0 = u0 * v0;
1040 t = u1 * v0 + (w0 >> half_bits);
1041
1042 w1 = t & half_mask;
1043 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1044 w1 = u0 * v1 + w1;
1045 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1046
1047 uint64_t value = u1 * v1 + w2 + w1;
1048 int64_t result;
1049 memcpy(&result, &value, sizeof(result));
1050 return result;
1051}
1052
Pierre Langloisd82faf62018-04-04 13:06:58 +01001053} // namespace internal
1054
Jacob Bramleyca789742018-09-13 14:25:46 +01001055// The default NaN values (for FPCR.DN=1).
1056extern const double kFP64DefaultNaN;
1057extern const float kFP32DefaultNaN;
1058extern const Float16 kFP16DefaultNaN;
1059
1060// Floating-point infinity values.
1061extern const Float16 kFP16PositiveInfinity;
1062extern const Float16 kFP16NegativeInfinity;
1063extern const float kFP32PositiveInfinity;
1064extern const float kFP32NegativeInfinity;
1065extern const double kFP64PositiveInfinity;
1066extern const double kFP64NegativeInfinity;
1067
1068// Floating-point zero values.
1069extern const Float16 kFP16PositiveZero;
1070extern const Float16 kFP16NegativeZero;
1071
1072// AArch64 floating-point specifics. These match IEEE-754.
1073const unsigned kDoubleMantissaBits = 52;
1074const unsigned kDoubleExponentBits = 11;
1075const unsigned kFloatMantissaBits = 23;
1076const unsigned kFloatExponentBits = 8;
1077const unsigned kFloat16MantissaBits = 10;
1078const unsigned kFloat16ExponentBits = 5;
1079
1080enum FPRounding {
1081 // The first four values are encodable directly by FPCR<RMode>.
1082 FPTieEven = 0x0,
1083 FPPositiveInfinity = 0x1,
1084 FPNegativeInfinity = 0x2,
1085 FPZero = 0x3,
1086
1087 // The final rounding modes are only available when explicitly specified by
1088 // the instruction (such as with fcvta). It cannot be set in FPCR.
1089 FPTieAway,
1090 FPRoundOdd
1091};
1092
1093enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1094
1095// Assemble the specified IEEE-754 components into the target type and apply
1096// appropriate rounding.
1097// sign: 0 = positive, 1 = negative
1098// exponent: Unbiased IEEE-754 exponent.
1099// mantissa: The mantissa of the input. The top bit (which is not encoded for
1100// normal IEEE-754 values) must not be omitted. This bit has the
1101// value 'pow(2, exponent)'.
1102//
1103// The input value is assumed to be a normalized value. That is, the input may
1104// not be infinity or NaN. If the source value is subnormal, it must be
1105// normalized before calling this function such that the highest set bit in the
1106// mantissa has the value 'pow(2, exponent)'.
1107//
1108// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1109// calling a templated FPRound.
1110template <class T, int ebits, int mbits>
1111T FPRound(int64_t sign,
1112 int64_t exponent,
1113 uint64_t mantissa,
1114 FPRounding round_mode) {
1115 VIXL_ASSERT((sign == 0) || (sign == 1));
1116
1117 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1118 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1119
1120 // Rounding can promote subnormals to normals, and normals to infinities. For
1121 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1122 // encodable as a float, but rounding based on the low-order mantissa bits
1123 // could make it overflow. With ties-to-even rounding, this value would become
1124 // an infinity.
1125
1126 // ---- Rounding Method ----
1127 //
1128 // The exponent is irrelevant in the rounding operation, so we treat the
1129 // lowest-order bit that will fit into the result ('onebit') as having
1130 // the value '1'. Similarly, the highest-order bit that won't fit into
1131 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1132 // 'onebit' and 'halfbit':
1133 //
1134 // These bits fit into the result.
1135 // |---------------------|
1136 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1137 // ||
1138 // / |
1139 // / halfbit
1140 // onebit
1141 //
1142 // For subnormal outputs, the range of representable bits is smaller and
1143 // the position of onebit and halfbit depends on the exponent of the
1144 // input, but the method is otherwise similar.
1145 //
1146 // onebit(frac)
1147 // |
1148 // | halfbit(frac) halfbit(adjusted)
1149 // | / /
1150 // | | |
1151 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1152 // 0b00.0... -> 0b00.0... -> 0b00
1153 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1154 // 0b00.1... -> 0b00.1... -> 0b01
1155 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1156 // 0b01.0... -> 0b01.0... -> 0b01
1157 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1158 // 0b01.1... -> 0b01.1... -> 0b10
1159 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1160 // 0b10.0... -> 0b10.0... -> 0b10
1161 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1162 // 0b10.1... -> 0b10.1... -> 0b11
1163 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1164 // ... / | / |
1165 // / | / |
1166 // / |
1167 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1168 //
1169 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1170
1171 static const int mantissa_offset = 0;
1172 static const int exponent_offset = mantissa_offset + mbits;
1173 static const int sign_offset = exponent_offset + ebits;
1174 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1175
1176 // Bail out early for zero inputs.
1177 if (mantissa == 0) {
1178 return static_cast<T>(sign << sign_offset);
1179 }
1180
1181 // If all bits in the exponent are set, the value is infinite or NaN.
1182 // This is true for all binary IEEE-754 formats.
1183 static const int infinite_exponent = (1 << ebits) - 1;
1184 static const int max_normal_exponent = infinite_exponent - 1;
1185
1186 // Apply the exponent bias to encode it for the result. Doing this early makes
1187 // it easy to detect values that will be infinite or subnormal.
1188 exponent += max_normal_exponent >> 1;
1189
1190 if (exponent > max_normal_exponent) {
1191 // Overflow: the input is too large for the result type to represent.
1192 if (round_mode == FPTieEven) {
1193 // FPTieEven rounding mode handles overflows using infinities.
1194 exponent = infinite_exponent;
1195 mantissa = 0;
1196 } else {
1197 VIXL_ASSERT(round_mode == FPRoundOdd);
1198 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1199 // normal number.
1200 exponent = max_normal_exponent;
1201 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1202 }
1203 return static_cast<T>((sign << sign_offset) |
1204 (exponent << exponent_offset) |
1205 (mantissa << mantissa_offset));
1206 }
1207
1208 // Calculate the shift required to move the top mantissa bit to the proper
1209 // place in the destination type.
1210 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1211 int shift = highest_significant_bit - mbits;
1212
1213 if (exponent <= 0) {
1214 // The output will be subnormal (before rounding).
1215 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1216 // is necessary because the exponent of a subnormal value (encoded as 0) is
1217 // the same as the exponent of the smallest normal value (encoded as 1).
Anton Kirilov088b01f2022-09-27 14:27:38 +01001218 shift += static_cast<int>(-exponent + 1);
Jacob Bramleyca789742018-09-13 14:25:46 +01001219
1220 // Handle inputs that would produce a zero output.
1221 //
1222 // Shifts higher than highest_significant_bit+1 will always produce a zero
1223 // result. A shift of exactly highest_significant_bit+1 might produce a
1224 // non-zero result after rounding.
1225 if (shift > (highest_significant_bit + 1)) {
1226 if (round_mode == FPTieEven) {
1227 // The result will always be +/-0.0.
1228 return static_cast<T>(sign << sign_offset);
1229 } else {
1230 VIXL_ASSERT(round_mode == FPRoundOdd);
1231 VIXL_ASSERT(mantissa != 0);
1232 // For FPRoundOdd, if the mantissa is too small to represent and
1233 // non-zero return the next "odd" value.
1234 return static_cast<T>((sign << sign_offset) | 1);
1235 }
1236 }
1237
1238 // Properly encode the exponent for a subnormal output.
1239 exponent = 0;
1240 } else {
1241 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1242 // normal values.
1243 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1244 }
1245
1246 // The casts below are only well-defined for unsigned integers.
1247 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1248 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1249
1250 if (shift > 0) {
1251 if (round_mode == FPTieEven) {
1252 // We have to shift the mantissa to the right. Some precision is lost, so
1253 // we need to apply rounding.
1254 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1255 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1256 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1257 uint64_t adjusted = mantissa - adjustment;
1258 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1259
1260 T result =
1261 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1262 ((mantissa >> shift) << mantissa_offset));
1263
1264 // A very large mantissa can overflow during rounding. If this happens,
1265 // the exponent should be incremented and the mantissa set to 1.0
1266 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1267 // has the nice side-effect that this case is handled for free.
1268 //
1269 // This also handles cases where a very large finite value overflows to
1270 // infinity, or where a very large subnormal value overflows to become
1271 // normal.
1272 return result + halfbit_adjusted;
1273 } else {
1274 VIXL_ASSERT(round_mode == FPRoundOdd);
1275 // If any bits at position halfbit or below are set, onebit (ie. the
1276 // bottom bit of the resulting mantissa) must be set.
1277 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1278 if (fractional_bits != 0) {
1279 mantissa |= UINT64_C(1) << shift;
1280 }
1281
1282 return static_cast<T>((sign << sign_offset) |
1283 (exponent << exponent_offset) |
1284 ((mantissa >> shift) << mantissa_offset));
1285 }
1286 } else {
1287 // We have to shift the mantissa to the left (or not at all). The input
1288 // mantissa is exactly representable in the output mantissa, so apply no
1289 // rounding correction.
1290 return static_cast<T>((sign << sign_offset) |
1291 (exponent << exponent_offset) |
1292 ((mantissa << -shift) << mantissa_offset));
1293 }
1294}
1295
1296
1297// See FPRound for a description of this function.
1298inline double FPRoundToDouble(int64_t sign,
1299 int64_t exponent,
1300 uint64_t mantissa,
1301 FPRounding round_mode) {
1302 uint64_t bits =
1303 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1304 exponent,
1305 mantissa,
1306 round_mode);
1307 return RawbitsToDouble(bits);
1308}
1309
1310
1311// See FPRound for a description of this function.
1312inline Float16 FPRoundToFloat16(int64_t sign,
1313 int64_t exponent,
1314 uint64_t mantissa,
1315 FPRounding round_mode) {
1316 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001317 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1318 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001319}
1320
1321
1322// See FPRound for a description of this function.
1323static inline float FPRoundToFloat(int64_t sign,
1324 int64_t exponent,
1325 uint64_t mantissa,
1326 FPRounding round_mode) {
1327 uint32_t bits =
1328 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1329 exponent,
1330 mantissa,
1331 round_mode);
1332 return RawbitsToFloat(bits);
1333}
1334
1335
1336float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1337float FPToFloat(double value,
1338 FPRounding round_mode,
1339 UseDefaultNaN DN,
1340 bool* exception = NULL);
1341
1342double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1343double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1344
1345Float16 FPToFloat16(float value,
1346 FPRounding round_mode,
1347 UseDefaultNaN DN,
1348 bool* exception = NULL);
1349
1350Float16 FPToFloat16(double value,
1351 FPRounding round_mode,
1352 UseDefaultNaN DN,
1353 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001354
1355// Like static_cast<T>(value), but with specialisations for the Float16 type.
1356template <typename T, typename F>
1357T StaticCastFPTo(F value) {
1358 return static_cast<T>(value);
1359}
1360
1361template <>
1362inline float StaticCastFPTo<float, Float16>(Float16 value) {
1363 return FPToFloat(value, kIgnoreDefaultNaN);
1364}
1365
1366template <>
1367inline double StaticCastFPTo<double, Float16>(Float16 value) {
1368 return FPToDouble(value, kIgnoreDefaultNaN);
1369}
1370
1371template <>
1372inline Float16 StaticCastFPTo<Float16, float>(float value) {
1373 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1374}
1375
1376template <>
1377inline Float16 StaticCastFPTo<Float16, double>(double value) {
1378 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1379}
1380
1381template <typename T>
1382uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1383 switch (size_in_bits) {
1384 case 16:
1385 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1386 case 32:
1387 return FloatToRawbits(StaticCastFPTo<float>(value));
1388 case 64:
1389 return DoubleToRawbits(StaticCastFPTo<double>(value));
1390 }
1391 VIXL_UNREACHABLE();
1392 return 0;
1393}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001394
1395template <typename T>
1396T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1397 VIXL_ASSERT(IsUintN(size_in_bits, value));
1398 switch (size_in_bits) {
1399 case 16:
1400 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1401 case 32:
1402 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1403 case 64:
1404 return StaticCastFPTo<T>(RawbitsToDouble(value));
1405 }
1406 VIXL_UNREACHABLE();
1407 return 0;
1408}
1409
Martyn Capewell6bf28752020-08-05 11:57:06 +01001410// Jenkins one-at-a-time hash, based on
1411// https://en.wikipedia.org/wiki/Jenkins_hash_function citing
1412// https://www.drdobbs.com/database/algorithm-alley/184410284.
1413constexpr uint32_t Hash(const char* str, uint32_t hash = 0) {
1414 if (*str == '\0') {
1415 hash += hash << 3;
1416 hash ^= hash >> 11;
1417 hash += hash << 15;
1418 return hash;
1419 } else {
1420 hash += *str;
1421 hash += hash << 10;
1422 hash ^= hash >> 6;
1423 return Hash(str + 1, hash);
1424 }
1425}
1426
Martyn Capewell8afff392022-04-19 18:08:39 +01001427constexpr uint32_t operator"" _h(const char* x, size_t) { return Hash(x); }
1428
armvixlad96eda2013-06-14 11:42:37 +01001429} // namespace vixl
1430
1431#endif // VIXL_UTILS_H