blob: 1dc3febb866e474609c00828423060b5c3887688 [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
24// Create a mask that starts at bit zero and includes "bit"
25static inline uint64_t
26MaskUpToBit (const uint64_t bit)
27{
28 return (1ull << (bit + 1ull)) - 1ull;
29}
30
31// Returns an integer result equal to the number of bits of x that are ones.
32static inline uint32_t
33BitCount (uint64_t x)
34{
35 // c accumulates the total bits set in x
36 uint32_t c;
37 for (c = 0; x; ++c)
38 {
39 x &= x - 1; // clear the least significant bit set
40 }
41 return c;
42}
43
44static inline bool
45BitIsSet (const uint64_t value, const uint64_t bit)
46{
47 return (value & (1ull << bit)) != 0;
48}
49
50static inline bool
51BitIsClear (const uint64_t value, const uint64_t bit)
52{
53 return (value & (1ull << bit)) == 0;
54}
55
56static inline uint64_t
57UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
58{
59 uint64_t result = value >> lsbit;
60 result &= MaskUpToBit (msbit - lsbit);
61 return result;
62}
63
64static inline int64_t
65SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
66{
67 uint64_t result = UnsignedBits (value, msbit, lsbit);
68 if (BitIsSet(value, msbit))
69 {
70 // Sign extend
71 result |= ~MaskUpToBit (msbit - lsbit);
72 }
73 return result;
74}
75
76} // namespace lldb_private
77
78#endif // lldb_InstructionUtils_h_