mirror of
https://github.com/intel/llvm.git
synced 2026-01-23 16:06:39 +08:00
SetBits32(uint32_t &bits, uint32_t bit, uint32_t val) to SetBit32(bits, bit, val). llvm-svn: 125312
100 lines
2.4 KiB
C++
100 lines
2.4 KiB
C++
//===-- lldb_InstructionUtils.h ---------------------------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef lldb_InstructionUtils_h_
|
|
#define lldb_InstructionUtils_h_
|
|
|
|
// Common utilities for manipulating instruction bit fields.
|
|
|
|
namespace lldb_private {
|
|
|
|
static inline uint32_t
|
|
Bits32 (const uint32_t value, const uint32_t msbit, const uint32_t lsbit)
|
|
{
|
|
assert(msbit < 32 && lsbit <= msbit);
|
|
return (value >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
|
|
}
|
|
|
|
static inline uint32_t
|
|
Bit32 (const uint32_t value, const uint32_t bit)
|
|
{
|
|
return Bits32(value, bit, bit);
|
|
}
|
|
|
|
static inline void
|
|
SetBits32(uint32_t &bits, unsigned msbit, unsigned lsbit, unsigned val)
|
|
{
|
|
assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
|
|
uint32_t mask = ((1 << (msbit - lsbit + 1)) - 1);
|
|
bits &= ~(mask << lsbit);
|
|
bits |= (val & mask) << lsbit;
|
|
}
|
|
|
|
static inline void
|
|
SetBit32(uint32_t &bits, unsigned bit, unsigned val)
|
|
{
|
|
SetBits32(bits, bit, bit, val);
|
|
}
|
|
|
|
// Create a mask that starts at bit zero and includes "bit"
|
|
static inline uint64_t
|
|
MaskUpToBit (const uint64_t bit)
|
|
{
|
|
return (1ull << (bit + 1ull)) - 1ull;
|
|
}
|
|
|
|
// Returns an integer result equal to the number of bits of x that are ones.
|
|
static inline uint32_t
|
|
BitCount (uint64_t x)
|
|
{
|
|
// c accumulates the total bits set in x
|
|
uint32_t c;
|
|
for (c = 0; x; ++c)
|
|
{
|
|
x &= x - 1; // clear the least significant bit set
|
|
}
|
|
return c;
|
|
}
|
|
|
|
static inline bool
|
|
BitIsSet (const uint64_t value, const uint64_t bit)
|
|
{
|
|
return (value & (1ull << bit)) != 0;
|
|
}
|
|
|
|
static inline bool
|
|
BitIsClear (const uint64_t value, const uint64_t bit)
|
|
{
|
|
return (value & (1ull << bit)) == 0;
|
|
}
|
|
|
|
static inline uint64_t
|
|
UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
|
|
{
|
|
uint64_t result = value >> lsbit;
|
|
result &= MaskUpToBit (msbit - lsbit);
|
|
return result;
|
|
}
|
|
|
|
static inline int64_t
|
|
SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
|
|
{
|
|
uint64_t result = UnsignedBits (value, msbit, lsbit);
|
|
if (BitIsSet(value, msbit))
|
|
{
|
|
// Sign extend
|
|
result |= ~MaskUpToBit (msbit - lsbit);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace lldb_private
|
|
|
|
#endif // lldb_InstructionUtils_h_
|