5 * Copyright 1992, Linus Torvalds.
10 * These have to be done with inline assembly: that way the bit-setting
11 * is guaranteed to be atomic. All bit operations return 0 if the bit
12 * was cleared before the operation and != 0 if it was not.
14 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
17 #include <asm-generic/bitops/fls.h>
18 #include <asm-generic/bitops/__fls.h>
19 #include <asm-generic/bitops/fls64.h>
22 #define LOCK_PREFIX "lock ; "
24 #define LOCK_PREFIX ""
27 #define ADDR (*(volatile long *) addr)
30 * set_bit - Atomically set a bit in memory
32 * @addr: the address to start counting from
34 * This function is atomic and may not be reordered. See __set_bit()
35 * if you do not require the atomic guarantees.
36 * Note that @nr may be almost arbitrarily large; this function is not
37 * restricted to acting on a single-word quantity.
39 static __inline__ void set_bit(int nr, volatile void * addr)
41 __asm__ __volatile__( LOCK_PREFIX
48 * __set_bit - Set a bit in memory
50 * @addr: the address to start counting from
52 * Unlike set_bit(), this function is non-atomic and may be reordered.
53 * If it's called on the same region of memory simultaneously, the effect
54 * may be that only one operation succeeds.
56 static __inline__ void __set_bit(int nr, volatile void * addr)
65 * clear_bit - Clears a bit in memory
67 * @addr: Address to start counting from
69 * clear_bit() is atomic and may not be reordered. However, it does
70 * not contain a memory barrier, so if it is used for locking purposes,
71 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
72 * in order to ensure changes are visible on other processors.
74 static __inline__ void clear_bit(int nr, volatile void * addr)
76 __asm__ __volatile__( LOCK_PREFIX
81 #define smp_mb__before_clear_bit() barrier()
82 #define smp_mb__after_clear_bit() barrier()
85 * __change_bit - Toggle a bit in memory
87 * @addr: the address to start counting from
89 * Unlike change_bit(), this function is non-atomic and may be reordered.
90 * If it's called on the same region of memory simultaneously, the effect
91 * may be that only one operation succeeds.
93 static __inline__ void __change_bit(int nr, volatile void * addr)
102 * change_bit - Toggle a bit in memory
104 * @addr: Address to start counting from
106 * change_bit() is atomic and may not be reordered.
107 * Note that @nr may be almost arbitrarily large; this function is not
108 * restricted to acting on a single-word quantity.
110 static __inline__ void change_bit(int nr, volatile void * addr)
112 __asm__ __volatile__( LOCK_PREFIX
119 * test_and_set_bit - Set a bit and return its old value
121 * @addr: Address to count from
123 * This operation is atomic and cannot be reordered.
124 * It also implies a memory barrier.
126 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
130 __asm__ __volatile__( LOCK_PREFIX
131 "btsl %2,%1\n\tsbbl %0,%0"
132 :"=r" (oldbit),"=m" (ADDR)
133 :"Ir" (nr) : "memory");
138 * __test_and_set_bit - Set a bit and return its old value
140 * @addr: Address to count from
142 * This operation is non-atomic and can be reordered.
143 * If two examples of this operation race, one can appear to succeed
144 * but actually fail. You must protect multiple accesses with a lock.
146 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
151 "btsl %2,%1\n\tsbbl %0,%0"
152 :"=r" (oldbit),"=m" (ADDR)
158 * test_and_clear_bit - Clear a bit and return its old value
160 * @addr: Address to count from
162 * This operation is atomic and cannot be reordered.
163 * It also implies a memory barrier.
165 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
169 __asm__ __volatile__( LOCK_PREFIX
170 "btrl %2,%1\n\tsbbl %0,%0"
171 :"=r" (oldbit),"=m" (ADDR)
172 :"Ir" (nr) : "memory");
177 * __test_and_clear_bit - Clear a bit and return its old value
179 * @addr: Address to count from
181 * This operation is non-atomic and can be reordered.
182 * If two examples of this operation race, one can appear to succeed
183 * but actually fail. You must protect multiple accesses with a lock.
185 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
190 "btrl %2,%1\n\tsbbl %0,%0"
191 :"=r" (oldbit),"=m" (ADDR)
196 /* WARNING: non atomic and it can be reordered! */
197 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
201 __asm__ __volatile__(
202 "btcl %2,%1\n\tsbbl %0,%0"
203 :"=r" (oldbit),"=m" (ADDR)
204 :"Ir" (nr) : "memory");
209 * test_and_change_bit - Change a bit and return its new value
211 * @addr: Address to count from
213 * This operation is atomic and cannot be reordered.
214 * It also implies a memory barrier.
216 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
220 __asm__ __volatile__( LOCK_PREFIX
221 "btcl %2,%1\n\tsbbl %0,%0"
222 :"=r" (oldbit),"=m" (ADDR)
223 :"Ir" (nr) : "memory");
227 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
229 * test_bit - Determine whether a bit is set
230 * @nr: bit number to test
231 * @addr: Address to start counting from
233 static int test_bit(int nr, const volatile void * addr);
236 static __inline__ int constant_test_bit(int nr, const volatile void * addr)
238 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
241 static __inline__ int variable_test_bit(int nr, volatile void * addr)
245 __asm__ __volatile__(
246 "btl %2,%1\n\tsbbl %0,%0"
248 :"m" (ADDR),"Ir" (nr));
252 #define test_bit(nr,addr) \
253 (__builtin_constant_p(nr) ? \
254 constant_test_bit((nr),(addr)) : \
255 variable_test_bit((nr),(addr)))
258 * find_first_zero_bit - find the first zero bit in a memory region
259 * @addr: The address to start the search at
260 * @size: The maximum size to search
262 * Returns the bit-number of the first zero bit, not the number of the byte
265 static __inline__ int find_first_zero_bit(void * addr, unsigned size)
272 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
273 __asm__ __volatile__(
275 "xorl %%edx,%%edx\n\t"
278 "xorl -4(%%edi),%%eax\n\t"
281 "1:\tsubl %%ebx,%%edi\n\t"
284 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
285 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
290 * find_next_zero_bit - find the first zero bit in a memory region
291 * @addr: The address to base the search on
292 * @offset: The bitnumber to start searching at
293 * @size: The maximum size to search
295 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
297 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
298 int set = 0, bit = offset & 31, res;
302 * Look for zero in first byte
304 __asm__("bsfl %1,%0\n\t"
309 : "r" (~(*p >> bit)));
310 if (set < (32 - bit))
316 * No zero yet, search remaining full bytes for a zero
318 res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
319 return (offset + set + res);
323 * ffz - find first zero in word.
324 * @word: The word to search
326 * Undefined if no zero exists, so code should check against ~0UL first.
328 static __inline__ unsigned long ffz(unsigned long word)
339 * __ffs - find first set bit in word
340 * @word: The word to search
342 * Undefined if no bit exists, so code should check against 0 first.
344 static inline unsigned long __ffs(unsigned long word)
346 __asm__("rep; bsf %1,%0"
353 * ffs - find first bit set
354 * @x: the word to search
356 * This is defined the same way as
357 * the libc and compiler builtin ffs routines, therefore
358 * differs in spirit from the above ffz (man ffs).
360 static __inline__ int ffs(int x)
364 __asm__("bsfl %1,%0\n\t"
367 "1:" : "=r" (r) : "rm" (x));
373 static inline int __ilog2(unsigned int x)
375 return generic_fls(x) - 1;
379 * hweightN - returns the hamming weight of a N-bit word
380 * @x: the word to weigh
382 * The Hamming Weight of a number is the total number of bits set in it.
385 #define hweight32(x) generic_hweight32(x)
386 #define hweight16(x) generic_hweight16(x)
387 #define hweight8(x) generic_hweight8(x)
389 #endif /* __KERNEL__ */
393 #define ext2_set_bit __test_and_set_bit
394 #define ext2_clear_bit __test_and_clear_bit
395 #define ext2_test_bit test_bit
396 #define ext2_find_first_zero_bit find_first_zero_bit
397 #define ext2_find_next_zero_bit find_next_zero_bit
399 /* Bitmap functions for the minix filesystem. */
400 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,addr)
401 #define minix_set_bit(nr,addr) __set_bit(nr,addr)
402 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,addr)
403 #define minix_test_bit(nr,addr) test_bit(nr,addr)
404 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
406 #endif /* __KERNEL__ */
408 #endif /* _I386_BITOPS_H */