2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LINUX_BITOPS_H_
29 #define _LINUX_BITOPS_H_
32 #define BITS_PER_LONG 64
34 #define BITS_PER_LONG 32
36 #define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
37 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
42 return (ffs(mask) - 1);
48 return (fls(mask) - 1);
54 return (ffsl(mask) - 1);
60 return (flsl(mask) - 1);
64 #define ffz(mask) __ffs(~(mask))
66 static inline unsigned long
67 find_first_bit(unsigned long *addr, unsigned long size)
72 for (bit = 0; size >= BITS_PER_LONG;
73 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
76 return (bit + __ffsl(*addr));
79 mask = (*addr) & BIT_MASK(size);
88 static inline unsigned long
89 find_first_zero_bit(unsigned long *addr, unsigned long size)
94 for (bit = 0; size >= BITS_PER_LONG;
95 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
98 return (bit + __ffsl(~(*addr)));
101 mask = ~(*addr) & BIT_MASK(size);
110 static inline unsigned long
111 find_last_bit(unsigned long *addr, unsigned long size)
118 pos = size / BITS_PER_LONG;
119 offs = size % BITS_PER_LONG;
120 bit = BITS_PER_LONG * pos;
123 mask = (*addr) & BIT_MASK(offs);
125 return (bit + __flsl(mask));
129 bit -= BITS_PER_LONG;
131 return (bit + __flsl(mask));
136 static inline unsigned long
137 find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
146 pos = offset / BITS_PER_LONG;
147 offs = offset % BITS_PER_LONG;
148 bit = BITS_PER_LONG * pos;
151 mask = (*addr) & ~BIT_MASK(offs);
153 return (bit + __ffsl(mask));
154 bit += BITS_PER_LONG;
157 for (size -= bit; size >= BITS_PER_LONG;
158 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
161 return (bit + __ffsl(*addr));
164 mask = (*addr) & BIT_MASK(size);
173 static inline unsigned long
174 find_next_zero_bit(unsigned long *addr, unsigned long size,
175 unsigned long offset)
184 pos = offset / BITS_PER_LONG;
185 offs = offset % BITS_PER_LONG;
186 bit = BITS_PER_LONG * pos;
189 mask = ~(*addr) & ~BIT_MASK(offs);
191 return (bit + __ffsl(mask));
192 bit += BITS_PER_LONG;
195 for (size -= bit; size >= BITS_PER_LONG;
196 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
199 return (bit + __ffsl(~(*addr)));
202 mask = ~(*addr) & BIT_MASK(size);
212 bitmap_zero(unsigned long *addr, int size)
216 len = BITS_TO_LONGS(size) * sizeof(long);
217 memset(addr, 0, len);
221 bitmap_fill(unsigned long *addr, int size)
226 len = (size / BITS_PER_LONG) * sizeof(long);
227 memset(addr, 0xff, len);
228 tail = size & (BITS_PER_LONG - 1);
230 addr[size / BITS_PER_LONG] = BIT_MASK(tail);
234 bitmap_full(unsigned long *addr, int size)
241 len = size / BITS_PER_LONG;
242 for (i = 0; i < len; i++)
245 tail = size & (BITS_PER_LONG - 1);
247 mask = BIT_MASK(tail);
248 if ((addr[i] & mask) != mask)
255 bitmap_empty(unsigned long *addr, int size)
262 len = size / BITS_PER_LONG;
263 for (i = 0; i < len; i++)
266 tail = size & (BITS_PER_LONG - 1);
268 mask = BIT_MASK(tail);
269 if ((addr[i] & mask) != 0)
275 #define NBINT (NBBY * sizeof(int))
277 #define set_bit(i, a) \
278 atomic_set_int(&((volatile int *)(a))[(i)/NBINT], 1 << (i) % NBINT)
280 #define clear_bit(i, a) \
281 atomic_clear_int(&((volatile int *)(a))[(i)/NBINT], 1 << (i) % NBINT)
283 #define test_bit(i, a) \
284 !!(atomic_load_acq_int(&((volatile int *)(a))[(i)/NBINT]) & 1 << ((i) % NBINT))
287 test_and_clear_bit(long bit, long *var)
293 val = *(volatile long *)var;
294 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
296 return !!(val & bit);
300 test_and_set_bit(long bit, long *var)
306 val = *(volatile long *)var;
307 } while (atomic_cmpset_long(var, val, val | bit) == 0);
309 return !!(val & bit);
312 #endif /* _LINUX_BITOPS_H_ */