blob: f95f6c8b4b4456703f3cf3009a2503f1f4b0df21 [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
mmc28a7eb48c62024-06-25 10:12:38 +0100242inline uint64_t RotateLeft(uint64_t value,
243 unsigned int rotate,
244 unsigned int width) {
245 return RotateRight(value, width - rotate, width);
246}
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000247
Jacob Bramleyca789742018-09-13 14:25:46 +0100248// Wrapper class for passing FP16 values through the assembler.
249// This is purely to aid with type checking/casting.
250class Float16 {
251 public:
252 explicit Float16(double dvalue);
253 Float16() : rawbits_(0x0) {}
254 friend uint16_t Float16ToRawbits(Float16 value);
255 friend Float16 RawbitsToFloat16(uint16_t bits);
256
257 protected:
258 uint16_t rawbits_;
259};
260
armvixlf37fdc02014-02-05 13:22:16 +0000261// Floating point representation.
Jacob Bramleyca789742018-09-13 14:25:46 +0100262uint16_t Float16ToRawbits(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100263
264
Pierre Langlois88c46b82016-06-02 18:15:32 +0100265uint32_t FloatToRawbits(float value);
266VIXL_DEPRECATED("FloatToRawbits",
267 inline uint32_t float_to_rawbits(float value)) {
268 return FloatToRawbits(value);
269}
armvixlad96eda2013-06-14 11:42:37 +0100270
Pierre Langlois88c46b82016-06-02 18:15:32 +0100271uint64_t DoubleToRawbits(double value);
272VIXL_DEPRECATED("DoubleToRawbits",
273 inline uint64_t double_to_rawbits(double value)) {
274 return DoubleToRawbits(value);
275}
armvixl5289c592015-03-02 13:52:04 +0000276
Jacob Bramleyca789742018-09-13 14:25:46 +0100277Float16 RawbitsToFloat16(uint16_t bits);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100278
Pierre Langlois88c46b82016-06-02 18:15:32 +0100279float RawbitsToFloat(uint32_t bits);
280VIXL_DEPRECATED("RawbitsToFloat",
281 inline float rawbits_to_float(uint32_t bits)) {
282 return RawbitsToFloat(bits);
283}
284
285double RawbitsToDouble(uint64_t bits);
286VIXL_DEPRECATED("RawbitsToDouble",
287 inline double rawbits_to_double(uint64_t bits)) {
288 return RawbitsToDouble(bits);
289}
290
Anton Kirilov088b01f2022-09-27 14:27:38 +0100291// Some compilers dislike negating unsigned integers,
292// so we provide an equivalent.
293template <typename T>
294T UnsignedNegate(T value) {
295 VIXL_STATIC_ASSERT(std::is_unsigned<T>::value);
296 return ~value + 1;
297}
298
mmc28a16609c82024-06-18 13:41:59 +0100299template <typename T>
300bool CanBeNegated(T value) {
301 VIXL_STATIC_ASSERT(std::is_signed<T>::value);
302 return (value == std::numeric_limits<T>::min()) ? false : true;
303}
304
mmc28ab5c57c92023-03-16 16:26:31 +0000305// An absolute operation for signed integers that is defined for results outside
306// the representable range. Specifically, Abs(MIN_INT) is MIN_INT.
307template <typename T>
308T Abs(T val) {
309 // TODO: this static assertion is for signed integer inputs, as that's the
310 // only type tested. However, the code should work for all numeric inputs.
311 // Remove the assertion and this comment when more tests are available.
312 VIXL_STATIC_ASSERT(std::is_signed<T>::value && std::is_integral<T>::value);
313 return ((val >= -std::numeric_limits<T>::max()) && (val < 0)) ? -val : val;
314}
315
Jacob Bramley03c0b512019-02-22 16:42:06 +0000316// Convert unsigned to signed numbers in a well-defined way (using two's
317// complement representations).
318inline int64_t RawbitsToInt64(uint64_t bits) {
319 return (bits >= UINT64_C(0x8000000000000000))
Anton Kirilov088b01f2022-09-27 14:27:38 +0100320 ? (-static_cast<int64_t>(UnsignedNegate(bits) - 1) - 1)
Jacob Bramley03c0b512019-02-22 16:42:06 +0000321 : static_cast<int64_t>(bits);
322}
323
324inline int32_t RawbitsToInt32(uint32_t bits) {
Anton Kirilov088b01f2022-09-27 14:27:38 +0100325 return (bits >= UINT64_C(0x80000000))
326 ? (-static_cast<int32_t>(UnsignedNegate(bits) - 1) - 1)
327 : static_cast<int32_t>(bits);
Jacob Bramley03c0b512019-02-22 16:42:06 +0000328}
329
Jacob Bramleyca789742018-09-13 14:25:46 +0100330namespace internal {
331
332// Internal simulation class used solely by the simulator to
333// provide an abstraction layer for any half-precision arithmetic.
334class SimFloat16 : public Float16 {
335 public:
336 // TODO: We should investigate making this constructor explicit.
337 // This is currently difficult to do due to a number of templated
338 // functions in the simulator which rely on returning double values.
339 SimFloat16(double dvalue) : Float16(dvalue) {} // NOLINT(runtime/explicit)
340 SimFloat16(Float16 f) { // NOLINT(runtime/explicit)
341 this->rawbits_ = Float16ToRawbits(f);
342 }
343 SimFloat16() : Float16() {}
344 SimFloat16 operator-() const;
345 SimFloat16 operator+(SimFloat16 rhs) const;
346 SimFloat16 operator-(SimFloat16 rhs) const;
347 SimFloat16 operator*(SimFloat16 rhs) const;
348 SimFloat16 operator/(SimFloat16 rhs) const;
349 bool operator<(SimFloat16 rhs) const;
350 bool operator>(SimFloat16 rhs) const;
351 bool operator==(SimFloat16 rhs) const;
352 bool operator!=(SimFloat16 rhs) const;
Josh Sorefb43d6ef2022-08-03 12:47:14 -0400353 // This is necessary for conversions performed in (macro asm) Fmov.
Jacob Bramleyca789742018-09-13 14:25:46 +0100354 bool operator==(double rhs) const;
355 operator double() const;
356};
357} // namespace internal
358
359uint32_t Float16Sign(internal::SimFloat16 value);
360
361uint32_t Float16Exp(internal::SimFloat16 value);
362
363uint32_t Float16Mantissa(internal::SimFloat16 value);
364
Pierre Langlois88c46b82016-06-02 18:15:32 +0100365uint32_t FloatSign(float value);
366VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
367 return FloatSign(value);
368}
369
370uint32_t FloatExp(float value);
371VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
372 return FloatExp(value);
373}
374
375uint32_t FloatMantissa(float value);
376VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
377 return FloatMantissa(value);
378}
379
380uint32_t DoubleSign(double value);
381VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
382 return DoubleSign(value);
383}
384
385uint32_t DoubleExp(double value);
386VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
387 return DoubleExp(value);
388}
389
390uint64_t DoubleMantissa(double value);
391VIXL_DEPRECATED("DoubleMantissa",
392 inline uint64_t double_mantissa(double value)) {
393 return DoubleMantissa(value);
394}
395
Jacob Bramleyca789742018-09-13 14:25:46 +0100396internal::SimFloat16 Float16Pack(uint16_t sign,
397 uint16_t exp,
398 uint16_t mantissa);
399
Pierre Langlois88c46b82016-06-02 18:15:32 +0100400float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
401VIXL_DEPRECATED("FloatPack",
402 inline float float_pack(uint32_t sign,
403 uint32_t exp,
404 uint32_t mantissa)) {
405 return FloatPack(sign, exp, mantissa);
406}
407
408double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
409VIXL_DEPRECATED("DoublePack",
410 inline double double_pack(uint32_t sign,
411 uint32_t exp,
412 uint64_t mantissa)) {
413 return DoublePack(sign, exp, mantissa);
414}
armvixl5289c592015-03-02 13:52:04 +0000415
416// An fpclassify() function for 16-bit half-precision floats.
Jacob Bramleyca789742018-09-13 14:25:46 +0100417int Float16Classify(Float16 value);
418VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
419 return Float16Classify(RawbitsToFloat16(value));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100420}
armvixlf37fdc02014-02-05 13:22:16 +0000421
Jacob Bramleyca789742018-09-13 14:25:46 +0100422bool IsZero(Float16 value);
Carey Williamsd8bb3572018-04-10 11:58:07 +0100423
Jacob Bramleyf48172b2020-07-13 14:47:15 +0100424inline bool IsPositiveZero(double value) {
425 return (value == 0.0) && (copysign(1.0, value) > 0.0);
426}
427
Jacob Bramleyca789742018-09-13 14:25:46 +0100428inline bool IsNaN(float value) { return std::isnan(value); }
429
430inline bool IsNaN(double value) { return std::isnan(value); }
431
432inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
433
434inline bool IsInf(float value) { return std::isinf(value); }
435
436inline bool IsInf(double value) { return std::isinf(value); }
437
438inline bool IsInf(Float16 value) {
439 return Float16Classify(value) == FP_INFINITE;
440}
Carey Williamsd8bb3572018-04-10 11:58:07 +0100441
442
armvixlf37fdc02014-02-05 13:22:16 +0000443// NaN tests.
444inline bool IsSignallingNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100445 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100446 uint64_t raw = DoubleToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100447 if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000448 return true;
449 }
450 return false;
451}
452
453
454inline bool IsSignallingNaN(float num) {
armvixlb0c8ae22014-03-21 14:03:59 +0000455 const uint32_t kFP32QuietNaNMask = 0x00400000;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100456 uint32_t raw = FloatToRawbits(num);
Jacob Bramleyca789742018-09-13 14:25:46 +0100457 if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
armvixlf37fdc02014-02-05 13:22:16 +0000458 return true;
459 }
460 return false;
461}
462
463
Jacob Bramleyca789742018-09-13 14:25:46 +0100464inline bool IsSignallingNaN(Float16 num) {
armvixl5289c592015-03-02 13:52:04 +0000465 const uint16_t kFP16QuietNaNMask = 0x0200;
Jacob Bramleyca789742018-09-13 14:25:46 +0100466 return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
armvixl5289c592015-03-02 13:52:04 +0000467}
468
469
armvixlf37fdc02014-02-05 13:22:16 +0000470template <typename T>
471inline bool IsQuietNaN(T num) {
Jacob Bramleyca789742018-09-13 14:25:46 +0100472 return IsNaN(num) && !IsSignallingNaN(num);
armvixlf37fdc02014-02-05 13:22:16 +0000473}
474
475
armvixlb0c8ae22014-03-21 14:03:59 +0000476// Convert the NaN in 'num' to a quiet NaN.
477inline double ToQuietNaN(double num) {
armvixl5799d6c2014-05-01 11:05:00 +0100478 const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
Jacob Bramleyca789742018-09-13 14:25:46 +0100479 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100480 return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000481}
482
483
484inline float ToQuietNaN(float num) {
485 const uint32_t kFP32QuietNaNMask = 0x00400000;
Jacob Bramleyca789742018-09-13 14:25:46 +0100486 VIXL_ASSERT(IsNaN(num));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100487 return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
armvixlb0c8ae22014-03-21 14:03:59 +0000488}
489
490
Jacob Bramleyca789742018-09-13 14:25:46 +0100491inline internal::SimFloat16 ToQuietNaN(internal::SimFloat16 num) {
492 const uint16_t kFP16QuietNaNMask = 0x0200;
493 VIXL_ASSERT(IsNaN(num));
494 return internal::SimFloat16(
495 RawbitsToFloat16(Float16ToRawbits(num) | kFP16QuietNaNMask));
496}
497
498
armvixlb0c8ae22014-03-21 14:03:59 +0000499// Fused multiply-add.
500inline double FusedMultiplyAdd(double op1, double op2, double a) {
501 return fma(op1, op2, a);
502}
503
504
505inline float FusedMultiplyAdd(float op1, float op2, float a) {
506 return fmaf(op1, op2, a);
507}
508
509
Anton Kirilov088b01f2022-09-27 14:27:38 +0100510inline uint64_t LowestSetBit(uint64_t value) {
511 return value & UnsignedNegate(value);
512}
armvixl6e2c8272015-03-31 11:04:14 +0100513
514
armvixl0f35e362016-05-10 13:57:58 +0100515template <typename T>
armvixl6e2c8272015-03-31 11:04:14 +0100516inline int HighestSetBitPosition(T value) {
517 VIXL_ASSERT(value != 0);
518 return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
519}
520
521
armvixl0f35e362016-05-10 13:57:58 +0100522template <typename V>
armvixl6e2c8272015-03-31 11:04:14 +0100523inline int WhichPowerOf2(V value) {
524 VIXL_ASSERT(IsPowerOf2(value));
525 return CountTrailingZeros(value);
526}
armvixlad96eda2013-06-14 11:42:37 +0100527
armvixldb644342015-07-21 11:37:10 +0100528
armvixl330dc712014-11-25 10:38:32 +0000529unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
530
armvixldb644342015-07-21 11:37:10 +0100531
Pierre Langlois88c46b82016-06-02 18:15:32 +0100532int BitCount(uint64_t value);
533
534
armvixldb644342015-07-21 11:37:10 +0100535template <typename T>
536T ReverseBits(T value) {
537 VIXL_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
538 (sizeof(value) == 4) || (sizeof(value) == 8));
539 T result = 0;
540 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
541 result = (result << 1) | (value & 1);
542 value >>= 1;
543 }
544 return result;
545}
546
547
548template <typename T>
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000549inline T SignExtend(T val, int size_in_bits) {
550 VIXL_ASSERT(size_in_bits > 0);
551 T mask = (T(2) << (size_in_bits - 1)) - T(1);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100552 val &= mask;
Jacob Bramleyacd32aa2019-12-12 18:08:20 +0000553 T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
Vincent Belliard4e52d4d2018-04-03 13:34:44 -0700554 val |= sign_bits;
Pierre Langlois88c46b82016-06-02 18:15:32 +0100555 return val;
556}
557
558
559template <typename T>
armvixldb644342015-07-21 11:37:10 +0100560T ReverseBytes(T value, int block_bytes_log2) {
561 VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
562 VIXL_ASSERT((1U << block_bytes_log2) <= sizeof(value));
563 // Split the 64-bit value into an 8-bit array, where b[0] is the least
564 // significant byte, and b[7] is the most significant.
565 uint8_t bytes[8];
armvixl788c84f2015-12-08 17:05:23 +0000566 uint64_t mask = UINT64_C(0xff00000000000000);
armvixldb644342015-07-21 11:37:10 +0100567 for (int i = 7; i >= 0; i--) {
568 bytes[i] = (static_cast<uint64_t>(value) & mask) >> (i * 8);
569 mask >>= 8;
570 }
571
572 // Permutation tables for REV instructions.
573 // permute_table[0] is used by REV16_x, REV16_w
574 // permute_table[1] is used by REV32_x, REV_w
575 // permute_table[2] is used by REV_x
576 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100577 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
578 {4, 5, 6, 7, 0, 1, 2, 3},
579 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000580 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100581 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000582 temp <<= 8;
583 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100584 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000585
586 T result;
587 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
588 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100589 return result;
590}
591
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700592template <unsigned MULTIPLE, typename T>
593inline bool IsMultiple(T value) {
594 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
595 return (value & (MULTIPLE - 1)) == 0;
596}
armvixldb644342015-07-21 11:37:10 +0100597
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000598template <typename T>
599inline bool IsMultiple(T value, unsigned multiple) {
600 VIXL_ASSERT(IsPowerOf2(multiple));
601 return (value & (multiple - 1)) == 0;
602}
603
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000604template <typename T>
605inline bool IsAligned(T pointer, int alignment) {
606 VIXL_ASSERT(IsPowerOf2(alignment));
607 return (pointer & (alignment - 1)) == 0;
608}
609
armvixlad96eda2013-06-14 11:42:37 +0100610// Pointer alignment
611// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100612template <unsigned ALIGN, typename T>
613inline bool IsAligned(T pointer) {
614 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100615 // Use C-style casts to get static_cast behaviour for integral types (T), and
616 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000617 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100618}
619
armvixl0f35e362016-05-10 13:57:58 +0100620template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100621bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100622 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100623}
624
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100625// Increment a pointer until it has the specified alignment. The alignment must
626// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100627template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100628T AlignUp(T pointer,
629 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100630 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100631 // Use C-style casts to get static_cast behaviour for integral types (T), and
632 // reinterpret_cast behaviour for other types.
633
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100634 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000635 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000636 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100637
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100638 size_t mask = alignment - 1;
639 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000640 VIXL_ASSERT(result >= pointer);
641
642 return result;
armvixlad96eda2013-06-14 11:42:37 +0100643}
644
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100645// Decrement a pointer until it has the specified alignment. The alignment must
646// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100647template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100648T AlignDown(T pointer,
649 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100650 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100651 // Use C-style casts to get static_cast behaviour for integral types (T), and
652 // reinterpret_cast behaviour for other types.
653
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100654 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000655 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000656 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100657
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100658 size_t mask = alignment - 1;
659 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000660}
661
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100662
Pierre Langlois88c46b82016-06-02 18:15:32 +0100663template <typename T>
664inline T ExtractBit(T value, unsigned bit) {
665 return (value >> bit) & T(1);
666}
667
668template <typename Ts, typename Td>
669inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
670 return Td((value >> least_significant_bit) & Ts(mask));
671}
672
673template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800674inline void AssignBit(Td& dst, // NOLINT(runtime/references)
675 int bit,
676 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100677 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
678 VIXL_ASSERT(bit >= 0);
679 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
680 Td mask(1);
681 dst &= ~(mask << bit);
682 dst |= Td(value) << bit;
683}
684
685template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800686inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100687 int least_significant_bit,
688 Ts mask,
689 Ts value) {
690 VIXL_ASSERT(least_significant_bit >= 0);
691 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
692 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
693 Td(mask));
694 VIXL_ASSERT((value & mask) == value);
695 dst &= ~(Td(mask) << least_significant_bit);
696 dst |= Td(value) << least_significant_bit;
697}
698
699class VFP {
700 public:
701 static uint32_t FP32ToImm8(float imm) {
702 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
703 uint32_t bits = FloatToRawbits(imm);
704 // bit7: a000.0000
705 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
706 // bit6: 0b00.0000
707 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
708 // bit5_to_0: 00cd.efgh
709 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
710 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
711 }
712 static uint32_t FP64ToImm8(double imm) {
713 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
714 // 0000.0000.0000.0000.0000.0000.0000.0000
715 uint64_t bits = DoubleToRawbits(imm);
716 // bit7: a000.0000
717 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
718 // bit6: 0b00.0000
719 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
720 // bit5_to_0: 00cd.efgh
721 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
722
723 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
724 }
725 static float Imm8ToFP32(uint32_t imm8) {
726 // Imm8: abcdefgh (8 bits)
727 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
728 // where B is b ^ 1
729 uint32_t bits = imm8;
730 uint32_t bit7 = (bits >> 7) & 0x1;
731 uint32_t bit6 = (bits >> 6) & 0x1;
732 uint32_t bit5_to_0 = bits & 0x3f;
733 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
734
735 return RawbitsToFloat(result);
736 }
737 static double Imm8ToFP64(uint32_t imm8) {
738 // Imm8: abcdefgh (8 bits)
739 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
740 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
741 // where B is b ^ 1
742 uint32_t bits = imm8;
743 uint64_t bit7 = (bits >> 7) & 0x1;
744 uint64_t bit6 = (bits >> 6) & 0x1;
745 uint64_t bit5_to_0 = bits & 0x3f;
746 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
747 return RawbitsToDouble(result);
748 }
749 static bool IsImmFP32(float imm) {
750 // Valid values will have the form:
751 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
752 uint32_t bits = FloatToRawbits(imm);
753 // bits[19..0] are cleared.
754 if ((bits & 0x7ffff) != 0) {
755 return false;
756 }
757
758
759 // bits[29..25] are all set or all cleared.
760 uint32_t b_pattern = (bits >> 16) & 0x3e00;
761 if (b_pattern != 0 && b_pattern != 0x3e00) {
762 return false;
763 }
764 // bit[30] and bit[29] are opposite.
765 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
766 return false;
767 }
768 return true;
769 }
770 static bool IsImmFP64(double imm) {
771 // Valid values will have the form:
772 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
773 // 0000.0000.0000.0000.0000.0000.0000.0000
774 uint64_t bits = DoubleToRawbits(imm);
775 // bits[47..0] are cleared.
776 if ((bits & 0x0000ffffffffffff) != 0) {
777 return false;
778 }
779 // bits[61..54] are all set or all cleared.
780 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
781 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
782 return false;
783 }
784 // bit[62] and bit[61] are opposite.
785 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
786 return false;
787 }
788 return true;
789 }
790};
791
792class BitField {
793 // ForEachBitHelper is a functor that will call
794 // bool ForEachBitHelper::execute(ElementType id) const
795 // and expects a boolean in return whether to continue (if true)
796 // or stop (if false)
797 // check_set will check if the bits are on (true) or off(false)
798 template <typename ForEachBitHelper, bool check_set>
799 bool ForEachBit(const ForEachBitHelper& helper) {
800 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
801 if (bitfield_[i] == check_set)
802 if (!helper.execute(i)) return false;
803 }
804 return true;
805 }
806
807 public:
808 explicit BitField(unsigned size) : bitfield_(size, 0) {}
809
810 void Set(int i) {
811 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
812 bitfield_[i] = true;
813 }
814
815 void Unset(int i) {
816 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
817 bitfield_[i] = true;
818 }
819
820 bool IsSet(int i) const { return bitfield_[i]; }
821
822 // For each bit not set in the bitfield call the execute functor
823 // execute.
824 // ForEachBitSetHelper::execute returns true if the iteration through
825 // the bits can continue, otherwise it will stop.
826 // struct ForEachBitSetHelper {
827 // bool execute(int /*id*/) { return false; }
828 // };
829 template <typename ForEachBitNotSetHelper>
830 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
831 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
832 }
833
834 // For each bit set in the bitfield call the execute functor
835 // execute.
836 template <typename ForEachBitSetHelper>
837 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
838 return ForEachBit<ForEachBitSetHelper, true>(helper);
839 }
840
841 private:
842 std::vector<bool> bitfield_;
843};
844
Pierre Langloisd82faf62018-04-04 13:06:58 +0100845namespace internal {
846
Pierre Langlois88c46b82016-06-02 18:15:32 +0100847typedef int64_t Int64;
848class Uint64;
849class Uint128;
850
851class Uint32 {
852 uint32_t data_;
853
854 public:
855 // Unlike uint32_t, Uint32 has a default constructor.
856 Uint32() { data_ = 0; }
857 explicit Uint32(uint32_t data) : data_(data) {}
858 inline explicit Uint32(Uint64 data);
859 uint32_t Get() const { return data_; }
860 template <int N>
861 int32_t GetSigned() const {
862 return ExtractSignedBitfield32(N - 1, 0, data_);
863 }
864 int32_t GetSigned() const { return data_; }
865 Uint32 operator~() const { return Uint32(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100866 Uint32 operator-() const { return Uint32(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100867 bool operator==(Uint32 value) const { return data_ == value.data_; }
868 bool operator!=(Uint32 value) const { return data_ != value.data_; }
869 bool operator>(Uint32 value) const { return data_ > value.data_; }
870 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
871 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
872 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
873 Uint32 operator&=(Uint32 value) {
874 data_ &= value.data_;
875 return *this;
876 }
877 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
878 Uint32 operator^=(Uint32 value) {
879 data_ ^= value.data_;
880 return *this;
881 }
882 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
883 Uint32 operator|=(Uint32 value) {
884 data_ |= value.data_;
885 return *this;
886 }
887 // Unlike uint32_t, the shift functions can accept negative shift and
888 // return 0 when the shift is too big.
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 Uint32 operator<<(int shift) const {
901 if (shift == 0) return *this;
902 if (shift < 0) {
903 int tmp = -shift;
904 if (tmp >= 32) return Uint32(0);
905 return Uint32(data_ >> tmp);
906 }
907 int tmp = shift;
908 if (tmp >= 32) return Uint32(0);
909 return Uint32(data_ << tmp);
910 }
911};
912
913class Uint64 {
914 uint64_t data_;
915
916 public:
917 // Unlike uint64_t, Uint64 has a default constructor.
918 Uint64() { data_ = 0; }
919 explicit Uint64(uint64_t data) : data_(data) {}
920 explicit Uint64(Uint32 data) : data_(data.Get()) {}
921 inline explicit Uint64(Uint128 data);
922 uint64_t Get() const { return data_; }
923 int64_t GetSigned(int N) const {
924 return ExtractSignedBitfield64(N - 1, 0, data_);
925 }
926 int64_t GetSigned() const { return data_; }
927 Uint32 ToUint32() const {
928 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100929 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100930 }
931 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100932 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100933 Uint64 operator~() const { return Uint64(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100934 Uint64 operator-() const { return Uint64(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100935 bool operator==(Uint64 value) const { return data_ == value.data_; }
936 bool operator!=(Uint64 value) const { return data_ != value.data_; }
937 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
938 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
939 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
940 Uint64 operator&=(Uint64 value) {
941 data_ &= value.data_;
942 return *this;
943 }
944 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
945 Uint64 operator^=(Uint64 value) {
946 data_ ^= value.data_;
947 return *this;
948 }
949 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
950 Uint64 operator|=(Uint64 value) {
951 data_ |= value.data_;
952 return *this;
953 }
954 // Unlike uint64_t, the shift functions can accept negative shift and
955 // return 0 when the shift is too big.
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 Uint64 operator<<(int shift) const {
968 if (shift == 0) return *this;
969 if (shift < 0) {
970 int tmp = -shift;
971 if (tmp >= 64) return Uint64(0);
972 return Uint64(data_ >> tmp);
973 }
974 int tmp = shift;
975 if (tmp >= 64) return Uint64(0);
976 return Uint64(data_ << tmp);
977 }
978};
979
980class Uint128 {
981 uint64_t data_high_;
982 uint64_t data_low_;
983
984 public:
985 Uint128() : data_high_(0), data_low_(0) {}
986 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
987 explicit Uint128(Uint64 data_low)
988 : data_high_(0), data_low_(data_low.Get()) {}
989 Uint128(uint64_t data_high, uint64_t data_low)
990 : data_high_(data_high), data_low_(data_low) {}
991 Uint64 ToUint64() const {
992 VIXL_ASSERT(data_high_ == 0);
993 return Uint64(data_low_);
994 }
995 Uint64 GetHigh64() const { return Uint64(data_high_); }
996 Uint64 GetLow64() const { return Uint64(data_low_); }
997 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
998 bool operator==(Uint128 value) const {
999 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
1000 }
1001 Uint128 operator&(Uint128 value) const {
1002 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
1003 }
1004 Uint128 operator&=(Uint128 value) {
1005 data_high_ &= value.data_high_;
1006 data_low_ &= value.data_low_;
1007 return *this;
1008 }
1009 Uint128 operator|=(Uint128 value) {
1010 data_high_ |= value.data_high_;
1011 data_low_ |= value.data_low_;
1012 return *this;
1013 }
1014 Uint128 operator>>(int shift) const {
1015 VIXL_ASSERT((shift >= 0) && (shift < 128));
1016 if (shift == 0) return *this;
1017 if (shift >= 64) {
1018 return Uint128(0, data_high_ >> (shift - 64));
1019 }
1020 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
1021 return Uint128(data_high_ >> shift, tmp);
1022 }
1023 Uint128 operator<<(int shift) const {
1024 VIXL_ASSERT((shift >= 0) && (shift < 128));
1025 if (shift == 0) return *this;
1026 if (shift >= 64) {
1027 return Uint128(data_low_ << (shift - 64), 0);
1028 }
1029 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
1030 return Uint128(tmp, data_low_ << shift);
1031 }
1032};
1033
1034Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
1035Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1036
1037Int64 BitCount(Uint32 value);
1038
TatWai Chong13634762019-07-16 16:20:45 -07001039// The algorithm used is adapted from the one described in section 8.2 of
1040// Hacker's Delight, by Henry S. Warren, Jr.
1041template <unsigned N, typename T>
1042int64_t MultiplyHigh(T u, T v) {
1043 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1044 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1045 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1046 uint64_t sign_ext = 0;
1047 unsigned half_bits = N / 2;
1048 uint64_t half_mask = GetUintMask(half_bits);
1049 if (std::numeric_limits<T>::is_signed) {
1050 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1051 }
1052
1053 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1054 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1055
1056 u0 = u & half_mask;
1057 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1058 v0 = v & half_mask;
1059 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1060
1061 w0 = u0 * v0;
1062 t = u1 * v0 + (w0 >> half_bits);
1063
1064 w1 = t & half_mask;
1065 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1066 w1 = u0 * v1 + w1;
1067 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1068
1069 uint64_t value = u1 * v1 + w2 + w1;
1070 int64_t result;
1071 memcpy(&result, &value, sizeof(result));
1072 return result;
1073}
1074
Pierre Langloisd82faf62018-04-04 13:06:58 +01001075} // namespace internal
1076
Jacob Bramleyca789742018-09-13 14:25:46 +01001077// The default NaN values (for FPCR.DN=1).
1078extern const double kFP64DefaultNaN;
1079extern const float kFP32DefaultNaN;
1080extern const Float16 kFP16DefaultNaN;
1081
1082// Floating-point infinity values.
1083extern const Float16 kFP16PositiveInfinity;
1084extern const Float16 kFP16NegativeInfinity;
1085extern const float kFP32PositiveInfinity;
1086extern const float kFP32NegativeInfinity;
1087extern const double kFP64PositiveInfinity;
1088extern const double kFP64NegativeInfinity;
1089
1090// Floating-point zero values.
1091extern const Float16 kFP16PositiveZero;
1092extern const Float16 kFP16NegativeZero;
1093
1094// AArch64 floating-point specifics. These match IEEE-754.
1095const unsigned kDoubleMantissaBits = 52;
1096const unsigned kDoubleExponentBits = 11;
1097const unsigned kFloatMantissaBits = 23;
1098const unsigned kFloatExponentBits = 8;
1099const unsigned kFloat16MantissaBits = 10;
1100const unsigned kFloat16ExponentBits = 5;
1101
1102enum FPRounding {
1103 // The first four values are encodable directly by FPCR<RMode>.
1104 FPTieEven = 0x0,
1105 FPPositiveInfinity = 0x1,
1106 FPNegativeInfinity = 0x2,
1107 FPZero = 0x3,
1108
1109 // The final rounding modes are only available when explicitly specified by
1110 // the instruction (such as with fcvta). It cannot be set in FPCR.
1111 FPTieAway,
1112 FPRoundOdd
1113};
1114
1115enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1116
1117// Assemble the specified IEEE-754 components into the target type and apply
1118// appropriate rounding.
1119// sign: 0 = positive, 1 = negative
1120// exponent: Unbiased IEEE-754 exponent.
1121// mantissa: The mantissa of the input. The top bit (which is not encoded for
1122// normal IEEE-754 values) must not be omitted. This bit has the
1123// value 'pow(2, exponent)'.
1124//
1125// The input value is assumed to be a normalized value. That is, the input may
1126// not be infinity or NaN. If the source value is subnormal, it must be
1127// normalized before calling this function such that the highest set bit in the
1128// mantissa has the value 'pow(2, exponent)'.
1129//
1130// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1131// calling a templated FPRound.
1132template <class T, int ebits, int mbits>
1133T FPRound(int64_t sign,
1134 int64_t exponent,
1135 uint64_t mantissa,
1136 FPRounding round_mode) {
1137 VIXL_ASSERT((sign == 0) || (sign == 1));
1138
1139 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1140 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1141
1142 // Rounding can promote subnormals to normals, and normals to infinities. For
1143 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1144 // encodable as a float, but rounding based on the low-order mantissa bits
1145 // could make it overflow. With ties-to-even rounding, this value would become
1146 // an infinity.
1147
1148 // ---- Rounding Method ----
1149 //
1150 // The exponent is irrelevant in the rounding operation, so we treat the
1151 // lowest-order bit that will fit into the result ('onebit') as having
1152 // the value '1'. Similarly, the highest-order bit that won't fit into
1153 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1154 // 'onebit' and 'halfbit':
1155 //
1156 // These bits fit into the result.
1157 // |---------------------|
1158 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1159 // ||
1160 // / |
1161 // / halfbit
1162 // onebit
1163 //
1164 // For subnormal outputs, the range of representable bits is smaller and
1165 // the position of onebit and halfbit depends on the exponent of the
1166 // input, but the method is otherwise similar.
1167 //
1168 // onebit(frac)
1169 // |
1170 // | halfbit(frac) halfbit(adjusted)
1171 // | / /
1172 // | | |
1173 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1174 // 0b00.0... -> 0b00.0... -> 0b00
1175 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1176 // 0b00.1... -> 0b00.1... -> 0b01
1177 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1178 // 0b01.0... -> 0b01.0... -> 0b01
1179 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1180 // 0b01.1... -> 0b01.1... -> 0b10
1181 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1182 // 0b10.0... -> 0b10.0... -> 0b10
1183 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1184 // 0b10.1... -> 0b10.1... -> 0b11
1185 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1186 // ... / | / |
1187 // / | / |
1188 // / |
1189 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1190 //
1191 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1192
1193 static const int mantissa_offset = 0;
1194 static const int exponent_offset = mantissa_offset + mbits;
1195 static const int sign_offset = exponent_offset + ebits;
1196 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1197
1198 // Bail out early for zero inputs.
1199 if (mantissa == 0) {
1200 return static_cast<T>(sign << sign_offset);
1201 }
1202
1203 // If all bits in the exponent are set, the value is infinite or NaN.
1204 // This is true for all binary IEEE-754 formats.
1205 static const int infinite_exponent = (1 << ebits) - 1;
1206 static const int max_normal_exponent = infinite_exponent - 1;
1207
1208 // Apply the exponent bias to encode it for the result. Doing this early makes
1209 // it easy to detect values that will be infinite or subnormal.
1210 exponent += max_normal_exponent >> 1;
1211
1212 if (exponent > max_normal_exponent) {
1213 // Overflow: the input is too large for the result type to represent.
1214 if (round_mode == FPTieEven) {
1215 // FPTieEven rounding mode handles overflows using infinities.
1216 exponent = infinite_exponent;
1217 mantissa = 0;
1218 } else {
1219 VIXL_ASSERT(round_mode == FPRoundOdd);
1220 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1221 // normal number.
1222 exponent = max_normal_exponent;
1223 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1224 }
1225 return static_cast<T>((sign << sign_offset) |
1226 (exponent << exponent_offset) |
1227 (mantissa << mantissa_offset));
1228 }
1229
1230 // Calculate the shift required to move the top mantissa bit to the proper
1231 // place in the destination type.
1232 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1233 int shift = highest_significant_bit - mbits;
1234
1235 if (exponent <= 0) {
1236 // The output will be subnormal (before rounding).
1237 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1238 // is necessary because the exponent of a subnormal value (encoded as 0) is
1239 // the same as the exponent of the smallest normal value (encoded as 1).
Anton Kirilov088b01f2022-09-27 14:27:38 +01001240 shift += static_cast<int>(-exponent + 1);
Jacob Bramleyca789742018-09-13 14:25:46 +01001241
1242 // Handle inputs that would produce a zero output.
1243 //
1244 // Shifts higher than highest_significant_bit+1 will always produce a zero
1245 // result. A shift of exactly highest_significant_bit+1 might produce a
1246 // non-zero result after rounding.
1247 if (shift > (highest_significant_bit + 1)) {
1248 if (round_mode == FPTieEven) {
1249 // The result will always be +/-0.0.
1250 return static_cast<T>(sign << sign_offset);
1251 } else {
1252 VIXL_ASSERT(round_mode == FPRoundOdd);
1253 VIXL_ASSERT(mantissa != 0);
1254 // For FPRoundOdd, if the mantissa is too small to represent and
1255 // non-zero return the next "odd" value.
1256 return static_cast<T>((sign << sign_offset) | 1);
1257 }
1258 }
1259
1260 // Properly encode the exponent for a subnormal output.
1261 exponent = 0;
1262 } else {
1263 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1264 // normal values.
1265 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1266 }
1267
1268 // The casts below are only well-defined for unsigned integers.
1269 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1270 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1271
1272 if (shift > 0) {
1273 if (round_mode == FPTieEven) {
1274 // We have to shift the mantissa to the right. Some precision is lost, so
1275 // we need to apply rounding.
1276 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1277 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1278 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1279 uint64_t adjusted = mantissa - adjustment;
1280 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1281
1282 T result =
1283 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1284 ((mantissa >> shift) << mantissa_offset));
1285
1286 // A very large mantissa can overflow during rounding. If this happens,
1287 // the exponent should be incremented and the mantissa set to 1.0
1288 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1289 // has the nice side-effect that this case is handled for free.
1290 //
1291 // This also handles cases where a very large finite value overflows to
1292 // infinity, or where a very large subnormal value overflows to become
1293 // normal.
1294 return result + halfbit_adjusted;
1295 } else {
1296 VIXL_ASSERT(round_mode == FPRoundOdd);
1297 // If any bits at position halfbit or below are set, onebit (ie. the
1298 // bottom bit of the resulting mantissa) must be set.
1299 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1300 if (fractional_bits != 0) {
1301 mantissa |= UINT64_C(1) << shift;
1302 }
1303
1304 return static_cast<T>((sign << sign_offset) |
1305 (exponent << exponent_offset) |
1306 ((mantissa >> shift) << mantissa_offset));
1307 }
1308 } else {
1309 // We have to shift the mantissa to the left (or not at all). The input
1310 // mantissa is exactly representable in the output mantissa, so apply no
1311 // rounding correction.
1312 return static_cast<T>((sign << sign_offset) |
1313 (exponent << exponent_offset) |
1314 ((mantissa << -shift) << mantissa_offset));
1315 }
1316}
1317
1318
1319// See FPRound for a description of this function.
1320inline double FPRoundToDouble(int64_t sign,
1321 int64_t exponent,
1322 uint64_t mantissa,
1323 FPRounding round_mode) {
1324 uint64_t bits =
1325 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1326 exponent,
1327 mantissa,
1328 round_mode);
1329 return RawbitsToDouble(bits);
1330}
1331
1332
1333// See FPRound for a description of this function.
1334inline Float16 FPRoundToFloat16(int64_t sign,
1335 int64_t exponent,
1336 uint64_t mantissa,
1337 FPRounding round_mode) {
1338 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001339 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1340 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001341}
1342
1343
1344// See FPRound for a description of this function.
1345static inline float FPRoundToFloat(int64_t sign,
1346 int64_t exponent,
1347 uint64_t mantissa,
1348 FPRounding round_mode) {
1349 uint32_t bits =
1350 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1351 exponent,
1352 mantissa,
1353 round_mode);
1354 return RawbitsToFloat(bits);
1355}
1356
1357
1358float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1359float FPToFloat(double value,
1360 FPRounding round_mode,
1361 UseDefaultNaN DN,
1362 bool* exception = NULL);
1363
1364double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1365double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1366
1367Float16 FPToFloat16(float value,
1368 FPRounding round_mode,
1369 UseDefaultNaN DN,
1370 bool* exception = NULL);
1371
1372Float16 FPToFloat16(double value,
1373 FPRounding round_mode,
1374 UseDefaultNaN DN,
1375 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001376
1377// Like static_cast<T>(value), but with specialisations for the Float16 type.
1378template <typename T, typename F>
1379T StaticCastFPTo(F value) {
1380 return static_cast<T>(value);
1381}
1382
1383template <>
1384inline float StaticCastFPTo<float, Float16>(Float16 value) {
1385 return FPToFloat(value, kIgnoreDefaultNaN);
1386}
1387
1388template <>
1389inline double StaticCastFPTo<double, Float16>(Float16 value) {
1390 return FPToDouble(value, kIgnoreDefaultNaN);
1391}
1392
1393template <>
1394inline Float16 StaticCastFPTo<Float16, float>(float value) {
1395 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1396}
1397
1398template <>
1399inline Float16 StaticCastFPTo<Float16, double>(double value) {
1400 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1401}
1402
1403template <typename T>
1404uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1405 switch (size_in_bits) {
1406 case 16:
1407 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1408 case 32:
1409 return FloatToRawbits(StaticCastFPTo<float>(value));
1410 case 64:
1411 return DoubleToRawbits(StaticCastFPTo<double>(value));
1412 }
1413 VIXL_UNREACHABLE();
1414 return 0;
1415}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001416
1417template <typename T>
1418T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1419 VIXL_ASSERT(IsUintN(size_in_bits, value));
1420 switch (size_in_bits) {
1421 case 16:
1422 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1423 case 32:
1424 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1425 case 64:
1426 return StaticCastFPTo<T>(RawbitsToDouble(value));
1427 }
1428 VIXL_UNREACHABLE();
1429 return 0;
1430}
1431
Martyn Capewell6bf28752020-08-05 11:57:06 +01001432// Jenkins one-at-a-time hash, based on
1433// https://en.wikipedia.org/wiki/Jenkins_hash_function citing
1434// https://www.drdobbs.com/database/algorithm-alley/184410284.
1435constexpr uint32_t Hash(const char* str, uint32_t hash = 0) {
1436 if (*str == '\0') {
1437 hash += hash << 3;
1438 hash ^= hash >> 11;
1439 hash += hash << 15;
1440 return hash;
1441 } else {
1442 hash += *str;
1443 hash += hash << 10;
1444 hash ^= hash >> 6;
1445 return Hash(str + 1, hash);
1446 }
1447}
1448
Martyn Capewell8afff392022-04-19 18:08:39 +01001449constexpr uint32_t operator"" _h(const char* x, size_t) { return Hash(x); }
1450
armvixlad96eda2013-06-14 11:42:37 +01001451} // namespace vixl
1452
1453#endif // VIXL_UTILS_H