1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
10 #define LOCK_PREFIX "lock ; "
12 #define LOCK_PREFIX ""
15 #define ADDR (*(volatile long *) addr)
18 * set_bit - Atomically set a bit in memory
20 * @addr: the address to start counting from
22 * This function is atomic and may not be reordered. See __set_bit()
23 * if you do not require the atomic guarantees.
24 * Note that @nr may be almost arbitrarily large; this function is not
25 * restricted to acting on a single-word quantity.
27 static __inline__ void set_bit(int nr, volatile void * addr)
29 __asm__ __volatile__( LOCK_PREFIX
32 :"dIr" (nr) : "memory");
36 * __set_bit - Set a bit in memory
38 * @addr: the address to start counting from
40 * Unlike set_bit(), this function is non-atomic and may be reordered.
41 * If it's called on the same region of memory simultaneously, the effect
42 * may be that only one operation succeeds.
44 static __inline__ void __set_bit(int nr, volatile void * addr)
49 :"dIr" (nr) : "memory");
53 * clear_bit - Clears a bit in memory
55 * @addr: Address to start counting from
57 * clear_bit() is atomic and may not be reordered. However, it does
58 * not contain a memory barrier, so if it is used for locking purposes,
59 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
60 * in order to ensure changes are visible on other processors.
62 static __inline__ void clear_bit(int nr, volatile void * addr)
64 __asm__ __volatile__( LOCK_PREFIX
70 static __inline__ void __clear_bit(int nr, volatile void * addr)
78 #define smp_mb__before_clear_bit() barrier()
79 #define smp_mb__after_clear_bit() barrier()
82 * __change_bit - Toggle a bit in memory
83 * @nr: the bit to change
84 * @addr: the address to start counting from
86 * Unlike change_bit(), this function is non-atomic and may be reordered.
87 * If it's called on the same region of memory simultaneously, the effect
88 * may be that only one operation succeeds.
90 static __inline__ void __change_bit(int nr, volatile void * addr)
99 * change_bit - Toggle a bit in memory
101 * @addr: Address to start counting from
103 * change_bit() is atomic and may not be reordered.
104 * Note that @nr may be almost arbitrarily large; this function is not
105 * restricted to acting on a single-word quantity.
107 static __inline__ void change_bit(int nr, volatile void * addr)
109 __asm__ __volatile__( LOCK_PREFIX
116 * test_and_set_bit - Set a bit and return its old value
118 * @addr: Address to count from
120 * This operation is atomic and cannot be reordered.
121 * It also implies a memory barrier.
123 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
127 __asm__ __volatile__( LOCK_PREFIX
128 "btsl %2,%1\n\tsbbl %0,%0"
129 :"=r" (oldbit),"+m" (ADDR)
130 :"dIr" (nr) : "memory");
135 * __test_and_set_bit - Set a bit and return its old value
137 * @addr: Address to count from
139 * This operation is non-atomic and can be reordered.
140 * If two examples of this operation race, one can appear to succeed
141 * but actually fail. You must protect multiple accesses with a lock.
143 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
148 "btsl %2,%1\n\tsbbl %0,%0"
149 :"=r" (oldbit),"+m" (ADDR)
155 * test_and_clear_bit - Clear a bit and return its old value
157 * @addr: Address to count from
159 * This operation is atomic and cannot be reordered.
160 * It also implies a memory barrier.
162 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
166 __asm__ __volatile__( LOCK_PREFIX
167 "btrl %2,%1\n\tsbbl %0,%0"
168 :"=r" (oldbit),"+m" (ADDR)
169 :"dIr" (nr) : "memory");
174 * __test_and_clear_bit - Clear a bit and return its old value
176 * @addr: Address to count from
178 * This operation is non-atomic and can be reordered.
179 * If two examples of this operation race, one can appear to succeed
180 * but actually fail. You must protect multiple accesses with a lock.
182 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
187 "btrl %2,%1\n\tsbbl %0,%0"
188 :"=r" (oldbit),"+m" (ADDR)
193 /* WARNING: non atomic and it can be reordered! */
194 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
198 __asm__ __volatile__(
199 "btcl %2,%1\n\tsbbl %0,%0"
200 :"=r" (oldbit),"+m" (ADDR)
201 :"dIr" (nr) : "memory");
206 * test_and_change_bit - Change a bit and return its old value
208 * @addr: Address to count from
210 * This operation is atomic and cannot be reordered.
211 * It also implies a memory barrier.
213 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
217 __asm__ __volatile__( LOCK_PREFIX
218 "btcl %2,%1\n\tsbbl %0,%0"
219 :"=r" (oldbit),"+m" (ADDR)
220 :"dIr" (nr) : "memory");
224 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
226 * test_bit - Determine whether a bit is set
227 * @nr: bit number to test
228 * @addr: Address to start counting from
230 static int test_bit(int nr, const volatile void * addr);
233 static __inline__ int constant_test_bit(int nr, const volatile void * addr)
235 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
238 static __inline__ int variable_test_bit(int nr, volatile const void * addr)
242 __asm__ __volatile__(
243 "btl %2,%1\n\tsbbl %0,%0"
245 :"m" (ADDR),"dIr" (nr));
249 #define test_bit(nr,addr) \
250 (__builtin_constant_p(nr) ? \
251 constant_test_bit((nr),(addr)) : \
252 variable_test_bit((nr),(addr)))
256 extern long find_first_zero_bit(const unsigned long * addr, unsigned long size);
257 extern long find_next_zero_bit (const unsigned long * addr, long size, long offset);
258 extern long find_first_bit(const unsigned long * addr, unsigned long size);
259 extern long find_next_bit(const unsigned long * addr, long size, long offset);
261 /* return index of first bet set in val or max when no bit is set */
262 static inline unsigned long __scanbit(unsigned long val, unsigned long max)
264 asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val) : "r" (val), "r" (max));
268 #define find_first_bit(addr,size) \
269 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
270 (__scanbit(*(unsigned long *)addr,(size))) : \
271 find_first_bit(addr,size)))
273 #define find_next_bit(addr,size,off) \
274 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
275 ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
276 find_next_bit(addr,size,off)))
278 #define find_first_zero_bit(addr,size) \
279 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
280 (__scanbit(~*(unsigned long *)addr,(size))) : \
281 find_first_zero_bit(addr,size)))
283 #define find_next_zero_bit(addr,size,off) \
284 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
285 ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
286 find_next_zero_bit(addr,size,off)))
289 * Find string of zero bits in a bitmap. -1 when not found.
292 find_next_zero_string(unsigned long *bitmap, long start, long nbits, int len);
294 static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
297 unsigned long end = i + len;
299 __set_bit(i, bitmap);
304 static inline void __clear_bit_string(unsigned long *bitmap, unsigned long i,
307 unsigned long end = i + len;
309 __clear_bit(i, bitmap);
315 * ffz - find first zero in word.
316 * @word: The word to search
318 * Undefined if no zero exists, so code should check against ~0UL first.
320 static __inline__ unsigned long ffz(unsigned long word)
329 * __ffs - find first bit in word.
330 * @word: The word to search
332 * Undefined if no bit exists, so code should check against 0 first.
334 static __inline__ unsigned long __ffs(unsigned long word)
343 * __fls: find last bit set.
344 * @word: The word to search
346 * Undefined if no zero exists, so code should check against ~0UL first.
348 static __inline__ unsigned long __fls(unsigned long word)
358 #include <asm-generic/bitops/sched.h>
361 * ffs - find first bit set
362 * @x: the word to search
364 * This is defined the same way as
365 * the libc and compiler builtin ffs routines, therefore
366 * differs in spirit from the above ffz (man ffs).
368 static __inline__ int ffs(int x)
372 __asm__("bsfl %1,%0\n\t"
374 : "=r" (r) : "rm" (x), "r" (-1));
379 * fls64 - find last bit set in 64 bit word
380 * @x: the word to search
382 * This is defined the same way as fls.
384 static __inline__ int fls64(__u64 x)
392 * fls - find last bit set
393 * @x: the word to search
395 * This is defined the same way as ffs.
397 static __inline__ int fls(int x)
401 __asm__("bsrl %1,%0\n\t"
403 : "=&r" (r) : "rm" (x), "rm" (-1));
407 #include <asm-generic/bitops/hweight.h>
409 #endif /* __KERNEL__ */
413 #include <asm-generic/bitops/ext2-non-atomic.h>
415 #define ext2_set_bit_atomic(lock,nr,addr) \
416 test_and_set_bit((nr),(unsigned long*)addr)
417 #define ext2_clear_bit_atomic(lock,nr,addr) \
418 test_and_clear_bit((nr),(unsigned long*)addr)
420 #include <asm-generic/bitops/minix.h>
422 #endif /* __KERNEL__ */
424 #endif /* _X86_64_BITOPS_H */