5 * Copyright 1992, Linus Torvalds.
8 #include <linux/config.h>
9 #include <linux/compiler.h>
12 * These have to be done with inline assembly: that way the bit-setting
13 * is guaranteed to be atomic. All bit operations return 0 if the bit
14 * was cleared before the operation and != 0 if it was not.
16 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
20 #define LOCK_PREFIX "lock ; "
22 #define LOCK_PREFIX ""
25 #define ADDR (*(volatile long *) addr)
28 * set_bit - Atomically set a bit in memory
30 * @addr: the address to start counting from
32 * This function is atomic and may not be reordered. See __set_bit()
33 * if you do not require the atomic guarantees.
34 * Note that @nr may be almost arbitrarily large; this function is not
35 * restricted to acting on a single-word quantity.
37 static inline void set_bit(int nr, volatile unsigned long * addr)
39 __asm__ __volatile__( LOCK_PREFIX
46 * __set_bit - Set a bit in memory
48 * @addr: the address to start counting from
50 * Unlike set_bit(), this function is non-atomic and may be reordered.
51 * If it's called on the same region of memory simultaneously, the effect
52 * may be that only one operation succeeds.
54 static inline void __set_bit(int nr, volatile unsigned long * addr)
63 * clear_bit - Clears a bit in memory
65 * @addr: Address to start counting from
67 * clear_bit() is atomic and may not be reordered. However, it does
68 * not contain a memory barrier, so if it is used for locking purposes,
69 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
70 * in order to ensure changes are visible on other processors.
72 static inline void clear_bit(int nr, volatile unsigned long * addr)
74 __asm__ __volatile__( LOCK_PREFIX
80 static inline void __clear_bit(int nr, volatile unsigned long * addr)
87 #define smp_mb__before_clear_bit() barrier()
88 #define smp_mb__after_clear_bit() barrier()
91 * __change_bit - Toggle a bit in memory
92 * @nr: the bit to change
93 * @addr: the address to start counting from
95 * Unlike change_bit(), this function is non-atomic and may be reordered.
96 * If it's called on the same region of memory simultaneously, the effect
97 * may be that only one operation succeeds.
99 static inline void __change_bit(int nr, volatile unsigned long * addr)
101 __asm__ __volatile__(
108 * change_bit - Toggle a bit in memory
110 * @addr: Address to start counting from
112 * change_bit() is atomic and may not be reordered.
113 * Note that @nr may be almost arbitrarily large; this function is not
114 * restricted to acting on a single-word quantity.
116 static inline void change_bit(int nr, volatile unsigned long * addr)
118 __asm__ __volatile__( LOCK_PREFIX
125 * test_and_set_bit - Set a bit and return its old value
127 * @addr: Address to count from
129 * This operation is atomic and cannot be reordered.
130 * It also implies a memory barrier.
132 static inline int test_and_set_bit(int nr, volatile unsigned long * addr)
136 __asm__ __volatile__( LOCK_PREFIX
137 "btsl %2,%1\n\tsbbl %0,%0"
138 :"=r" (oldbit),"=m" (ADDR)
139 :"Ir" (nr) : "memory");
144 * __test_and_set_bit - Set a bit and return its old value
146 * @addr: Address to count from
148 * This operation is non-atomic and can be reordered.
149 * If two examples of this operation race, one can appear to succeed
150 * but actually fail. You must protect multiple accesses with a lock.
152 static inline int __test_and_set_bit(int nr, volatile unsigned long * addr)
157 "btsl %2,%1\n\tsbbl %0,%0"
158 :"=r" (oldbit),"=m" (ADDR)
164 * test_and_clear_bit - Clear a bit and return its old value
166 * @addr: Address to count from
168 * This operation is atomic and cannot be reordered.
169 * It also implies a memory barrier.
171 static inline int test_and_clear_bit(int nr, volatile unsigned long * addr)
175 __asm__ __volatile__( LOCK_PREFIX
176 "btrl %2,%1\n\tsbbl %0,%0"
177 :"=r" (oldbit),"=m" (ADDR)
178 :"Ir" (nr) : "memory");
183 * __test_and_clear_bit - Clear a bit and return its old value
185 * @addr: Address to count from
187 * This operation is non-atomic and can be reordered.
188 * If two examples of this operation race, one can appear to succeed
189 * but actually fail. You must protect multiple accesses with a lock.
191 static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
196 "btrl %2,%1\n\tsbbl %0,%0"
197 :"=r" (oldbit),"=m" (ADDR)
202 /* WARNING: non atomic and it can be reordered! */
203 static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
207 __asm__ __volatile__(
208 "btcl %2,%1\n\tsbbl %0,%0"
209 :"=r" (oldbit),"=m" (ADDR)
210 :"Ir" (nr) : "memory");
215 * test_and_change_bit - Change a bit and return its old value
217 * @addr: Address to count from
219 * This operation is atomic and cannot be reordered.
220 * It also implies a memory barrier.
222 static inline int test_and_change_bit(int nr, volatile unsigned long* addr)
226 __asm__ __volatile__( LOCK_PREFIX
227 "btcl %2,%1\n\tsbbl %0,%0"
228 :"=r" (oldbit),"=m" (ADDR)
229 :"Ir" (nr) : "memory");
233 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
235 * test_bit - Determine whether a bit is set
236 * @nr: bit number to test
237 * @addr: Address to start counting from
239 static int test_bit(int nr, const volatile void * addr);
242 static inline int constant_test_bit(int nr, const volatile unsigned long *addr)
244 return ((1UL << (nr & 31)) & (addr[nr >> 5])) != 0;
247 static inline int variable_test_bit(int nr, const volatile unsigned long * addr)
251 __asm__ __volatile__(
252 "btl %2,%1\n\tsbbl %0,%0"
254 :"m" (ADDR),"Ir" (nr));
258 #define test_bit(nr,addr) \
259 (__builtin_constant_p(nr) ? \
260 constant_test_bit((nr),(addr)) : \
261 variable_test_bit((nr),(addr)))
266 * find_first_zero_bit - find the first zero bit in a memory region
267 * @addr: The address to start the search at
268 * @size: The maximum size to search
270 * Returns the bit-number of the first zero bit, not the number of the byte
273 static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
280 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
281 __asm__ __volatile__(
283 "xorl %%edx,%%edx\n\t"
286 "xorl -4(%%edi),%%eax\n\t"
289 "1:\tsubl %%ebx,%%edi\n\t"
292 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
293 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
298 * find_next_zero_bit - find the first zero bit in a memory region
299 * @addr: The address to base the search on
300 * @offset: The bitnumber to start searching at
301 * @size: The maximum size to search
303 int find_next_zero_bit(const unsigned long *addr, int size, int offset);
306 * find_first_bit - find the first set bit in a memory region
307 * @addr: The address to start the search at
308 * @size: The maximum size to search
310 * Returns the bit-number of the first set bit, not the number of the byte
313 static inline int find_first_bit(const unsigned long *addr, unsigned size)
318 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
319 __asm__ __volatile__(
320 "xorl %%eax,%%eax\n\t"
323 "leal -4(%%edi),%%edi\n\t"
324 "bsfl (%%edi),%%eax\n"
325 "1:\tsubl %%ebx,%%edi\n\t"
328 :"=a" (res), "=&c" (d0), "=&D" (d1)
329 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
334 * find_next_bit - find the first set bit in a memory region
335 * @addr: The address to base the search on
336 * @offset: The bitnumber to start searching at
337 * @size: The maximum size to search
339 int find_next_bit(const unsigned long *addr, int size, int offset);
342 * ffz - find first zero in word.
343 * @word: The word to search
345 * Undefined if no zero exists, so code should check against ~0UL first.
347 static inline unsigned long ffz(unsigned long word)
356 * __ffs - find first bit in word.
357 * @word: The word to search
359 * Undefined if no bit exists, so code should check against 0 first.
361 static inline unsigned long __ffs(unsigned long word)
370 * fls: find last bit set.
373 #define fls(x) generic_fls(x)
378 * Every architecture must define this function. It's the fastest
379 * way of searching a 140-bit bitmap where the first 100 bits are
380 * unlikely to be set. It's guaranteed that at least one of the 140
383 static inline int sched_find_first_bit(const unsigned long *b)
388 return __ffs(b[1]) + 32;
390 return __ffs(b[2]) + 64;
392 return __ffs(b[3]) + 96;
393 return __ffs(b[4]) + 128;
397 * ffs - find first bit set
398 * @x: the word to search
400 * This is defined the same way as
401 * the libc and compiler builtin ffs routines, therefore
402 * differs in spirit from the above ffz (man ffs).
404 static inline int ffs(int x)
408 __asm__("bsfl %1,%0\n\t"
411 "1:" : "=r" (r) : "rm" (x));
416 * hweightN - returns the hamming weight of a N-bit word
417 * @x: the word to weigh
419 * The Hamming Weight of a number is the total number of bits set in it.
422 #define hweight32(x) generic_hweight32(x)
423 #define hweight16(x) generic_hweight16(x)
424 #define hweight8(x) generic_hweight8(x)
426 #endif /* __KERNEL__ */
430 #define ext2_set_bit(nr,addr) \
431 __test_and_set_bit((nr),(unsigned long*)addr)
432 #define ext2_set_bit_atomic(lock,nr,addr) \
433 test_and_set_bit((nr),(unsigned long*)addr)
434 #define ext2_clear_bit(nr, addr) \
435 __test_and_clear_bit((nr),(unsigned long*)addr)
436 #define ext2_clear_bit_atomic(lock,nr, addr) \
437 test_and_clear_bit((nr),(unsigned long*)addr)
438 #define ext2_test_bit(nr, addr) test_bit((nr),(unsigned long*)addr)
439 #define ext2_find_first_zero_bit(addr, size) \
440 find_first_zero_bit((unsigned long*)addr, size)
441 #define ext2_find_next_zero_bit(addr, size, off) \
442 find_next_zero_bit((unsigned long*)addr, size, off)
444 /* Bitmap functions for the minix filesystem. */
445 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,(void*)addr)
446 #define minix_set_bit(nr,addr) __set_bit(nr,(void*)addr)
447 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,(void*)addr)
448 #define minix_test_bit(nr,addr) test_bit(nr,(void*)addr)
449 #define minix_find_first_zero_bit(addr,size) \
450 find_first_zero_bit((void*)addr,size)
452 #endif /* __KERNEL__ */
454 #endif /* _I386_BITOPS_H */