V4L/DVB: V4L: dm644x_ccdc: Add Suspend/Resume Support
[linux-2.6/btrfs-unstable.git] / include / linux / bitops.h
blobb796eab5ca75325bcfb9ff1c56c9527aee42feff
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 #include <asm/types.h>
5 #ifdef __KERNEL__
6 #define BIT(nr) (1UL << (nr))
7 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
9 #define BITS_PER_BYTE 8
10 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
11 #endif
14 * Include this here because some architectures need generic_ffs/fls in
15 * scope
17 #include <asm/bitops.h>
19 #define for_each_set_bit(bit, addr, size) \
20 for ((bit) = find_first_bit((addr), (size)); \
21 (bit) < (size); \
22 (bit) = find_next_bit((addr), (size), (bit) + 1))
24 static __inline__ int get_bitmask_order(unsigned int count)
26 int order;
28 order = fls(count);
29 return order; /* We could be slightly more clever with -1 here... */
32 static __inline__ int get_count_order(unsigned int count)
34 int order;
36 order = fls(count) - 1;
37 if (count & (count - 1))
38 order++;
39 return order;
42 static inline unsigned long hweight_long(unsigned long w)
44 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
48 * Clearly slow versions of the hweightN() functions, their benefit is
49 * of course compile time evaluation of constant arguments.
51 #define HWEIGHT8(w) \
52 ( BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + \
53 (!!((w) & (1ULL << 0))) + \
54 (!!((w) & (1ULL << 1))) + \
55 (!!((w) & (1ULL << 2))) + \
56 (!!((w) & (1ULL << 3))) + \
57 (!!((w) & (1ULL << 4))) + \
58 (!!((w) & (1ULL << 5))) + \
59 (!!((w) & (1ULL << 6))) + \
60 (!!((w) & (1ULL << 7))) )
62 #define HWEIGHT16(w) (HWEIGHT8(w) + HWEIGHT8((w) >> 8))
63 #define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
64 #define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
67 * Type invariant version that simply casts things to the
68 * largest type.
70 #define HWEIGHT(w) HWEIGHT64((u64)(w))
72 /**
73 * rol32 - rotate a 32-bit value left
74 * @word: value to rotate
75 * @shift: bits to roll
77 static inline __u32 rol32(__u32 word, unsigned int shift)
79 return (word << shift) | (word >> (32 - shift));
82 /**
83 * ror32 - rotate a 32-bit value right
84 * @word: value to rotate
85 * @shift: bits to roll
87 static inline __u32 ror32(__u32 word, unsigned int shift)
89 return (word >> shift) | (word << (32 - shift));
92 /**
93 * rol16 - rotate a 16-bit value left
94 * @word: value to rotate
95 * @shift: bits to roll
97 static inline __u16 rol16(__u16 word, unsigned int shift)
99 return (word << shift) | (word >> (16 - shift));
103 * ror16 - rotate a 16-bit value right
104 * @word: value to rotate
105 * @shift: bits to roll
107 static inline __u16 ror16(__u16 word, unsigned int shift)
109 return (word >> shift) | (word << (16 - shift));
113 * rol8 - rotate an 8-bit value left
114 * @word: value to rotate
115 * @shift: bits to roll
117 static inline __u8 rol8(__u8 word, unsigned int shift)
119 return (word << shift) | (word >> (8 - shift));
123 * ror8 - rotate an 8-bit value right
124 * @word: value to rotate
125 * @shift: bits to roll
127 static inline __u8 ror8(__u8 word, unsigned int shift)
129 return (word >> shift) | (word << (8 - shift));
132 static inline unsigned fls_long(unsigned long l)
134 if (sizeof(l) == 4)
135 return fls(l);
136 return fls64(l);
140 * __ffs64 - find first set bit in a 64 bit word
141 * @word: The 64 bit word
143 * On 64 bit arches this is a synomyn for __ffs
144 * The result is not defined if no bits are set, so check that @word
145 * is non-zero before calling this.
147 static inline unsigned long __ffs64(u64 word)
149 #if BITS_PER_LONG == 32
150 if (((u32)word) == 0UL)
151 return __ffs((u32)(word >> 32)) + 32;
152 #elif BITS_PER_LONG != 64
153 #error BITS_PER_LONG not 32 or 64
154 #endif
155 return __ffs((unsigned long)word);
158 #ifdef __KERNEL__
159 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
162 * find_first_bit - find the first set bit in a memory region
163 * @addr: The address to start the search at
164 * @size: The maximum size to search
166 * Returns the bit number of the first set bit.
168 extern unsigned long find_first_bit(const unsigned long *addr,
169 unsigned long size);
172 * find_first_zero_bit - find the first cleared bit in a memory region
173 * @addr: The address to start the search at
174 * @size: The maximum size to search
176 * Returns the bit number of the first cleared bit.
178 extern unsigned long find_first_zero_bit(const unsigned long *addr,
179 unsigned long size);
180 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
182 #ifdef CONFIG_GENERIC_FIND_LAST_BIT
184 * find_last_bit - find the last set bit in a memory region
185 * @addr: The address to start the search at
186 * @size: The maximum size to search
188 * Returns the bit number of the first set bit, or size.
190 extern unsigned long find_last_bit(const unsigned long *addr,
191 unsigned long size);
192 #endif /* CONFIG_GENERIC_FIND_LAST_BIT */
194 #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
197 * find_next_bit - find the next set bit in a memory region
198 * @addr: The address to base the search on
199 * @offset: The bitnumber to start searching at
200 * @size: The bitmap size in bits
202 extern unsigned long find_next_bit(const unsigned long *addr,
203 unsigned long size, unsigned long offset);
206 * find_next_zero_bit - find the next cleared bit in a memory region
207 * @addr: The address to base the search on
208 * @offset: The bitnumber to start searching at
209 * @size: The bitmap size in bits
212 extern unsigned long find_next_zero_bit(const unsigned long *addr,
213 unsigned long size,
214 unsigned long offset);
216 #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
217 #endif /* __KERNEL__ */
218 #endif