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 15namespace 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. 19static inline uint64_t 20Bits64 (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. 28static inline uint32_t 29Bits32 (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. 36static inline uint32_t 37Bit32 (const uint32_t bits, const uint32_t bit) 38{ 39 return (bits >> bit) & 1u; 40} 41 42static inline uint64_t 43Bit64 (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'. 50static inline void 51SetBits32(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'. 60static inline void 61SetBit32(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. 67static inline uint32_t 68Rotr32 (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. 75static inline uint32_t 76Rotl32 (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" 83static inline uint64_t 84MaskUpToBit (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. 90static inline uint32_t 91BitCount (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 102static inline bool 103BitIsSet (const uint64_t value, const uint64_t bit) 104{ 105 return (value & (1ull << bit)) != 0; 106} 107 108static inline bool 109BitIsClear (const uint64_t value, const uint64_t bit) 110{ 111 return (value & (1ull << bit)) == 0; 112} 113 114static inline uint64_t 115UnsignedBits (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 122static inline int64_t 123SignedBits (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