1 //===-- InstructionUtils.h --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef lldb_InstructionUtils_h_ 11 #define lldb_InstructionUtils_h_ 12 13 // Common utilities for manipulating instruction bit fields. 14 15 namespace lldb_private { 16 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) 21 { 22 assert(msbit < 64 && lsbit <= msbit); 23 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1); 24 } 25 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) 30 { 31 assert(msbit < 32 && lsbit <= msbit); 32 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1); 33 } 34 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) 38 { 39 return (bits >> bit) & 1u; 40 } 41 42 static inline uint64_t 43 Bit64 (const uint64_t bits, const uint32_t bit) 44 { 45 return (bits >> bit) & 1ull; 46 } 47 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'. 50 static inline void 51 SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val) 52 { 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; 57 } 58 59 // Set the 'bit' position of a 32-bit unsigned value to 'val'. 60 static inline void 61 SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val) 62 { 63 SetBits32(bits, bit, bit, val); 64 } 65 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) 69 { 70 assert(amt < 32 && "Invalid rotate amount"); 71 return (bits >> amt) | (bits << ((32-amt)&31)); 72 } 73 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) 77 { 78 assert(amt < 32 && "Invalid rotate amount"); 79 return (bits << amt) | (bits >> ((32-amt)&31)); 80 } 81 82 // Create a mask that starts at bit zero and includes "bit" 83 static inline uint64_t 84 MaskUpToBit (const uint64_t bit) 85 { 86 return (1ull << (bit + 1ull)) - 1ull; 87 } 88 89 // Return an integer result equal to the number of bits of x that are ones. 90 static inline uint32_t 91 BitCount (uint64_t x) 92 { 93 // c accumulates the total bits set in x 94 uint32_t c; 95 for (c = 0; x; ++c) 96 { 97 x &= x - 1; // clear the least significant bit set 98 } 99 return c; 100 } 101 102 static inline bool 103 BitIsSet (const uint64_t value, const uint64_t bit) 104 { 105 return (value & (1ull << bit)) != 0; 106 } 107 108 static inline bool 109 BitIsClear (const uint64_t value, const uint64_t bit) 110 { 111 return (value & (1ull << bit)) == 0; 112 } 113 114 static inline uint64_t 115 UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) 116 { 117 uint64_t result = value >> lsbit; 118 result &= MaskUpToBit (msbit - lsbit); 119 return result; 120 } 121 122 static inline int64_t 123 SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) 124 { 125 uint64_t result = UnsignedBits (value, msbit, lsbit); 126 if (BitIsSet(value, msbit)) 127 { 128 // Sign extend 129 result |= ~MaskUpToBit (msbit - lsbit); 130 } 131 return result; 132 } 133 134 } // namespace lldb_private 135 136 #endif // lldb_InstructionUtils_h_ 137