blob: d4b8a3594aaef0f23eb5f97e8ce43f27aa13be1b [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));
TheLastRar49d6efa2024-10-15 16:37:27 +0100562 VIXL_ASSERT((uint64_t{1} << block_bytes_log2) <= sizeof(value));
armvixldb644342015-07-21 11:37:10 +0100563 // 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--) {
TheLastRar49d6efa2024-10-15 16:37:27 +0100568 bytes[i] =
569 static_cast<uint8_t>((static_cast<uint64_t>(value) & mask) >> (i * 8));
armvixldb644342015-07-21 11:37:10 +0100570 mask >>= 8;
571 }
572
573 // Permutation tables for REV instructions.
574 // permute_table[0] is used by REV16_x, REV16_w
575 // permute_table[1] is used by REV32_x, REV_w
576 // permute_table[2] is used by REV_x
577 VIXL_ASSERT((0 < block_bytes_log2) && (block_bytes_log2 < 4));
armvixl0f35e362016-05-10 13:57:58 +0100578 static const uint8_t permute_table[3][8] = {{6, 7, 4, 5, 2, 3, 0, 1},
579 {4, 5, 6, 7, 0, 1, 2, 3},
580 {0, 1, 2, 3, 4, 5, 6, 7}};
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000581 uint64_t temp = 0;
armvixldb644342015-07-21 11:37:10 +0100582 for (int i = 0; i < 8; i++) {
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000583 temp <<= 8;
584 temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
armvixldb644342015-07-21 11:37:10 +0100585 }
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000586
587 T result;
588 VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
589 memcpy(&result, &temp, sizeof(result));
armvixldb644342015-07-21 11:37:10 +0100590 return result;
591}
592
Vincent Belliard96ff2a42016-10-27 08:51:55 -0700593template <unsigned MULTIPLE, typename T>
594inline bool IsMultiple(T value) {
595 VIXL_ASSERT(IsPowerOf2(MULTIPLE));
596 return (value & (MULTIPLE - 1)) == 0;
597}
armvixldb644342015-07-21 11:37:10 +0100598
Martyn Capewellfb8e3df2016-11-03 15:50:19 +0000599template <typename T>
600inline bool IsMultiple(T value, unsigned multiple) {
601 VIXL_ASSERT(IsPowerOf2(multiple));
602 return (value & (multiple - 1)) == 0;
603}
604
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000605template <typename T>
606inline bool IsAligned(T pointer, int alignment) {
607 VIXL_ASSERT(IsPowerOf2(alignment));
608 return (pointer & (alignment - 1)) == 0;
609}
610
armvixlad96eda2013-06-14 11:42:37 +0100611// Pointer alignment
612// TODO: rename/refactor to make it specific to instructions.
Pierre Langlois88c46b82016-06-02 18:15:32 +0100613template <unsigned ALIGN, typename T>
614inline bool IsAligned(T pointer) {
615 VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t)); // NOLINT(runtime/sizeof)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100616 // Use C-style casts to get static_cast behaviour for integral types (T), and
617 // reinterpret_cast behaviour for other types.
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000618 return IsAligned((intptr_t)(pointer), ALIGN);
Pierre Langlois88c46b82016-06-02 18:15:32 +0100619}
620
armvixl0f35e362016-05-10 13:57:58 +0100621template <typename T>
armvixlad96eda2013-06-14 11:42:37 +0100622bool IsWordAligned(T pointer) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100623 return IsAligned<4>(pointer);
armvixlad96eda2013-06-14 11:42:37 +0100624}
625
Jacob Bramley080c9422025-01-03 16:09:19 +0000626template <unsigned BITS, typename T>
627bool IsRepeatingPattern(T value) {
628 VIXL_STATIC_ASSERT(std::is_unsigned<T>::value);
629 VIXL_ASSERT(IsMultiple(sizeof(value) * kBitsPerByte, BITS));
630 VIXL_ASSERT(IsMultiple(BITS, 2));
631 VIXL_STATIC_ASSERT(BITS >= 2);
632#if (defined(__x86_64__) || defined(__i386)) && \
633 __clang_major__ >= 17 && __clang_major__ <= 19
634 // Workaround for https://github.com/llvm/llvm-project/issues/108722
635 unsigned hbits = BITS / 2;
636 T midmask = (~static_cast<T>(0) >> BITS) << hbits;
637 // E.g. for bytes in a word (0xb3b2b1b0): .b3b2b1. == .b2b1b0.
638 return (((value >> hbits) & midmask) == ((value << hbits) & midmask));
639#else
640 return value == RotateRight(value, BITS, sizeof(value) * kBitsPerByte);
641#endif
642}
643
644template <typename T>
645bool AllBytesMatch(T value) {
646 return IsRepeatingPattern<kBitsPerByte>(value);
647}
648
649template <typename T>
650bool AllHalfwordsMatch(T value) {
651 return IsRepeatingPattern<kBitsPerByte * 2>(value);
652}
653
654template <typename T>
655bool AllWordsMatch(T value) {
656 return IsRepeatingPattern<kBitsPerByte * 4>(value);
657}
658
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100659// Increment a pointer until it has the specified alignment. The alignment must
660// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100661template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100662T AlignUp(T pointer,
663 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100664 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100665 // Use C-style casts to get static_cast behaviour for integral types (T), and
666 // reinterpret_cast behaviour for other types.
667
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100668 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000669 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000670 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100671
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100672 size_t mask = alignment - 1;
673 T result = (T)((pointer_raw + mask) & ~mask);
Alexandre Rames47ed2652016-11-09 14:44:06 +0000674 VIXL_ASSERT(result >= pointer);
675
676 return result;
armvixlad96eda2013-06-14 11:42:37 +0100677}
678
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100679// Decrement a pointer until it has the specified alignment. The alignment must
680// be a power of two.
armvixl0f35e362016-05-10 13:57:58 +0100681template <class T>
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100682T AlignDown(T pointer,
683 typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100684 VIXL_ASSERT(IsPowerOf2(alignment));
armvixl4a102ba2014-07-14 09:02:40 +0100685 // Use C-style casts to get static_cast behaviour for integral types (T), and
686 // reinterpret_cast behaviour for other types.
687
Pierre Langlois0ad8a6f2017-05-16 08:58:34 +0100688 typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
Jacob Bramley2fe55ec2020-03-20 17:03:48 +0000689 (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
armvixl330dc712014-11-25 10:38:32 +0000690 VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
armvixl4a102ba2014-07-14 09:02:40 +0100691
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100692 size_t mask = alignment - 1;
693 return (T)(pointer_raw & ~mask);
armvixlb0c8ae22014-03-21 14:03:59 +0000694}
695
Georgia Kouvelibbd09d62017-05-03 17:23:47 +0100696
Pierre Langlois88c46b82016-06-02 18:15:32 +0100697template <typename T>
698inline T ExtractBit(T value, unsigned bit) {
699 return (value >> bit) & T(1);
700}
701
702template <typename Ts, typename Td>
703inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
704 return Td((value >> least_significant_bit) & Ts(mask));
705}
706
707template <typename Ts, typename Td>
Vincent Belliard60241a52016-11-10 12:41:11 -0800708inline void AssignBit(Td& dst, // NOLINT(runtime/references)
709 int bit,
710 Ts value) {
Pierre Langlois88c46b82016-06-02 18:15:32 +0100711 VIXL_ASSERT((value == Ts(0)) || (value == Ts(1)));
712 VIXL_ASSERT(bit >= 0);
713 VIXL_ASSERT(bit < static_cast<int>(sizeof(Td) * 8));
714 Td mask(1);
715 dst &= ~(mask << bit);
716 dst |= Td(value) << bit;
717}
718
719template <typename Td, typename Ts>
Vincent Belliard60241a52016-11-10 12:41:11 -0800720inline void AssignBits(Td& dst, // NOLINT(runtime/references)
Pierre Langlois88c46b82016-06-02 18:15:32 +0100721 int least_significant_bit,
722 Ts mask,
723 Ts value) {
724 VIXL_ASSERT(least_significant_bit >= 0);
725 VIXL_ASSERT(least_significant_bit < static_cast<int>(sizeof(Td) * 8));
726 VIXL_ASSERT(((Td(mask) << least_significant_bit) >> least_significant_bit) ==
727 Td(mask));
728 VIXL_ASSERT((value & mask) == value);
729 dst &= ~(Td(mask) << least_significant_bit);
730 dst |= Td(value) << least_significant_bit;
731}
732
733class VFP {
734 public:
735 static uint32_t FP32ToImm8(float imm) {
736 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
737 uint32_t bits = FloatToRawbits(imm);
738 // bit7: a000.0000
739 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
740 // bit6: 0b00.0000
741 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
742 // bit5_to_0: 00cd.efgh
743 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
744 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
745 }
746 static uint32_t FP64ToImm8(double imm) {
747 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
748 // 0000.0000.0000.0000.0000.0000.0000.0000
749 uint64_t bits = DoubleToRawbits(imm);
750 // bit7: a000.0000
751 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
752 // bit6: 0b00.0000
753 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
754 // bit5_to_0: 00cd.efgh
755 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
756
757 return static_cast<uint32_t>(bit7 | bit6 | bit5_to_0);
758 }
759 static float Imm8ToFP32(uint32_t imm8) {
760 // Imm8: abcdefgh (8 bits)
761 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
762 // where B is b ^ 1
763 uint32_t bits = imm8;
764 uint32_t bit7 = (bits >> 7) & 0x1;
765 uint32_t bit6 = (bits >> 6) & 0x1;
766 uint32_t bit5_to_0 = bits & 0x3f;
767 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
768
769 return RawbitsToFloat(result);
770 }
771 static double Imm8ToFP64(uint32_t imm8) {
772 // Imm8: abcdefgh (8 bits)
773 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
774 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
775 // where B is b ^ 1
776 uint32_t bits = imm8;
777 uint64_t bit7 = (bits >> 7) & 0x1;
778 uint64_t bit6 = (bits >> 6) & 0x1;
779 uint64_t bit5_to_0 = bits & 0x3f;
780 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
781 return RawbitsToDouble(result);
782 }
783 static bool IsImmFP32(float imm) {
784 // Valid values will have the form:
785 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
786 uint32_t bits = FloatToRawbits(imm);
787 // bits[19..0] are cleared.
788 if ((bits & 0x7ffff) != 0) {
789 return false;
790 }
791
792
793 // bits[29..25] are all set or all cleared.
794 uint32_t b_pattern = (bits >> 16) & 0x3e00;
795 if (b_pattern != 0 && b_pattern != 0x3e00) {
796 return false;
797 }
798 // bit[30] and bit[29] are opposite.
799 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
800 return false;
801 }
802 return true;
803 }
804 static bool IsImmFP64(double imm) {
805 // Valid values will have the form:
806 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
807 // 0000.0000.0000.0000.0000.0000.0000.0000
808 uint64_t bits = DoubleToRawbits(imm);
809 // bits[47..0] are cleared.
810 if ((bits & 0x0000ffffffffffff) != 0) {
811 return false;
812 }
813 // bits[61..54] are all set or all cleared.
814 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
815 if ((b_pattern != 0) && (b_pattern != 0x3fc0)) {
816 return false;
817 }
818 // bit[62] and bit[61] are opposite.
819 if (((bits ^ (bits << 1)) & (UINT64_C(1) << 62)) == 0) {
820 return false;
821 }
822 return true;
823 }
824};
825
826class BitField {
827 // ForEachBitHelper is a functor that will call
828 // bool ForEachBitHelper::execute(ElementType id) const
829 // and expects a boolean in return whether to continue (if true)
830 // or stop (if false)
831 // check_set will check if the bits are on (true) or off(false)
832 template <typename ForEachBitHelper, bool check_set>
833 bool ForEachBit(const ForEachBitHelper& helper) {
834 for (int i = 0; static_cast<size_t>(i) < bitfield_.size(); i++) {
835 if (bitfield_[i] == check_set)
836 if (!helper.execute(i)) return false;
837 }
838 return true;
839 }
840
841 public:
842 explicit BitField(unsigned size) : bitfield_(size, 0) {}
843
844 void Set(int i) {
845 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
846 bitfield_[i] = true;
847 }
848
849 void Unset(int i) {
850 VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
851 bitfield_[i] = true;
852 }
853
854 bool IsSet(int i) const { return bitfield_[i]; }
855
856 // For each bit not set in the bitfield call the execute functor
857 // execute.
858 // ForEachBitSetHelper::execute returns true if the iteration through
859 // the bits can continue, otherwise it will stop.
860 // struct ForEachBitSetHelper {
861 // bool execute(int /*id*/) { return false; }
862 // };
863 template <typename ForEachBitNotSetHelper>
864 bool ForEachBitNotSet(const ForEachBitNotSetHelper& helper) {
865 return ForEachBit<ForEachBitNotSetHelper, false>(helper);
866 }
867
868 // For each bit set in the bitfield call the execute functor
869 // execute.
870 template <typename ForEachBitSetHelper>
871 bool ForEachBitSet(const ForEachBitSetHelper& helper) {
872 return ForEachBit<ForEachBitSetHelper, true>(helper);
873 }
874
875 private:
876 std::vector<bool> bitfield_;
877};
878
Pierre Langloisd82faf62018-04-04 13:06:58 +0100879namespace internal {
880
Pierre Langlois88c46b82016-06-02 18:15:32 +0100881typedef int64_t Int64;
882class Uint64;
883class Uint128;
884
885class Uint32 {
886 uint32_t data_;
887
888 public:
889 // Unlike uint32_t, Uint32 has a default constructor.
890 Uint32() { data_ = 0; }
891 explicit Uint32(uint32_t data) : data_(data) {}
892 inline explicit Uint32(Uint64 data);
893 uint32_t Get() const { return data_; }
894 template <int N>
895 int32_t GetSigned() const {
896 return ExtractSignedBitfield32(N - 1, 0, data_);
897 }
898 int32_t GetSigned() const { return data_; }
899 Uint32 operator~() const { return Uint32(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100900 Uint32 operator-() const { return Uint32(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100901 bool operator==(Uint32 value) const { return data_ == value.data_; }
902 bool operator!=(Uint32 value) const { return data_ != value.data_; }
903 bool operator>(Uint32 value) const { return data_ > value.data_; }
904 Uint32 operator+(Uint32 value) const { return Uint32(data_ + value.data_); }
905 Uint32 operator-(Uint32 value) const { return Uint32(data_ - value.data_); }
906 Uint32 operator&(Uint32 value) const { return Uint32(data_ & value.data_); }
907 Uint32 operator&=(Uint32 value) {
908 data_ &= value.data_;
909 return *this;
910 }
911 Uint32 operator^(Uint32 value) const { return Uint32(data_ ^ value.data_); }
912 Uint32 operator^=(Uint32 value) {
913 data_ ^= value.data_;
914 return *this;
915 }
916 Uint32 operator|(Uint32 value) const { return Uint32(data_ | value.data_); }
917 Uint32 operator|=(Uint32 value) {
918 data_ |= value.data_;
919 return *this;
920 }
921 // Unlike uint32_t, the shift functions can accept negative shift and
922 // return 0 when the shift is too big.
923 Uint32 operator>>(int shift) const {
924 if (shift == 0) return *this;
925 if (shift < 0) {
926 int tmp = -shift;
927 if (tmp >= 32) return Uint32(0);
928 return Uint32(data_ << tmp);
929 }
930 int tmp = shift;
931 if (tmp >= 32) return Uint32(0);
932 return Uint32(data_ >> tmp);
933 }
934 Uint32 operator<<(int shift) const {
935 if (shift == 0) return *this;
936 if (shift < 0) {
937 int tmp = -shift;
938 if (tmp >= 32) return Uint32(0);
939 return Uint32(data_ >> tmp);
940 }
941 int tmp = shift;
942 if (tmp >= 32) return Uint32(0);
943 return Uint32(data_ << tmp);
944 }
945};
946
947class Uint64 {
948 uint64_t data_;
949
950 public:
951 // Unlike uint64_t, Uint64 has a default constructor.
952 Uint64() { data_ = 0; }
953 explicit Uint64(uint64_t data) : data_(data) {}
954 explicit Uint64(Uint32 data) : data_(data.Get()) {}
955 inline explicit Uint64(Uint128 data);
956 uint64_t Get() const { return data_; }
957 int64_t GetSigned(int N) const {
958 return ExtractSignedBitfield64(N - 1, 0, data_);
959 }
960 int64_t GetSigned() const { return data_; }
961 Uint32 ToUint32() const {
962 VIXL_ASSERT((data_ >> 32) == 0);
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100963 return Uint32(static_cast<uint32_t>(data_));
Pierre Langlois88c46b82016-06-02 18:15:32 +0100964 }
965 Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
Pierre Langloisf5348ce2016-09-22 11:15:35 +0100966 Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100967 Uint64 operator~() const { return Uint64(~data_); }
Anton Kirilov088b01f2022-09-27 14:27:38 +0100968 Uint64 operator-() const { return Uint64(UnsignedNegate(data_)); }
Pierre Langlois88c46b82016-06-02 18:15:32 +0100969 bool operator==(Uint64 value) const { return data_ == value.data_; }
970 bool operator!=(Uint64 value) const { return data_ != value.data_; }
971 Uint64 operator+(Uint64 value) const { return Uint64(data_ + value.data_); }
972 Uint64 operator-(Uint64 value) const { return Uint64(data_ - value.data_); }
973 Uint64 operator&(Uint64 value) const { return Uint64(data_ & value.data_); }
974 Uint64 operator&=(Uint64 value) {
975 data_ &= value.data_;
976 return *this;
977 }
978 Uint64 operator^(Uint64 value) const { return Uint64(data_ ^ value.data_); }
979 Uint64 operator^=(Uint64 value) {
980 data_ ^= value.data_;
981 return *this;
982 }
983 Uint64 operator|(Uint64 value) const { return Uint64(data_ | value.data_); }
984 Uint64 operator|=(Uint64 value) {
985 data_ |= value.data_;
986 return *this;
987 }
988 // Unlike uint64_t, the shift functions can accept negative shift and
989 // return 0 when the shift is too big.
990 Uint64 operator>>(int shift) const {
991 if (shift == 0) return *this;
992 if (shift < 0) {
993 int tmp = -shift;
994 if (tmp >= 64) return Uint64(0);
995 return Uint64(data_ << tmp);
996 }
997 int tmp = shift;
998 if (tmp >= 64) return Uint64(0);
999 return Uint64(data_ >> tmp);
1000 }
1001 Uint64 operator<<(int shift) const {
1002 if (shift == 0) return *this;
1003 if (shift < 0) {
1004 int tmp = -shift;
1005 if (tmp >= 64) return Uint64(0);
1006 return Uint64(data_ >> tmp);
1007 }
1008 int tmp = shift;
1009 if (tmp >= 64) return Uint64(0);
1010 return Uint64(data_ << tmp);
1011 }
1012};
1013
1014class Uint128 {
1015 uint64_t data_high_;
1016 uint64_t data_low_;
1017
1018 public:
1019 Uint128() : data_high_(0), data_low_(0) {}
1020 explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
1021 explicit Uint128(Uint64 data_low)
1022 : data_high_(0), data_low_(data_low.Get()) {}
1023 Uint128(uint64_t data_high, uint64_t data_low)
1024 : data_high_(data_high), data_low_(data_low) {}
1025 Uint64 ToUint64() const {
1026 VIXL_ASSERT(data_high_ == 0);
1027 return Uint64(data_low_);
1028 }
1029 Uint64 GetHigh64() const { return Uint64(data_high_); }
1030 Uint64 GetLow64() const { return Uint64(data_low_); }
1031 Uint128 operator~() const { return Uint128(~data_high_, ~data_low_); }
1032 bool operator==(Uint128 value) const {
1033 return (data_high_ == value.data_high_) && (data_low_ == value.data_low_);
1034 }
1035 Uint128 operator&(Uint128 value) const {
1036 return Uint128(data_high_ & value.data_high_, data_low_ & value.data_low_);
1037 }
1038 Uint128 operator&=(Uint128 value) {
1039 data_high_ &= value.data_high_;
1040 data_low_ &= value.data_low_;
1041 return *this;
1042 }
1043 Uint128 operator|=(Uint128 value) {
1044 data_high_ |= value.data_high_;
1045 data_low_ |= value.data_low_;
1046 return *this;
1047 }
1048 Uint128 operator>>(int shift) const {
1049 VIXL_ASSERT((shift >= 0) && (shift < 128));
1050 if (shift == 0) return *this;
1051 if (shift >= 64) {
1052 return Uint128(0, data_high_ >> (shift - 64));
1053 }
1054 uint64_t tmp = (data_high_ << (64 - shift)) | (data_low_ >> shift);
1055 return Uint128(data_high_ >> shift, tmp);
1056 }
1057 Uint128 operator<<(int shift) const {
1058 VIXL_ASSERT((shift >= 0) && (shift < 128));
1059 if (shift == 0) return *this;
1060 if (shift >= 64) {
1061 return Uint128(data_low_ << (shift - 64), 0);
1062 }
1063 uint64_t tmp = (data_high_ << shift) | (data_low_ >> (64 - shift));
1064 return Uint128(tmp, data_low_ << shift);
1065 }
1066};
1067
1068Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
1069Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1070
1071Int64 BitCount(Uint32 value);
1072
TatWai Chong13634762019-07-16 16:20:45 -07001073// The algorithm used is adapted from the one described in section 8.2 of
1074// Hacker's Delight, by Henry S. Warren, Jr.
1075template <unsigned N, typename T>
1076int64_t MultiplyHigh(T u, T v) {
1077 uint64_t u0, v0, w0, u1, v1, w1, w2, t;
1078 VIXL_STATIC_ASSERT((N == 8) || (N == 16) || (N == 32) || (N == 64));
1079 uint64_t sign_mask = UINT64_C(1) << (N - 1);
1080 uint64_t sign_ext = 0;
1081 unsigned half_bits = N / 2;
1082 uint64_t half_mask = GetUintMask(half_bits);
1083 if (std::numeric_limits<T>::is_signed) {
1084 sign_ext = UINT64_C(0xffffffffffffffff) << half_bits;
1085 }
1086
1087 VIXL_ASSERT(sizeof(u) == sizeof(uint64_t));
1088 VIXL_ASSERT(sizeof(u) == sizeof(u0));
1089
1090 u0 = u & half_mask;
1091 u1 = u >> half_bits | (((u & sign_mask) != 0) ? sign_ext : 0);
1092 v0 = v & half_mask;
1093 v1 = v >> half_bits | (((v & sign_mask) != 0) ? sign_ext : 0);
1094
1095 w0 = u0 * v0;
1096 t = u1 * v0 + (w0 >> half_bits);
1097
1098 w1 = t & half_mask;
1099 w2 = t >> half_bits | (((t & sign_mask) != 0) ? sign_ext : 0);
1100 w1 = u0 * v1 + w1;
1101 w1 = w1 >> half_bits | (((w1 & sign_mask) != 0) ? sign_ext : 0);
1102
1103 uint64_t value = u1 * v1 + w2 + w1;
1104 int64_t result;
1105 memcpy(&result, &value, sizeof(result));
1106 return result;
1107}
1108
Pierre Langloisd82faf62018-04-04 13:06:58 +01001109} // namespace internal
1110
Jacob Bramleyca789742018-09-13 14:25:46 +01001111// The default NaN values (for FPCR.DN=1).
1112extern const double kFP64DefaultNaN;
1113extern const float kFP32DefaultNaN;
1114extern const Float16 kFP16DefaultNaN;
1115
1116// Floating-point infinity values.
1117extern const Float16 kFP16PositiveInfinity;
1118extern const Float16 kFP16NegativeInfinity;
1119extern const float kFP32PositiveInfinity;
1120extern const float kFP32NegativeInfinity;
1121extern const double kFP64PositiveInfinity;
1122extern const double kFP64NegativeInfinity;
1123
1124// Floating-point zero values.
1125extern const Float16 kFP16PositiveZero;
1126extern const Float16 kFP16NegativeZero;
1127
1128// AArch64 floating-point specifics. These match IEEE-754.
1129const unsigned kDoubleMantissaBits = 52;
1130const unsigned kDoubleExponentBits = 11;
1131const unsigned kFloatMantissaBits = 23;
1132const unsigned kFloatExponentBits = 8;
1133const unsigned kFloat16MantissaBits = 10;
1134const unsigned kFloat16ExponentBits = 5;
1135
1136enum FPRounding {
1137 // The first four values are encodable directly by FPCR<RMode>.
1138 FPTieEven = 0x0,
1139 FPPositiveInfinity = 0x1,
1140 FPNegativeInfinity = 0x2,
1141 FPZero = 0x3,
1142
1143 // The final rounding modes are only available when explicitly specified by
1144 // the instruction (such as with fcvta). It cannot be set in FPCR.
1145 FPTieAway,
1146 FPRoundOdd
1147};
1148
1149enum UseDefaultNaN { kUseDefaultNaN, kIgnoreDefaultNaN };
1150
1151// Assemble the specified IEEE-754 components into the target type and apply
1152// appropriate rounding.
1153// sign: 0 = positive, 1 = negative
1154// exponent: Unbiased IEEE-754 exponent.
1155// mantissa: The mantissa of the input. The top bit (which is not encoded for
1156// normal IEEE-754 values) must not be omitted. This bit has the
1157// value 'pow(2, exponent)'.
1158//
1159// The input value is assumed to be a normalized value. That is, the input may
1160// not be infinity or NaN. If the source value is subnormal, it must be
1161// normalized before calling this function such that the highest set bit in the
1162// mantissa has the value 'pow(2, exponent)'.
1163//
1164// Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than
1165// calling a templated FPRound.
1166template <class T, int ebits, int mbits>
1167T FPRound(int64_t sign,
1168 int64_t exponent,
1169 uint64_t mantissa,
1170 FPRounding round_mode) {
1171 VIXL_ASSERT((sign == 0) || (sign == 1));
1172
1173 // Only FPTieEven and FPRoundOdd rounding modes are implemented.
1174 VIXL_ASSERT((round_mode == FPTieEven) || (round_mode == FPRoundOdd));
1175
1176 // Rounding can promote subnormals to normals, and normals to infinities. For
1177 // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be
1178 // encodable as a float, but rounding based on the low-order mantissa bits
1179 // could make it overflow. With ties-to-even rounding, this value would become
1180 // an infinity.
1181
1182 // ---- Rounding Method ----
1183 //
1184 // The exponent is irrelevant in the rounding operation, so we treat the
1185 // lowest-order bit that will fit into the result ('onebit') as having
1186 // the value '1'. Similarly, the highest-order bit that won't fit into
1187 // the result ('halfbit') has the value '0.5'. The 'point' sits between
1188 // 'onebit' and 'halfbit':
1189 //
1190 // These bits fit into the result.
1191 // |---------------------|
1192 // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1193 // ||
1194 // / |
1195 // / halfbit
1196 // onebit
1197 //
1198 // For subnormal outputs, the range of representable bits is smaller and
1199 // the position of onebit and halfbit depends on the exponent of the
1200 // input, but the method is otherwise similar.
1201 //
1202 // onebit(frac)
1203 // |
1204 // | halfbit(frac) halfbit(adjusted)
1205 // | / /
1206 // | | |
1207 // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00
1208 // 0b00.0... -> 0b00.0... -> 0b00
1209 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00
1210 // 0b00.1... -> 0b00.1... -> 0b01
1211 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01
1212 // 0b01.0... -> 0b01.0... -> 0b01
1213 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10
1214 // 0b01.1... -> 0b01.1... -> 0b10
1215 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10
1216 // 0b10.0... -> 0b10.0... -> 0b10
1217 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10
1218 // 0b10.1... -> 0b10.1... -> 0b11
1219 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11
1220 // ... / | / |
1221 // / | / |
1222 // / |
1223 // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / |
1224 //
1225 // mantissa = (mantissa >> shift) + halfbit(adjusted);
1226
1227 static const int mantissa_offset = 0;
1228 static const int exponent_offset = mantissa_offset + mbits;
1229 static const int sign_offset = exponent_offset + ebits;
1230 VIXL_ASSERT(sign_offset == (sizeof(T) * 8 - 1));
1231
1232 // Bail out early for zero inputs.
1233 if (mantissa == 0) {
1234 return static_cast<T>(sign << sign_offset);
1235 }
1236
1237 // If all bits in the exponent are set, the value is infinite or NaN.
1238 // This is true for all binary IEEE-754 formats.
1239 static const int infinite_exponent = (1 << ebits) - 1;
1240 static const int max_normal_exponent = infinite_exponent - 1;
1241
1242 // Apply the exponent bias to encode it for the result. Doing this early makes
1243 // it easy to detect values that will be infinite or subnormal.
1244 exponent += max_normal_exponent >> 1;
1245
1246 if (exponent > max_normal_exponent) {
1247 // Overflow: the input is too large for the result type to represent.
1248 if (round_mode == FPTieEven) {
1249 // FPTieEven rounding mode handles overflows using infinities.
1250 exponent = infinite_exponent;
1251 mantissa = 0;
1252 } else {
1253 VIXL_ASSERT(round_mode == FPRoundOdd);
1254 // FPRoundOdd rounding mode handles overflows using the largest magnitude
1255 // normal number.
1256 exponent = max_normal_exponent;
1257 mantissa = (UINT64_C(1) << exponent_offset) - 1;
1258 }
1259 return static_cast<T>((sign << sign_offset) |
1260 (exponent << exponent_offset) |
1261 (mantissa << mantissa_offset));
1262 }
1263
1264 // Calculate the shift required to move the top mantissa bit to the proper
1265 // place in the destination type.
1266 const int highest_significant_bit = 63 - CountLeadingZeros(mantissa);
1267 int shift = highest_significant_bit - mbits;
1268
1269 if (exponent <= 0) {
1270 // The output will be subnormal (before rounding).
1271 // For subnormal outputs, the shift must be adjusted by the exponent. The +1
1272 // is necessary because the exponent of a subnormal value (encoded as 0) is
1273 // the same as the exponent of the smallest normal value (encoded as 1).
Anton Kirilov088b01f2022-09-27 14:27:38 +01001274 shift += static_cast<int>(-exponent + 1);
Jacob Bramleyca789742018-09-13 14:25:46 +01001275
1276 // Handle inputs that would produce a zero output.
1277 //
1278 // Shifts higher than highest_significant_bit+1 will always produce a zero
1279 // result. A shift of exactly highest_significant_bit+1 might produce a
1280 // non-zero result after rounding.
1281 if (shift > (highest_significant_bit + 1)) {
1282 if (round_mode == FPTieEven) {
1283 // The result will always be +/-0.0.
1284 return static_cast<T>(sign << sign_offset);
1285 } else {
1286 VIXL_ASSERT(round_mode == FPRoundOdd);
1287 VIXL_ASSERT(mantissa != 0);
1288 // For FPRoundOdd, if the mantissa is too small to represent and
1289 // non-zero return the next "odd" value.
1290 return static_cast<T>((sign << sign_offset) | 1);
1291 }
1292 }
1293
1294 // Properly encode the exponent for a subnormal output.
1295 exponent = 0;
1296 } else {
1297 // Clear the topmost mantissa bit, since this is not encoded in IEEE-754
1298 // normal values.
1299 mantissa &= ~(UINT64_C(1) << highest_significant_bit);
1300 }
1301
1302 // The casts below are only well-defined for unsigned integers.
1303 VIXL_STATIC_ASSERT(std::numeric_limits<T>::is_integer);
1304 VIXL_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
1305
1306 if (shift > 0) {
1307 if (round_mode == FPTieEven) {
1308 // We have to shift the mantissa to the right. Some precision is lost, so
1309 // we need to apply rounding.
1310 uint64_t onebit_mantissa = (mantissa >> (shift)) & 1;
1311 uint64_t halfbit_mantissa = (mantissa >> (shift - 1)) & 1;
1312 uint64_t adjustment = (halfbit_mantissa & ~onebit_mantissa);
1313 uint64_t adjusted = mantissa - adjustment;
1314 T halfbit_adjusted = (adjusted >> (shift - 1)) & 1;
1315
1316 T result =
1317 static_cast<T>((sign << sign_offset) | (exponent << exponent_offset) |
1318 ((mantissa >> shift) << mantissa_offset));
1319
1320 // A very large mantissa can overflow during rounding. If this happens,
1321 // the exponent should be incremented and the mantissa set to 1.0
1322 // (encoded as 0). Applying halfbit_adjusted after assembling the float
1323 // has the nice side-effect that this case is handled for free.
1324 //
1325 // This also handles cases where a very large finite value overflows to
1326 // infinity, or where a very large subnormal value overflows to become
1327 // normal.
1328 return result + halfbit_adjusted;
1329 } else {
1330 VIXL_ASSERT(round_mode == FPRoundOdd);
1331 // If any bits at position halfbit or below are set, onebit (ie. the
1332 // bottom bit of the resulting mantissa) must be set.
1333 uint64_t fractional_bits = mantissa & ((UINT64_C(1) << shift) - 1);
1334 if (fractional_bits != 0) {
1335 mantissa |= UINT64_C(1) << shift;
1336 }
1337
1338 return static_cast<T>((sign << sign_offset) |
1339 (exponent << exponent_offset) |
1340 ((mantissa >> shift) << mantissa_offset));
1341 }
1342 } else {
1343 // We have to shift the mantissa to the left (or not at all). The input
1344 // mantissa is exactly representable in the output mantissa, so apply no
1345 // rounding correction.
1346 return static_cast<T>((sign << sign_offset) |
1347 (exponent << exponent_offset) |
1348 ((mantissa << -shift) << mantissa_offset));
1349 }
1350}
1351
1352
1353// See FPRound for a description of this function.
1354inline double FPRoundToDouble(int64_t sign,
1355 int64_t exponent,
1356 uint64_t mantissa,
1357 FPRounding round_mode) {
1358 uint64_t bits =
1359 FPRound<uint64_t, kDoubleExponentBits, kDoubleMantissaBits>(sign,
1360 exponent,
1361 mantissa,
1362 round_mode);
1363 return RawbitsToDouble(bits);
1364}
1365
1366
1367// See FPRound for a description of this function.
1368inline Float16 FPRoundToFloat16(int64_t sign,
1369 int64_t exponent,
1370 uint64_t mantissa,
1371 FPRounding round_mode) {
1372 return RawbitsToFloat16(
Jacob Bramley2fe55ec2020-03-20 17:03:48 +00001373 FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1374 sign, exponent, mantissa, round_mode));
Jacob Bramleyca789742018-09-13 14:25:46 +01001375}
1376
1377
1378// See FPRound for a description of this function.
1379static inline float FPRoundToFloat(int64_t sign,
1380 int64_t exponent,
1381 uint64_t mantissa,
1382 FPRounding round_mode) {
1383 uint32_t bits =
1384 FPRound<uint32_t, kFloatExponentBits, kFloatMantissaBits>(sign,
1385 exponent,
1386 mantissa,
1387 round_mode);
1388 return RawbitsToFloat(bits);
1389}
1390
1391
1392float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1393float FPToFloat(double value,
1394 FPRounding round_mode,
1395 UseDefaultNaN DN,
1396 bool* exception = NULL);
1397
1398double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1399double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1400
1401Float16 FPToFloat16(float value,
1402 FPRounding round_mode,
1403 UseDefaultNaN DN,
1404 bool* exception = NULL);
1405
1406Float16 FPToFloat16(double value,
1407 FPRounding round_mode,
1408 UseDefaultNaN DN,
1409 bool* exception = NULL);
Jacob Bramley0f62eab2019-10-23 17:07:47 +01001410
1411// Like static_cast<T>(value), but with specialisations for the Float16 type.
1412template <typename T, typename F>
1413T StaticCastFPTo(F value) {
1414 return static_cast<T>(value);
1415}
1416
1417template <>
1418inline float StaticCastFPTo<float, Float16>(Float16 value) {
1419 return FPToFloat(value, kIgnoreDefaultNaN);
1420}
1421
1422template <>
1423inline double StaticCastFPTo<double, Float16>(Float16 value) {
1424 return FPToDouble(value, kIgnoreDefaultNaN);
1425}
1426
1427template <>
1428inline Float16 StaticCastFPTo<Float16, float>(float value) {
1429 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1430}
1431
1432template <>
1433inline Float16 StaticCastFPTo<Float16, double>(double value) {
1434 return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1435}
1436
1437template <typename T>
1438uint64_t FPToRawbitsWithSize(unsigned size_in_bits, T value) {
1439 switch (size_in_bits) {
1440 case 16:
1441 return Float16ToRawbits(StaticCastFPTo<Float16>(value));
1442 case 32:
1443 return FloatToRawbits(StaticCastFPTo<float>(value));
1444 case 64:
1445 return DoubleToRawbits(StaticCastFPTo<double>(value));
1446 }
1447 VIXL_UNREACHABLE();
1448 return 0;
1449}
TatWai Chongdb7437c2020-01-09 17:44:10 -08001450
1451template <typename T>
1452T RawbitsWithSizeToFP(unsigned size_in_bits, uint64_t value) {
1453 VIXL_ASSERT(IsUintN(size_in_bits, value));
1454 switch (size_in_bits) {
1455 case 16:
1456 return StaticCastFPTo<T>(RawbitsToFloat16(static_cast<uint16_t>(value)));
1457 case 32:
1458 return StaticCastFPTo<T>(RawbitsToFloat(static_cast<uint32_t>(value)));
1459 case 64:
1460 return StaticCastFPTo<T>(RawbitsToDouble(value));
1461 }
1462 VIXL_UNREACHABLE();
1463 return 0;
1464}
1465
Martyn Capewell6bf28752020-08-05 11:57:06 +01001466// Jenkins one-at-a-time hash, based on
1467// https://en.wikipedia.org/wiki/Jenkins_hash_function citing
1468// https://www.drdobbs.com/database/algorithm-alley/184410284.
1469constexpr uint32_t Hash(const char* str, uint32_t hash = 0) {
1470 if (*str == '\0') {
1471 hash += hash << 3;
1472 hash ^= hash >> 11;
1473 hash += hash << 15;
1474 return hash;
1475 } else {
1476 hash += *str;
1477 hash += hash << 10;
1478 hash ^= hash >> 6;
1479 return Hash(str + 1, hash);
1480 }
1481}
1482
Martyn Capewell8afff392022-04-19 18:08:39 +01001483constexpr uint32_t operator"" _h(const char* x, size_t) { return Hash(x); }
1484
armvixlad96eda2013-06-14 11:42:37 +01001485} // namespace vixl
1486
1487#endif // VIXL_UTILS_H