1 #ifndef _ASM_X86_BITOPS_H
2 #define _ASM_X86_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
7 * Note: inlines with more than a single statement should be marked
8 * __always_inline to avoid problems with older gcc's inlining heuristics.
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
15 #include <linux/compiler.h>
16 #include <asm/alternative.h>
18 #if BITS_PER_LONG == 32
19 # define _BITOPS_LONG_SHIFT 5
20 #elif BITS_PER_LONG == 64
21 # define _BITOPS_LONG_SHIFT 6
23 # error "Unexpected BITS_PER_LONG"
26 #define BIT_64(n) (U64_C(1) << (n))
29 * These have to be done with inline assembly: that way the bit-setting
30 * is guaranteed to be atomic. All bit operations return 0 if the bit
31 * was cleared before the operation and != 0 if it was not.
33 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
36 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
37 /* Technically wrong, but this avoids compilation errors on some gcc
39 #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
41 #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
44 #define ADDR BITOP_ADDR(addr)
47 * We do the locked ops that don't return the old value as
48 * a mask operation on a byte.
50 #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
51 #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
52 #define CONST_MASK(nr) (1 << ((nr) & 7))
55 * set_bit - Atomically set a bit in memory
57 * @addr: the address to start counting from
59 * This function is atomic and may not be reordered. See __set_bit()
60 * if you do not require the atomic guarantees.
62 * Note: there are no guarantees that this function will not be reordered
63 * on non x86 architectures, so if you are writing portable code,
64 * make sure not to rely on its reordering guarantees.
66 * Note that @nr may be almost arbitrarily large; this function is not
67 * restricted to acting on a single-word quantity.
69 static __always_inline
void
70 set_bit(long nr
, volatile unsigned long *addr
)
72 if (IS_IMMEDIATE(nr
)) {
73 asm volatile(LOCK_PREFIX
"orb %1,%0"
74 : CONST_MASK_ADDR(nr
, addr
)
75 : "iq" ((u8
)CONST_MASK(nr
))
78 asm volatile(LOCK_PREFIX
"bts %1,%0"
79 : BITOP_ADDR(addr
) : "Ir" (nr
) : "memory");
84 * __set_bit - Set a bit in memory
86 * @addr: the address to start counting from
88 * Unlike set_bit(), this function is non-atomic and may be reordered.
89 * If it's called on the same region of memory simultaneously, the effect
90 * may be that only one operation succeeds.
92 static inline void __set_bit(long nr
, volatile unsigned long *addr
)
94 asm volatile("bts %1,%0" : ADDR
: "Ir" (nr
) : "memory");
98 * clear_bit - Clears a bit in memory
100 * @addr: Address to start counting from
102 * clear_bit() is atomic and may not be reordered. However, it does
103 * not contain a memory barrier, so if it is used for locking purposes,
104 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
105 * in order to ensure changes are visible on other processors.
107 static __always_inline
void
108 clear_bit(long nr
, volatile unsigned long *addr
)
110 if (IS_IMMEDIATE(nr
)) {
111 asm volatile(LOCK_PREFIX
"andb %1,%0"
112 : CONST_MASK_ADDR(nr
, addr
)
113 : "iq" ((u8
)~CONST_MASK(nr
)));
115 asm volatile(LOCK_PREFIX
"btr %1,%0"
122 * clear_bit_unlock - Clears a bit in memory
124 * @addr: Address to start counting from
126 * clear_bit() is atomic and implies release semantics before the memory
127 * operation. It can be used for an unlock.
129 static inline void clear_bit_unlock(long nr
, volatile unsigned long *addr
)
135 static inline void __clear_bit(long nr
, volatile unsigned long *addr
)
137 asm volatile("btr %1,%0" : ADDR
: "Ir" (nr
));
141 * __clear_bit_unlock - Clears a bit in memory
143 * @addr: Address to start counting from
145 * __clear_bit() is non-atomic and implies release semantics before the memory
146 * operation. It can be used for an unlock if no other CPUs can concurrently
147 * modify other bits in the word.
149 * No memory barrier is required here, because x86 cannot reorder stores past
150 * older loads. Same principle as spin_unlock.
152 static inline void __clear_bit_unlock(long nr
, volatile unsigned long *addr
)
155 __clear_bit(nr
, addr
);
158 #define smp_mb__before_clear_bit() barrier()
159 #define smp_mb__after_clear_bit() barrier()
162 * __change_bit - Toggle a bit in memory
163 * @nr: the bit to change
164 * @addr: the address to start counting from
166 * Unlike change_bit(), this function is non-atomic and may be reordered.
167 * If it's called on the same region of memory simultaneously, the effect
168 * may be that only one operation succeeds.
170 static inline void __change_bit(long nr
, volatile unsigned long *addr
)
172 asm volatile("btc %1,%0" : ADDR
: "Ir" (nr
));
176 * change_bit - Toggle a bit in memory
178 * @addr: Address to start counting from
180 * change_bit() is atomic and may not be reordered.
181 * Note that @nr may be almost arbitrarily large; this function is not
182 * restricted to acting on a single-word quantity.
184 static inline void change_bit(long nr
, volatile unsigned long *addr
)
186 if (IS_IMMEDIATE(nr
)) {
187 asm volatile(LOCK_PREFIX
"xorb %1,%0"
188 : CONST_MASK_ADDR(nr
, addr
)
189 : "iq" ((u8
)CONST_MASK(nr
)));
191 asm volatile(LOCK_PREFIX
"btc %1,%0"
198 * test_and_set_bit - Set a bit and return its old value
200 * @addr: Address to count from
202 * This operation is atomic and cannot be reordered.
203 * It also implies a memory barrier.
205 static inline int test_and_set_bit(long nr
, volatile unsigned long *addr
)
209 asm volatile(LOCK_PREFIX
"bts %2,%1\n\t"
210 "sbb %0,%0" : "=r" (oldbit
), ADDR
: "Ir" (nr
) : "memory");
216 * test_and_set_bit_lock - Set a bit and return its old value for lock
218 * @addr: Address to count from
220 * This is the same as test_and_set_bit on x86.
222 static __always_inline
int
223 test_and_set_bit_lock(long nr
, volatile unsigned long *addr
)
225 return test_and_set_bit(nr
, addr
);
229 * __test_and_set_bit - Set a bit and return its old value
231 * @addr: Address to count from
233 * This operation is non-atomic and can be reordered.
234 * If two examples of this operation race, one can appear to succeed
235 * but actually fail. You must protect multiple accesses with a lock.
237 static inline int __test_and_set_bit(long nr
, volatile unsigned long *addr
)
243 : "=r" (oldbit
), ADDR
249 * test_and_clear_bit - Clear a bit and return its old value
251 * @addr: Address to count from
253 * This operation is atomic and cannot be reordered.
254 * It also implies a memory barrier.
256 static inline int test_and_clear_bit(long nr
, volatile unsigned long *addr
)
260 asm volatile(LOCK_PREFIX
"btr %2,%1\n\t"
262 : "=r" (oldbit
), ADDR
: "Ir" (nr
) : "memory");
268 * __test_and_clear_bit - Clear a bit and return its old value
270 * @addr: Address to count from
272 * This operation is non-atomic and can be reordered.
273 * If two examples of this operation race, one can appear to succeed
274 * but actually fail. You must protect multiple accesses with a lock.
276 * Note: the operation is performed atomically with respect to
277 * the local CPU, but not other CPUs. Portable code should not
278 * rely on this behaviour.
279 * KVM relies on this behaviour on x86 for modifying memory that is also
280 * accessed from a hypervisor on the same CPU if running in a VM: don't change
281 * this without also updating arch/x86/kernel/kvm.c
283 static inline int __test_and_clear_bit(long nr
, volatile unsigned long *addr
)
287 asm volatile("btr %2,%1\n\t"
289 : "=r" (oldbit
), ADDR
294 /* WARNING: non atomic and it can be reordered! */
295 static inline int __test_and_change_bit(long nr
, volatile unsigned long *addr
)
299 asm volatile("btc %2,%1\n\t"
301 : "=r" (oldbit
), ADDR
302 : "Ir" (nr
) : "memory");
308 * test_and_change_bit - Change a bit and return its old value
310 * @addr: Address to count from
312 * This operation is atomic and cannot be reordered.
313 * It also implies a memory barrier.
315 static inline int test_and_change_bit(long nr
, volatile unsigned long *addr
)
319 asm volatile(LOCK_PREFIX
"btc %2,%1\n\t"
321 : "=r" (oldbit
), ADDR
: "Ir" (nr
) : "memory");
326 static __always_inline
int constant_test_bit(long nr
, const volatile unsigned long *addr
)
328 return ((1UL << (nr
& (BITS_PER_LONG
-1))) &
329 (addr
[nr
>> _BITOPS_LONG_SHIFT
])) != 0;
332 static inline int variable_test_bit(long nr
, volatile const unsigned long *addr
)
336 asm volatile("bt %2,%1\n\t"
339 : "m" (*(unsigned long *)addr
), "Ir" (nr
));
344 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
346 * test_bit - Determine whether a bit is set
347 * @nr: bit number to test
348 * @addr: Address to start counting from
350 static int test_bit(int nr
, const volatile unsigned long *addr
);
353 #define test_bit(nr, addr) \
354 (__builtin_constant_p((nr)) \
355 ? constant_test_bit((nr), (addr)) \
356 : variable_test_bit((nr), (addr)))
359 * __ffs - find first set bit in word
360 * @word: The word to search
362 * Undefined if no bit exists, so code should check against 0 first.
364 static inline unsigned long __ffs(unsigned long word
)
373 * ffz - find first zero bit in word
374 * @word: The word to search
376 * Undefined if no zero exists, so code should check against ~0UL first.
378 static inline unsigned long ffz(unsigned long word
)
387 * __fls: find last set bit in word
388 * @word: The word to search
390 * Undefined if no set bit exists, so code should check against 0 first.
392 static inline unsigned long __fls(unsigned long word
)
404 * ffs - find first set bit in word
405 * @x: the word to search
407 * This is defined the same way as the libc and compiler builtin ffs
408 * routines, therefore differs in spirit from the other bitops.
410 * ffs(value) returns 0 if value is 0 or the position of the first
411 * set bit if value is nonzero. The first (least significant) bit
414 static inline int ffs(int x
)
420 * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the
421 * dest reg is undefined if x==0, but their CPU architect says its
422 * value is written to set it to the same as before, except that the
423 * top 32 bits will be cleared.
425 * We cannot do this on 32 bits because at the very least some
426 * 486 CPUs did not behave this way.
430 : "rm" (x
), "0" (-1));
431 #elif defined(CONFIG_X86_CMOV)
434 : "=&r" (r
) : "rm" (x
), "r" (-1));
439 "1:" : "=r" (r
) : "rm" (x
));
445 * fls - find last set bit in word
446 * @x: the word to search
448 * This is defined in a similar way as the libc and compiler builtin
449 * ffs, but returns the position of the most significant set bit.
451 * fls(value) returns 0 if value is 0 or the position of the last
452 * set bit if value is nonzero. The last (most significant) bit is
455 static inline int fls(int x
)
461 * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the
462 * dest reg is undefined if x==0, but their CPU architect says its
463 * value is written to set it to the same as before, except that the
464 * top 32 bits will be cleared.
466 * We cannot do this on 32 bits because at the very least some
467 * 486 CPUs did not behave this way.
471 : "rm" (x
), "0" (-1));
472 #elif defined(CONFIG_X86_CMOV)
475 : "=&r" (r
) : "rm" (x
), "rm" (-1));
480 "1:" : "=r" (r
) : "rm" (x
));
486 * fls64 - find last set bit in a 64-bit word
487 * @x: the word to search
489 * This is defined in a similar way as the libc and compiler builtin
490 * ffsll, but returns the position of the most significant set bit.
492 * fls64(value) returns 0 if value is 0 or the position of the last
493 * set bit if value is nonzero. The last (most significant) bit is
497 static __always_inline
int fls64(__u64 x
)
501 * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the
502 * dest reg is undefined if x==0, but their CPU architect says its
503 * value is written to set it to the same as before.
511 #include <asm-generic/bitops/fls64.h>
514 #include <asm-generic/bitops/find.h>
516 #include <asm-generic/bitops/sched.h>
518 #define ARCH_HAS_FAST_MULTIPLIER 1
520 #include <asm/arch_hweight.h>
522 #include <asm-generic/bitops/const_hweight.h>
524 #include <asm-generic/bitops/le.h>
526 #include <asm-generic/bitops/ext2-atomic-setbit.h>
528 #endif /* __KERNEL__ */
529 #endif /* _ASM_X86_BITOPS_H */