2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _LINUX_BITOPS_H_
32 #define _LINUX_BITOPS_H_
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
38 #define BIT(nr) (1UL << (nr))
40 #define BITS_PER_LONG 64
42 #define BITS_PER_LONG 32
44 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
45 #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
46 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
47 #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
48 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
49 #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
50 #define BITS_PER_BYTE 8
55 return (ffs(mask) - 1);
61 return (fls(mask) - 1);
67 return (ffsl(mask) - 1);
73 return (flsl(mask) - 1);
76 static inline uint32_t
77 ror32(uint32_t word, unsigned int shift)
80 return ((word >> shift) | (word << (32 - shift)));
83 #define ffz(mask) __ffs(~(mask))
85 static inline int get_count_order(unsigned int count)
89 order = fls(count) - 1;
90 if (count & (count - 1))
95 static inline unsigned long
96 find_first_bit(const unsigned long *addr, unsigned long size)
101 for (bit = 0; size >= BITS_PER_LONG;
102 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
105 return (bit + __ffsl(*addr));
108 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
117 static inline unsigned long
118 find_first_zero_bit(const unsigned long *addr, unsigned long size)
123 for (bit = 0; size >= BITS_PER_LONG;
124 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
127 return (bit + __ffsl(~(*addr)));
130 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
139 static inline unsigned long
140 find_last_bit(const unsigned long *addr, unsigned long size)
147 pos = size / BITS_PER_LONG;
148 offs = size % BITS_PER_LONG;
149 bit = BITS_PER_LONG * pos;
152 mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
154 return (bit + __flsl(mask));
158 bit -= BITS_PER_LONG;
160 return (bit + __flsl(*addr));
165 static inline unsigned long
166 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
175 pos = offset / BITS_PER_LONG;
176 offs = offset % BITS_PER_LONG;
177 bit = BITS_PER_LONG * pos;
180 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
182 return (bit + __ffsl(mask));
183 if (size - bit <= BITS_PER_LONG)
185 bit += BITS_PER_LONG;
188 for (size -= bit; size >= BITS_PER_LONG;
189 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
192 return (bit + __ffsl(*addr));
195 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
204 static inline unsigned long
205 find_next_zero_bit(const unsigned long *addr, unsigned long size,
206 unsigned long offset)
215 pos = offset / BITS_PER_LONG;
216 offs = offset % BITS_PER_LONG;
217 bit = BITS_PER_LONG * pos;
220 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
222 return (bit + __ffsl(mask));
223 if (size - bit <= BITS_PER_LONG)
225 bit += BITS_PER_LONG;
228 for (size -= bit; size >= BITS_PER_LONG;
229 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
232 return (bit + __ffsl(~(*addr)));
235 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
245 bitmap_zero(unsigned long *addr, int size)
249 len = BITS_TO_LONGS(size) * sizeof(long);
250 memset(addr, 0, len);
254 bitmap_fill(unsigned long *addr, int size)
259 len = (size / BITS_PER_LONG) * sizeof(long);
260 memset(addr, 0xff, len);
261 tail = size & (BITS_PER_LONG - 1);
263 addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail);
267 bitmap_full(unsigned long *addr, int size)
274 len = size / BITS_PER_LONG;
275 for (i = 0; i < len; i++)
278 tail = size & (BITS_PER_LONG - 1);
280 mask = BITMAP_LAST_WORD_MASK(tail);
281 if ((addr[i] & mask) != mask)
288 bitmap_empty(unsigned long *addr, int size)
295 len = size / BITS_PER_LONG;
296 for (i = 0; i < len; i++)
299 tail = size & (BITS_PER_LONG - 1);
301 mask = BITMAP_LAST_WORD_MASK(tail);
302 if ((addr[i] & mask) != 0)
308 #define __set_bit(i, a) \
309 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
311 #define set_bit(i, a) \
312 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
314 #define __clear_bit(i, a) \
315 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
317 #define clear_bit(i, a) \
318 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
320 #define test_bit(i, a) \
321 !!(atomic_load_acq_long(&((volatile unsigned long *)(a))[BIT_WORD(i)]) & \
325 test_and_clear_bit(long bit, volatile unsigned long *var)
329 var += BIT_WORD(bit);
330 bit %= BITS_PER_LONG;
334 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
336 return !!(val & bit);
340 test_and_set_bit(long bit, volatile unsigned long *var)
344 var += BIT_WORD(bit);
345 bit %= BITS_PER_LONG;
349 } while (atomic_cmpset_long(var, val, val | bit) == 0);
351 return !!(val & bit);
355 bitmap_set(unsigned long *map, int start, int nr)
357 unsigned long *p = map + BIT_WORD(start);
358 const int size = start + nr;
359 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
360 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
362 while (nr - bits_to_set >= 0) {
365 bits_to_set = BITS_PER_LONG;
370 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
376 bitmap_clear(unsigned long *map, int start, int nr)
378 unsigned long *p = map + BIT_WORD(start);
379 const int size = start + nr;
380 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
381 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
383 while (nr - bits_to_clear >= 0) {
384 *p &= ~mask_to_clear;
386 bits_to_clear = BITS_PER_LONG;
387 mask_to_clear = ~0UL;
391 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
392 *p &= ~mask_to_clear;
403 __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
414 nbits_reg = 1 << order;
415 index = pos / BITS_PER_LONG;
416 offset = pos - (index * BITS_PER_LONG);
417 nlongs_reg = BITS_TO_LONGS(nbits_reg);
418 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
420 mask = (1UL << (nbitsinlong - 1));
426 for (i = 0; i < nlongs_reg; i++) {
427 if (bitmap[index + i] & mask)
434 for (i = 0; i < nlongs_reg; i++)
435 bitmap[index + i] |= mask;
439 for (i = 0; i < nlongs_reg; i++)
440 bitmap[index + i] &= ~mask;
448 bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
453 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
454 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
456 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
463 bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
465 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
467 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
472 bitmap_release_region(unsigned long *bitmap, int pos, int order)
474 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
477 #define for_each_set_bit(bit, addr, size) \
478 for ((bit) = find_first_bit((addr), (size)); \
480 (bit) = find_next_bit((addr), (size), (bit) + 1))
482 static inline unsigned
483 bitmap_weight(unsigned long *bitmap, unsigned nbits)
488 for_each_set_bit(bit, bitmap, nbits)
494 bitmap_equal(const unsigned long *pa,
495 const unsigned long *pb, unsigned bits)
498 unsigned y = bits / BITS_PER_LONG;
500 for (x = 0; x != y; x++) {
505 y = bits % BITS_PER_LONG;
507 if ((pa[x] ^ pb[x]) & BITMAP_LAST_WORD_MASK(y))
513 #endif /* _LINUX_BITOPS_H_ */