Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bitops.h
Go to the documentation of this file.
1 #ifndef _PERF_LINUX_BITOPS_H_
2 #define _PERF_LINUX_BITOPS_H_
3 
4 #include <linux/kernel.h>
5 #include <linux/compiler.h>
6 #include <asm/hweight.h>
7 
8 #ifndef __WORDSIZE
9 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
10 #endif
11 
12 #define BITS_PER_LONG __WORDSIZE
13 #define BITS_PER_BYTE 8
14 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
15 #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
16 #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
17 
18 #define for_each_set_bit(bit, addr, size) \
19  for ((bit) = find_first_bit((addr), (size)); \
20  (bit) < (size); \
21  (bit) = find_next_bit((addr), (size), (bit) + 1))
22 
23 /* same as for_each_set_bit() but use bit as value to start with */
24 #define for_each_set_bit_from(bit, addr, size) \
25  for ((bit) = find_next_bit((addr), (size), (bit)); \
26  (bit) < (size); \
27  (bit) = find_next_bit((addr), (size), (bit) + 1))
28 
29 static inline void set_bit(int nr, unsigned long *addr)
30 {
31  addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
32 }
33 
34 static inline void clear_bit(int nr, unsigned long *addr)
35 {
36  addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
37 }
38 
39 static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
40 {
41  return ((1UL << (nr % BITS_PER_LONG)) &
42  (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
43 }
44 
45 static inline unsigned long hweight_long(unsigned long w)
46 {
47  return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
48 }
49 
50 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
51 
58 static __always_inline unsigned long __ffs(unsigned long word)
59 {
60  int num = 0;
61 
62 #if BITS_PER_LONG == 64
63  if ((word & 0xffffffff) == 0) {
64  num += 32;
65  word >>= 32;
66  }
67 #endif
68  if ((word & 0xffff) == 0) {
69  num += 16;
70  word >>= 16;
71  }
72  if ((word & 0xff) == 0) {
73  num += 8;
74  word >>= 8;
75  }
76  if ((word & 0xf) == 0) {
77  num += 4;
78  word >>= 4;
79  }
80  if ((word & 0x3) == 0) {
81  num += 2;
82  word >>= 2;
83  }
84  if ((word & 0x1) == 0)
85  num += 1;
86  return num;
87 }
88 
89 /*
90  * Find the first set bit in a memory region.
91  */
92 static inline unsigned long
93 find_first_bit(const unsigned long *addr, unsigned long size)
94 {
95  const unsigned long *p = addr;
96  unsigned long result = 0;
97  unsigned long tmp;
98 
99  while (size & ~(BITS_PER_LONG-1)) {
100  if ((tmp = *(p++)))
101  goto found;
102  result += BITS_PER_LONG;
103  size -= BITS_PER_LONG;
104  }
105  if (!size)
106  return result;
107 
108  tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
109  if (tmp == 0UL) /* Are any bits set? */
110  return result + size; /* Nope. */
111 found:
112  return result + __ffs(tmp);
113 }
114 
115 /*
116  * Find the next set bit in a memory region.
117  */
118 static inline unsigned long
119 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
120 {
121  const unsigned long *p = addr + BITOP_WORD(offset);
122  unsigned long result = offset & ~(BITS_PER_LONG-1);
123  unsigned long tmp;
124 
125  if (offset >= size)
126  return size;
127  size -= result;
128  offset %= BITS_PER_LONG;
129  if (offset) {
130  tmp = *(p++);
131  tmp &= (~0UL << offset);
132  if (size < BITS_PER_LONG)
133  goto found_first;
134  if (tmp)
135  goto found_middle;
136  size -= BITS_PER_LONG;
137  result += BITS_PER_LONG;
138  }
139  while (size & ~(BITS_PER_LONG-1)) {
140  if ((tmp = *(p++)))
141  goto found_middle;
142  result += BITS_PER_LONG;
143  size -= BITS_PER_LONG;
144  }
145  if (!size)
146  return result;
147  tmp = *p;
148 
149 found_first:
150  tmp &= (~0UL >> (BITS_PER_LONG - size));
151  if (tmp == 0UL) /* Are any bits set? */
152  return result + size; /* Nope. */
153 found_middle:
154  return result + __ffs(tmp);
155 }
156 
157 #endif