1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #include <linux/config.h>
11 * These have to be done with inline assembly: that way the bit-setting
12 * is guaranteed to be atomic. All bit operations return 0 if the bit
13 * was cleared before the operation and != 0 if it was not.
15 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
19 #define LOCK_PREFIX "lock ; "
21 #define LOCK_PREFIX ""
24 #define ADDR (*(volatile long *) addr)
27 * set_bit - Atomically set a bit in memory
29 * @addr: the address to start counting from
31 * This function is atomic and may not be reordered. See __set_bit()
32 * if you do not require the atomic 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(long nr, volatile void * addr)
38 __asm__ __volatile__( LOCK_PREFIX
41 :"dIr" (nr), "m" (ADDR));
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(long nr, volatile void * addr)
58 :"dIr" (nr), "m" (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(long nr, volatile void * addr)
73 __asm__ __volatile__( LOCK_PREFIX
76 :"dIr" (nr), "m" (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
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(long nr, volatile void * addr)
95 :"dIr" (nr), "m" (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(long nr, volatile void * addr)
109 __asm__ __volatile__( LOCK_PREFIX
112 :"dIr" (nr), "m" (ADDR));
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(long nr, volatile void * addr)
127 __asm__ __volatile__( LOCK_PREFIX
128 "btsq %2,%1\n\tsbbq %0,%0"
129 :"=r" (oldbit),"=m" (ADDR)
130 :"dIr" (nr), "m" (ADDR) : "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(long nr, volatile void * addr)
148 "btsq %2,%1\n\tsbbq %0,%0"
149 :"=r" (oldbit),"=m" (ADDR)
150 :"dIr" (nr), "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(long nr, volatile void * addr)
166 __asm__ __volatile__( LOCK_PREFIX
167 "btrq %2,%1\n\tsbbq %0,%0"
168 :"=r" (oldbit),"=m" (ADDR)
169 :"dIr" (nr), "m" (ADDR) : "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(long nr, volatile void * addr)
187 "btrq %2,%1\n\tsbbq %0,%0"
188 :"=r" (oldbit),"=m" (ADDR)
189 :"dIr" (nr), "m" (ADDR));
193 /* WARNING: non atomic and it can be reordered! */
194 static __inline__ int __test_and_change_bit(long nr, volatile void * addr)
198 __asm__ __volatile__(
199 "btcq %2,%1\n\tsbbq %0,%0"
200 :"=r" (oldbit),"=m" (ADDR)
201 :"dIr" (nr), "m" (ADDR) : "memory");
206 * test_and_change_bit - Change a bit and return its new 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(long nr, volatile void * addr)
217 __asm__ __volatile__( LOCK_PREFIX
218 "btcq %2,%1\n\tsbbq %0,%0"
219 :"=r" (oldbit),"=m" (ADDR)
220 :"dIr" (nr), "m" (ADDR) : "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(long 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(long nr, volatile void * addr)
242 __asm__ __volatile__(
243 "btq %2,%1\n\tsbbq %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)))
255 * find_first_zero_bit - find the first zero bit in a memory region
256 * @addr: The address to start the search at
257 * @size: The maximum bitnumber to search
259 * Returns the bit-number of the first zero bit, not the number of the byte
260 * containing a bit. -1 when none found.
262 static __inline__ int find_first_zero_bit(void * addr, unsigned size)
269 __asm__ __volatile__(
271 "xorl %%edx,%%edx\n\t"
274 "xorl -4(%%rdi),%%eax\n\t"
277 "1:\tsubq %%rbx,%%rdi\n\t"
280 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
281 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
286 * find_next_zero_bit - find the first zero bit in a memory region
287 * @addr: The address to base the search on
288 * @offset: The bitnumber to start searching at
289 * @size: The maximum size to search
291 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
293 unsigned int * p = ((unsigned int *) addr) + (offset >> 5);
294 int set = 0, bit = offset & 31, res;
298 * Look for zero in first byte
300 __asm__("bsfl %1,%0\n\t"
305 : "r" (~(*p >> bit)));
306 if (set < (32 - bit))
312 * No zero yet, search remaining full bytes for a zero
314 res = find_first_zero_bit (p, size - 32 * (p - (unsigned int *) addr));
315 return (offset + set + res);
319 * Find string of zero bits in a bitmap. -1 when not found.
322 find_next_zero_string(unsigned long *bitmap, long start, long nbits, int len);
324 static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
327 unsigned long end = i + len;
329 __set_bit(i, bitmap);
334 static inline void clear_bit_string(unsigned long *bitmap, unsigned long i,
337 unsigned long end = i + len;
339 clear_bit(i, bitmap);
345 * ffz - find first zero in word.
346 * @word: The word to search
348 * Undefined if no zero exists, so code should check against ~0UL first.
350 static __inline__ unsigned long ffz(unsigned long word)
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"
375 "1:" : "=r" (r) : "g" (x));
380 * hweightN - returns the hamming weight of a N-bit word
381 * @x: the word to weigh
383 * The Hamming Weight of a number is the total number of bits set in it.
386 #define hweight32(x) generic_hweight32(x)
387 #define hweight16(x) generic_hweight16(x)
388 #define hweight8(x) generic_hweight8(x)
390 #endif /* __KERNEL__ */
394 #define ext2_set_bit __test_and_set_bit
395 #define ext2_clear_bit __test_and_clear_bit
396 #define ext2_test_bit test_bit
397 #define ext2_find_first_zero_bit find_first_zero_bit
398 #define ext2_find_next_zero_bit find_next_zero_bit
400 /* Bitmap functions for the minix filesystem. */
401 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,addr)
402 #define minix_set_bit(nr,addr) __set_bit(nr,addr)
403 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,addr)
404 #define minix_test_bit(nr,addr) test_bit(nr,addr)
405 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
407 #endif /* __KERNEL__ */
409 #endif /* _X86_64_BITOPS_H */