blob: 8ed2a71e36c26fcb011b6b963dcfc5efa60b2898 [file] [log] [blame]
Johnny Chen74889b22011-01-26 01:00:55 +00001//===-- lldb_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
17static inline uint32_t
18Bits32 (const uint32_t value, const uint32_t msbit, const uint32_t lsbit)
19{
20 assert(msbit < 32 && lsbit <= msbit);
21 return (value >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
22}
23
Johnny Chenea745e82011-02-04 23:02:47 +000024static inline void
25SetBits32(uint32_t &bits, unsigned msbit, unsigned lsbit, unsigned val)
26{
27 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
28 uint32_t mask = ((1 << (msbit - lsbit + 1)) - 1);
29 bits &= ~(mask << lsbit);
30 bits |= (val & mask) << lsbit;
31}
32
Johnny Chen74889b22011-01-26 01:00:55 +000033// Create a mask that starts at bit zero and includes "bit"
34static inline uint64_t
35MaskUpToBit (const uint64_t bit)
36{
37 return (1ull << (bit + 1ull)) - 1ull;
38}
39
40// Returns an integer result equal to the number of bits of x that are ones.
41static inline uint32_t
42BitCount (uint64_t x)
43{
44 // c accumulates the total bits set in x
45 uint32_t c;
46 for (c = 0; x; ++c)
47 {
48 x &= x - 1; // clear the least significant bit set
49 }
50 return c;
51}
52
53static inline bool
54BitIsSet (const uint64_t value, const uint64_t bit)
55{
56 return (value & (1ull << bit)) != 0;
57}
58
59static inline bool
60BitIsClear (const uint64_t value, const uint64_t bit)
61{
62 return (value & (1ull << bit)) == 0;
63}
64
65static inline uint64_t
66UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
67{
68 uint64_t result = value >> lsbit;
69 result &= MaskUpToBit (msbit - lsbit);
70 return result;
71}
72
73static inline int64_t
74SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
75{
76 uint64_t result = UnsignedBits (value, msbit, lsbit);
77 if (BitIsSet(value, msbit))
78 {
79 // Sign extend
80 result |= ~MaskUpToBit (msbit - lsbit);
81 }
82 return result;
83}
84
85} // namespace lldb_private
86
87#endif // lldb_InstructionUtils_h_