]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/asm-x86/bitops_64.h
1 #ifndef _X86_64_BITOPS_H
2 #define _X86_64_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #ifndef _LINUX_BITOPS_H
9 #error only <linux/bitops.h> can be included directly
12 #include <asm/alternative.h>
14 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
15 /* Technically wrong, but this avoids compilation errors on some gcc
17 #define ADDR "=m" (*(volatile long *) addr)
19 #define ADDR "+m" (*(volatile long *) addr)
23 * set_bit - Atomically set a bit in memory
25 * @addr: the address to start counting from
27 * This function is atomic and may not be reordered. See __set_bit()
28 * if you do not require the atomic guarantees.
29 * Note that @nr may be almost arbitrarily large; this function is not
30 * restricted to acting on a single-word quantity.
32 static __inline__
void set_bit(int nr
, volatile void * addr
)
34 __asm__
__volatile__( LOCK_PREFIX
37 :"dIr" (nr
) : "memory");
41 * __set_bit - Set a bit in memory
43 * @addr: the address to start counting from
45 * Unlike set_bit(), this function is non-atomic and may be reordered.
46 * If it's called on the same region of memory simultaneously, the effect
47 * may be that only one operation succeeds.
49 static __inline__
void __set_bit(int nr
, volatile void * addr
)
54 :"dIr" (nr
) : "memory");
58 * clear_bit - Clears a bit in memory
60 * @addr: Address to start counting from
62 * clear_bit() is atomic and may not be reordered. However, it does
63 * not contain a memory barrier, so if it is used for locking purposes,
64 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
65 * in order to ensure changes are visible on other processors.
67 static __inline__
void clear_bit(int nr
, volatile void * addr
)
69 __asm__
__volatile__( LOCK_PREFIX
75 static __inline__
void __clear_bit(int nr
, volatile void * addr
)
83 #define smp_mb__before_clear_bit() barrier()
84 #define smp_mb__after_clear_bit() barrier()
87 * __change_bit - Toggle a bit in memory
88 * @nr: the bit to change
89 * @addr: the address to start counting from
91 * Unlike change_bit(), this function is non-atomic and may be reordered.
92 * If it's called on the same region of memory simultaneously, the effect
93 * may be that only one operation succeeds.
95 static __inline__
void __change_bit(int nr
, volatile void * addr
)
104 * change_bit - Toggle a bit in memory
106 * @addr: Address to start counting from
108 * change_bit() is atomic and may not be reordered.
109 * Note that @nr may be almost arbitrarily large; this function is not
110 * restricted to acting on a single-word quantity.
112 static __inline__
void change_bit(int nr
, volatile void * addr
)
114 __asm__
__volatile__( LOCK_PREFIX
121 * test_and_set_bit - Set a bit and return its old value
123 * @addr: Address to count from
125 * This operation is atomic and cannot be reordered.
126 * It also implies a memory barrier.
128 static __inline__
int test_and_set_bit(int nr
, volatile void * addr
)
132 __asm__
__volatile__( LOCK_PREFIX
133 "btsl %2,%1\n\tsbbl %0,%0"
135 :"dIr" (nr
) : "memory");
140 * __test_and_set_bit - Set a bit and return its old value
142 * @addr: Address to count from
144 * This operation is non-atomic and can be reordered.
145 * If two examples of this operation race, one can appear to succeed
146 * but actually fail. You must protect multiple accesses with a lock.
148 static __inline__
int __test_and_set_bit(int nr
, volatile void * addr
)
153 "btsl %2,%1\n\tsbbl %0,%0"
160 * test_and_clear_bit - Clear a bit and return its old value
162 * @addr: Address to count from
164 * This operation is atomic and cannot be reordered.
165 * It also implies a memory barrier.
167 static __inline__
int test_and_clear_bit(int nr
, volatile void * addr
)
171 __asm__
__volatile__( LOCK_PREFIX
172 "btrl %2,%1\n\tsbbl %0,%0"
174 :"dIr" (nr
) : "memory");
179 * __test_and_clear_bit - Clear a bit and return its old value
181 * @addr: Address to count from
183 * This operation is non-atomic and can be reordered.
184 * If two examples of this operation race, one can appear to succeed
185 * but actually fail. You must protect multiple accesses with a lock.
187 static __inline__
int __test_and_clear_bit(int nr
, volatile void * addr
)
192 "btrl %2,%1\n\tsbbl %0,%0"
198 /* WARNING: non atomic and it can be reordered! */
199 static __inline__
int __test_and_change_bit(int nr
, volatile void * addr
)
203 __asm__
__volatile__(
204 "btcl %2,%1\n\tsbbl %0,%0"
206 :"dIr" (nr
) : "memory");
211 * test_and_change_bit - Change a bit and return its old value
213 * @addr: Address to count from
215 * This operation is atomic and cannot be reordered.
216 * It also implies a memory barrier.
218 static __inline__
int test_and_change_bit(int nr
, volatile void * addr
)
222 __asm__
__volatile__( LOCK_PREFIX
223 "btcl %2,%1\n\tsbbl %0,%0"
225 :"dIr" (nr
) : "memory");
229 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
231 * test_bit - Determine whether a bit is set
232 * @nr: bit number to test
233 * @addr: Address to start counting from
235 static int test_bit(int nr
, const volatile void * addr
);
238 static __inline__
int constant_test_bit(int nr
, const volatile void * addr
)
240 return ((1UL << (nr
& 31)) & (((const volatile unsigned int *) addr
)[nr
>> 5])) != 0;
243 static __inline__
int variable_test_bit(int nr
, volatile const void * addr
)
247 __asm__
__volatile__(
248 "btl %2,%1\n\tsbbl %0,%0"
250 :"m" (*(volatile long *)addr
),"dIr" (nr
));
254 #define test_bit(nr,addr) \
255 (__builtin_constant_p(nr) ? \
256 constant_test_bit((nr),(addr)) : \
257 variable_test_bit((nr),(addr)))
261 extern long find_first_zero_bit(const unsigned long * addr
, unsigned long size
);
262 extern long find_next_zero_bit (const unsigned long * addr
, long size
, long offset
);
263 extern long find_first_bit(const unsigned long * addr
, unsigned long size
);
264 extern long find_next_bit(const unsigned long * addr
, long size
, long offset
);
266 /* return index of first bet set in val or max when no bit is set */
267 static inline long __scanbit(unsigned long val
, unsigned long max
)
269 asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val
) : "r" (val
), "r" (max
));
273 #define find_first_bit(addr,size) \
274 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
275 (__scanbit(*(unsigned long *)addr,(size))) : \
276 find_first_bit(addr,size)))
278 #define find_next_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_bit(addr,size,off)))
283 #define find_first_zero_bit(addr,size) \
284 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
285 (__scanbit(~*(unsigned long *)addr,(size))) : \
286 find_first_zero_bit(addr,size)))
288 #define find_next_zero_bit(addr,size,off) \
289 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
290 ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
291 find_next_zero_bit(addr,size,off)))
294 * Find string of zero bits in a bitmap. -1 when not found.
297 find_next_zero_string(unsigned long *bitmap
, long start
, long nbits
, int len
);
299 static inline void set_bit_string(unsigned long *bitmap
, unsigned long i
,
302 unsigned long end
= i
+ len
;
304 __set_bit(i
, bitmap
);
309 static inline void __clear_bit_string(unsigned long *bitmap
, unsigned long i
,
312 unsigned long end
= i
+ len
;
314 __clear_bit(i
, bitmap
);
320 * ffz - find first zero in word.
321 * @word: The word to search
323 * Undefined if no zero exists, so code should check against ~0UL first.
325 static __inline__
unsigned long ffz(unsigned long word
)
334 * __ffs - find first bit in word.
335 * @word: The word to search
337 * Undefined if no bit exists, so code should check against 0 first.
339 static __inline__
unsigned long __ffs(unsigned long word
)
348 * __fls: find last bit set.
349 * @word: The word to search
351 * Undefined if no zero exists, so code should check against ~0UL first.
353 static __inline__
unsigned long __fls(unsigned long word
)
363 #include <asm-generic/bitops/sched.h>
366 * ffs - find first bit set
367 * @x: the word to search
369 * This is defined the same way as
370 * the libc and compiler builtin ffs routines, therefore
371 * differs in spirit from the above ffz (man ffs).
373 static __inline__
int ffs(int x
)
377 __asm__("bsfl %1,%0\n\t"
379 : "=r" (r
) : "rm" (x
), "r" (-1));
384 * fls64 - find last bit set in 64 bit word
385 * @x: the word to search
387 * This is defined the same way as fls.
389 static __inline__
int fls64(__u64 x
)
397 * fls - find last bit set
398 * @x: the word to search
400 * This is defined the same way as ffs.
402 static __inline__
int fls(int x
)
406 __asm__("bsrl %1,%0\n\t"
408 : "=&r" (r
) : "rm" (x
), "rm" (-1));
412 #define ARCH_HAS_FAST_MULTIPLIER 1
414 #include <asm-generic/bitops/hweight.h>
415 #include <asm-generic/bitops/lock.h>
417 #endif /* __KERNEL__ */
421 #include <asm-generic/bitops/ext2-non-atomic.h>
423 #define ext2_set_bit_atomic(lock,nr,addr) \
424 test_and_set_bit((nr),(unsigned long*)addr)
425 #define ext2_clear_bit_atomic(lock,nr,addr) \
426 test_and_clear_bit((nr),(unsigned long*)addr)
428 #include <asm-generic/bitops/minix.h>
430 #endif /* __KERNEL__ */
432 #endif /* _X86_64_BITOPS_H */