• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015, VIXL authors
2 // 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 
30 #include <cmath>
31 #include <cstring>
32 #include <limits>
33 #include <type_traits>
34 #include <vector>
35 
36 #include "compiler-intrinsics-vixl.h"
37 #include "globals-vixl.h"
38 
39 namespace vixl {
40 
41 // Macros for compile-time format checking.
42 #if GCC_VERSION_OR_NEWER(4, 4, 0)
43 #define PRINTF_CHECK(format_index, varargs_index) \
44   __attribute__((format(gnu_printf, format_index, varargs_index)))
45 #else
46 #define PRINTF_CHECK(format_index, varargs_index)
47 #endif
48 
49 #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 
70 template <typename T, size_t n>
ArrayLength(const T (&)[n])71 constexpr size_t ArrayLength(const T (&)[n]) {
72   return n;
73 }
74 
GetUintMask(unsigned bits)75 inline 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 
GetSignMask(unsigned bits)81 inline uint64_t GetSignMask(unsigned bits) {
82   VIXL_ASSERT(bits <= 64);
83   return UINT64_C(1) << (bits - 1);
84 }
85 
86 // Check number width.
87 // TODO: Refactor these using templates.
IsIntN(unsigned n,uint32_t x)88 inline bool IsIntN(unsigned n, uint32_t x) {
89   VIXL_ASSERT((0 < n) && (n <= 32));
90   return x <= static_cast<uint32_t>(INT32_MAX >> (32 - n));
91 }
IsIntN(unsigned n,int32_t x)92 inline bool IsIntN(unsigned n, int32_t x) {
93   VIXL_ASSERT((0 < n) && (n <= 32));
94   if (n == 32) return true;
95   int32_t limit = INT32_C(1) << (n - 1);
96   return (-limit <= x) && (x < limit);
97 }
IsIntN(unsigned n,uint64_t x)98 inline bool IsIntN(unsigned n, uint64_t x) {
99   VIXL_ASSERT((0 < n) && (n <= 64));
100   return x <= static_cast<uint64_t>(INT64_MAX >> (64 - n));
101 }
IsIntN(unsigned n,int64_t x)102 inline bool IsIntN(unsigned n, int64_t x) {
103   VIXL_ASSERT((0 < n) && (n <= 64));
104   if (n == 64) return true;
105   int64_t limit = INT64_C(1) << (n - 1);
106   return (-limit <= x) && (x < limit);
107 }
is_intn(unsigned n,int64_t x)108 VIXL_DEPRECATED("IsIntN", inline bool is_intn(unsigned n, int64_t x)) {
109   return IsIntN(n, x);
110 }
111 
IsUintN(unsigned n,uint32_t x)112 inline bool IsUintN(unsigned n, uint32_t x) {
113   VIXL_ASSERT((0 < n) && (n <= 32));
114   if (n >= 32) return true;
115   return !(x >> n);
116 }
IsUintN(unsigned n,int32_t x)117 inline 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 }
IsUintN(unsigned n,uint64_t x)122 inline bool IsUintN(unsigned n, uint64_t x) {
123   VIXL_ASSERT((0 < n) && (n <= 64));
124   if (n >= 64) return true;
125   return !(x >> n);
126 }
IsUintN(unsigned n,int64_t x)127 inline 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 }
is_uintn(unsigned n,int64_t x)132 VIXL_DEPRECATED("IsUintN", inline bool is_uintn(unsigned n, int64_t x)) {
133   return IsUintN(n, x);
134 }
135 
TruncateToUintN(unsigned n,uint64_t x)136 inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
137   VIXL_ASSERT((0 < n) && (n < 64));
138   return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
139 }
140 VIXL_DEPRECATED("TruncateToUintN",
141                 inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
142   return TruncateToUintN(n, x);
143 }
144 
145 // clang-format off
146 #define INT_1_TO_32_LIST(V)                                                    \
147 V(1)  V(2)  V(3)  V(4)  V(5)  V(6)  V(7)  V(8)                                 \
148 V(9)  V(10) V(11) V(12) V(13) V(14) V(15) V(16)                                \
149 V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24)                                \
150 V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32)
151 
152 #define INT_33_TO_63_LIST(V)                                                   \
153 V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40)                                \
154 V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48)                                \
155 V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56)                                \
156 V(57) V(58) V(59) V(60) V(61) V(62) V(63)
157 
158 #define INT_1_TO_63_LIST(V) INT_1_TO_32_LIST(V) INT_33_TO_63_LIST(V)
159 
160 // clang-format on
161 
162 #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 
174 #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);                                   \
181   }
182 
183 INT_1_TO_63_LIST(DECLARE_IS_INT_N)
INT_1_TO_63_LIST(DECLARE_IS_UINT_N)184 INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
185 INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
186 
187 #undef DECLARE_IS_INT_N
188 #undef DECLARE_IS_UINT_N
189 #undef DECLARE_TRUNCATE_TO_INT_N
190 
191 // Bit field extraction.
192 inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
193   VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
194               (msb >= lsb));
195   if ((msb == 63) && (lsb == 0)) return x;
196   return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
197 }
198 
199 
ExtractUnsignedBitfield32(int msb,int lsb,uint64_t x)200 inline uint32_t ExtractUnsignedBitfield32(int msb, int lsb, uint64_t x) {
201   VIXL_ASSERT((static_cast<size_t>(msb) < sizeof(x) * 8) && (lsb >= 0) &&
202               (msb >= lsb));
203   return TruncateToUint32(ExtractUnsignedBitfield64(msb, lsb, x));
204 }
205 
206 
ExtractSignedBitfield64(int msb,int lsb,uint64_t x)207 inline int64_t ExtractSignedBitfield64(int msb, int lsb, uint64_t x) {
208   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;
218 }
219 
ExtractSignedBitfield32(int msb,int lsb,uint64_t x)220 inline int32_t ExtractSignedBitfield32(int msb, int lsb, uint64_t x) {
221   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 
RotateRight(uint64_t value,unsigned int rotate,unsigned int width)229 inline 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 
RotateLeft(uint64_t value,unsigned int rotate,unsigned int width)242 inline uint64_t RotateLeft(uint64_t value,
243                            unsigned int rotate,
244                            unsigned int width) {
245   return RotateRight(value, width - rotate, width);
246 }
247 
248 // Wrapper class for passing FP16 values through the assembler.
249 // This is purely to aid with type checking/casting.
250 class Float16 {
251  public:
252   explicit Float16(double dvalue);
Float16()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 
261 // Floating point representation.
262 uint16_t Float16ToRawbits(Float16 value);
263 
264 
265 uint32_t FloatToRawbits(float value);
266 VIXL_DEPRECATED("FloatToRawbits",
267                 inline uint32_t float_to_rawbits(float value)) {
268   return FloatToRawbits(value);
269 }
270 
271 uint64_t DoubleToRawbits(double value);
272 VIXL_DEPRECATED("DoubleToRawbits",
273                 inline uint64_t double_to_rawbits(double value)) {
274   return DoubleToRawbits(value);
275 }
276 
277 Float16 RawbitsToFloat16(uint16_t bits);
278 
279 float RawbitsToFloat(uint32_t bits);
280 VIXL_DEPRECATED("RawbitsToFloat",
rawbits_to_float(uint32_t bits)281                 inline float rawbits_to_float(uint32_t bits)) {
282   return RawbitsToFloat(bits);
283 }
284 
285 double RawbitsToDouble(uint64_t bits);
286 VIXL_DEPRECATED("RawbitsToDouble",
rawbits_to_double(uint64_t bits)287                 inline double rawbits_to_double(uint64_t bits)) {
288   return RawbitsToDouble(bits);
289 }
290 
291 // Some compilers dislike negating unsigned integers,
292 // so we provide an equivalent.
293 template <typename T>
UnsignedNegate(T value)294 T UnsignedNegate(T value) {
295   VIXL_STATIC_ASSERT(std::is_unsigned<T>::value);
296   return ~value + 1;
297 }
298 
299 template <typename T>
CanBeNegated(T value)300 bool CanBeNegated(T value) {
301   VIXL_STATIC_ASSERT(std::is_signed<T>::value);
302   return (value == std::numeric_limits<T>::min()) ? false : true;
303 }
304 
305 // An absolute operation for signed integers that is defined for results outside
306 // the representable range. Specifically, Abs(MIN_INT) is MIN_INT.
307 template <typename T>
Abs(T val)308 T 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 
316 // Convert unsigned to signed numbers in a well-defined way (using two's
317 // complement representations).
RawbitsToInt64(uint64_t bits)318 inline int64_t RawbitsToInt64(uint64_t bits) {
319   return (bits >= UINT64_C(0x8000000000000000))
320              ? (-static_cast<int64_t>(UnsignedNegate(bits) - 1) - 1)
321              : static_cast<int64_t>(bits);
322 }
323 
RawbitsToInt32(uint32_t bits)324 inline int32_t RawbitsToInt32(uint32_t bits) {
325   return (bits >= UINT64_C(0x80000000))
326              ? (-static_cast<int32_t>(UnsignedNegate(bits) - 1) - 1)
327              : static_cast<int32_t>(bits);
328 }
329 
330 namespace internal {
331 
332 // Internal simulation class used solely by the simulator to
333 // provide an abstraction layer for any half-precision arithmetic.
334 class 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.
SimFloat16(double dvalue)339   SimFloat16(double dvalue) : Float16(dvalue) {}  // NOLINT(runtime/explicit)
SimFloat16(Float16 f)340   SimFloat16(Float16 f) {                         // NOLINT(runtime/explicit)
341     this->rawbits_ = Float16ToRawbits(f);
342   }
SimFloat16()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;
353   // This is necessary for conversions performed in (macro asm) Fmov.
354   bool operator==(double rhs) const;
355   operator double() const;
356 };
357 }  // namespace internal
358 
359 uint32_t Float16Sign(internal::SimFloat16 value);
360 
361 uint32_t Float16Exp(internal::SimFloat16 value);
362 
363 uint32_t Float16Mantissa(internal::SimFloat16 value);
364 
365 uint32_t FloatSign(float value);
366 VIXL_DEPRECATED("FloatSign", inline uint32_t float_sign(float value)) {
367   return FloatSign(value);
368 }
369 
370 uint32_t FloatExp(float value);
371 VIXL_DEPRECATED("FloatExp", inline uint32_t float_exp(float value)) {
372   return FloatExp(value);
373 }
374 
375 uint32_t FloatMantissa(float value);
376 VIXL_DEPRECATED("FloatMantissa", inline uint32_t float_mantissa(float value)) {
377   return FloatMantissa(value);
378 }
379 
380 uint32_t DoubleSign(double value);
381 VIXL_DEPRECATED("DoubleSign", inline uint32_t double_sign(double value)) {
382   return DoubleSign(value);
383 }
384 
385 uint32_t DoubleExp(double value);
386 VIXL_DEPRECATED("DoubleExp", inline uint32_t double_exp(double value)) {
387   return DoubleExp(value);
388 }
389 
390 uint64_t DoubleMantissa(double value);
391 VIXL_DEPRECATED("DoubleMantissa",
392                 inline uint64_t double_mantissa(double value)) {
393   return DoubleMantissa(value);
394 }
395 
396 internal::SimFloat16 Float16Pack(uint16_t sign,
397                                  uint16_t exp,
398                                  uint16_t mantissa);
399 
400 float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa);
401 VIXL_DEPRECATED("FloatPack",
float_pack(uint32_t sign,uint32_t exp,uint32_t mantissa)402                 inline float float_pack(uint32_t sign,
403                                         uint32_t exp,
404                                         uint32_t mantissa)) {
405   return FloatPack(sign, exp, mantissa);
406 }
407 
408 double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa);
409 VIXL_DEPRECATED("DoublePack",
double_pack(uint32_t sign,uint32_t exp,uint64_t mantissa)410                 inline double double_pack(uint32_t sign,
411                                           uint32_t exp,
412                                           uint64_t mantissa)) {
413   return DoublePack(sign, exp, mantissa);
414 }
415 
416 // An fpclassify() function for 16-bit half-precision floats.
417 int Float16Classify(Float16 value);
float16classify(uint16_t value)418 VIXL_DEPRECATED("Float16Classify", inline int float16classify(uint16_t value)) {
419   return Float16Classify(RawbitsToFloat16(value));
420 }
421 
422 bool IsZero(Float16 value);
423 
IsPositiveZero(double value)424 inline bool IsPositiveZero(double value) {
425   return (value == 0.0) && (copysign(1.0, value) > 0.0);
426 }
427 
IsNaN(float value)428 inline bool IsNaN(float value) { return std::isnan(value); }
429 
IsNaN(double value)430 inline bool IsNaN(double value) { return std::isnan(value); }
431 
IsNaN(Float16 value)432 inline bool IsNaN(Float16 value) { return Float16Classify(value) == FP_NAN; }
433 
IsInf(float value)434 inline bool IsInf(float value) { return std::isinf(value); }
435 
IsInf(double value)436 inline bool IsInf(double value) { return std::isinf(value); }
437 
IsInf(Float16 value)438 inline bool IsInf(Float16 value) {
439   return Float16Classify(value) == FP_INFINITE;
440 }
441 
442 
443 // NaN tests.
IsSignallingNaN(double num)444 inline bool IsSignallingNaN(double num) {
445   const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
446   uint64_t raw = DoubleToRawbits(num);
447   if (IsNaN(num) && ((raw & kFP64QuietNaNMask) == 0)) {
448     return true;
449   }
450   return false;
451 }
452 
453 
IsSignallingNaN(float num)454 inline bool IsSignallingNaN(float num) {
455   const uint32_t kFP32QuietNaNMask = 0x00400000;
456   uint32_t raw = FloatToRawbits(num);
457   if (IsNaN(num) && ((raw & kFP32QuietNaNMask) == 0)) {
458     return true;
459   }
460   return false;
461 }
462 
463 
IsSignallingNaN(Float16 num)464 inline bool IsSignallingNaN(Float16 num) {
465   const uint16_t kFP16QuietNaNMask = 0x0200;
466   return IsNaN(num) && ((Float16ToRawbits(num) & kFP16QuietNaNMask) == 0);
467 }
468 
469 
470 template <typename T>
IsQuietNaN(T num)471 inline bool IsQuietNaN(T num) {
472   return IsNaN(num) && !IsSignallingNaN(num);
473 }
474 
475 
476 // Convert the NaN in 'num' to a quiet NaN.
ToQuietNaN(double num)477 inline double ToQuietNaN(double num) {
478   const uint64_t kFP64QuietNaNMask = UINT64_C(0x0008000000000000);
479   VIXL_ASSERT(IsNaN(num));
480   return RawbitsToDouble(DoubleToRawbits(num) | kFP64QuietNaNMask);
481 }
482 
483 
ToQuietNaN(float num)484 inline float ToQuietNaN(float num) {
485   const uint32_t kFP32QuietNaNMask = 0x00400000;
486   VIXL_ASSERT(IsNaN(num));
487   return RawbitsToFloat(FloatToRawbits(num) | kFP32QuietNaNMask);
488 }
489 
490 
ToQuietNaN(internal::SimFloat16 num)491 inline 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 
499 // Fused multiply-add.
FusedMultiplyAdd(double op1,double op2,double a)500 inline double FusedMultiplyAdd(double op1, double op2, double a) {
501   return fma(op1, op2, a);
502 }
503 
504 
FusedMultiplyAdd(float op1,float op2,float a)505 inline float FusedMultiplyAdd(float op1, float op2, float a) {
506   return fmaf(op1, op2, a);
507 }
508 
509 
LowestSetBit(uint64_t value)510 inline uint64_t LowestSetBit(uint64_t value) {
511   return value & UnsignedNegate(value);
512 }
513 
514 
515 template <typename T>
HighestSetBitPosition(T value)516 inline int HighestSetBitPosition(T value) {
517   VIXL_ASSERT(value != 0);
518   return (sizeof(value) * 8 - 1) - CountLeadingZeros(value);
519 }
520 
521 
522 template <typename V>
WhichPowerOf2(V value)523 inline int WhichPowerOf2(V value) {
524   VIXL_ASSERT(IsPowerOf2(value));
525   return CountTrailingZeros(value);
526 }
527 
528 
529 unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size);
530 
531 
532 int BitCount(uint64_t value);
533 
534 
535 template <typename T>
ReverseBits(T value)536 T 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 
548 template <typename T>
SignExtend(T val,int size_in_bits)549 inline 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);
552   val &= mask;
553   T sign_bits = -((val >> (size_in_bits - 1)) << size_in_bits);
554   val |= sign_bits;
555   return val;
556 }
557 
558 
559 template <typename T>
ReverseBytes(T value,int block_bytes_log2)560 T ReverseBytes(T value, int block_bytes_log2) {
561   VIXL_ASSERT((sizeof(value) == 4) || (sizeof(value) == 8));
562   VIXL_ASSERT((uint64_t{1} << 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];
566   uint64_t mask = UINT64_C(0xff00000000000000);
567   for (int i = 7; i >= 0; i--) {
568     bytes[i] =
569         static_cast<uint8_t>((static_cast<uint64_t>(value) & mask) >> (i * 8));
570     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));
578   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}};
581   uint64_t temp = 0;
582   for (int i = 0; i < 8; i++) {
583     temp <<= 8;
584     temp |= bytes[permute_table[block_bytes_log2 - 1][i]];
585   }
586 
587   T result;
588   VIXL_STATIC_ASSERT(sizeof(result) <= sizeof(temp));
589   memcpy(&result, &temp, sizeof(result));
590   return result;
591 }
592 
593 template <unsigned MULTIPLE, typename T>
IsMultiple(T value)594 inline bool IsMultiple(T value) {
595   VIXL_ASSERT(IsPowerOf2(MULTIPLE));
596   return (value & (MULTIPLE - 1)) == 0;
597 }
598 
599 template <typename T>
IsMultiple(T value,unsigned multiple)600 inline bool IsMultiple(T value, unsigned multiple) {
601   VIXL_ASSERT(IsPowerOf2(multiple));
602   return (value & (multiple - 1)) == 0;
603 }
604 
605 template <typename T>
IsAligned(T pointer,int alignment)606 inline bool IsAligned(T pointer, int alignment) {
607   VIXL_ASSERT(IsPowerOf2(alignment));
608   return (pointer & (alignment - 1)) == 0;
609 }
610 
611 // Pointer alignment
612 // TODO: rename/refactor to make it specific to instructions.
613 template <unsigned ALIGN, typename T>
IsAligned(T pointer)614 inline bool IsAligned(T pointer) {
615   VIXL_ASSERT(sizeof(pointer) == sizeof(intptr_t));  // NOLINT(runtime/sizeof)
616   // Use C-style casts to get static_cast behaviour for integral types (T), and
617   // reinterpret_cast behaviour for other types.
618   return IsAligned((intptr_t)(pointer), ALIGN);
619 }
620 
621 template <typename T>
IsWordAligned(T pointer)622 bool IsWordAligned(T pointer) {
623   return IsAligned<4>(pointer);
624 }
625 
626 template <unsigned BITS, typename T>
IsRepeatingPattern(T value)627 bool 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)) && __clang_major__ >= 17 && \
633     __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 
644 template <typename T>
AllBytesMatch(T value)645 bool AllBytesMatch(T value) {
646   return IsRepeatingPattern<kBitsPerByte>(value);
647 }
648 
649 template <typename T>
AllHalfwordsMatch(T value)650 bool AllHalfwordsMatch(T value) {
651   return IsRepeatingPattern<kBitsPerByte * 2>(value);
652 }
653 
654 template <typename T>
AllWordsMatch(T value)655 bool AllWordsMatch(T value) {
656   return IsRepeatingPattern<kBitsPerByte * 4>(value);
657 }
658 
659 // Increment a pointer until it has the specified alignment. The alignment must
660 // be a power of two.
661 template <class T>
AlignUp(T pointer,typename Unsigned<sizeof (T)* kBitsPerByte>::type alignment)662 T AlignUp(T pointer,
663           typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
664   VIXL_ASSERT(IsPowerOf2(alignment));
665   // Use C-style casts to get static_cast behaviour for integral types (T), and
666   // reinterpret_cast behaviour for other types.
667 
668   typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
669       (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
670   VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
671 
672   size_t mask = alignment - 1;
673   T result = (T)((pointer_raw + mask) & ~mask);
674   VIXL_ASSERT(result >= pointer);
675 
676   return result;
677 }
678 
679 // Decrement a pointer until it has the specified alignment. The alignment must
680 // be a power of two.
681 template <class T>
AlignDown(T pointer,typename Unsigned<sizeof (T)* kBitsPerByte>::type alignment)682 T AlignDown(T pointer,
683             typename Unsigned<sizeof(T) * kBitsPerByte>::type alignment) {
684   VIXL_ASSERT(IsPowerOf2(alignment));
685   // Use C-style casts to get static_cast behaviour for integral types (T), and
686   // reinterpret_cast behaviour for other types.
687 
688   typename Unsigned<sizeof(T)* kBitsPerByte>::type pointer_raw =
689       (typename Unsigned<sizeof(T) * kBitsPerByte>::type) pointer;
690   VIXL_STATIC_ASSERT(sizeof(pointer) <= sizeof(pointer_raw));
691 
692   size_t mask = alignment - 1;
693   return (T)(pointer_raw & ~mask);
694 }
695 
696 
697 template <typename T>
ExtractBit(T value,unsigned bit)698 inline T ExtractBit(T value, unsigned bit) {
699   return (value >> bit) & T(1);
700 }
701 
702 template <typename Ts, typename Td>
ExtractBits(Ts value,int least_significant_bit,Td mask)703 inline Td ExtractBits(Ts value, int least_significant_bit, Td mask) {
704   return Td((value >> least_significant_bit) & Ts(mask));
705 }
706 
707 template <typename Ts, typename Td>
AssignBit(Td & dst,int bit,Ts value)708 inline void AssignBit(Td& dst,  // NOLINT(runtime/references)
709                       int bit,
710                       Ts value) {
711   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 
719 template <typename Td, typename Ts>
AssignBits(Td & dst,int least_significant_bit,Ts mask,Ts value)720 inline void AssignBits(Td& dst,  // NOLINT(runtime/references)
721                        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 
733 class VFP {
734  public:
FP32ToImm8(float imm)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   }
FP64ToImm8(double imm)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   }
Imm8ToFP32(uint32_t imm8)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   }
Imm8ToFP64(uint32_t imm8)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   }
IsImmFP32(float imm)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   }
IsImmFP64(double imm)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 
826 class 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>
ForEachBit(const ForEachBitHelper & helper)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:
BitField(unsigned size)842   explicit BitField(unsigned size) : bitfield_(size, 0) {}
843 
Set(int i)844   void Set(int i) {
845     VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
846     bitfield_[i] = true;
847   }
848 
Unset(int i)849   void Unset(int i) {
850     VIXL_ASSERT((i >= 0) && (static_cast<size_t>(i) < bitfield_.size()));
851     bitfield_[i] = true;
852   }
853 
IsSet(int i)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>
ForEachBitNotSet(const ForEachBitNotSetHelper & helper)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>
ForEachBitSet(const ForEachBitSetHelper & helper)871   bool ForEachBitSet(const ForEachBitSetHelper& helper) {
872     return ForEachBit<ForEachBitSetHelper, true>(helper);
873   }
874 
875  private:
876   std::vector<bool> bitfield_;
877 };
878 
879 namespace internal {
880 
881 typedef int64_t Int64;
882 class Uint64;
883 class Uint128;
884 
885 class Uint32 {
886   uint32_t data_;
887 
888  public:
889   // Unlike uint32_t, Uint32 has a default constructor.
Uint32()890   Uint32() { data_ = 0; }
Uint32(uint32_t data)891   explicit Uint32(uint32_t data) : data_(data) {}
892   inline explicit Uint32(Uint64 data);
Get()893   uint32_t Get() const { return data_; }
894   template <int N>
GetSigned()895   int32_t GetSigned() const {
896     return ExtractSignedBitfield32(N - 1, 0, data_);
897   }
GetSigned()898   int32_t GetSigned() const { return data_; }
899   Uint32 operator~() const { return Uint32(~data_); }
900   Uint32 operator-() const { return Uint32(UnsignedNegate(data_)); }
901   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 
947 class Uint64 {
948   uint64_t data_;
949 
950  public:
951   // Unlike uint64_t, Uint64 has a default constructor.
Uint64()952   Uint64() { data_ = 0; }
Uint64(uint64_t data)953   explicit Uint64(uint64_t data) : data_(data) {}
Uint64(Uint32 data)954   explicit Uint64(Uint32 data) : data_(data.Get()) {}
955   inline explicit Uint64(Uint128 data);
Get()956   uint64_t Get() const { return data_; }
GetSigned(int N)957   int64_t GetSigned(int N) const {
958     return ExtractSignedBitfield64(N - 1, 0, data_);
959   }
GetSigned()960   int64_t GetSigned() const { return data_; }
ToUint32()961   Uint32 ToUint32() const {
962     VIXL_ASSERT((data_ >> 32) == 0);
963     return Uint32(static_cast<uint32_t>(data_));
964   }
GetHigh32()965   Uint32 GetHigh32() const { return Uint32(data_ >> 32); }
GetLow32()966   Uint32 GetLow32() const { return Uint32(data_ & 0xffffffff); }
967   Uint64 operator~() const { return Uint64(~data_); }
968   Uint64 operator-() const { return Uint64(UnsignedNegate(data_)); }
969   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 
1014 class Uint128 {
1015   uint64_t data_high_;
1016   uint64_t data_low_;
1017 
1018  public:
Uint128()1019   Uint128() : data_high_(0), data_low_(0) {}
Uint128(uint64_t data_low)1020   explicit Uint128(uint64_t data_low) : data_high_(0), data_low_(data_low) {}
Uint128(Uint64 data_low)1021   explicit Uint128(Uint64 data_low)
1022       : data_high_(0), data_low_(data_low.Get()) {}
Uint128(uint64_t data_high,uint64_t data_low)1023   Uint128(uint64_t data_high, uint64_t data_low)
1024       : data_high_(data_high), data_low_(data_low) {}
ToUint64()1025   Uint64 ToUint64() const {
1026     VIXL_ASSERT(data_high_ == 0);
1027     return Uint64(data_low_);
1028   }
GetHigh64()1029   Uint64 GetHigh64() const { return Uint64(data_high_); }
GetLow64()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 
Uint32(Uint64 data)1068 Uint32::Uint32(Uint64 data) : data_(data.ToUint32().Get()) {}
Uint64(Uint128 data)1069 Uint64::Uint64(Uint128 data) : data_(data.ToUint64().Get()) {}
1070 
1071 Int64 BitCount(Uint32 value);
1072 
1073 // The algorithm used is adapted from the one described in section 8.2 of
1074 // Hacker's Delight, by Henry S. Warren, Jr.
1075 template <unsigned N, typename T>
MultiplyHigh(T u,T v)1076 int64_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 
1109 }  // namespace internal
1110 
1111 // The default NaN values (for FPCR.DN=1).
1112 extern const double kFP64DefaultNaN;
1113 extern const float kFP32DefaultNaN;
1114 extern const Float16 kFP16DefaultNaN;
1115 
1116 // Floating-point infinity values.
1117 extern const Float16 kFP16PositiveInfinity;
1118 extern const Float16 kFP16NegativeInfinity;
1119 extern const float kFP32PositiveInfinity;
1120 extern const float kFP32NegativeInfinity;
1121 extern const double kFP64PositiveInfinity;
1122 extern const double kFP64NegativeInfinity;
1123 
1124 // Floating-point zero values.
1125 extern const Float16 kFP16PositiveZero;
1126 extern const Float16 kFP16NegativeZero;
1127 
1128 // AArch64 floating-point specifics. These match IEEE-754.
1129 const unsigned kDoubleMantissaBits = 52;
1130 const unsigned kDoubleExponentBits = 11;
1131 const unsigned kFloatMantissaBits = 23;
1132 const unsigned kFloatExponentBits = 8;
1133 const unsigned kFloat16MantissaBits = 10;
1134 const unsigned kFloat16ExponentBits = 5;
1135 
1136 enum 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 
1149 enum 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.
1166 template <class T, int ebits, int mbits>
FPRound(int64_t sign,int64_t exponent,uint64_t mantissa,FPRounding round_mode)1167 T 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).
1274     shift += static_cast<int>(-exponent + 1);
1275 
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.
FPRoundToDouble(int64_t sign,int64_t exponent,uint64_t mantissa,FPRounding round_mode)1354 inline 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.
FPRoundToFloat16(int64_t sign,int64_t exponent,uint64_t mantissa,FPRounding round_mode)1368 inline Float16 FPRoundToFloat16(int64_t sign,
1369                                 int64_t exponent,
1370                                 uint64_t mantissa,
1371                                 FPRounding round_mode) {
1372   return RawbitsToFloat16(
1373       FPRound<uint16_t, kFloat16ExponentBits, kFloat16MantissaBits>(
1374           sign, exponent, mantissa, round_mode));
1375 }
1376 
1377 
1378 // See FPRound for a description of this function.
FPRoundToFloat(int64_t sign,int64_t exponent,uint64_t mantissa,FPRounding round_mode)1379 static 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 
1392 float FPToFloat(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1393 float FPToFloat(double value,
1394                 FPRounding round_mode,
1395                 UseDefaultNaN DN,
1396                 bool* exception = NULL);
1397 
1398 double FPToDouble(Float16 value, UseDefaultNaN DN, bool* exception = NULL);
1399 double FPToDouble(float value, UseDefaultNaN DN, bool* exception = NULL);
1400 
1401 Float16 FPToFloat16(float value,
1402                     FPRounding round_mode,
1403                     UseDefaultNaN DN,
1404                     bool* exception = NULL);
1405 
1406 Float16 FPToFloat16(double value,
1407                     FPRounding round_mode,
1408                     UseDefaultNaN DN,
1409                     bool* exception = NULL);
1410 
1411 // Like static_cast<T>(value), but with specialisations for the Float16 type.
1412 template <typename T, typename F>
StaticCastFPTo(F value)1413 T StaticCastFPTo(F value) {
1414   return static_cast<T>(value);
1415 }
1416 
1417 template <>
1418 inline float StaticCastFPTo<float, Float16>(Float16 value) {
1419   return FPToFloat(value, kIgnoreDefaultNaN);
1420 }
1421 
1422 template <>
1423 inline double StaticCastFPTo<double, Float16>(Float16 value) {
1424   return FPToDouble(value, kIgnoreDefaultNaN);
1425 }
1426 
1427 template <>
1428 inline Float16 StaticCastFPTo<Float16, float>(float value) {
1429   return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1430 }
1431 
1432 template <>
1433 inline Float16 StaticCastFPTo<Float16, double>(double value) {
1434   return FPToFloat16(value, FPTieEven, kIgnoreDefaultNaN);
1435 }
1436 
1437 template <typename T>
FPToRawbitsWithSize(unsigned size_in_bits,T value)1438 uint64_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 }
1450 
1451 template <typename T>
RawbitsWithSizeToFP(unsigned size_in_bits,uint64_t value)1452 T 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 
1466 // 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.
1469 constexpr 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 
1483 constexpr uint32_t operator""_h(const char* x, size_t) { return Hash(x); }
1484 
1485 }  // namespace vixl
1486 
1487 #endif  // VIXL_UTILS_H
1488