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.
35 * Note: there are no guarantees that this function will not be reordered
36 * on non x86 architectures, so if you are writting portable code,
37 * make sure not to rely on its reordering guarantees.
39 * Note that @nr may be almost arbitrarily large; this function is not
40 * restricted to acting on a single-word quantity.
42 static inline void set_bit(int nr, volatile unsigned long * addr)
44 __asm__ __volatile__( LOCK_PREFIX
51 * __set_bit - Set a bit in memory
53 * @addr: the address to start counting from
55 * Unlike set_bit(), this function is non-atomic and may be reordered.
56 * If it's called on the same region of memory simultaneously, the effect
57 * may be that only one operation succeeds.
59 static inline void __set_bit(int nr, volatile unsigned long * addr)
68 * clear_bit - Clears a bit in memory
70 * @addr: Address to start counting from
72 * clear_bit() is atomic and may not be reordered. However, it does
73 * not contain a memory barrier, so if it is used for locking purposes,
74 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
75 * in order to ensure changes are visible on other processors.
77 static inline void clear_bit(int nr, volatile unsigned long * addr)
79 __asm__ __volatile__( LOCK_PREFIX
85 static inline void __clear_bit(int nr, volatile unsigned long * addr)
92 #define smp_mb__before_clear_bit() barrier()
93 #define smp_mb__after_clear_bit() barrier()
96 * __change_bit - Toggle a bit in memory
97 * @nr: the bit to change
98 * @addr: the address to start counting from
100 * Unlike change_bit(), this function is non-atomic and may be reordered.
101 * If it's called on the same region of memory simultaneously, the effect
102 * may be that only one operation succeeds.
104 static inline void __change_bit(int nr, volatile unsigned long * addr)
106 __asm__ __volatile__(
113 * change_bit - Toggle a bit in memory
115 * @addr: Address to start counting from
117 * change_bit() is atomic and may not be reordered. It may be
118 * reordered on other architectures than x86.
119 * Note that @nr may be almost arbitrarily large; this function is not
120 * restricted to acting on a single-word quantity.
122 static inline void change_bit(int nr, volatile unsigned long * addr)
124 __asm__ __volatile__( LOCK_PREFIX
131 * test_and_set_bit - Set a bit and return its old value
133 * @addr: Address to count from
135 * This operation is atomic and cannot be reordered.
136 * It may be reordered on other architectures than x86.
137 * It also implies a memory barrier.
139 static inline int test_and_set_bit(int nr, volatile unsigned long * addr)
143 __asm__ __volatile__( LOCK_PREFIX
144 "btsl %2,%1\n\tsbbl %0,%0"
145 :"=r" (oldbit),"=m" (ADDR)
146 :"Ir" (nr) : "memory");
151 * __test_and_set_bit - Set a bit and return its old value
153 * @addr: Address to count from
155 * This operation is non-atomic and can be reordered.
156 * If two examples of this operation race, one can appear to succeed
157 * but actually fail. You must protect multiple accesses with a lock.
159 static inline int __test_and_set_bit(int nr, volatile unsigned long * addr)
164 "btsl %2,%1\n\tsbbl %0,%0"
165 :"=r" (oldbit),"=m" (ADDR)
171 * test_and_clear_bit - Clear a bit and return its old value
173 * @addr: Address to count from
175 * This operation is atomic and cannot be reordered.
176 * It can be reorderdered on other architectures other than x86.
177 * It also implies a memory barrier.
179 static inline int test_and_clear_bit(int nr, volatile unsigned long * addr)
183 __asm__ __volatile__( LOCK_PREFIX
184 "btrl %2,%1\n\tsbbl %0,%0"
185 :"=r" (oldbit),"=m" (ADDR)
186 :"Ir" (nr) : "memory");
191 * __test_and_clear_bit - Clear a bit and return its old value
193 * @addr: Address to count from
195 * This operation is non-atomic and can be reordered.
196 * If two examples of this operation race, one can appear to succeed
197 * but actually fail. You must protect multiple accesses with a lock.
199 static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
204 "btrl %2,%1\n\tsbbl %0,%0"
205 :"=r" (oldbit),"=m" (ADDR)
210 /* WARNING: non atomic and it can be reordered! */
211 static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
215 __asm__ __volatile__(
216 "btcl %2,%1\n\tsbbl %0,%0"
217 :"=r" (oldbit),"=m" (ADDR)
218 :"Ir" (nr) : "memory");
223 * test_and_change_bit - Change a bit and return its old value
225 * @addr: Address to count from
227 * This operation is atomic and cannot be reordered.
228 * It also implies a memory barrier.
230 static inline int test_and_change_bit(int nr, volatile unsigned long* addr)
234 __asm__ __volatile__( LOCK_PREFIX
235 "btcl %2,%1\n\tsbbl %0,%0"
236 :"=r" (oldbit),"=m" (ADDR)
237 :"Ir" (nr) : "memory");
241 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
243 * test_bit - Determine whether a bit is set
244 * @nr: bit number to test
245 * @addr: Address to start counting from
247 static int test_bit(int nr, const volatile void * addr);
250 static inline int constant_test_bit(int nr, const volatile unsigned long *addr)
252 return ((1UL << (nr & 31)) & (addr[nr >> 5])) != 0;
255 static inline int variable_test_bit(int nr, const volatile unsigned long * addr)
259 __asm__ __volatile__(
260 "btl %2,%1\n\tsbbl %0,%0"
262 :"m" (ADDR),"Ir" (nr));
266 #define test_bit(nr,addr) \
267 (__builtin_constant_p(nr) ? \
268 constant_test_bit((nr),(addr)) : \
269 variable_test_bit((nr),(addr)))
274 * find_first_zero_bit - find the first zero bit in a memory region
275 * @addr: The address to start the search at
276 * @size: The maximum size to search
278 * Returns the bit-number of the first zero bit, not the number of the byte
281 static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
288 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
289 __asm__ __volatile__(
291 "xorl %%edx,%%edx\n\t"
294 "xorl -4(%%edi),%%eax\n\t"
297 "1:\tsubl %%ebx,%%edi\n\t"
300 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
301 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
306 * find_next_zero_bit - find the first zero bit in a memory region
307 * @addr: The address to base the search on
308 * @offset: The bitnumber to start searching at
309 * @size: The maximum size to search
311 int find_next_zero_bit(const unsigned long *addr, int size, int offset);
314 * __ffs - find first bit in word.
315 * @word: The word to search
317 * Undefined if no bit exists, so code should check against 0 first.
319 static inline unsigned long __ffs(unsigned long word)
328 * find_first_bit - find the first set bit in a memory region
329 * @addr: The address to start the search at
330 * @size: The maximum size to search
332 * Returns the bit-number of the first set bit, not the number of the byte
335 static inline int find_first_bit(const unsigned long *addr, unsigned size)
340 unsigned long val = *addr++;
342 return __ffs(val) + x;
343 x += (sizeof(*addr)<<3);
349 * find_next_bit - find the first set bit in a memory region
350 * @addr: The address to base the search on
351 * @offset: The bitnumber to start searching at
352 * @size: The maximum size to search
354 int find_next_bit(const unsigned long *addr, int size, int offset);
357 * ffz - find first zero in word.
358 * @word: The word to search
360 * Undefined if no zero exists, so code should check against ~0UL first.
362 static inline unsigned long ffz(unsigned long word)
371 * fls: find last bit set.
374 #define fls(x) generic_fls(x)
375 #define fls64(x) generic_fls64(x)
380 * Every architecture must define this function. It's the fastest
381 * way of searching a 140-bit bitmap where the first 100 bits are
382 * unlikely to be set. It's guaranteed that at least one of the 140
385 static inline int sched_find_first_bit(const unsigned long *b)
390 return __ffs(b[1]) + 32;
392 return __ffs(b[2]) + 64;
394 return __ffs(b[3]) + 96;
395 return __ffs(b[4]) + 128;
399 * ffs - find first bit set
400 * @x: the word to search
402 * This is defined the same way as
403 * the libc and compiler builtin ffs routines, therefore
404 * differs in spirit from the above ffz (man ffs).
406 static inline int ffs(int x)
410 __asm__("bsfl %1,%0\n\t"
413 "1:" : "=r" (r) : "rm" (x));
418 * hweightN - returns the hamming weight of a N-bit word
419 * @x: the word to weigh
421 * The Hamming Weight of a number is the total number of bits set in it.
424 #define hweight32(x) generic_hweight32(x)
425 #define hweight16(x) generic_hweight16(x)
426 #define hweight8(x) generic_hweight8(x)
428 #endif /* __KERNEL__ */
432 #define ext2_set_bit(nr,addr) \
433 __test_and_set_bit((nr),(unsigned long*)addr)
434 #define ext2_set_bit_atomic(lock,nr,addr) \
435 test_and_set_bit((nr),(unsigned long*)addr)
436 #define ext2_clear_bit(nr, addr) \
437 __test_and_clear_bit((nr),(unsigned long*)addr)
438 #define ext2_clear_bit_atomic(lock,nr, addr) \
439 test_and_clear_bit((nr),(unsigned long*)addr)
440 #define ext2_test_bit(nr, addr) test_bit((nr),(unsigned long*)addr)
441 #define ext2_find_first_zero_bit(addr, size) \
442 find_first_zero_bit((unsigned long*)addr, size)
443 #define ext2_find_next_zero_bit(addr, size, off) \
444 find_next_zero_bit((unsigned long*)addr, size, off)
446 /* Bitmap functions for the minix filesystem. */
447 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,(void*)addr)
448 #define minix_set_bit(nr,addr) __set_bit(nr,(void*)addr)
449 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,(void*)addr)
450 #define minix_test_bit(nr,addr) test_bit(nr,(void*)addr)
451 #define minix_find_first_zero_bit(addr,size) \
452 find_first_zero_bit((void*)addr,size)
454 #endif /* __KERNEL__ */
456 #endif /* _I386_BITOPS_H */