5 * Copyright 1992, Linus Torvalds.
8 #include <linux/compiler.h>
9 #include <asm/alternative.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).
19 #define ADDR (*(volatile long *) addr)
22 * set_bit - Atomically set a bit in memory
24 * @addr: the address to start counting from
26 * This function is atomic and may not be reordered. See __set_bit()
27 * if you do not require the atomic guarantees.
29 * Note: there are no guarantees that this function will not be reordered
30 * on non x86 architectures, so if you are writting portable code,
31 * make sure not to rely on its reordering guarantees.
33 * Note that @nr may be almost arbitrarily large; this function is not
34 * restricted to acting on a single-word quantity.
36 static inline void set_bit(int nr
, volatile unsigned long * addr
)
38 __asm__
__volatile__( LOCK_PREFIX
45 * __set_bit - Set a bit in memory
47 * @addr: the address to start counting from
49 * Unlike set_bit(), this function is non-atomic and may be reordered.
50 * If it's called on the same region of memory simultaneously, the effect
51 * may be that only one operation succeeds.
53 static inline void __set_bit(int nr
, volatile unsigned long * addr
)
62 * clear_bit - Clears a bit in memory
64 * @addr: Address to start counting from
66 * clear_bit() is atomic and may not be reordered. However, it does
67 * not contain a memory barrier, so if it is used for locking purposes,
68 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
69 * in order to ensure changes are visible on other processors.
71 static inline void clear_bit(int nr
, volatile unsigned long * addr
)
73 __asm__
__volatile__( LOCK_PREFIX
79 static inline void __clear_bit(int nr
, volatile unsigned long * addr
)
86 #define smp_mb__before_clear_bit() barrier()
87 #define smp_mb__after_clear_bit() barrier()
90 * __change_bit - Toggle a bit in memory
91 * @nr: the bit to change
92 * @addr: the address to start counting from
94 * Unlike change_bit(), this function is non-atomic and may be reordered.
95 * If it's called on the same region of memory simultaneously, the effect
96 * may be that only one operation succeeds.
98 static inline void __change_bit(int nr
, volatile unsigned long * addr
)
100 __asm__
__volatile__(
107 * change_bit - Toggle a bit in memory
109 * @addr: Address to start counting from
111 * change_bit() is atomic and may not be reordered. It may be
112 * reordered on other architectures than x86.
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 may be reordered on other architectures than x86.
131 * It also implies a memory barrier.
133 static inline int test_and_set_bit(int nr
, volatile unsigned long * addr
)
137 __asm__
__volatile__( LOCK_PREFIX
138 "btsl %2,%1\n\tsbbl %0,%0"
139 :"=r" (oldbit
),"+m" (ADDR
)
140 :"Ir" (nr
) : "memory");
145 * __test_and_set_bit - Set a bit and return its old value
147 * @addr: Address to count from
149 * This operation is non-atomic and can be reordered.
150 * If two examples of this operation race, one can appear to succeed
151 * but actually fail. You must protect multiple accesses with a lock.
153 static inline int __test_and_set_bit(int nr
, volatile unsigned long * addr
)
158 "btsl %2,%1\n\tsbbl %0,%0"
159 :"=r" (oldbit
),"+m" (ADDR
)
165 * test_and_clear_bit - Clear a bit and return its old value
167 * @addr: Address to count from
169 * This operation is atomic and cannot be reordered.
170 * It can be reorderdered on other architectures other than x86.
171 * It also implies a memory barrier.
173 static inline int test_and_clear_bit(int nr
, volatile unsigned long * addr
)
177 __asm__
__volatile__( LOCK_PREFIX
178 "btrl %2,%1\n\tsbbl %0,%0"
179 :"=r" (oldbit
),"+m" (ADDR
)
180 :"Ir" (nr
) : "memory");
185 * __test_and_clear_bit - Clear a bit and return its old value
187 * @addr: Address to count from
189 * This operation is non-atomic and can be reordered.
190 * If two examples of this operation race, one can appear to succeed
191 * but actually fail. You must protect multiple accesses with a lock.
193 static inline int __test_and_clear_bit(int nr
, volatile unsigned long *addr
)
198 "btrl %2,%1\n\tsbbl %0,%0"
199 :"=r" (oldbit
),"+m" (ADDR
)
204 /* WARNING: non atomic and it can be reordered! */
205 static inline int __test_and_change_bit(int nr
, volatile unsigned long *addr
)
209 __asm__
__volatile__(
210 "btcl %2,%1\n\tsbbl %0,%0"
211 :"=r" (oldbit
),"+m" (ADDR
)
212 :"Ir" (nr
) : "memory");
217 * test_and_change_bit - Change a bit and return its old value
219 * @addr: Address to count from
221 * This operation is atomic and cannot be reordered.
222 * It also implies a memory barrier.
224 static inline int test_and_change_bit(int nr
, volatile unsigned long* addr
)
228 __asm__
__volatile__( LOCK_PREFIX
229 "btcl %2,%1\n\tsbbl %0,%0"
230 :"=r" (oldbit
),"+m" (ADDR
)
231 :"Ir" (nr
) : "memory");
235 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
237 * test_bit - Determine whether a bit is set
238 * @nr: bit number to test
239 * @addr: Address to start counting from
241 static int test_bit(int nr
, const volatile void * addr
);
244 static __always_inline
int constant_test_bit(int nr
, const volatile unsigned long *addr
)
246 return ((1UL << (nr
& 31)) & (addr
[nr
>> 5])) != 0;
249 static inline int variable_test_bit(int nr
, const volatile unsigned long * addr
)
253 __asm__
__volatile__(
254 "btl %2,%1\n\tsbbl %0,%0"
256 :"m" (ADDR
),"Ir" (nr
));
260 #define test_bit(nr,addr) \
261 (__builtin_constant_p(nr) ? \
262 constant_test_bit((nr),(addr)) : \
263 variable_test_bit((nr),(addr)))
268 * find_first_zero_bit - find the first zero bit in a memory region
269 * @addr: The address to start the search at
270 * @size: The maximum size to search
272 * Returns the bit-number of the first zero bit, not the number of the byte
275 static inline int find_first_zero_bit(const unsigned long *addr
, unsigned size
)
282 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
283 __asm__
__volatile__(
285 "xorl %%edx,%%edx\n\t"
288 "xorl -4(%%edi),%%eax\n\t"
291 "1:\tsubl %%ebx,%%edi\n\t"
294 :"=d" (res
), "=&c" (d0
), "=&D" (d1
), "=&a" (d2
)
295 :"1" ((size
+ 31) >> 5), "2" (addr
), "b" (addr
) : "memory");
300 * find_next_zero_bit - find the first zero bit in a memory region
301 * @addr: The address to base the search on
302 * @offset: The bitnumber to start searching at
303 * @size: The maximum size to search
305 int find_next_zero_bit(const unsigned long *addr
, int size
, int offset
);
308 * __ffs - find first bit in word.
309 * @word: The word to search
311 * Undefined if no bit exists, so code should check against 0 first.
313 static inline unsigned long __ffs(unsigned long word
)
322 * find_first_bit - find the first set bit in a memory region
323 * @addr: The address to start the search at
324 * @size: The maximum size to search
326 * Returns the bit-number of the first set bit, not the number of the byte
329 static inline unsigned find_first_bit(const unsigned long *addr
, unsigned size
)
334 unsigned long val
= *addr
++;
336 return __ffs(val
) + x
;
337 x
+= (sizeof(*addr
)<<3);
343 * find_next_bit - find the first set bit in a memory region
344 * @addr: The address to base the search on
345 * @offset: The bitnumber to start searching at
346 * @size: The maximum size to search
348 int find_next_bit(const unsigned long *addr
, int size
, int offset
);
351 * ffz - find first zero in word.
352 * @word: The word to search
354 * Undefined if no zero exists, so code should check against ~0UL first.
356 static inline unsigned long ffz(unsigned long word
)
366 #include <asm-generic/bitops/sched.h>
369 * ffs - find first bit set
370 * @x: the word to search
372 * This is defined the same way as
373 * the libc and compiler builtin ffs routines, therefore
374 * differs in spirit from the above ffz() (man ffs).
376 static inline int ffs(int x
)
380 __asm__("bsfl %1,%0\n\t"
383 "1:" : "=r" (r
) : "rm" (x
));
388 * fls - find last bit set
389 * @x: the word to search
391 * This is defined the same way as ffs().
393 static inline int fls(int x
)
397 __asm__("bsrl %1,%0\n\t"
400 "1:" : "=r" (r
) : "rm" (x
));
404 #include <asm-generic/bitops/hweight.h>
406 #endif /* __KERNEL__ */
408 #include <asm-generic/bitops/fls64.h>
412 #include <asm-generic/bitops/ext2-non-atomic.h>
414 #define ext2_set_bit_atomic(lock,nr,addr) \
415 test_and_set_bit((nr),(unsigned long*)addr)
416 #define ext2_clear_bit_atomic(lock,nr, addr) \
417 test_and_clear_bit((nr),(unsigned long*)addr)
419 #include <asm-generic/bitops/minix.h>
421 #endif /* __KERNEL__ */
423 #endif /* _I386_BITOPS_H */