2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 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/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/libkern.h>
40 #define BIT(nr) (1UL << (nr))
41 #define BIT_ULL(nr) (1ULL << (nr))
43 #define BITS_PER_LONG 64
45 #define BITS_PER_LONG 32
48 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
49 #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
50 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
51 #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
52 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
53 #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
54 #define BITS_PER_BYTE 8
56 #define hweight8(x) bitcount((uint8_t)(x))
57 #define hweight16(x) bitcount16(x)
58 #define hweight32(x) bitcount32(x)
59 #define hweight64(x) bitcount64(x)
60 #define hweight_long(x) bitcountl(x)
65 return (ffs(mask) - 1);
71 return (fls(mask) - 1);
77 return (ffsl(mask) - 1);
83 return (flsl(mask) - 1);
92 static inline uint32_t
93 ror32(uint32_t word, unsigned int shift)
95 return ((word >> shift) | (word << (32 - shift)));
98 #define ffz(mask) __ffs(~(mask))
100 static inline int get_count_order(unsigned int count)
104 order = fls(count) - 1;
105 if (count & (count - 1))
110 static inline unsigned long
111 find_first_bit(const unsigned long *addr, unsigned long size)
116 for (bit = 0; size >= BITS_PER_LONG;
117 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
120 return (bit + __ffsl(*addr));
123 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
132 static inline unsigned long
133 find_first_zero_bit(const unsigned long *addr, unsigned long size)
138 for (bit = 0; size >= BITS_PER_LONG;
139 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
142 return (bit + __ffsl(~(*addr)));
145 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
154 static inline unsigned long
155 find_last_bit(const unsigned long *addr, unsigned long size)
162 pos = size / BITS_PER_LONG;
163 offs = size % BITS_PER_LONG;
164 bit = BITS_PER_LONG * pos;
167 mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
169 return (bit + __flsl(mask));
173 bit -= BITS_PER_LONG;
175 return (bit + __flsl(*addr));
180 static inline unsigned long
181 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
190 pos = offset / BITS_PER_LONG;
191 offs = offset % BITS_PER_LONG;
192 bit = BITS_PER_LONG * pos;
195 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
197 return (bit + __ffsl(mask));
198 if (size - bit <= BITS_PER_LONG)
200 bit += BITS_PER_LONG;
203 for (size -= bit; size >= BITS_PER_LONG;
204 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
207 return (bit + __ffsl(*addr));
210 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
219 static inline unsigned long
220 find_next_zero_bit(const unsigned long *addr, unsigned long size,
221 unsigned long offset)
230 pos = offset / BITS_PER_LONG;
231 offs = offset % BITS_PER_LONG;
232 bit = BITS_PER_LONG * pos;
235 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
237 return (bit + __ffsl(mask));
238 if (size - bit <= BITS_PER_LONG)
240 bit += BITS_PER_LONG;
243 for (size -= bit; size >= BITS_PER_LONG;
244 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
247 return (bit + __ffsl(~(*addr)));
250 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
259 #define __set_bit(i, a) \
260 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
262 #define set_bit(i, a) \
263 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
265 #define __clear_bit(i, a) \
266 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
268 #define clear_bit(i, a) \
269 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
271 #define test_bit(i, a) \
272 !!(atomic_load_acq_long(&((volatile unsigned long *)(a))[BIT_WORD(i)]) & \
276 test_and_clear_bit(long bit, volatile unsigned long *var)
280 var += BIT_WORD(bit);
281 bit %= BITS_PER_LONG;
285 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
287 return !!(val & bit);
291 __test_and_clear_bit(long bit, volatile unsigned long *var)
295 var += BIT_WORD(bit);
296 bit %= BITS_PER_LONG;
302 return !!(val & bit);
306 test_and_set_bit(long bit, volatile unsigned long *var)
310 var += BIT_WORD(bit);
311 bit %= BITS_PER_LONG;
315 } while (atomic_cmpset_long(var, val, val | bit) == 0);
317 return !!(val & bit);
321 __test_and_set_bit(long bit, volatile unsigned long *var)
325 var += BIT_WORD(bit);
326 bit %= BITS_PER_LONG;
332 return !!(val & bit);
342 linux_reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
353 nbits_reg = 1 << order;
354 index = pos / BITS_PER_LONG;
355 offset = pos - (index * BITS_PER_LONG);
356 nlongs_reg = BITS_TO_LONGS(nbits_reg);
357 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
359 mask = (1UL << (nbitsinlong - 1));
365 for (i = 0; i < nlongs_reg; i++) {
366 if (bitmap[index + i] & mask)
373 for (i = 0; i < nlongs_reg; i++)
374 bitmap[index + i] |= mask;
378 for (i = 0; i < nlongs_reg; i++)
379 bitmap[index + i] &= ~mask;
386 #define for_each_set_bit(bit, addr, size) \
387 for ((bit) = find_first_bit((addr), (size)); \
389 (bit) = find_next_bit((addr), (size), (bit) + 1))
392 static inline uint64_t
393 sign_extend64(uint64_t value, int index)
395 uint8_t shift = 63 - index;
397 return ((int64_t)(value << shift) >> shift);
400 #endif /* _LINUX_BITOPS_H_ */