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_
13 // Common utilities for manipulating instruction bit fields.
15 namespace lldb_private {
17 // Return the bit field(s) from the most significant bit (msbit) to the
18 // least significant bit (lsbit) of a 64-bit unsigned value.
19 static inline uint64_t
20 Bits64 (const uint64_t bits, const uint32_t msbit, const uint32_t lsbit)
22 assert(msbit < 64 && lsbit <= msbit);
23 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
26 // Return the bit field(s) from the most significant bit (msbit) to the
27 // least significant bit (lsbit) of a 32-bit unsigned value.
28 static inline uint32_t
29 Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
31 assert(msbit < 32 && lsbit <= msbit);
32 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
35 // Return the bit value from the 'bit' position of a 32-bit unsigned value.
36 static inline uint32_t
37 Bit32 (const uint32_t bits, const uint32_t bit)
39 return (bits >> bit) & 1u;
42 static inline uint64_t
43 Bit64 (const uint64_t bits, const uint32_t bit)
45 return (bits >> bit) & 1ull;
48 // Set the bit field(s) from the most significant bit (msbit) to the
49 // least significant bit (lsbit) of a 32-bit unsigned value to 'val'.
51 SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
53 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
54 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
55 bits &= ~(mask << lsbit);
56 bits |= (val & mask) << lsbit;
59 // Set the 'bit' position of a 32-bit unsigned value to 'val'.
61 SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
63 SetBits32(bits, bit, bit, val);
66 // Rotate a 32-bit unsigned value right by the specified amount.
67 static inline uint32_t
68 Rotr32 (uint32_t bits, uint32_t amt)
70 assert(amt < 32 && "Invalid rotate amount");
71 return (bits >> amt) | (bits << ((32-amt)&31));
74 // Rotate a 32-bit unsigned value left by the specified amount.
75 static inline uint32_t
76 Rotl32 (uint32_t bits, uint32_t amt)
78 assert(amt < 32 && "Invalid rotate amount");
79 return (bits << amt) | (bits >> ((32-amt)&31));
82 // Create a mask that starts at bit zero and includes "bit"
83 static inline uint64_t
84 MaskUpToBit (const uint64_t bit)
86 return (1ull << (bit + 1ull)) - 1ull;
89 // Return an integer result equal to the number of bits of x that are ones.
90 static inline uint32_t
93 // c accumulates the total bits set in x
97 x &= x - 1; // clear the least significant bit set
103 BitIsSet (const uint64_t value, const uint64_t bit)
105 return (value & (1ull << bit)) != 0;
109 BitIsClear (const uint64_t value, const uint64_t bit)
111 return (value & (1ull << bit)) == 0;
114 static inline uint64_t
115 UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
117 uint64_t result = value >> lsbit;
118 result &= MaskUpToBit (msbit - lsbit);
122 static inline int64_t
123 SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
125 uint64_t result = UnsignedBits (value, msbit, lsbit);
126 if (BitIsSet(value, msbit))
129 result |= ~MaskUpToBit (msbit - lsbit);
134 } // namespace lldb_private
136 #endif // lldb_InstructionUtils_h_