1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #define ADDR (*(volatile long *) addr)
11 * set_bit - Atomically set a bit in memory
13 * @addr: the address to start counting from
15 * This function is atomic and may not be reordered. See __set_bit()
16 * if you do not require the atomic guarantees.
17 * Note that @nr may be almost arbitrarily large; this function is not
18 * restricted to acting on a single-word quantity.
20 static __inline__ void set_bit(int nr, volatile void * addr)
25 :"dIr" (nr) : "memory");
29 * __set_bit - Set a bit in memory
31 * @addr: the address to start counting from
33 * Unlike set_bit(), this function is non-atomic and may be reordered.
34 * If it's called on the same region of memory simultaneously, the effect
35 * may be that only one operation succeeds.
37 static __inline__ void __set_bit(int nr, volatile void * addr)
42 :"dIr" (nr) : "memory");
46 * clear_bit - Clears a bit in memory
48 * @addr: Address to start counting from
50 * clear_bit() is atomic and may not be reordered. However, it does
51 * not contain a memory barrier, so if it is used for locking purposes,
52 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
53 * in order to ensure changes are visible on other processors.
55 static __inline__ void clear_bit(int nr, volatile void * addr)
63 static __inline__ void __clear_bit(int nr, volatile void * addr)
71 #define smp_mb__before_clear_bit() barrier()
72 #define smp_mb__after_clear_bit() barrier()
75 * __change_bit - Toggle a bit in memory
76 * @nr: the bit to change
77 * @addr: the address to start counting from
79 * Unlike change_bit(), this function is non-atomic and may be reordered.
80 * If it's called on the same region of memory simultaneously, the effect
81 * may be that only one operation succeeds.
83 static __inline__ void __change_bit(int nr, volatile void * addr)
92 * change_bit - Toggle a bit in memory
94 * @addr: Address to start counting from
96 * change_bit() is atomic and may not be reordered.
97 * Note that @nr may be almost arbitrarily large; this function is not
98 * restricted to acting on a single-word quantity.
100 static __inline__ void change_bit(int nr, volatile void * addr)
102 __asm__ __volatile__(
109 * test_and_set_bit - Set a bit and return its old value
111 * @addr: Address to count from
113 * This operation is atomic and cannot be reordered.
114 * It also implies a memory barrier.
116 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
120 __asm__ __volatile__(
121 "lock ; btsl %2,%1\n\tsbbl %0,%0"
122 :"=r" (oldbit),"+m" (ADDR)
123 :"dIr" (nr) : "memory");
128 * __test_and_set_bit - Set a bit and return its old value
130 * @addr: Address to count from
132 * This operation is non-atomic and can be reordered.
133 * If two examples of this operation race, one can appear to succeed
134 * but actually fail. You must protect multiple accesses with a lock.
136 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
141 "btsl %2,%1\n\tsbbl %0,%0"
142 :"=r" (oldbit),"+m" (ADDR)
148 * test_and_clear_bit - Clear a bit and return its old value
150 * @addr: Address to count from
152 * This operation is atomic and cannot be reordered.
153 * It also implies a memory barrier.
155 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
159 __asm__ __volatile__(
160 "lock ; btrl %2,%1\n\tsbbl %0,%0"
161 :"=r" (oldbit),"+m" (ADDR)
162 :"dIr" (nr) : "memory");
167 * __test_and_clear_bit - Clear a bit and return its old value
169 * @addr: Address to count from
171 * This operation is non-atomic and can be reordered.
172 * If two examples of this operation race, one can appear to succeed
173 * but actually fail. You must protect multiple accesses with a lock.
175 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
180 "btrl %2,%1\n\tsbbl %0,%0"
181 :"=r" (oldbit),"+m" (ADDR)
186 /* WARNING: non atomic and it can be reordered! */
187 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
191 __asm__ __volatile__(
192 "btcl %2,%1\n\tsbbl %0,%0"
193 :"=r" (oldbit),"+m" (ADDR)
194 :"dIr" (nr) : "memory");
199 * test_and_change_bit - Change a bit and return its old value
201 * @addr: Address to count from
203 * This operation is atomic and cannot be reordered.
204 * It also implies a memory barrier.
206 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
210 __asm__ __volatile__(
211 "lock ; btcl %2,%1\n\tsbbl %0,%0"
212 :"=r" (oldbit),"+m" (ADDR)
213 :"dIr" (nr) : "memory");
217 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
219 * test_bit - Determine whether a bit is set
220 * @nr: bit number to test
221 * @addr: Address to start counting from
223 static int test_bit(int nr, const volatile void * addr);
226 static __inline__ int constant_test_bit(int nr, const volatile void * addr)
228 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
231 static __inline__ int variable_test_bit(int nr, volatile const void * addr)
235 __asm__ __volatile__(
236 "btl %2,%1\n\tsbbl %0,%0"
238 :"m" (ADDR),"dIr" (nr));
242 #define test_bit(nr,addr) \
243 (__builtin_constant_p(nr) ? \
244 constant_test_bit((nr),(addr)) : \
245 variable_test_bit((nr),(addr)))
249 extern long find_first_zero_bit(const unsigned long * addr, unsigned long size);
250 extern long find_next_zero_bit (const unsigned long * addr, long size, long offset);
251 extern long find_first_bit(const unsigned long * addr, unsigned long size);
252 extern long find_next_bit(const unsigned long * addr, long size, long offset);
254 /* return index of first bet set in val or max when no bit is set */
255 static inline unsigned long __scanbit(unsigned long val, unsigned long max)
257 asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val) : "r" (val), "r" (max));
261 #define find_first_bit(addr,size) \
262 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
263 (__scanbit(*(unsigned long *)addr,(size))) : \
264 find_first_bit(addr,size)))
266 #define find_next_bit(addr,size,off) \
267 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
268 ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
269 find_next_bit(addr,size,off)))
271 #define find_first_zero_bit(addr,size) \
272 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
273 (__scanbit(~*(unsigned long *)addr,(size))) : \
274 find_first_zero_bit(addr,size)))
276 #define find_next_zero_bit(addr,size,off) \
277 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
278 ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
279 find_next_zero_bit(addr,size,off)))
282 * Find string of zero bits in a bitmap. -1 when not found.
285 find_next_zero_string(unsigned long *bitmap, long start, long nbits, int len);
287 static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
290 unsigned long end = i + len;
292 __set_bit(i, bitmap);
297 static inline void __clear_bit_string(unsigned long *bitmap, unsigned long i,
300 unsigned long end = i + len;
302 __clear_bit(i, bitmap);
308 * ffz - find first zero in word.
309 * @word: The word to search
311 * Undefined if no zero exists, so code should check against ~0UL first.
313 static __inline__ unsigned long ffz(unsigned long word)
322 * __ffs - find first bit in word.
323 * @word: The word to search
325 * Undefined if no bit exists, so code should check against 0 first.
327 static __inline__ unsigned long __ffs(unsigned long word)
336 * __fls: find last bit set.
337 * @word: The word to search
339 * Undefined if no zero exists, so code should check against ~0UL first.
341 static __inline__ unsigned long __fls(unsigned long word)
352 * ffs - find first bit set
353 * @x: the word to search
355 * This is defined the same way as
356 * the libc and compiler builtin ffs routines, therefore
357 * differs in spirit from the above ffz (man ffs).
359 static __inline__ int ffs(int x)
363 __asm__("bsfl %1,%0\n\t"
365 : "=r" (r) : "rm" (x), "r" (-1));
370 * fls64 - find last bit set in 64 bit word
371 * @x: the word to search
373 * This is defined the same way as fls.
375 static __inline__ int fls64(__u64 x)
383 * fls - find last bit set
384 * @x: the word to search
386 * This is defined the same way as ffs.
388 static __inline__ int fls(int x)
392 __asm__("bsrl %1,%0\n\t"
394 : "=&r" (r) : "rm" (x), "rm" (-1));
398 #include <arch-generic/bitops/hweight.h>
400 #endif /* __KERNEL__ */
402 #endif /* _X86_64_BITOPS_H */