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