1 #ifndef _ASM_X86_BITOPS_H
2 #define _ASM_X86_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #ifndef _LINUX_BITOPS_H
9 #error only <linux/bitops.h> can be included directly
12 #include <linux/compiler.h>
13 #include <asm/alternative.h>
16 * These have to be done with inline assembly: that way the bit-setting
17 * is guaranteed to be atomic. All bit operations return 0 if the bit
18 * was cleared before the operation and != 0 if it was not.
20 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
23 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
24 /* Technically wrong, but this avoids compilation errors on some gcc
26 #define ADDR "=m" (*(volatile long *) addr)
28 #define ADDR "+m" (*(volatile long *) addr)
32 * set_bit - Atomically set a bit in memory
34 * @addr: the address to start counting from
36 * This function is atomic and may not be reordered. See __set_bit()
37 * if you do not require the atomic guarantees.
39 * Note: there are no guarantees that this function will not be reordered
40 * on non x86 architectures, so if you are writing portable code,
41 * make sure not to rely on its reordering guarantees.
43 * Note that @nr may be almost arbitrarily large; this function is not
44 * restricted to acting on a single-word quantity.
46 static inline void set_bit(int nr, volatile void *addr)
48 asm volatile(LOCK_PREFIX "bts %1,%0"
50 : "Ir" (nr) : "memory");
54 * __set_bit - Set a bit in memory
56 * @addr: the address to start counting from
58 * Unlike set_bit(), this function is non-atomic and may be reordered.
59 * If it's called on the same region of memory simultaneously, the effect
60 * may be that only one operation succeeds.
62 static inline void __set_bit(int nr, volatile void *addr)
64 asm volatile("bts %1,%0"
66 : "Ir" (nr) : "memory");
71 * clear_bit - Clears a bit in memory
73 * @addr: Address to start counting from
75 * clear_bit() is atomic and may not be reordered. However, it does
76 * not contain a memory barrier, so if it is used for locking purposes,
77 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
78 * in order to ensure changes are visible on other processors.
80 static inline void clear_bit(int nr, volatile void *addr)
82 asm volatile(LOCK_PREFIX "btr %1,%0"
88 * clear_bit_unlock - Clears a bit in memory
90 * @addr: Address to start counting from
92 * clear_bit() is atomic and implies release semantics before the memory
93 * operation. It can be used for an unlock.
95 static inline void clear_bit_unlock(unsigned nr, volatile void *addr)
101 static inline void __clear_bit(int nr, volatile void *addr)
103 asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
107 * __clear_bit_unlock - Clears a bit in memory
109 * @addr: Address to start counting from
111 * __clear_bit() is non-atomic and implies release semantics before the memory
112 * operation. It can be used for an unlock if no other CPUs can concurrently
113 * modify other bits in the word.
115 * No memory barrier is required here, because x86 cannot reorder stores past
116 * older loads. Same principle as spin_unlock.
118 static inline void __clear_bit_unlock(unsigned nr, volatile void *addr)
121 __clear_bit(nr, addr);
124 #define smp_mb__before_clear_bit() barrier()
125 #define smp_mb__after_clear_bit() barrier()
128 * __change_bit - Toggle a bit in memory
129 * @nr: the bit to change
130 * @addr: the address to start counting from
132 * Unlike change_bit(), this function is non-atomic and may be reordered.
133 * If it's called on the same region of memory simultaneously, the effect
134 * may be that only one operation succeeds.
136 static inline void __change_bit(int nr, volatile void *addr)
138 asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
142 * change_bit - Toggle a bit in memory
144 * @addr: Address to start counting from
146 * change_bit() is atomic and may not be reordered.
147 * Note that @nr may be almost arbitrarily large; this function is not
148 * restricted to acting on a single-word quantity.
150 static inline void change_bit(int nr, volatile void *addr)
152 asm volatile(LOCK_PREFIX "btc %1,%0"
157 * test_and_set_bit - Set a bit and return its old value
159 * @addr: Address to count from
161 * This operation is atomic and cannot be reordered.
162 * It also implies a memory barrier.
164 static inline int test_and_set_bit(int nr, volatile void *addr)
168 asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
170 : "=r" (oldbit), ADDR
171 : "Ir" (nr) : "memory");
177 * test_and_set_bit_lock - Set a bit and return its old value for lock
179 * @addr: Address to count from
181 * This is the same as test_and_set_bit on x86.
183 static inline int test_and_set_bit_lock(int nr, volatile void *addr)
185 return test_and_set_bit(nr, addr);
189 * __test_and_set_bit - Set a bit and return its old value
191 * @addr: Address to count from
193 * This operation is non-atomic and can be reordered.
194 * If two examples of this operation race, one can appear to succeed
195 * but actually fail. You must protect multiple accesses with a lock.
197 static inline int __test_and_set_bit(int nr, volatile void *addr)
203 : "=r" (oldbit), ADDR
209 * test_and_clear_bit - Clear a bit and return its old value
211 * @addr: Address to count from
213 * This operation is atomic and cannot be reordered.
214 * It also implies a memory barrier.
216 static inline int test_and_clear_bit(int nr, volatile void *addr)
220 asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
222 : "=r" (oldbit), ADDR
223 : "Ir" (nr) : "memory");
229 * __test_and_clear_bit - Clear 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_clear_bit(int nr, volatile void *addr)
241 asm volatile("btr %2,%1\n\t"
243 : "=r" (oldbit), ADDR
248 /* WARNING: non atomic and it can be reordered! */
249 static inline int __test_and_change_bit(int nr, volatile void *addr)
253 asm volatile("btc %2,%1\n\t"
255 : "=r" (oldbit), ADDR
256 : "Ir" (nr) : "memory");
262 * test_and_change_bit - Change a bit and return its old value
264 * @addr: Address to count from
266 * This operation is atomic and cannot be reordered.
267 * It also implies a memory barrier.
269 static inline int test_and_change_bit(int nr, volatile void *addr)
273 asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
275 : "=r" (oldbit), ADDR
276 : "Ir" (nr) : "memory");
281 static inline int constant_test_bit(int nr, const volatile void *addr)
283 return ((1UL << (nr % BITS_PER_LONG)) &
284 (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
287 static inline int variable_test_bit(int nr, volatile const void *addr)
291 asm volatile("bt %2,%1\n\t"
294 : "m" (*(unsigned long *)addr), "Ir" (nr));
299 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
301 * test_bit - Determine whether a bit is set
302 * @nr: bit number to test
303 * @addr: Address to start counting from
305 static int test_bit(int nr, const volatile unsigned long *addr);
308 #define test_bit(nr,addr) \
309 (__builtin_constant_p(nr) ? \
310 constant_test_bit((nr),(addr)) : \
311 variable_test_bit((nr),(addr)))
316 # include "bitops_32.h"
318 # include "bitops_64.h"
321 #endif /* _ASM_X86_BITOPS_H */