1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #include <asm/alternative.h>
10 #define ADDR (*(volatile long *) addr)
13 * set_bit - Atomically set a bit in memory
15 * @addr: the address to start counting from
17 * This function is atomic and may not be reordered. See __set_bit()
18 * if you do not require the atomic guarantees.
19 * Note that @nr may be almost arbitrarily large; this function is not
20 * restricted to acting on a single-word quantity.
22 static __inline__
void set_bit(int nr
, volatile void * addr
)
24 __asm__
__volatile__( LOCK_PREFIX
27 :"dIr" (nr
) : "memory");
31 * __set_bit - Set a bit in memory
33 * @addr: the address to start counting from
35 * Unlike set_bit(), this function is non-atomic and may be reordered.
36 * If it's called on the same region of memory simultaneously, the effect
37 * may be that only one operation succeeds.
39 static __inline__
void __set_bit(int nr
, volatile void * addr
)
44 :"dIr" (nr
) : "memory");
48 * clear_bit - Clears a bit in memory
50 * @addr: Address to start counting from
52 * clear_bit() is atomic and may not be reordered. However, it does
53 * not contain a memory barrier, so if it is used for locking purposes,
54 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
55 * in order to ensure changes are visible on other processors.
57 static __inline__
void clear_bit(int nr
, volatile void * addr
)
59 __asm__
__volatile__( LOCK_PREFIX
65 static __inline__
void __clear_bit(int nr
, volatile void * addr
)
73 #define smp_mb__before_clear_bit() barrier()
74 #define smp_mb__after_clear_bit() barrier()
77 * __change_bit - Toggle a bit in memory
78 * @nr: the bit to change
79 * @addr: the address to start counting from
81 * Unlike change_bit(), this function is non-atomic and may be reordered.
82 * If it's called on the same region of memory simultaneously, the effect
83 * may be that only one operation succeeds.
85 static __inline__
void __change_bit(int nr
, volatile void * addr
)
94 * change_bit - Toggle a bit in memory
96 * @addr: Address to start counting from
98 * change_bit() is atomic and may not be reordered.
99 * Note that @nr may be almost arbitrarily large; this function is not
100 * restricted to acting on a single-word quantity.
102 static __inline__
void change_bit(int nr
, volatile void * addr
)
104 __asm__
__volatile__( LOCK_PREFIX
111 * test_and_set_bit - Set a bit and return its old value
113 * @addr: Address to count from
115 * This operation is atomic and cannot be reordered.
116 * It also implies a memory barrier.
118 static __inline__
int test_and_set_bit(int nr
, volatile void * addr
)
122 __asm__
__volatile__( LOCK_PREFIX
123 "btsl %2,%1\n\tsbbl %0,%0"
124 :"=r" (oldbit
),"+m" (ADDR
)
125 :"dIr" (nr
) : "memory");
130 * __test_and_set_bit - Set a bit and return its old value
132 * @addr: Address to count from
134 * This operation is non-atomic and can be reordered.
135 * If two examples of this operation race, one can appear to succeed
136 * but actually fail. You must protect multiple accesses with a lock.
138 static __inline__
int __test_and_set_bit(int nr
, volatile void * addr
)
143 "btsl %2,%1\n\tsbbl %0,%0"
144 :"=r" (oldbit
),"+m" (ADDR
)
150 * test_and_clear_bit - Clear a bit and return its old value
152 * @addr: Address to count from
154 * This operation is atomic and cannot be reordered.
155 * It also implies a memory barrier.
157 static __inline__
int test_and_clear_bit(int nr
, volatile void * addr
)
161 __asm__
__volatile__( LOCK_PREFIX
162 "btrl %2,%1\n\tsbbl %0,%0"
163 :"=r" (oldbit
),"+m" (ADDR
)
164 :"dIr" (nr
) : "memory");
169 * __test_and_clear_bit - Clear a bit and return its old value
171 * @addr: Address to count from
173 * This operation is non-atomic and can be reordered.
174 * If two examples of this operation race, one can appear to succeed
175 * but actually fail. You must protect multiple accesses with a lock.
177 static __inline__
int __test_and_clear_bit(int nr
, volatile void * addr
)
182 "btrl %2,%1\n\tsbbl %0,%0"
183 :"=r" (oldbit
),"+m" (ADDR
)
188 /* WARNING: non atomic and it can be reordered! */
189 static __inline__
int __test_and_change_bit(int nr
, volatile void * addr
)
193 __asm__
__volatile__(
194 "btcl %2,%1\n\tsbbl %0,%0"
195 :"=r" (oldbit
),"+m" (ADDR
)
196 :"dIr" (nr
) : "memory");
201 * test_and_change_bit - Change a bit and return its old value
203 * @addr: Address to count from
205 * This operation is atomic and cannot be reordered.
206 * It also implies a memory barrier.
208 static __inline__
int test_and_change_bit(int nr
, volatile void * addr
)
212 __asm__
__volatile__( LOCK_PREFIX
213 "btcl %2,%1\n\tsbbl %0,%0"
214 :"=r" (oldbit
),"+m" (ADDR
)
215 :"dIr" (nr
) : "memory");
219 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
221 * test_bit - Determine whether a bit is set
222 * @nr: bit number to test
223 * @addr: Address to start counting from
225 static int test_bit(int nr
, const volatile void * addr
);
228 static __inline__
int constant_test_bit(int nr
, const volatile void * addr
)
230 return ((1UL << (nr
& 31)) & (((const volatile unsigned int *) addr
)[nr
>> 5])) != 0;
233 static __inline__
int variable_test_bit(int nr
, volatile const void * addr
)
237 __asm__
__volatile__(
238 "btl %2,%1\n\tsbbl %0,%0"
240 :"m" (ADDR
),"dIr" (nr
));
244 #define test_bit(nr,addr) \
245 (__builtin_constant_p(nr) ? \
246 constant_test_bit((nr),(addr)) : \
247 variable_test_bit((nr),(addr)))
251 extern long find_first_zero_bit(const unsigned long * addr
, unsigned long size
);
252 extern long find_next_zero_bit (const unsigned long * addr
, long size
, long offset
);
253 extern long find_first_bit(const unsigned long * addr
, unsigned long size
);
254 extern long find_next_bit(const unsigned long * addr
, long size
, long offset
);
256 /* return index of first bet set in val or max when no bit is set */
257 static inline unsigned long __scanbit(unsigned long val
, unsigned long max
)
259 asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val
) : "r" (val
), "r" (max
));
263 #define find_first_bit(addr,size) \
264 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
265 (__scanbit(*(unsigned long *)addr,(size))) : \
266 find_first_bit(addr,size)))
268 #define find_next_bit(addr,size,off) \
269 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
270 ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
271 find_next_bit(addr,size,off)))
273 #define find_first_zero_bit(addr,size) \
274 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
275 (__scanbit(~*(unsigned long *)addr,(size))) : \
276 find_first_zero_bit(addr,size)))
278 #define find_next_zero_bit(addr,size,off) \
279 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
280 ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
281 find_next_zero_bit(addr,size,off)))
284 * Find string of zero bits in a bitmap. -1 when not found.
287 find_next_zero_string(unsigned long *bitmap
, long start
, long nbits
, int len
);
289 static inline void set_bit_string(unsigned long *bitmap
, unsigned long i
,
292 unsigned long end
= i
+ len
;
294 __set_bit(i
, bitmap
);
299 static inline void __clear_bit_string(unsigned long *bitmap
, unsigned long i
,
302 unsigned long end
= i
+ len
;
304 __clear_bit(i
, bitmap
);
310 * ffz - find first zero in word.
311 * @word: The word to search
313 * Undefined if no zero exists, so code should check against ~0UL first.
315 static __inline__
unsigned long ffz(unsigned long word
)
324 * __ffs - find first bit in word.
325 * @word: The word to search
327 * Undefined if no bit exists, so code should check against 0 first.
329 static __inline__
unsigned long __ffs(unsigned long word
)
338 * __fls: find last bit set.
339 * @word: The word to search
341 * Undefined if no zero exists, so code should check against ~0UL first.
343 static __inline__
unsigned long __fls(unsigned long word
)
353 #include <asm-generic/bitops/sched.h>
356 * ffs - find first bit set
357 * @x: the word to search
359 * This is defined the same way as
360 * the libc and compiler builtin ffs routines, therefore
361 * differs in spirit from the above ffz (man ffs).
363 static __inline__
int ffs(int x
)
367 __asm__("bsfl %1,%0\n\t"
369 : "=r" (r
) : "rm" (x
), "r" (-1));
374 * fls64 - find last bit set in 64 bit word
375 * @x: the word to search
377 * This is defined the same way as fls.
379 static __inline__
int fls64(__u64 x
)
387 * fls - find last bit set
388 * @x: the word to search
390 * This is defined the same way as ffs.
392 static __inline__
int fls(int x
)
396 __asm__("bsrl %1,%0\n\t"
398 : "=&r" (r
) : "rm" (x
), "rm" (-1));
402 #define ARCH_HAS_FAST_MULTIPLIER 1
404 #include <asm-generic/bitops/hweight.h>
406 #endif /* __KERNEL__ */
410 #include <asm-generic/bitops/ext2-non-atomic.h>
412 #define ext2_set_bit_atomic(lock,nr,addr) \
413 test_and_set_bit((nr),(unsigned long*)addr)
414 #define ext2_clear_bit_atomic(lock,nr,addr) \
415 test_and_clear_bit((nr),(unsigned long*)addr)
417 #include <asm-generic/bitops/minix.h>
419 #endif /* __KERNEL__ */
421 #endif /* _X86_64_BITOPS_H */