]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/bitmap.h
lib/bitmap.c: elide bitmap_copy_le on little-endian
[mirror_ubuntu-artful-kernel.git] / include / linux / bitmap.h
CommitLineData
1da177e4
LT
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>
14ed9d23 9#include <linux/kernel.h>
1da177e4
LT
10
11/*
12 * bitmaps provide bit arrays that consume one or more unsigned
13 * longs. The bitmap interface and available operations are listed
14 * here, in bitmap.h
15 *
16 * Function implementations generic to all architectures are in
17 * lib/bitmap.c. Functions implementations that are architecture
18 * specific are in various include/asm-<arch>/bitops.h headers
19 * and other arch/<arch> specific files.
20 *
21 * See lib/bitmap.c for more details.
22 */
23
24/*
25 * The available bitmap operations and their rough meaning in the
26 * case that the bitmap is a single unsigned long are thus:
27 *
08cd3657
AK
28 * Note that nbits should be always a compile time evaluable constant.
29 * Otherwise many inlines will generate horrible code.
30 *
1da177e4
LT
31 * bitmap_zero(dst, nbits) *dst = 0UL
32 * bitmap_fill(dst, nbits) *dst = ~0UL
33 * bitmap_copy(dst, src, nbits) *dst = *src
34 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
35 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
36 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
37 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
38 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
39 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
40 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
41 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
42 * bitmap_empty(src, nbits) Are all bits zero in *src?
43 * bitmap_full(src, nbits) Are all bits set in *src?
44 * bitmap_weight(src, nbits) Hamming Weight: number set bits
c1a2a962
AM
45 * bitmap_set(dst, pos, nbits) Set specified bit area
46 * bitmap_clear(dst, pos, nbits) Clear specified bit area
47 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
5e19b013 48 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
1da177e4
LT
49 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
50 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
fb5eeeee
PJ
51 * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
52 * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
7ea931c9
PJ
53 * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
54 * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
1da177e4 55 * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf
01a3ee2b
RC
56 * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
57 * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
1da177e4 58 * bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf
4b060420
MT
59 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
60 * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
87e24802
PJ
61 * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
62 * bitmap_release_region(bitmap, pos, order) Free specified bit region
63 * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
5aaba363 64 * bitmap_print_to_pagebuf(list, buf, mask, nbits) Print bitmap src as list/hex
1da177e4
LT
65 */
66
67/*
68 * Also the following operations in asm/bitops.h apply to bitmaps.
69 *
70 * set_bit(bit, addr) *addr |= bit
71 * clear_bit(bit, addr) *addr &= ~bit
72 * change_bit(bit, addr) *addr ^= bit
73 * test_bit(bit, addr) Is bit set in *addr?
74 * test_and_set_bit(bit, addr) Set bit and return old value
75 * test_and_clear_bit(bit, addr) Clear bit and return old value
76 * test_and_change_bit(bit, addr) Change bit and return old value
77 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
78 * find_first_bit(addr, nbits) Position first set bit in *addr
79 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
80 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
81 */
82
83/*
84 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
85 * to declare an array named 'name' of just enough unsigned longs to
86 * contain all bit positions from 0 to 'bits' - 1.
87 */
88
89/*
90 * lib/bitmap.c provides these functions:
91 */
92
0679cc48 93extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
8397927c 94extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
1da177e4 95extern int __bitmap_equal(const unsigned long *bitmap1,
5e068069 96 const unsigned long *bitmap2, unsigned int nbits);
1da177e4 97extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
3d6684f4 98 unsigned int nbits);
1da177e4
LT
99extern void __bitmap_shift_right(unsigned long *dst,
100 const unsigned long *src, int shift, int bits);
101extern void __bitmap_shift_left(unsigned long *dst,
102 const unsigned long *src, int shift, int bits);
f4b0373b 103extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 104 const unsigned long *bitmap2, unsigned int nbits);
1da177e4 105extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 106 const unsigned long *bitmap2, unsigned int nbits);
1da177e4 107extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 108 const unsigned long *bitmap2, unsigned int nbits);
f4b0373b 109extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
2f9305eb 110 const unsigned long *bitmap2, unsigned int nbits);
1da177e4 111extern int __bitmap_intersects(const unsigned long *bitmap1,
6dfe9799 112 const unsigned long *bitmap2, unsigned int nbits);
1da177e4 113extern int __bitmap_subset(const unsigned long *bitmap1,
5be20213 114 const unsigned long *bitmap2, unsigned int nbits);
877d9f3b 115extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
1da177e4 116
fb5ac542 117extern void bitmap_set(unsigned long *map, unsigned int start, int len);
154f5e38 118extern void bitmap_clear(unsigned long *map, unsigned int start, int len);
5e19b013
MN
119
120extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
121 unsigned long size,
122 unsigned long start,
123 unsigned int nr,
124 unsigned long align_mask,
125 unsigned long align_offset);
126
127/**
128 * bitmap_find_next_zero_area - find a contiguous aligned zero area
129 * @map: The address to base the search on
130 * @size: The bitmap size in bits
131 * @start: The bitnumber to start searching at
132 * @nr: The number of zeroed bits we're looking for
133 * @align_mask: Alignment mask for zero area
134 *
135 * The @align_mask should be one less than a power of 2; the effect is that
136 * the bit offset of all zero areas this function finds is multiples of that
137 * power of 2. A @align_mask of 0 means no alignment is required.
138 */
139static inline unsigned long
140bitmap_find_next_zero_area(unsigned long *map,
141 unsigned long size,
142 unsigned long start,
143 unsigned int nr,
144 unsigned long align_mask)
145{
146 return bitmap_find_next_zero_area_off(map, size, start, nr,
147 align_mask, 0);
148}
c1a2a962 149
1da177e4
LT
150extern int bitmap_scnprintf(char *buf, unsigned int len,
151 const unsigned long *src, int nbits);
01a3ee2b
RC
152extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
153 unsigned long *dst, int nbits);
154extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
1da177e4
LT
155 unsigned long *dst, int nbits);
156extern int bitmap_scnlistprintf(char *buf, unsigned int len,
157 const unsigned long *src, int nbits);
158extern int bitmap_parselist(const char *buf, unsigned long *maskp,
159 int nmaskbits);
4b060420
MT
160extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
161 unsigned long *dst, int nbits);
fb5eeeee 162extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
9814ec13 163 const unsigned long *old, const unsigned long *new, unsigned int nbits);
fb5eeeee
PJ
164extern int bitmap_bitremap(int oldbit,
165 const unsigned long *old, const unsigned long *new, int bits);
7ea931c9 166extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
eb569883 167 const unsigned long *relmap, unsigned int bits);
7ea931c9 168extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
b26ad583 169 unsigned int sz, unsigned int nbits);
9279d328
RV
170extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
171extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
172extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
e8f24278 173#ifdef __BIG_ENDIAN
9b6c2d2e 174extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
e8f24278
RV
175#else
176#define bitmap_copy_le bitmap_copy
177#endif
f6a1f5db 178extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
5aaba363
SH
179extern int bitmap_print_to_pagebuf(bool list, char *buf,
180 const unsigned long *maskp, int nmaskbits);
1da177e4 181
7f184275 182#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
1da177e4
LT
183#define BITMAP_LAST_WORD_MASK(nbits) \
184( \
185 ((nbits) % BITS_PER_LONG) ? \
186 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
187)
188
4b0bc0bc
RR
189#define small_const_nbits(nbits) \
190 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
191
8b4daad5 192static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
1da177e4 193{
4b0bc0bc 194 if (small_const_nbits(nbits))
1da177e4
LT
195 *dst = 0UL;
196 else {
8b4daad5 197 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
1da177e4
LT
198 memset(dst, 0, len);
199 }
200}
201
8b4daad5 202static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
1da177e4 203{
8b4daad5 204 unsigned int nlongs = BITS_TO_LONGS(nbits);
4b0bc0bc 205 if (!small_const_nbits(nbits)) {
8b4daad5 206 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
1da177e4
LT
207 memset(dst, 0xff, len);
208 }
209 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
210}
211
212static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
8b4daad5 213 unsigned int nbits)
1da177e4 214{
4b0bc0bc 215 if (small_const_nbits(nbits))
1da177e4
LT
216 *dst = *src;
217 else {
8b4daad5 218 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
1da177e4
LT
219 memcpy(dst, src, len);
220 }
221}
222
f4b0373b 223static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
2f9305eb 224 const unsigned long *src2, unsigned int nbits)
1da177e4 225{
4b0bc0bc 226 if (small_const_nbits(nbits))
7e5f97d1 227 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 228 return __bitmap_and(dst, src1, src2, nbits);
1da177e4
LT
229}
230
231static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
2f9305eb 232 const unsigned long *src2, unsigned int nbits)
1da177e4 233{
4b0bc0bc 234 if (small_const_nbits(nbits))
1da177e4
LT
235 *dst = *src1 | *src2;
236 else
237 __bitmap_or(dst, src1, src2, nbits);
238}
239
240static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
2f9305eb 241 const unsigned long *src2, unsigned int nbits)
1da177e4 242{
4b0bc0bc 243 if (small_const_nbits(nbits))
1da177e4
LT
244 *dst = *src1 ^ *src2;
245 else
246 __bitmap_xor(dst, src1, src2, nbits);
247}
248
f4b0373b 249static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
2f9305eb 250 const unsigned long *src2, unsigned int nbits)
1da177e4 251{
4b0bc0bc 252 if (small_const_nbits(nbits))
74e76531 253 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
f4b0373b 254 return __bitmap_andnot(dst, src1, src2, nbits);
1da177e4
LT
255}
256
257static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
3d6684f4 258 unsigned int nbits)
1da177e4 259{
4b0bc0bc 260 if (small_const_nbits(nbits))
65b4ee62 261 *dst = ~(*src);
1da177e4
LT
262 else
263 __bitmap_complement(dst, src, nbits);
264}
265
266static inline int bitmap_equal(const unsigned long *src1,
3d6684f4 267 const unsigned long *src2, unsigned int nbits)
1da177e4 268{
4b0bc0bc 269 if (small_const_nbits(nbits))
1da177e4
LT
270 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
271 else
272 return __bitmap_equal(src1, src2, nbits);
273}
274
275static inline int bitmap_intersects(const unsigned long *src1,
6dfe9799 276 const unsigned long *src2, unsigned int nbits)
1da177e4 277{
4b0bc0bc 278 if (small_const_nbits(nbits))
1da177e4
LT
279 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
280 else
281 return __bitmap_intersects(src1, src2, nbits);
282}
283
284static inline int bitmap_subset(const unsigned long *src1,
5be20213 285 const unsigned long *src2, unsigned int nbits)
1da177e4 286{
4b0bc0bc 287 if (small_const_nbits(nbits))
1da177e4
LT
288 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
289 else
290 return __bitmap_subset(src1, src2, nbits);
291}
292
0679cc48 293static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
1da177e4 294{
4b0bc0bc 295 if (small_const_nbits(nbits))
1da177e4
LT
296 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
297 else
298 return __bitmap_empty(src, nbits);
299}
300
8397927c 301static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
1da177e4 302{
4b0bc0bc 303 if (small_const_nbits(nbits))
1da177e4
LT
304 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
305 else
306 return __bitmap_full(src, nbits);
307}
308
877d9f3b 309static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
1da177e4 310{
4b0bc0bc 311 if (small_const_nbits(nbits))
08cd3657 312 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
1da177e4
LT
313 return __bitmap_weight(src, nbits);
314}
315
316static inline void bitmap_shift_right(unsigned long *dst,
317 const unsigned long *src, int n, int nbits)
318{
4b0bc0bc 319 if (small_const_nbits(nbits))
c5341ec8 320 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> n;
1da177e4
LT
321 else
322 __bitmap_shift_right(dst, src, n, nbits);
323}
324
325static inline void bitmap_shift_left(unsigned long *dst,
326 const unsigned long *src, int n, int nbits)
327{
4b0bc0bc 328 if (small_const_nbits(nbits))
1da177e4
LT
329 *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
330 else
331 __bitmap_shift_left(dst, src, n, nbits);
332}
333
01a3ee2b
RC
334static inline int bitmap_parse(const char *buf, unsigned int buflen,
335 unsigned long *maskp, int nmaskbits)
336{
337 return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
338}
339
1da177e4
LT
340#endif /* __ASSEMBLY__ */
341
342#endif /* __LINUX_BITMAP_H */