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 BITS_PER_LONG_LONG 64
50 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
51 #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
52 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
53 #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
54 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
55 #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
56 #define GENMASK_ULL(h, l) (((~0ULL) >> (BITS_PER_LONG_LONG - (h) - 1)) & ((~0ULL) << (l)))
57 #define BITS_PER_BYTE 8
58 #define BITS_PER_TYPE(t) (sizeof(t) * BITS_PER_BYTE)
60 #define hweight8(x) bitcount((uint8_t)(x))
61 #define hweight16(x) bitcount16(x)
62 #define hweight32(x) bitcount32(x)
63 #define hweight64(x) bitcount64(x)
64 #define hweight_long(x) bitcountl(x)
69 return (ffs(mask) - 1);
75 return (fls(mask) - 1);
81 return (ffsl(mask) - 1);
87 return (flsl(mask) - 1);
96 static inline uint32_t
97 ror32(uint32_t word, unsigned int shift)
99 return ((word >> shift) | (word << (32 - shift)));
102 #define ffz(mask) __ffs(~(mask))
104 static inline int get_count_order(unsigned int count)
108 order = fls(count) - 1;
109 if (count & (count - 1))
114 static inline unsigned long
115 find_first_bit(const unsigned long *addr, unsigned long size)
120 for (bit = 0; size >= BITS_PER_LONG;
121 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
124 return (bit + __ffsl(*addr));
127 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
136 static inline unsigned long
137 find_first_zero_bit(const unsigned long *addr, unsigned long size)
142 for (bit = 0; size >= BITS_PER_LONG;
143 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
146 return (bit + __ffsl(~(*addr)));
149 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
158 static inline unsigned long
159 find_last_bit(const unsigned long *addr, unsigned long size)
166 pos = size / BITS_PER_LONG;
167 offs = size % BITS_PER_LONG;
168 bit = BITS_PER_LONG * pos;
171 mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
173 return (bit + __flsl(mask));
177 bit -= BITS_PER_LONG;
179 return (bit + __flsl(*addr));
184 static inline unsigned long
185 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
194 pos = offset / BITS_PER_LONG;
195 offs = offset % BITS_PER_LONG;
196 bit = BITS_PER_LONG * pos;
199 mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
201 return (bit + __ffsl(mask));
202 if (size - bit <= BITS_PER_LONG)
204 bit += BITS_PER_LONG;
207 for (size -= bit; size >= BITS_PER_LONG;
208 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
211 return (bit + __ffsl(*addr));
214 mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
223 static inline unsigned long
224 find_next_zero_bit(const unsigned long *addr, unsigned long size,
225 unsigned long offset)
234 pos = offset / BITS_PER_LONG;
235 offs = offset % BITS_PER_LONG;
236 bit = BITS_PER_LONG * pos;
239 mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
241 return (bit + __ffsl(mask));
242 if (size - bit <= BITS_PER_LONG)
244 bit += BITS_PER_LONG;
247 for (size -= bit; size >= BITS_PER_LONG;
248 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
251 return (bit + __ffsl(~(*addr)));
254 mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
263 #define __set_bit(i, a) \
264 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
266 #define set_bit(i, a) \
267 atomic_set_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
269 #define __clear_bit(i, a) \
270 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
272 #define clear_bit(i, a) \
273 atomic_clear_long(&((volatile unsigned long *)(a))[BIT_WORD(i)], BIT_MASK(i))
275 #define test_bit(i, a) \
276 !!(READ_ONCE(((volatile unsigned long *)(a))[BIT_WORD(i)]) & BIT_MASK(i))
279 test_and_clear_bit(long bit, volatile unsigned long *var)
283 var += BIT_WORD(bit);
284 bit %= BITS_PER_LONG;
288 while (!atomic_fcmpset_long(var, &val, val & ~bit))
290 return !!(val & bit);
294 __test_and_clear_bit(long bit, volatile unsigned long *var)
298 var += BIT_WORD(bit);
299 bit %= BITS_PER_LONG;
305 return !!(val & bit);
309 test_and_set_bit(long bit, volatile unsigned long *var)
313 var += BIT_WORD(bit);
314 bit %= BITS_PER_LONG;
318 while (!atomic_fcmpset_long(var, &val, val | bit))
320 return !!(val & bit);
324 __test_and_set_bit(long bit, volatile unsigned long *var)
328 var += BIT_WORD(bit);
329 bit %= BITS_PER_LONG;
335 return !!(val & bit);
345 linux_reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
356 nbits_reg = 1 << order;
357 index = pos / BITS_PER_LONG;
358 offset = pos - (index * BITS_PER_LONG);
359 nlongs_reg = BITS_TO_LONGS(nbits_reg);
360 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
362 mask = (1UL << (nbitsinlong - 1));
368 for (i = 0; i < nlongs_reg; i++) {
369 if (bitmap[index + i] & mask)
376 for (i = 0; i < nlongs_reg; i++)
377 bitmap[index + i] |= mask;
381 for (i = 0; i < nlongs_reg; i++)
382 bitmap[index + i] &= ~mask;
389 #define for_each_set_bit(bit, addr, size) \
390 for ((bit) = find_first_bit((addr), (size)); \
392 (bit) = find_next_bit((addr), (size), (bit) + 1))
394 #define for_each_clear_bit(bit, addr, size) \
395 for ((bit) = find_first_zero_bit((addr), (size)); \
397 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
399 static inline uint64_t
400 sign_extend64(uint64_t value, int index)
402 uint8_t shift = 63 - index;
404 return ((int64_t)(value << shift) >> shift);
407 #endif /* _LINUX_BITOPS_H_ */