blob: 7af9483920c25c380967d5494595149c7e685af1 [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
mmc28ab5c57c92023-03-16 16:26:31 +0000294// An absolute operation for signed integers that is defined for results outside
295// the representable range. Specifically, Abs(MIN_INT) is MIN_INT.
296template <typename T>
297T Abs(T val) {
298 // TODO: this static assertion is for signed integer inputs, as that's the
299 // only type tested. However, the code should work for all numeric inputs.
300 // Remove the assertion and this comment when more tests are available.
301 VIXL_STATIC_ASSERT(std::is_signed<T>::value && std::is_integral<T>::value);
302 return ((val >= -std::numeric_limits<T>::max()) && (val < 0)) ? -val : val;
303}
304
Jacob Bramley03c0b512019-02-22 16:42:06 +0000305// Convert unsigned to signed numbers in a well-defined way (using two's
306// complement representations).
307inline int64_t RawbitsToInt64(uint64_t bits) {
308 return (bits >= UINT64_C(0x8000000000000000))
Anton Kirilov088b01f2022-09-27 14:27:38 +0100309 ? (-static_cast<int64_t>(UnsignedNegate(bits) - 1) - 1)
Jacob Bramley03c0b512019-02-22 16:42:06 +0000310 : static_cast<int64_t>(bits);
311}
312
313inline int32_t RawbitsToInt32(uint32_t bits) {
Anton Kirilov088b01f2022-09-27 14:27:38 +0100314 return (bits >= UINT64_C(0x80000000))
315 ? (-static_cast<int32_t>(UnsignedNegate(bits) - 1) - 1)
316 : static_cast<int32_t>(bits);
Jacob Bramley03c0b512019-02-22 16:42:06 +0000317}
318
Jacob Bramleyca789742018-09-13 14:25:46 +0100319namespace internal {
320
321// Internal simulation class used solely by the simulator to
322// provide an abstraction layer for any half-precision arithmetic.
323class SimFloat16 : public Float16 {
324 public:
325 // TODO: We should investigate making this constructor explicit.
326 // This is currently difficult to do due to a number of templated
327 // functions in the simulator which rely on returning double values.
328 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
329 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
330 this->rawbits_ = Float16ToRawbits(f);
331 }
332 SimFloat16() : Float16() {}
333 SimFloat16 operator-() const;
334 SimFloat16 operator+(SimFloat16 rhs) const;
335 SimFloat16 operator-(SimFloat16 rhs) const;
336 SimFloat16 operator*(SimFloat16 rhs) const;
337 SimFloat16 operator/(SimFloat16 rhs) const;
338 bool operator<(SimFloat16 rhs) const;
339 bool operator>(SimFloat16 rhs) const;
340 bool operator==(SimFloat16 rhs) const;
341 bool operator!=(SimFloat16 rhs) const;
Josh Sorefb43d6ef2022-08-03 12:47:14 -0400342 // This is necessary for conversions performed in (macro asm) Fmov.
Jacob Bramleyca789742018-09-13 14:25:46 +0100343 bool operator==(double rhs) const;
344 operator double() const;
345};
346} // namespace internal
347
348uint32_t Float16Sign(internal::SimFloat16 value);
349
350uint32_t Float16Exp(internal::SimFloat16 value);
351
352uint32_t Float16Mantissa(internal::SimFloat16 value);
353
Pierre Langlois88c46b82016-06-02 18:15:32 +0100354uint32_t FloatSign(float value);
355VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
356 return FloatSign(value);
357}
358
359uint32_t FloatExp(float value);
360VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
361 return FloatExp(value);
362}
363
364uint32_t FloatMantissa(float value);
365VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
366 return FloatMantissa(value);
367}
368
369uint32_t DoubleSign(double value);
370VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
371 return DoubleSign(value);
372}
373
374uint32_t DoubleExp(double value);
375VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
376 return DoubleExp(value);
377}
378
379uint64_t DoubleMantissa(double value);
380VIXL_DEPRECATED("DoubleMantissa",
381 inline uint64_t double_mantissa(double value)) {
382 return DoubleMantissa(value);
383}
384
Jacob Bramleyca789742018-09-13 14:25:46 +0100385internal::SimFloat16 Float16Pack(uint16_t sign,
386 uint16_t exp,
387 uint16_t mantissa);
388
Pierre Langlois88c46b82016-06-02 18:15:32 +0100389float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
390VIXL_DEPRECATED("FloatPack",
391 inline float float_pack(uint32_t sign,
392 uint32_t exp,
393 uint32_t mantissa)) {
394 return FloatPack(sign, exp, mantissa);
395}
396
397double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
398VIXL_DEPRECATED("DoublePack",
399 inline double double_pack(uint32_t sign,
400 uint32_t exp,
401 uint64_t mantissa)) {
402 return DoublePack(sign, exp, mantissa);
403}
armvixl5289c592015-03-02 13:52:04 +0000404
405// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100406int Float16Classify(Float16 value);
407VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
408 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100409}
armvixlf37fdc02014-02-05 13:22:16 +0000410
Jacob Bramleyca789742018-09-13 14:25:46 +0100411bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100412
Jacob Bramleyf48172b2020-07-13 14:47:15 +0100413inline bool IsPositiveZero(double value) {
414 return (value == 0.0) && (copysign(1.0, value) > 0.0);
415}
416
Jacob Bramleyca789742018-09-13 14:25:46 +0100417inline bool IsNaN(float value) { return std::isnan(value); }
418
419inline bool IsNaN(double value) { return std::isnan(value); }
420
421inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
422
423inline bool IsInf(float value) { return std::isinf(value); }
424
425inline bool IsInf(double value) { return std::isinf(value); }
426
427inline bool IsInf(Float16 value) {
428 return Float16Classify(value) == FP_INFINITE;
429}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100430
431
armvixlf37fdc02014-02-05 13:22:16 +0000432// NaN tests.
433inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100434 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100435 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100436 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000437 return true;
438 }
439 return false;
440}
441
442
443inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000444 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100445 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100446 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000447 return true;
448 }
449 return false;
450}
451
452
Jacob Bramleyca789742018-09-13 14:25:46 +0100453inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000454 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100455 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000456}
457
458
armvixlf37fdc02014-02-05 13:22:16 +0000459template <typename T>
460inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100461 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000462}
463
464
armvixlb0c8ae22014-03-21 14:03:59 +0000465// Convert the NaN in 'num' to a quiet NaN.
466inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100467 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100468 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100469 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000470}
471
472
473inline float ToQuietNaN(float num) {
474 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100475 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100476 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000477}
478
479
Jacob Bramleyca789742018-09-13 14:25:46 +0100480inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
481 const uint16_t kFP16QuietNaNMask = 0x0200;
482 VIXL_ASSERT(IsNaN(num));
483 return internal::SimFloat16(
484 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
485}
486
487
armvixlb0c8ae22014-03-21 14:03:59 +0000488// Fused multiply-add.
489inline double FusedMultiplyAdd(double op1, double op2, double a) {
490 return fma(op1, op2, a);
491}
492
493
494inline float FusedMultiplyAdd(float op1, float op2, float a) {
495 return fmaf(op1, op2, a);
496}
497
498
Anton Kirilov088b01f2022-09-27 14:27:38 +0100499inline uint64_t LowestSetBit(uint64_t value) {
500 return value & UnsignedNegate(value);
501}
armvixl6e2c8272015-03-31 11:04:14 +0100502
503
armvixl0f35e362016-05-10 13:57:58 +0100504template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100505inline int HighestSetBitPosition(T value) {
506 VIXL_ASSERT(value != 0);
507 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
508}
509
510
armvixl0f35e362016-05-10 13:57:58 +0100511template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100512inline int WhichPowerOf2(V value) {
513 VIXL_ASSERT(IsPowerOf2(value));
514 return CountTrailingZeros(value);
515}
armvixlad96eda2013-06-14 11:42:37 +0100516
armvixldb644342015-07-21 11:37:10 +0100517
armvixl330dc712014-11-25 10:38:32 +0000518unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
519
armvixldb644342015-07-21 11:37:10 +0100520
Pierre Langlois88c46b82016-06-02 18:15:32 +0100521int BitCount(uint64_t value);
522
523
armvixldb644342015-07-21 11:37:10 +0100524template <typename T>
525T ReverseBits(T value) {
526 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
527 (sizeof(value) == 4) || (sizeof(value) == 8));
528 T result = 0;
529 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
530 result = (result << 1) | (value & 1);
531 value >>= 1;
532 }
533 return result;
534}
535
536
537template <typename T>
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000538inline T SignExtend(T val, int size_in_bits) {
539 VIXL_ASSERT(size_in_bits > 0);
540 T mask = (T(2) << (size_in_bits - 1)) - T(1);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100541 val &= mask;
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000542 T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700543 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100544 return val;
545}
546
547
548template <typename T>
armvixldb644342015-07-21 11:37:10 +0100549T ReverseBytes(T value, int block_bytes_log2) {
550 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
551 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
552 // Split the 64-bit value into an 8-bit array, where b[0] is the least
553 // significant byte, and b[7] is the most significant.
554 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000555 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100556 for (int i = 7; i >= 0; i--) {
557 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
558 mask >>= 8;
559 }
560
561 // Permutation tables for REV instructions.
562 // permute_table[0] is used by REV16_x, REV16_w
563 // permute_table[1] is used by REV32_x, REV_w
564 // permute_table[2] is used by REV_x
565 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100566 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
567 {4, 5, 6, 7, 0, 1, 2, 3},
568 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000569 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100570 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000571 temp <<= 8;
572 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100573 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000574
575 T result;
576 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
577 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100578 return result;
579}
580
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700581template <unsigned MULTIPLE, typename T>
582inline bool IsMultiple(T value) {
583 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
584 return (value & (MULTIPLE - 1)) == 0;
585}
armvixldb644342015-07-21 11:37:10 +0100586
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000587template <typename T>
588inline bool IsMultiple(T value, unsigned multiple) {
589 VIXL_ASSERT(IsPowerOf2(multiple));
590 return (value & (multiple - 1)) == 0;
591}
592
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000593template <typename T>
594inline bool IsAligned(T pointer, int alignment) {
595 VIXL_ASSERT(IsPowerOf2(alignment));
596 return (pointer & (alignment - 1)) == 0;
597}
598
armvixlad96eda2013-06-14 11:42:37 +0100599// Pointer alignment
600// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100601template <unsigned ALIGN, typename T>
602inline bool IsAligned(T pointer) {
603 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100604 // Use C-style casts to get static_cast behaviour for integral types (T), and
605 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000606 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100607}
608
armvixl0f35e362016-05-10 13:57:58 +0100609template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100610bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100611 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100612}
613
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100614// Increment a pointer until it has the specified alignment. The alignment must
615// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100616template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100617T AlignUp(T pointer,
618 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100619 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100620 // Use C-style casts to get static_cast behaviour for integral types (T), and
621 // reinterpret_cast behaviour for other types.
622
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100623 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000624 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000625 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100626
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100627 size_t mask = alignment - 1;
628 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000629 VIXL_ASSERT(result >= pointer);
630
631 return result;
armvixlad96eda2013-06-14 11:42:37 +0100632}
633
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100634// Decrement a pointer until it has the specified alignment. The alignment must
635// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100636template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100637T AlignDown(T pointer,
638 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100639 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100640 // Use C-style casts to get static_cast behaviour for integral types (T), and
641 // reinterpret_cast behaviour for other types.
642
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100643 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000644 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000645 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100646
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100647 size_t mask = alignment - 1;
648 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000649}
650
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100651
Pierre Langlois88c46b82016-06-02 18:15:32 +0100652template <typename T>
653inline T ExtractBit(T value, unsigned bit) {
654 return (value >> bit) & T(1);
655}
656
657template <typename Ts, typename Td>
658inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
659 return Td((value >> least_significant_bit) & Ts(mask));
660}
661
662template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800663inline void AssignBit(Td& dst, // NOLINT(runtime/references)
664 int bit,
665 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100666 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
667 VIXL_ASSERT(bit >= 0);
668 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
669 Td mask(1);
670 dst &= ~(mask << bit);
671 dst |= Td(value) << bit;
672}
673
674template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800675inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100676 int least_significant_bit,
677 Ts mask,
678 Ts value) {
679 VIXL_ASSERT(least_significant_bit >= 0);
680 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
681 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
682 Td(mask));
683 VIXL_ASSERT((value & mask) == value);
684 dst &= ~(Td(mask) << least_significant_bit);
685 dst |= Td(value) << least_significant_bit;
686}
687
688class VFP {
689 public:
690 static uint32_t FP32ToImm8(float imm) {
691 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
692 uint32_t bits = FloatToRawbits(imm);
693 // bit7: a000.0000
694 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
695 // bit6: 0b00.0000
696 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
697 // bit5_to_0: 00cd.efgh
698 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
699 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
700 }
701 static uint32_t FP64ToImm8(double imm) {
702 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
703 // 0000.0000.0000.0000.0000.0000.0000.0000
704 uint64_t bits = DoubleToRawbits(imm);
705 // bit7: a000.0000
706 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
707 // bit6: 0b00.0000
708 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
709 // bit5_to_0: 00cd.efgh
710 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
711
712 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
713 }
714 static float Imm8ToFP32(uint32_t imm8) {
715 // Imm8: abcdefgh (8 bits)
716 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
717 // where B is b ^ 1
718 uint32_t bits = imm8;
719 uint32_t bit7 = (bits >> 7) & 0x1;
720 uint32_t bit6 = (bits >> 6) & 0x1;
721 uint32_t bit5_to_0 = bits & 0x3f;
722 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
723
724 return RawbitsToFloat(result);
725 }
726 static double Imm8ToFP64(uint32_t imm8) {
727 // Imm8: abcdefgh (8 bits)
728 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
729 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
730 // where B is b ^ 1
731 uint32_t bits = imm8;
732 uint64_t bit7 = (bits >> 7) & 0x1;
733 uint64_t bit6 = (bits >> 6) & 0x1;
734 uint64_t bit5_to_0 = bits & 0x3f;
735 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
736 return RawbitsToDouble(result);
737 }
738 static bool IsImmFP32(float imm) {
739 // Valid values will have the form:
740 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
741 uint32_t bits = FloatToRawbits(imm);
742 // bits[19..0] are cleared.
743 if ((bits & 0x7ffff) != 0) {
744 return false;
745 }
746
747
748 // bits[29..25] are all set or all cleared.
749 uint32_t b_pattern = (bits >> 16) & 0x3e00;
750 if (b_pattern != 0 && b_pattern != 0x3e00) {
751 return false;
752 }
753 // bit[30] and bit[29] are opposite.
754 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
755 return false;
756 }
757 return true;
758 }
759 static bool IsImmFP64(double imm) {
760 // Valid values will have the form:
761 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
762 // 0000.0000.0000.0000.0000.0000.0000.0000
763 uint64_t bits = DoubleToRawbits(imm);
764 // bits[47..0] are cleared.
765 if ((bits & 0x0000ffffffffffff) != 0) {
766 return false;
767 }
768 // bits[61..54] are all set or all cleared.
769 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
770 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
771 return false;
772 }
773 // bit[62] and bit[61] are opposite.
774 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
775 return false;
776 }
777 return true;
778 }
779};
780
781class BitField {
782 // ForEachBitHelper is a functor that will call
783 // bool ForEachBitHelper::execute(ElementType id) const
784 // and expects a boolean in return whether to continue (if true)
785 // or stop (if false)
786 // check_set will check if the bits are on (true) or off(false)
787 template <typename ForEachBitHelper, bool check_set>
788 bool ForEachBit(const ForEachBitHelper& helper) {
789 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
790 if (bitfield_[i] == check_set)
791 if (!helper.execute(i)) return false;
792 }
793 return true;
794 }
795
796 public:
797 explicit BitField(unsigned size) : bitfield_(size, 0) {}
798
799 void Set(int i) {
800 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
801 bitfield_[i] = true;
802 }
803
804 void Unset(int i) {
805 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
806 bitfield_[i] = true;
807 }
808
809 bool IsSet(int i) const { return bitfield_[i]; }
810
811 // For each bit not set in the bitfield call the execute functor
812 // execute.
813 // ForEachBitSetHelper::execute returns true if the iteration through
814 // the bits can continue, otherwise it will stop.
815 // struct ForEachBitSetHelper {
816 // bool execute(int /*id*/) { return false; }
817 // };
818 template <typename ForEachBitNotSetHelper>
819 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
820 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
821 }
822
823 // For each bit set in the bitfield call the execute functor
824 // execute.
825 template <typename ForEachBitSetHelper>
826 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
827 return ForEachBit<ForEachBitSetHelper, true>(helper);
828 }
829
830 private:
831 std::vector<bool> bitfield_;
832};
833
Pierre Langloisd82faf62018-04-04 13:06:58 +0100834namespace internal {
835
Pierre Langlois88c46b82016-06-02 18:15:32 +0100836typedef int64_t Int64;
837class Uint64;
838class Uint128;
839
840class Uint32 {
841 uint32_t data_;
842
843 public:
844 // Unlike uint32_t, Uint32 has a default constructor.
845 Uint32() { data_ = 0; }
846 explicit Uint32(uint32_t data) : data_(data) {}
847 inline explicit Uint32(Uint64 data);
848 uint32_t Get() const { return data_; }
849 template <int N>
850 int32_t GetSigned() const {
851 return ExtractSignedBitfield32(N - 1, 0, data_);
852 }
853 int32_t GetSigned() const { return data_; }
854 Uint32 operator~() const { return Uint32(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100855 Uint32 operator-() const { return Uint32(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100856 bool operator==(Uint32 value) const { return data_ == value.data_; }
857 bool operator!=(Uint32 value) const { return data_ != value.data_; }
858 bool operator>(Uint32 value) const { return data_ > value.data_; }
859 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
860 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
861 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
862 Uint32 operator&=(Uint32 value) {
863 data_ &= value.data_;
864 return *this;
865 }
866 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
867 Uint32 operator^=(Uint32 value) {
868 data_ ^= value.data_;
869 return *this;
870 }
871 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
872 Uint32 operator|=(Uint32 value) {
873 data_ |= value.data_;
874 return *this;
875 }
876 // Unlike uint32_t, the shift functions can accept negative shift and
877 // return 0 when the shift is too big.
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 Uint32 operator<<(int shift) const {
890 if (shift == 0) return *this;
891 if (shift < 0) {
892 int tmp = -shift;
893 if (tmp >= 32) return Uint32(0);
894 return Uint32(data_ >> tmp);
895 }
896 int tmp = shift;
897 if (tmp >= 32) return Uint32(0);
898 return Uint32(data_ << tmp);
899 }
900};
901
902class Uint64 {
903 uint64_t data_;
904
905 public:
906 // Unlike uint64_t, Uint64 has a default constructor.
907 Uint64() { data_ = 0; }
908 explicit Uint64(uint64_t data) : data_(data) {}
909 explicit Uint64(Uint32 data) : data_(data.Get()) {}
910 inline explicit Uint64(Uint128 data);
911 uint64_t Get() const { return data_; }
912 int64_t GetSigned(int N) const {
913 return ExtractSignedBitfield64(N - 1, 0, data_);
914 }
915 int64_t GetSigned() const { return data_; }
916 Uint32 ToUint32() const {
917 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100918 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100919 }
920 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100921 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100922 Uint64 operator~() const { return Uint64(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100923 Uint64 operator-() const { return Uint64(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100924 bool operator==(Uint64 value) const { return data_ == value.data_; }
925 bool operator!=(Uint64 value) const { return data_ != value.data_; }
926 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
927 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
928 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
929 Uint64 operator&=(Uint64 value) {
930 data_ &= value.data_;
931 return *this;
932 }
933 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
934 Uint64 operator^=(Uint64 value) {
935 data_ ^= value.data_;
936 return *this;
937 }
938 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
939 Uint64 operator|=(Uint64 value) {
940 data_ |= value.data_;
941 return *this;
942 }
943 // Unlike uint64_t, the shift functions can accept negative shift and
944 // return 0 when the shift is too big.
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 Uint64 operator<<(int shift) const {
957 if (shift == 0) return *this;
958 if (shift < 0) {
959 int tmp = -shift;
960 if (tmp >= 64) return Uint64(0);
961 return Uint64(data_ >> tmp);
962 }
963 int tmp = shift;
964 if (tmp >= 64) return Uint64(0);
965 return Uint64(data_ << tmp);
966 }
967};
968
969class Uint128 {
970 uint64_t data_high_;
971 uint64_t data_low_;
972
973 public:
974 Uint128() : data_high_(0), data_low_(0) {}
975 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
976 explicit Uint128(Uint64 data_low)
977 : data_high_(0), data_low_(data_low.Get()) {}
978 Uint128(uint64_t data_high, uint64_t data_low)
979 : data_high_(data_high), data_low_(data_low) {}
980 Uint64 ToUint64() const {
981 VIXL_ASSERT(data_high_ == 0);
982 return Uint64(data_low_);
983 }
984 Uint64 GetHigh64() const { return Uint64(data_high_); }
985 Uint64 GetLow64() const { return Uint64(data_low_); }
986 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
987 bool operator==(Uint128 value) const {
988 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
989 }
990 Uint128 operator&(Uint128 value) const {
991 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
992 }
993 Uint128 operator&=(Uint128 value) {
994 data_high_ &= value.data_high_;
995 data_low_ &= value.data_low_;
996 return *this;
997 }
998 Uint128 operator|=(Uint128 value) {
999 data_high_ |= value.data_high_;
1000 data_low_ |= value.data_low_;
1001 return *this;
1002 }
1003 Uint128 operator>>(int shift) const {
1004 VIXL_ASSERT((shift >= 0) && (shift < 128));
1005 if (shift == 0) return *this;
1006 if (shift >= 64) {
1007 return Uint128(0, data_high_ >> (shift - 64));
1008 }
1009 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
1010 return Uint128(data_high_ >> shift, tmp);
1011 }
1012 Uint128 operator<<(int shift) const {
1013 VIXL_ASSERT((shift >= 0) && (shift < 128));
1014 if (shift == 0) return *this;
1015 if (shift >= 64) {
1016 return Uint128(data_low_ << (shift - 64), 0);
1017 }
1018 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
1019 return Uint128(tmp, data_low_ << shift);
1020 }
1021};
1022
1023Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
1024Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1025
1026Int64 BitCount(Uint32 value);
1027
TatWai Chong13634762019-07-16 16:20:45 -07001028// The algorithm used is adapted from the one described in section 8.2 of
1029// Hacker's Delight, by Henry S. Warren, Jr.
1030template <unsigned N, typename T>
1031int64_t MultiplyHigh(T u, T v) {
1032 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1033 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1034 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1035 uint64_t sign_ext = 0;
1036 unsigned half_bits = N / 2;
1037 uint64_t half_mask = GetUintMask(half_bits);
1038 if (std::numeric_limits<T>::is_signed) {
1039 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1040 }
1041
1042 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1043 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1044
1045 u0 = u & half_mask;
1046 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1047 v0 = v & half_mask;
1048 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1049
1050 w0 = u0 * v0;
1051 t = u1 * v0 + (w0 >> half_bits);
1052
1053 w1 = t & half_mask;
1054 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1055 w1 = u0 * v1 + w1;
1056 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1057
1058 uint64_t value = u1 * v1 + w2 + w1;
1059 int64_t result;
1060 memcpy(&result, &value, sizeof(result));
1061 return result;
1062}
1063
Pierre Langloisd82faf62018-04-04 13:06:58 +01001064} // namespace internal
1065
Jacob Bramleyca789742018-09-13 14:25:46 +01001066// The default NaN values (for FPCR.DN=1).
1067extern const double kFP64DefaultNaN;
1068extern const float kFP32DefaultNaN;
1069extern const Float16 kFP16DefaultNaN;
1070
1071// Floating-point infinity values.
1072extern const Float16 kFP16PositiveInfinity;
1073extern const Float16 kFP16NegativeInfinity;
1074extern const float kFP32PositiveInfinity;
1075extern const float kFP32NegativeInfinity;
1076extern const double kFP64PositiveInfinity;
1077extern const double kFP64NegativeInfinity;
1078
1079// Floating-point zero values.
1080extern const Float16 kFP16PositiveZero;
1081extern const Float16 kFP16NegativeZero;
1082
1083// AArch64 floating-point specifics. These match IEEE-754.
1084const unsigned kDoubleMantissaBits = 52;
1085const unsigned kDoubleExponentBits = 11;
1086const unsigned kFloatMantissaBits = 23;
1087const unsigned kFloatExponentBits = 8;
1088const unsigned kFloat16MantissaBits = 10;
1089const unsigned kFloat16ExponentBits = 5;
1090
1091enum FPRounding {
1092 // The first four values are encodable directly by FPCR<RMode>.
1093 FPTieEven = 0x0,
1094 FPPositiveInfinity = 0x1,
1095 FPNegativeInfinity = 0x2,
1096 FPZero = 0x3,
1097
1098 // The final rounding modes are only available when explicitly specified by
1099 // the instruction (such as with fcvta). It cannot be set in FPCR.
1100 FPTieAway,
1101 FPRoundOdd
1102};
1103
1104enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1105
1106// Assemble the specified IEEE-754 components into the target type and apply
1107// appropriate rounding.
1108// sign: 0 = positive, 1 = negative
1109// exponent: Unbiased IEEE-754 exponent.
1110// mantissa: The mantissa of the input. The top bit (which is not encoded for
1111// normal IEEE-754 values) must not be omitted. This bit has the
1112// value 'pow(2, exponent)'.
1113//
1114// The input value is assumed to be a normalized value. That is, the input may
1115// not be infinity or NaN. If the source value is subnormal, it must be
1116// normalized before calling this function such that the highest set bit in the
1117// mantissa has the value 'pow(2, exponent)'.
1118//
1119// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1120// calling a templated FPRound.
1121template <class T, int ebits, int mbits>
1122T FPRound(int64_t sign,
1123 int64_t exponent,
1124 uint64_t mantissa,
1125 FPRounding round_mode) {
1126 VIXL_ASSERT((sign == 0) || (sign == 1));
1127
1128 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1129 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1130
1131 // Rounding can promote subnormals to normals, and normals to infinities. For
1132 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1133 // encodable as a float, but rounding based on the low-order mantissa bits
1134 // could make it overflow. With ties-to-even rounding, this value would become
1135 // an infinity.
1136
1137 // ---- Rounding Method ----
1138 //
1139 // The exponent is irrelevant in the rounding operation, so we treat the
1140 // lowest-order bit that will fit into the result ('onebit') as having
1141 // the value '1'. Similarly, the highest-order bit that won't fit into
1142 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1143 // 'onebit' and 'halfbit':
1144 //
1145 // These bits fit into the result.
1146 // |---------------------|
1147 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1148 // ||
1149 // / |
1150 // / halfbit
1151 // onebit
1152 //
1153 // For subnormal outputs, the range of representable bits is smaller and
1154 // the position of onebit and halfbit depends on the exponent of the
1155 // input, but the method is otherwise similar.
1156 //
1157 // onebit(frac)
1158 // |
1159 // | halfbit(frac) halfbit(adjusted)
1160 // | / /
1161 // | | |
1162 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1163 // 0b00.0... -> 0b00.0... -> 0b00
1164 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1165 // 0b00.1... -> 0b00.1... -> 0b01
1166 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1167 // 0b01.0... -> 0b01.0... -> 0b01
1168 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1169 // 0b01.1... -> 0b01.1... -> 0b10
1170 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1171 // 0b10.0... -> 0b10.0... -> 0b10
1172 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1173 // 0b10.1... -> 0b10.1... -> 0b11
1174 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1175 // ... / | / |
1176 // / | / |
1177 // / |
1178 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1179 //
1180 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1181
1182 static const int mantissa_offset = 0;
1183 static const int exponent_offset = mantissa_offset + mbits;
1184 static const int sign_offset = exponent_offset + ebits;
1185 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1186
1187 // Bail out early for zero inputs.
1188 if (mantissa == 0) {
1189 return static_cast<T>(sign << sign_offset);
1190 }
1191
1192 // If all bits in the exponent are set, the value is infinite or NaN.
1193 // This is true for all binary IEEE-754 formats.
1194 static const int infinite_exponent = (1 << ebits) - 1;
1195 static const int max_normal_exponent = infinite_exponent - 1;
1196
1197 // Apply the exponent bias to encode it for the result. Doing this early makes
1198 // it easy to detect values that will be infinite or subnormal.
1199 exponent += max_normal_exponent >> 1;
1200
1201 if (exponent > max_normal_exponent) {
1202 // Overflow: the input is too large for the result type to represent.
1203 if (round_mode == FPTieEven) {
1204 // FPTieEven rounding mode handles overflows using infinities.
1205 exponent = infinite_exponent;
1206 mantissa = 0;
1207 } else {
1208 VIXL_ASSERT(round_mode == FPRoundOdd);
1209 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1210 // normal number.
1211 exponent = max_normal_exponent;
1212 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1213 }
1214 return static_cast<T>((sign << sign_offset) |
1215 (exponent << exponent_offset) |
1216 (mantissa << mantissa_offset));
1217 }
1218
1219 // Calculate the shift required to move the top mantissa bit to the proper
1220 // place in the destination type.
1221 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1222 int shift = highest_significant_bit - mbits;
1223
1224 if (exponent <= 0) {
1225 // The output will be subnormal (before rounding).
1226 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1227 // is necessary because the exponent of a subnormal value (encoded as 0) is
1228 // the same as the exponent of the smallest normal value (encoded as 1).
Anton Kirilov088b01f2022-09-27 14:27:38 +01001229 shift += static_cast<int>(-exponent + 1);
Jacob Bramleyca789742018-09-13 14:25:46 +01001230
1231 // Handle inputs that would produce a zero output.
1232 //
1233 // Shifts higher than highest_significant_bit+1 will always produce a zero
1234 // result. A shift of exactly highest_significant_bit+1 might produce a
1235 // non-zero result after rounding.
1236 if (shift > (highest_significant_bit + 1)) {
1237 if (round_mode == FPTieEven) {
1238 // The result will always be +/-0.0.
1239 return static_cast<T>(sign << sign_offset);
1240 } else {
1241 VIXL_ASSERT(round_mode == FPRoundOdd);
1242 VIXL_ASSERT(mantissa != 0);
1243 // For FPRoundOdd, if the mantissa is too small to represent and
1244 // non-zero return the next "odd" value.
1245 return static_cast<T>((sign << sign_offset) | 1);
1246 }
1247 }
1248
1249 // Properly encode the exponent for a subnormal output.
1250 exponent = 0;
1251 } else {
1252 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1253 // normal values.
1254 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1255 }
1256
1257 // The casts below are only well-defined for unsigned integers.
1258 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1259 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1260
1261 if (shift > 0) {
1262 if (round_mode == FPTieEven) {
1263 // We have to shift the mantissa to the right. Some precision is lost, so
1264 // we need to apply rounding.
1265 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1266 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1267 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1268 uint64_t adjusted = mantissa - adjustment;
1269 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1270
1271 T result =
1272 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1273 ((mantissa >> shift) << mantissa_offset));
1274
1275 // A very large mantissa can overflow during rounding. If this happens,
1276 // the exponent should be incremented and the mantissa set to 1.0
1277 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1278 // has the nice side-effect that this case is handled for free.
1279 //
1280 // This also handles cases where a very large finite value overflows to
1281 // infinity, or where a very large subnormal value overflows to become
1282 // normal.
1283 return result + halfbit_adjusted;
1284 } else {
1285 VIXL_ASSERT(round_mode == FPRoundOdd);
1286 // If any bits at position halfbit or below are set, onebit (ie. the
1287 // bottom bit of the resulting mantissa) must be set.
1288 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1289 if (fractional_bits != 0) {
1290 mantissa |= UINT64_C(1) << shift;
1291 }
1292
1293 return static_cast<T>((sign << sign_offset) |
1294 (exponent << exponent_offset) |
1295 ((mantissa >> shift) << mantissa_offset));
1296 }
1297 } else {
1298 // We have to shift the mantissa to the left (or not at all). The input
1299 // mantissa is exactly representable in the output mantissa, so apply no
1300 // rounding correction.
1301 return static_cast<T>((sign << sign_offset) |
1302 (exponent << exponent_offset) |
1303 ((mantissa << -shift) << mantissa_offset));
1304 }
1305}
1306
1307
1308// See FPRound for a description of this function.
1309inline double FPRoundToDouble(int64_t sign,
1310 int64_t exponent,
1311 uint64_t mantissa,
1312 FPRounding round_mode) {
1313 uint64_t bits =
1314 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1315 exponent,
1316 mantissa,
1317 round_mode);
1318 return RawbitsToDouble(bits);
1319}
1320
1321
1322// See FPRound for a description of this function.
1323inline Float16 FPRoundToFloat16(int64_t sign,
1324 int64_t exponent,
1325 uint64_t mantissa,
1326 FPRounding round_mode) {
1327 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001328 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1329 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001330}
1331
1332
1333// See FPRound for a description of this function.
1334static inline float FPRoundToFloat(int64_t sign,
1335 int64_t exponent,
1336 uint64_t mantissa,
1337 FPRounding round_mode) {
1338 uint32_t bits =
1339 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1340 exponent,
1341 mantissa,
1342 round_mode);
1343 return RawbitsToFloat(bits);
1344}
1345
1346
1347float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1348float FPToFloat(double value,
1349 FPRounding round_mode,
1350 UseDefaultNaN DN,
1351 bool* exception = NULL);
1352
1353double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1354double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1355
1356Float16 FPToFloat16(float value,
1357 FPRounding round_mode,
1358 UseDefaultNaN DN,
1359 bool* exception = NULL);
1360
1361Float16 FPToFloat16(double value,
1362 FPRounding round_mode,
1363 UseDefaultNaN DN,
1364 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001365
1366// Like static_cast<T>(value), but with specialisations for the Float16 type.
1367template <typename T, typename F>
1368T StaticCastFPTo(F value) {
1369 return static_cast<T>(value);
1370}
1371
1372template <>
1373inline float StaticCastFPTo<float, Float16>(Float16 value) {
1374 return FPToFloat(value, kIgnoreDefaultNaN);
1375}
1376
1377template <>
1378inline double StaticCastFPTo<double, Float16>(Float16 value) {
1379 return FPToDouble(value, kIgnoreDefaultNaN);
1380}
1381
1382template <>
1383inline Float16 StaticCastFPTo<Float16, float>(float value) {
1384 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1385}
1386
1387template <>
1388inline Float16 StaticCastFPTo<Float16, double>(double value) {
1389 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1390}
1391
1392template <typename T>
1393uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1394 switch (size_in_bits) {
1395 case 16:
1396 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1397 case 32:
1398 return FloatToRawbits(StaticCastFPTo<float>(value));
1399 case 64:
1400 return DoubleToRawbits(StaticCastFPTo<double>(value));
1401 }
1402 VIXL_UNREACHABLE();
1403 return 0;
1404}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001405
1406template <typename T>
1407T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1408 VIXL_ASSERT(IsUintN(size_in_bits, value));
1409 switch (size_in_bits) {
1410 case 16:
1411 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1412 case 32:
1413 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1414 case 64:
1415 return StaticCastFPTo<T>(RawbitsToDouble(value));
1416 }
1417 VIXL_UNREACHABLE();
1418 return 0;
1419}
1420
Martyn Capewell6bf28752020-08-05 11:57:06 +01001421// Jenkins one-at-a-time hash, based on
1422// https://en.wikipedia.org/wiki/Jenkins_hash_function citing
1423// https://www.drdobbs.com/database/algorithm-alley/184410284.
1424constexpr uint32_t Hash(const char* str, uint32_t hash = 0) {
1425 if (*str == '\0') {
1426 hash += hash << 3;
1427 hash ^= hash >> 11;
1428 hash += hash << 15;
1429 return hash;
1430 } else {
1431 hash += *str;
1432 hash += hash << 10;
1433 hash ^= hash >> 6;
1434 return Hash(str + 1, hash);
1435 }
1436}
1437
Martyn Capewell8afff392022-04-19 18:08:39 +01001438constexpr uint32_t operator"" _h(const char* x, size_t) { return Hash(x); }
1439
armvixlad96eda2013-06-14 11:42:37 +01001440} // namespace vixl
1441
1442#endif // VIXL_UTILS_H