Merge branch 'omap-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / include / linux / bitops.h
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 #include <asm/types.h>
4
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
12
13 /*
14  * Include this here because some architectures need generic_ffs/fls in
15  * scope
16  */
17 #include <asm/bitops.h>
18
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))
23
24 static __inline__ int get_bitmask_order(unsigned int count)
25 {
26         int order;
27
28         order = fls(count);
29         return order;   /* We could be slightly more clever with -1 here... */
30 }
31
32 static __inline__ int get_count_order(unsigned int count)
33 {
34         int order;
35
36         order = fls(count) - 1;
37         if (count & (count - 1))
38                 order++;
39         return order;
40 }
41
42 static inline unsigned long hweight_long(unsigned long w)
43 {
44         return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
45 }
46
47 /*
48  * Clearly slow versions of the hweightN() functions, their benefit is
49  * of course compile time evaluation of constant arguments.
50  */
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))) )
61
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))
65
66 /*
67  * Type invariant version that simply casts things to the
68  * largest type.
69  */
70 #define HWEIGHT(w)   HWEIGHT64((u64)(w))
71
72 /**
73  * rol32 - rotate a 32-bit value left
74  * @word: value to rotate
75  * @shift: bits to roll
76  */
77 static inline __u32 rol32(__u32 word, unsigned int shift)
78 {
79         return (word << shift) | (word >> (32 - shift));
80 }
81
82 /**
83  * ror32 - rotate a 32-bit value right
84  * @word: value to rotate
85  * @shift: bits to roll
86  */
87 static inline __u32 ror32(__u32 word, unsigned int shift)
88 {
89         return (word >> shift) | (word << (32 - shift));
90 }
91
92 /**
93  * rol16 - rotate a 16-bit value left
94  * @word: value to rotate
95  * @shift: bits to roll
96  */
97 static inline __u16 rol16(__u16 word, unsigned int shift)
98 {
99         return (word << shift) | (word >> (16 - shift));
100 }
101
102 /**
103  * ror16 - rotate a 16-bit value right
104  * @word: value to rotate
105  * @shift: bits to roll
106  */
107 static inline __u16 ror16(__u16 word, unsigned int shift)
108 {
109         return (word >> shift) | (word << (16 - shift));
110 }
111
112 /**
113  * rol8 - rotate an 8-bit value left
114  * @word: value to rotate
115  * @shift: bits to roll
116  */
117 static inline __u8 rol8(__u8 word, unsigned int shift)
118 {
119         return (word << shift) | (word >> (8 - shift));
120 }
121
122 /**
123  * ror8 - rotate an 8-bit value right
124  * @word: value to rotate
125  * @shift: bits to roll
126  */
127 static inline __u8 ror8(__u8 word, unsigned int shift)
128 {
129         return (word >> shift) | (word << (8 - shift));
130 }
131
132 static inline unsigned fls_long(unsigned long l)
133 {
134         if (sizeof(l) == 4)
135                 return fls(l);
136         return fls64(l);
137 }
138
139 /**
140  * __ffs64 - find first set bit in a 64 bit word
141  * @word: The 64 bit word
142  *
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.
146  */
147 static inline unsigned long __ffs64(u64 word)
148 {
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);
156 }
157
158 #ifdef __KERNEL__
159 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
160
161 /**
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
165  *
166  * Returns the bit number of the first set bit.
167  */
168 extern unsigned long find_first_bit(const unsigned long *addr,
169                                     unsigned long size);
170
171 /**
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
175  *
176  * Returns the bit number of the first cleared bit.
177  */
178 extern unsigned long find_first_zero_bit(const unsigned long *addr,
179                                          unsigned long size);
180 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
181
182 #ifdef CONFIG_GENERIC_FIND_LAST_BIT
183 /**
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
187  *
188  * Returns the bit number of the first set bit, or size.
189  */
190 extern unsigned long find_last_bit(const unsigned long *addr,
191                                    unsigned long size);
192 #endif /* CONFIG_GENERIC_FIND_LAST_BIT */
193
194 #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
195
196 /**
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
201  */
202 extern unsigned long find_next_bit(const unsigned long *addr,
203                                    unsigned long size, unsigned long offset);
204
205 /**
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
210  */
211
212 extern unsigned long find_next_zero_bit(const unsigned long *addr,
213                                         unsigned long size,
214                                         unsigned long offset);
215
216 #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
217 #endif /* __KERNEL__ */
218 #endif