drm: merge in Linus mainline
[pandora-kernel.git] / include / linux / bitmap.h
1 #ifndef __LINUX_BITMAP_H
2 #define __LINUX_BITMAP_H
3
4 #ifndef __ASSEMBLY__
5
6 #include <linux/types.h>
7 #include <linux/bitops.h>
8 #include <linux/string.h>
9
10 /*
11  * bitmaps provide bit arrays that consume one or more unsigned
12  * longs.  The bitmap interface and available operations are listed
13  * here, in bitmap.h
14  *
15  * Function implementations generic to all architectures are in
16  * lib/bitmap.c.  Functions implementations that are architecture
17  * specific are in various include/asm-<arch>/bitops.h headers
18  * and other arch/<arch> specific files.
19  *
20  * See lib/bitmap.c for more details.
21  */
22
23 /*
24  * The available bitmap operations and their rough meaning in the
25  * case that the bitmap is a single unsigned long are thus:
26  *
27  * bitmap_zero(dst, nbits)                      *dst = 0UL
28  * bitmap_fill(dst, nbits)                      *dst = ~0UL
29  * bitmap_copy(dst, src, nbits)                 *dst = *src
30  * bitmap_and(dst, src1, src2, nbits)           *dst = *src1 & *src2
31  * bitmap_or(dst, src1, src2, nbits)            *dst = *src1 | *src2
32  * bitmap_xor(dst, src1, src2, nbits)           *dst = *src1 ^ *src2
33  * bitmap_andnot(dst, src1, src2, nbits)        *dst = *src1 & ~(*src2)
34  * bitmap_complement(dst, src, nbits)           *dst = ~(*src)
35  * bitmap_equal(src1, src2, nbits)              Are *src1 and *src2 equal?
36  * bitmap_intersects(src1, src2, nbits)         Do *src1 and *src2 overlap?
37  * bitmap_subset(src1, src2, nbits)             Is *src1 a subset of *src2?
38  * bitmap_empty(src, nbits)                     Are all bits zero in *src?
39  * bitmap_full(src, nbits)                      Are all bits set in *src?
40  * bitmap_weight(src, nbits)                    Hamming Weight: number set bits
41  * bitmap_shift_right(dst, src, n, nbits)       *dst = *src >> n
42  * bitmap_shift_left(dst, src, n, nbits)        *dst = *src << n
43  * bitmap_remap(dst, src, old, new, nbits)      *dst = map(old, new)(src)
44  * bitmap_bitremap(oldbit, old, new, nbits)     newbit = map(old, new)(oldbit)
45  * bitmap_scnprintf(buf, len, src, nbits)       Print bitmap src to buf
46  * bitmap_parse(ubuf, ulen, dst, nbits)         Parse bitmap dst from user buf
47  * bitmap_scnlistprintf(buf, len, src, nbits)   Print bitmap src as list to buf
48  * bitmap_parselist(buf, dst, nbits)            Parse bitmap dst from list
49  */
50
51 /*
52  * Also the following operations in asm/bitops.h apply to bitmaps.
53  *
54  * set_bit(bit, addr)                   *addr |= bit
55  * clear_bit(bit, addr)                 *addr &= ~bit
56  * change_bit(bit, addr)                *addr ^= bit
57  * test_bit(bit, addr)                  Is bit set in *addr?
58  * test_and_set_bit(bit, addr)          Set bit and return old value
59  * test_and_clear_bit(bit, addr)        Clear bit and return old value
60  * test_and_change_bit(bit, addr)       Change bit and return old value
61  * find_first_zero_bit(addr, nbits)     Position first zero bit in *addr
62  * find_first_bit(addr, nbits)          Position first set bit in *addr
63  * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
64  * find_next_bit(addr, nbits, bit)      Position next set bit in *addr >= bit
65  */
66
67 /*
68  * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
69  * to declare an array named 'name' of just enough unsigned longs to
70  * contain all bit positions from 0 to 'bits' - 1.
71  */
72
73 /*
74  * lib/bitmap.c provides these functions:
75  */
76
77 extern int __bitmap_empty(const unsigned long *bitmap, int bits);
78 extern int __bitmap_full(const unsigned long *bitmap, int bits);
79 extern int __bitmap_equal(const unsigned long *bitmap1,
80                         const unsigned long *bitmap2, int bits);
81 extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
82                         int bits);
83 extern void __bitmap_shift_right(unsigned long *dst,
84                         const unsigned long *src, int shift, int bits);
85 extern void __bitmap_shift_left(unsigned long *dst,
86                         const unsigned long *src, int shift, int bits);
87 extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
88                         const unsigned long *bitmap2, int bits);
89 extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
90                         const unsigned long *bitmap2, int bits);
91 extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
92                         const unsigned long *bitmap2, int bits);
93 extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
94                         const unsigned long *bitmap2, int bits);
95 extern int __bitmap_intersects(const unsigned long *bitmap1,
96                         const unsigned long *bitmap2, int bits);
97 extern int __bitmap_subset(const unsigned long *bitmap1,
98                         const unsigned long *bitmap2, int bits);
99 extern int __bitmap_weight(const unsigned long *bitmap, int bits);
100
101 extern int bitmap_scnprintf(char *buf, unsigned int len,
102                         const unsigned long *src, int nbits);
103 extern int bitmap_parse(const char __user *ubuf, unsigned int ulen,
104                         unsigned long *dst, int nbits);
105 extern int bitmap_scnlistprintf(char *buf, unsigned int len,
106                         const unsigned long *src, int nbits);
107 extern int bitmap_parselist(const char *buf, unsigned long *maskp,
108                         int nmaskbits);
109 extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
110                 const unsigned long *old, const unsigned long *new, int bits);
111 extern int bitmap_bitremap(int oldbit,
112                 const unsigned long *old, const unsigned long *new, int bits);
113 extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
114 extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
115 extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
116
117 #define BITMAP_LAST_WORD_MASK(nbits)                                    \
118 (                                                                       \
119         ((nbits) % BITS_PER_LONG) ?                                     \
120                 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL               \
121 )
122
123 static inline void bitmap_zero(unsigned long *dst, int nbits)
124 {
125         if (nbits <= BITS_PER_LONG)
126                 *dst = 0UL;
127         else {
128                 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
129                 memset(dst, 0, len);
130         }
131 }
132
133 static inline void bitmap_fill(unsigned long *dst, int nbits)
134 {
135         size_t nlongs = BITS_TO_LONGS(nbits);
136         if (nlongs > 1) {
137                 int len = (nlongs - 1) * sizeof(unsigned long);
138                 memset(dst, 0xff,  len);
139         }
140         dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
141 }
142
143 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
144                         int nbits)
145 {
146         if (nbits <= BITS_PER_LONG)
147                 *dst = *src;
148         else {
149                 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
150                 memcpy(dst, src, len);
151         }
152 }
153
154 static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
155                         const unsigned long *src2, int nbits)
156 {
157         if (nbits <= BITS_PER_LONG)
158                 *dst = *src1 & *src2;
159         else
160                 __bitmap_and(dst, src1, src2, nbits);
161 }
162
163 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
164                         const unsigned long *src2, int nbits)
165 {
166         if (nbits <= BITS_PER_LONG)
167                 *dst = *src1 | *src2;
168         else
169                 __bitmap_or(dst, src1, src2, nbits);
170 }
171
172 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
173                         const unsigned long *src2, int nbits)
174 {
175         if (nbits <= BITS_PER_LONG)
176                 *dst = *src1 ^ *src2;
177         else
178                 __bitmap_xor(dst, src1, src2, nbits);
179 }
180
181 static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
182                         const unsigned long *src2, int nbits)
183 {
184         if (nbits <= BITS_PER_LONG)
185                 *dst = *src1 & ~(*src2);
186         else
187                 __bitmap_andnot(dst, src1, src2, nbits);
188 }
189
190 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
191                         int nbits)
192 {
193         if (nbits <= BITS_PER_LONG)
194                 *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
195         else
196                 __bitmap_complement(dst, src, nbits);
197 }
198
199 static inline int bitmap_equal(const unsigned long *src1,
200                         const unsigned long *src2, int nbits)
201 {
202         if (nbits <= BITS_PER_LONG)
203                 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
204         else
205                 return __bitmap_equal(src1, src2, nbits);
206 }
207
208 static inline int bitmap_intersects(const unsigned long *src1,
209                         const unsigned long *src2, int nbits)
210 {
211         if (nbits <= BITS_PER_LONG)
212                 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
213         else
214                 return __bitmap_intersects(src1, src2, nbits);
215 }
216
217 static inline int bitmap_subset(const unsigned long *src1,
218                         const unsigned long *src2, int nbits)
219 {
220         if (nbits <= BITS_PER_LONG)
221                 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
222         else
223                 return __bitmap_subset(src1, src2, nbits);
224 }
225
226 static inline int bitmap_empty(const unsigned long *src, int nbits)
227 {
228         if (nbits <= BITS_PER_LONG)
229                 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
230         else
231                 return __bitmap_empty(src, nbits);
232 }
233
234 static inline int bitmap_full(const unsigned long *src, int nbits)
235 {
236         if (nbits <= BITS_PER_LONG)
237                 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
238         else
239                 return __bitmap_full(src, nbits);
240 }
241
242 static inline int bitmap_weight(const unsigned long *src, int nbits)
243 {
244         return __bitmap_weight(src, nbits);
245 }
246
247 static inline void bitmap_shift_right(unsigned long *dst,
248                         const unsigned long *src, int n, int nbits)
249 {
250         if (nbits <= BITS_PER_LONG)
251                 *dst = *src >> n;
252         else
253                 __bitmap_shift_right(dst, src, n, nbits);
254 }
255
256 static inline void bitmap_shift_left(unsigned long *dst,
257                         const unsigned long *src, int n, int nbits)
258 {
259         if (nbits <= BITS_PER_LONG)
260                 *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
261         else
262                 __bitmap_shift_left(dst, src, n, nbits);
263 }
264
265 #endif /* __ASSEMBLY__ */
266
267 #endif /* __LINUX_BITMAP_H */