1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #include <linux/config.h>
11 #define LOCK_PREFIX "lock ; "
13 #define LOCK_PREFIX ""
16 #define ADDR (*(volatile long *) addr)
19 * set_bit - Atomically set a bit in memory
21 * @addr: the address to start counting from
23 * This function is atomic and may not be reordered. See __set_bit()
24 * if you do not require the atomic guarantees.
25 * Note that @nr may be almost arbitrarily large; this function is not
26 * restricted to acting on a single-word quantity.
28 static __inline__ void set_bit(long nr, volatile void * addr)
30 __asm__ __volatile__( LOCK_PREFIX
33 :"dIr" (nr) : "memory");
37 * __set_bit - Set a bit in memory
39 * @addr: the address to start counting from
41 * Unlike set_bit(), this function is non-atomic and may be reordered.
42 * If it's called on the same region of memory simultaneously, the effect
43 * may be that only one operation succeeds.
45 static __inline__ void __set_bit(int nr, volatile void * addr)
50 :"dIr" (nr) : "memory");
54 * clear_bit - Clears a bit in memory
56 * @addr: Address to start counting from
58 * clear_bit() is atomic and may not be reordered. However, it does
59 * not contain a memory barrier, so if it is used for locking purposes,
60 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
61 * in order to ensure changes are visible on other processors.
63 static __inline__ void clear_bit(int nr, volatile void * addr)
65 __asm__ __volatile__( LOCK_PREFIX
71 static __inline__ void __clear_bit(int nr, volatile void * addr)
79 #define smp_mb__before_clear_bit() barrier()
80 #define smp_mb__after_clear_bit() barrier()
83 * __change_bit - Toggle a bit in memory
84 * @nr: the bit to change
85 * @addr: the address to start counting from
87 * Unlike change_bit(), this function is non-atomic and may be reordered.
88 * If it's called on the same region of memory simultaneously, the effect
89 * may be that only one operation succeeds.
91 static __inline__ void __change_bit(int nr, volatile void * addr)
100 * change_bit - Toggle a bit in memory
102 * @addr: Address to start counting from
104 * change_bit() is atomic and may not be reordered.
105 * Note that @nr may be almost arbitrarily large; this function is not
106 * restricted to acting on a single-word quantity.
108 static __inline__ void change_bit(int nr, volatile void * addr)
110 __asm__ __volatile__( LOCK_PREFIX
117 * test_and_set_bit - Set a bit and return its old value
119 * @addr: Address to count from
121 * This operation is atomic and cannot be reordered.
122 * It also implies a memory barrier.
124 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
128 __asm__ __volatile__( LOCK_PREFIX
129 "btsl %2,%1\n\tsbbl %0,%0"
130 :"=r" (oldbit),"=m" (ADDR)
131 :"dIr" (nr) : "memory");
136 * __test_and_set_bit - Set a bit and return its old value
138 * @addr: Address to count from
140 * This operation is non-atomic and can be reordered.
141 * If two examples of this operation race, one can appear to succeed
142 * but actually fail. You must protect multiple accesses with a lock.
144 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
149 "btsl %2,%1\n\tsbbl %0,%0"
150 :"=r" (oldbit),"=m" (ADDR)
156 * test_and_clear_bit - Clear a bit and return its old value
158 * @addr: Address to count from
160 * This operation is atomic and cannot be reordered.
161 * It also implies a memory barrier.
163 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
167 __asm__ __volatile__( LOCK_PREFIX
168 "btrl %2,%1\n\tsbbl %0,%0"
169 :"=r" (oldbit),"=m" (ADDR)
170 :"dIr" (nr) : "memory");
175 * __test_and_clear_bit - Clear a bit and return its old value
177 * @addr: Address to count from
179 * This operation is non-atomic and can be reordered.
180 * If two examples of this operation race, one can appear to succeed
181 * but actually fail. You must protect multiple accesses with a lock.
183 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
188 "btrl %2,%1\n\tsbbl %0,%0"
189 :"=r" (oldbit),"=m" (ADDR)
194 /* WARNING: non atomic and it can be reordered! */
195 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
199 __asm__ __volatile__(
200 "btcl %2,%1\n\tsbbl %0,%0"
201 :"=r" (oldbit),"=m" (ADDR)
202 :"dIr" (nr) : "memory");
207 * test_and_change_bit - Change a bit and return its old value
209 * @addr: Address to count from
211 * This operation is atomic and cannot be reordered.
212 * It also implies a memory barrier.
214 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
218 __asm__ __volatile__( LOCK_PREFIX
219 "btcl %2,%1\n\tsbbl %0,%0"
220 :"=r" (oldbit),"=m" (ADDR)
221 :"dIr" (nr) : "memory");
225 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
227 * test_bit - Determine whether a bit is set
228 * @nr: bit number to test
229 * @addr: Address to start counting from
231 static int test_bit(int nr, const volatile void * addr);
234 static __inline__ int constant_test_bit(int nr, const volatile void * addr)
236 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
239 static __inline__ int variable_test_bit(int nr, volatile const void * addr)
243 __asm__ __volatile__(
244 "btl %2,%1\n\tsbbl %0,%0"
246 :"m" (ADDR),"dIr" (nr));
250 #define test_bit(nr,addr) \
251 (__builtin_constant_p(nr) ? \
252 constant_test_bit((nr),(addr)) : \
253 variable_test_bit((nr),(addr)))
258 * find_first_zero_bit - find the first zero bit in a memory region
259 * @addr: The address to start the search at
260 * @size: The maximum size to search
262 * Returns the bit-number of the first zero bit, not the number of the byte
265 static __inline__ int find_first_zero_bit(const unsigned long * addr, unsigned size)
272 __asm__ __volatile__(
274 "xorl %%edx,%%edx\n\t"
277 "xorl -4(%%rdi),%%eax\n\t"
280 "1:\tsubq %%rbx,%%rdi\n\t"
283 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
284 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
289 * find_next_zero_bit - find the first zero bit in a memory region
290 * @addr: The address to base the search on
291 * @offset: The bitnumber to start searching at
292 * @size: The maximum size to search
294 static __inline__ int find_next_zero_bit (const unsigned long * addr, int size, int offset)
296 unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
297 unsigned long set = 0;
298 unsigned long res, bit = offset&63;
302 * Look for zero in first word
304 __asm__("bsfq %1,%0\n\t"
307 : "r" (~(*p >> bit)), "r"(64L));
308 if (set < (64 - bit))
314 * No zero yet, search remaining full words for a zero
316 res = find_first_zero_bit ((const unsigned long *)p, size - 64 * (p - (unsigned long *) addr));
317 return (offset + set + res);
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__ int find_first_bit(const unsigned long * addr, unsigned size)
334 /* This looks at memory. Mark it volatile to tell gcc not to move it around */
335 __asm__ __volatile__(
336 "xorl %%eax,%%eax\n\t"
339 "leaq -4(%%rdi),%%rdi\n\t"
340 "bsfq (%%rdi),%%rax\n"
341 "1:\tsubl %%ebx,%%edi\n\t"
344 :"=a" (res), "=&c" (d0), "=&D" (d1)
345 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
351 * find_next_bit - find the first set bit in a memory region
352 * @addr: The address to base the search on
353 * @offset: The bitnumber to start searching at
354 * @size: The maximum size to search
356 static __inline__ int find_next_bit(const unsigned long * addr, int size, int offset)
358 const unsigned long * p = addr + (offset >> 6);
359 unsigned long set = 0, bit = offset & 63, res;
363 * Look for nonzero in the first 64 bits:
365 __asm__("bsfq %1,%0\n\t"
368 : "r" (*p >> bit), "r" (64L));
369 if (set < (64 - bit))
375 * No set bit yet, search remaining full words for a bit
377 res = find_first_bit (p, size - 64 * (p - addr));
378 return (offset + set + res);
382 * Find string of zero bits in a bitmap. -1 when not found.
385 find_next_zero_string(unsigned long *bitmap, long start, long nbits, int len);
387 static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
390 unsigned long end = i + len;
392 __set_bit(i, bitmap);
397 static inline void __clear_bit_string(unsigned long *bitmap, unsigned long i,
400 unsigned long end = i + len;
402 __clear_bit(i, bitmap);
408 * ffz - find first zero in word.
409 * @word: The word to search
411 * Undefined if no zero exists, so code should check against ~0UL first.
413 static __inline__ unsigned long ffz(unsigned long word)
422 * __ffs - find first bit in word.
423 * @word: The word to search
425 * Undefined if no bit exists, so code should check against 0 first.
427 static __inline__ unsigned long __ffs(unsigned long word)
437 static inline int sched_find_first_bit(const unsigned long *b)
442 return __ffs(b[1]) + 64;
444 return __ffs(b[2]) + 128;
448 * ffs - find first bit set
449 * @x: the word to search
451 * This is defined the same way as
452 * the libc and compiler builtin ffs routines, therefore
453 * differs in spirit from the above ffz (man ffs).
455 static __inline__ int ffs(int x)
459 __asm__("bsfl %1,%0\n\t"
461 : "=r" (r) : "g" (x), "r" (-1));
466 * hweightN - returns the hamming weight of a N-bit word
467 * @x: the word to weigh
469 * The Hamming Weight of a number is the total number of bits set in it.
472 #define hweight64(x) generic_hweight64(x)
473 #define hweight32(x) generic_hweight32(x)
474 #define hweight16(x) generic_hweight16(x)
475 #define hweight8(x) generic_hweight8(x)
477 #endif /* __KERNEL__ */
481 #define ext2_set_bit(nr,addr) \
482 __test_and_set_bit((nr),(unsigned long*)addr)
483 #define ext2_set_bit_atomic(lock,nr,addr) \
484 test_and_set_bit((nr),(unsigned long*)addr)
485 #define ext2_clear_bit(nr, addr) \
486 __test_and_clear_bit((nr),(unsigned long*)addr)
487 #define ext2_clear_bit_atomic(lock,nr,addr) \
488 test_and_clear_bit((nr),(unsigned long*)addr)
489 #define ext2_test_bit(nr, addr) test_bit((nr),(unsigned long*)addr)
490 #define ext2_find_first_zero_bit(addr, size) \
491 find_first_zero_bit((unsigned long*)addr, size)
492 #define ext2_find_next_zero_bit(addr, size, off) \
493 find_next_zero_bit((unsigned long*)addr, size, off)
495 /* Bitmap functions for the minix filesystem. */
496 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,(void*)addr)
497 #define minix_set_bit(nr,addr) __set_bit(nr,(void*)addr)
498 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,(void*)addr)
499 #define minix_test_bit(nr,addr) test_bit(nr,(void*)addr)
500 #define minix_find_first_zero_bit(addr,size) \
501 find_first_zero_bit((void*)addr,size)
503 /* find last set bit */
504 #define fls(x) generic_fls(x)
506 #define ARCH_HAS_ATOMIC_UNSIGNED 1
508 #endif /* __KERNEL__ */
510 #endif /* _X86_64_BITOPS_H */