x86/bitops: Move BIT_64() for a wider use
[linux/fpc-iii.git] / arch / x86 / include / asm / bitops.h
bloba6983b2772201c6109060ea25d983499f0281531
1 #ifndef _ASM_X86_BITOPS_H
2 #define _ASM_X86_BITOPS_H
4 /*
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.
9 */
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
13 #endif
15 #include <linux/compiler.h>
16 #include <asm/alternative.h>
18 #define BIT_64(n) (U64_C(1) << (n))
21 * These have to be done with inline assembly: that way the bit-setting
22 * is guaranteed to be atomic. All bit operations return 0 if the bit
23 * was cleared before the operation and != 0 if it was not.
25 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
28 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
29 /* Technically wrong, but this avoids compilation errors on some gcc
30 versions. */
31 #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
32 #else
33 #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
34 #endif
36 #define ADDR BITOP_ADDR(addr)
39 * We do the locked ops that don't return the old value as
40 * a mask operation on a byte.
42 #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
43 #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
44 #define CONST_MASK(nr) (1 << ((nr) & 7))
46 /**
47 * set_bit - Atomically set a bit in memory
48 * @nr: the bit to set
49 * @addr: the address to start counting from
51 * This function is atomic and may not be reordered. See __set_bit()
52 * if you do not require the atomic guarantees.
54 * Note: there are no guarantees that this function will not be reordered
55 * on non x86 architectures, so if you are writing portable code,
56 * make sure not to rely on its reordering guarantees.
58 * Note that @nr may be almost arbitrarily large; this function is not
59 * restricted to acting on a single-word quantity.
61 static __always_inline void
62 set_bit(unsigned int nr, volatile unsigned long *addr)
64 if (IS_IMMEDIATE(nr)) {
65 asm volatile(LOCK_PREFIX "orb %1,%0"
66 : CONST_MASK_ADDR(nr, addr)
67 : "iq" ((u8)CONST_MASK(nr))
68 : "memory");
69 } else {
70 asm volatile(LOCK_PREFIX "bts %1,%0"
71 : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
75 /**
76 * __set_bit - Set a bit in memory
77 * @nr: the bit to set
78 * @addr: the address to start counting from
80 * Unlike set_bit(), this function is non-atomic and may be reordered.
81 * If it's called on the same region of memory simultaneously, the effect
82 * may be that only one operation succeeds.
84 static inline void __set_bit(int nr, volatile unsigned long *addr)
86 asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
89 /**
90 * clear_bit - Clears a bit in memory
91 * @nr: Bit to clear
92 * @addr: Address to start counting from
94 * clear_bit() is atomic and may not be reordered. However, it does
95 * not contain a memory barrier, so if it is used for locking purposes,
96 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
97 * in order to ensure changes are visible on other processors.
99 static __always_inline void
100 clear_bit(int nr, volatile unsigned long *addr)
102 if (IS_IMMEDIATE(nr)) {
103 asm volatile(LOCK_PREFIX "andb %1,%0"
104 : CONST_MASK_ADDR(nr, addr)
105 : "iq" ((u8)~CONST_MASK(nr)));
106 } else {
107 asm volatile(LOCK_PREFIX "btr %1,%0"
108 : BITOP_ADDR(addr)
109 : "Ir" (nr));
114 * clear_bit_unlock - Clears a bit in memory
115 * @nr: Bit to clear
116 * @addr: Address to start counting from
118 * clear_bit() is atomic and implies release semantics before the memory
119 * operation. It can be used for an unlock.
121 static inline void clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
123 barrier();
124 clear_bit(nr, addr);
127 static inline void __clear_bit(int nr, volatile unsigned long *addr)
129 asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
133 * __clear_bit_unlock - Clears a bit in memory
134 * @nr: Bit to clear
135 * @addr: Address to start counting from
137 * __clear_bit() is non-atomic and implies release semantics before the memory
138 * operation. It can be used for an unlock if no other CPUs can concurrently
139 * modify other bits in the word.
141 * No memory barrier is required here, because x86 cannot reorder stores past
142 * older loads. Same principle as spin_unlock.
144 static inline void __clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
146 barrier();
147 __clear_bit(nr, addr);
150 #define smp_mb__before_clear_bit() barrier()
151 #define smp_mb__after_clear_bit() barrier()
154 * __change_bit - Toggle a bit in memory
155 * @nr: the bit to change
156 * @addr: the address to start counting from
158 * Unlike change_bit(), this function is non-atomic and may be reordered.
159 * If it's called on the same region of memory simultaneously, the effect
160 * may be that only one operation succeeds.
162 static inline void __change_bit(int nr, volatile unsigned long *addr)
164 asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
168 * change_bit - Toggle a bit in memory
169 * @nr: Bit to change
170 * @addr: Address to start counting from
172 * change_bit() is atomic and may not be reordered.
173 * Note that @nr may be almost arbitrarily large; this function is not
174 * restricted to acting on a single-word quantity.
176 static inline void change_bit(int nr, volatile unsigned long *addr)
178 if (IS_IMMEDIATE(nr)) {
179 asm volatile(LOCK_PREFIX "xorb %1,%0"
180 : CONST_MASK_ADDR(nr, addr)
181 : "iq" ((u8)CONST_MASK(nr)));
182 } else {
183 asm volatile(LOCK_PREFIX "btc %1,%0"
184 : BITOP_ADDR(addr)
185 : "Ir" (nr));
190 * test_and_set_bit - Set a bit and return its old value
191 * @nr: Bit to set
192 * @addr: Address to count from
194 * This operation is atomic and cannot be reordered.
195 * It also implies a memory barrier.
197 static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
199 int oldbit;
201 asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
202 "sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
204 return oldbit;
208 * test_and_set_bit_lock - Set a bit and return its old value for lock
209 * @nr: Bit to set
210 * @addr: Address to count from
212 * This is the same as test_and_set_bit on x86.
214 static __always_inline int
215 test_and_set_bit_lock(int nr, volatile unsigned long *addr)
217 return test_and_set_bit(nr, addr);
221 * __test_and_set_bit - Set a bit and return its old value
222 * @nr: Bit to set
223 * @addr: Address to count from
225 * This operation is non-atomic and can be reordered.
226 * If two examples of this operation race, one can appear to succeed
227 * but actually fail. You must protect multiple accesses with a lock.
229 static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
231 int oldbit;
233 asm("bts %2,%1\n\t"
234 "sbb %0,%0"
235 : "=r" (oldbit), ADDR
236 : "Ir" (nr));
237 return oldbit;
241 * test_and_clear_bit - Clear a bit and return its old value
242 * @nr: Bit to clear
243 * @addr: Address to count from
245 * This operation is atomic and cannot be reordered.
246 * It also implies a memory barrier.
248 static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
250 int oldbit;
252 asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
253 "sbb %0,%0"
254 : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
256 return oldbit;
260 * __test_and_clear_bit - Clear a bit and return its old value
261 * @nr: Bit to clear
262 * @addr: Address to count from
264 * This operation is non-atomic and can be reordered.
265 * If two examples of this operation race, one can appear to succeed
266 * but actually fail. You must protect multiple accesses with a lock.
268 static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
270 int oldbit;
272 asm volatile("btr %2,%1\n\t"
273 "sbb %0,%0"
274 : "=r" (oldbit), ADDR
275 : "Ir" (nr));
276 return oldbit;
279 /* WARNING: non atomic and it can be reordered! */
280 static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
282 int oldbit;
284 asm volatile("btc %2,%1\n\t"
285 "sbb %0,%0"
286 : "=r" (oldbit), ADDR
287 : "Ir" (nr) : "memory");
289 return oldbit;
293 * test_and_change_bit - Change a bit and return its old value
294 * @nr: Bit to change
295 * @addr: Address to count from
297 * This operation is atomic and cannot be reordered.
298 * It also implies a memory barrier.
300 static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
302 int oldbit;
304 asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
305 "sbb %0,%0"
306 : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
308 return oldbit;
311 static __always_inline int constant_test_bit(unsigned int nr, const volatile unsigned long *addr)
313 return ((1UL << (nr % BITS_PER_LONG)) &
314 (addr[nr / BITS_PER_LONG])) != 0;
317 static inline int variable_test_bit(int nr, volatile const unsigned long *addr)
319 int oldbit;
321 asm volatile("bt %2,%1\n\t"
322 "sbb %0,%0"
323 : "=r" (oldbit)
324 : "m" (*(unsigned long *)addr), "Ir" (nr));
326 return oldbit;
329 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
331 * test_bit - Determine whether a bit is set
332 * @nr: bit number to test
333 * @addr: Address to start counting from
335 static int test_bit(int nr, const volatile unsigned long *addr);
336 #endif
338 #define test_bit(nr, addr) \
339 (__builtin_constant_p((nr)) \
340 ? constant_test_bit((nr), (addr)) \
341 : variable_test_bit((nr), (addr)))
344 * __ffs - find first set bit in word
345 * @word: The word to search
347 * Undefined if no bit exists, so code should check against 0 first.
349 static inline unsigned long __ffs(unsigned long word)
351 asm("bsf %1,%0"
352 : "=r" (word)
353 : "rm" (word));
354 return word;
358 * ffz - find first zero bit in word
359 * @word: The word to search
361 * Undefined if no zero exists, so code should check against ~0UL first.
363 static inline unsigned long ffz(unsigned long word)
365 asm("bsf %1,%0"
366 : "=r" (word)
367 : "r" (~word));
368 return word;
372 * __fls: find last set bit in word
373 * @word: The word to search
375 * Undefined if no set bit exists, so code should check against 0 first.
377 static inline unsigned long __fls(unsigned long word)
379 asm("bsr %1,%0"
380 : "=r" (word)
381 : "rm" (word));
382 return word;
385 #undef ADDR
387 #ifdef __KERNEL__
389 * ffs - find first set bit in word
390 * @x: the word to search
392 * This is defined the same way as the libc and compiler builtin ffs
393 * routines, therefore differs in spirit from the other bitops.
395 * ffs(value) returns 0 if value is 0 or the position of the first
396 * set bit if value is nonzero. The first (least significant) bit
397 * is at position 1.
399 static inline int ffs(int x)
401 int r;
403 #ifdef CONFIG_X86_64
405 * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the
406 * dest reg is undefined if x==0, but their CPU architect says its
407 * value is written to set it to the same as before, except that the
408 * top 32 bits will be cleared.
410 * We cannot do this on 32 bits because at the very least some
411 * 486 CPUs did not behave this way.
413 long tmp = -1;
414 asm("bsfl %1,%0"
415 : "=r" (r)
416 : "rm" (x), "0" (tmp));
417 #elif defined(CONFIG_X86_CMOV)
418 asm("bsfl %1,%0\n\t"
419 "cmovzl %2,%0"
420 : "=&r" (r) : "rm" (x), "r" (-1));
421 #else
422 asm("bsfl %1,%0\n\t"
423 "jnz 1f\n\t"
424 "movl $-1,%0\n"
425 "1:" : "=r" (r) : "rm" (x));
426 #endif
427 return r + 1;
431 * fls - find last set bit in word
432 * @x: the word to search
434 * This is defined in a similar way as the libc and compiler builtin
435 * ffs, but returns the position of the most significant set bit.
437 * fls(value) returns 0 if value is 0 or the position of the last
438 * set bit if value is nonzero. The last (most significant) bit is
439 * at position 32.
441 static inline int fls(int x)
443 int r;
445 #ifdef CONFIG_X86_64
447 * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the
448 * dest reg is undefined if x==0, but their CPU architect says its
449 * value is written to set it to the same as before, except that the
450 * top 32 bits will be cleared.
452 * We cannot do this on 32 bits because at the very least some
453 * 486 CPUs did not behave this way.
455 long tmp = -1;
456 asm("bsrl %1,%0"
457 : "=r" (r)
458 : "rm" (x), "0" (tmp));
459 #elif defined(CONFIG_X86_CMOV)
460 asm("bsrl %1,%0\n\t"
461 "cmovzl %2,%0"
462 : "=&r" (r) : "rm" (x), "rm" (-1));
463 #else
464 asm("bsrl %1,%0\n\t"
465 "jnz 1f\n\t"
466 "movl $-1,%0\n"
467 "1:" : "=r" (r) : "rm" (x));
468 #endif
469 return r + 1;
473 * fls64 - find last set bit in a 64-bit word
474 * @x: the word to search
476 * This is defined in a similar way as the libc and compiler builtin
477 * ffsll, but returns the position of the most significant set bit.
479 * fls64(value) returns 0 if value is 0 or the position of the last
480 * set bit if value is nonzero. The last (most significant) bit is
481 * at position 64.
483 #ifdef CONFIG_X86_64
484 static __always_inline int fls64(__u64 x)
486 long bitpos = -1;
488 * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the
489 * dest reg is undefined if x==0, but their CPU architect says its
490 * value is written to set it to the same as before.
492 asm("bsrq %1,%0"
493 : "+r" (bitpos)
494 : "rm" (x));
495 return bitpos + 1;
497 #else
498 #include <asm-generic/bitops/fls64.h>
499 #endif
501 #include <asm-generic/bitops/find.h>
503 #include <asm-generic/bitops/sched.h>
505 #define ARCH_HAS_FAST_MULTIPLIER 1
507 #include <asm/arch_hweight.h>
509 #include <asm-generic/bitops/const_hweight.h>
511 #include <asm-generic/bitops/le.h>
513 #include <asm-generic/bitops/ext2-atomic-setbit.h>
515 #endif /* __KERNEL__ */
516 #endif /* _ASM_X86_BITOPS_H */