1 //===-- InstructionUtils.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_InstructionUtils_h_
11 #define lldb_InstructionUtils_h_
16 // Common utilities for manipulating instruction bit fields.
18 namespace lldb_private {
20 // Return the bit field(s) from the most significant bit (msbit) to the
21 // least significant bit (lsbit) of a 64-bit unsigned value.
22 static inline uint64_t Bits64(const uint64_t bits, const uint32_t msbit,
23 const uint32_t lsbit) {
24 assert(msbit < 64 && lsbit <= msbit);
25 return (bits >> lsbit) & ((1ull << (msbit - lsbit + 1)) - 1);
28 // Return the bit field(s) from the most significant bit (msbit) to the
29 // least significant bit (lsbit) of a 32-bit unsigned value.
30 static inline uint32_t Bits32(const uint32_t bits, const uint32_t msbit,
31 const uint32_t lsbit) {
32 assert(msbit < 32 && lsbit <= msbit);
33 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
36 // Return the bit value from the 'bit' position of a 32-bit unsigned value.
37 static inline uint32_t Bit32(const uint32_t bits, const uint32_t bit) {
38 return (bits >> bit) & 1u;
41 static inline uint64_t Bit64(const uint64_t bits, const uint32_t bit) {
42 return (bits >> bit) & 1ull;
45 // Set the bit field(s) from the most significant bit (msbit) to the
46 // least significant bit (lsbit) of a 32-bit unsigned value to 'val'.
47 static inline void SetBits32(uint32_t &bits, const uint32_t msbit,
48 const uint32_t lsbit, const uint32_t val) {
49 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
50 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
51 bits &= ~(mask << lsbit);
52 bits |= (val & mask) << lsbit;
55 // Set the 'bit' position of a 32-bit unsigned value to 'val'.
56 static inline void SetBit32(uint32_t &bits, const uint32_t bit,
58 SetBits32(bits, bit, bit, val);
61 // Rotate a 32-bit unsigned value right by the specified amount.
62 static inline uint32_t Rotr32(uint32_t bits, uint32_t amt) {
63 assert(amt < 32 && "Invalid rotate amount");
64 return (bits >> amt) | (bits << ((32 - amt) & 31));
67 // Rotate a 32-bit unsigned value left by the specified amount.
68 static inline uint32_t Rotl32(uint32_t bits, uint32_t amt) {
69 assert(amt < 32 && "Invalid rotate amount");
70 return (bits << amt) | (bits >> ((32 - amt) & 31));
73 // Create a mask that starts at bit zero and includes "bit"
74 static inline uint64_t MaskUpToBit(const uint64_t bit) {
77 return (1ull << (bit + 1ull)) - 1ull;
80 // Return an integer result equal to the number of bits of x that are ones.
81 static inline uint32_t BitCount(uint64_t x) {
82 // c accumulates the total bits set in x
85 x &= x - 1; // clear the least significant bit set
90 static inline bool BitIsSet(const uint64_t value, const uint64_t bit) {
91 return (value & (1ull << bit)) != 0;
94 static inline bool BitIsClear(const uint64_t value, const uint64_t bit) {
95 return (value & (1ull << bit)) == 0;
98 static inline uint64_t UnsignedBits(const uint64_t value, const uint64_t msbit,
99 const uint64_t lsbit) {
100 uint64_t result = value >> lsbit;
101 result &= MaskUpToBit(msbit - lsbit);
105 static inline int64_t SignedBits(const uint64_t value, const uint64_t msbit,
106 const uint64_t lsbit) {
107 uint64_t result = UnsignedBits(value, msbit, lsbit);
108 if (BitIsSet(value, msbit)) {
110 result |= ~MaskUpToBit(msbit - lsbit);
115 } // namespace lldb_private
117 #endif // lldb_InstructionUtils_h_