2008-10-23 05:26:29 +00:00
|
|
|
#ifndef _ASM_X86_BITOPS_H
|
|
|
|
#define _ASM_X86_BITOPS_H
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1992, Linus Torvalds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_BITOPS_H
|
|
|
|
#error only <linux/bitops.h> can be included directly
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <asm/alternative.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These have to be done with inline assembly: that way the bit-setting
|
|
|
|
* is guaranteed to be atomic. All bit operations return 0 if the bit
|
|
|
|
* was cleared before the operation and != 0 if it was not.
|
|
|
|
*
|
|
|
|
* bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
|
|
|
|
/* Technically wrong, but this avoids compilation errors on some gcc
|
|
|
|
versions. */
|
x86, bitops: make constant-bit set/clear_bit ops faster
On Wed, 18 Jun 2008, Linus Torvalds wrote:
>
> And yes, the "lock andl" should be noticeably faster than the xchgl.
I dunno. Here's a untested (!!) patch that turns constant-bit
set/clear_bit ops into byte mask ops (lock orb/andb).
It's not exactly pretty. The reason for using the byte versions is that a
locked op is serialized in the memory pipeline anyway, so there are no
forwarding issues (that could slow down things when we access things with
different sizes), and the byte ops are a lot smaller than 32-bit and
particularly 64-bit ops (big constants, and the 64-bit ops need the REX
prefix byte too).
[ Side note: I wonder if we should turn the "test_bit()" C version into a
"char *" version too.. It could actually help with alias analysis, since
char pointers can alias anything. So it might be the RightThing(tm) to
do for multiple reasons. I dunno. It's a separate issue. ]
It does actually shrink the kernel image a bit (a couple of hundred bytes
on the text segment for my everything-compiled-in image), and while it's
totally untested the (admittedly few) code generation points I looked at
seemed sane. And "lock orb" should be noticeably faster than "lock bts".
If somebody wants to play with it, go wild. I didn't do "change_bit()",
because nobody sane uses that thing anyway. I guarantee nothing. And if it
breaks, nobody saw me do anything. You can't prove this email wasn't sent
by somebody who is good at forging smtp.
This does require a gcc that is recent enough for "__builtin_constant_p()"
to work in an inline function, but I suspect our kernel requirements are
already higher than that. And if you do have an old gcc that is supported,
the worst that would happen is that the optimization doesn't trigger.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-06-19 04:03:26 +00:00
|
|
|
#define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
|
2008-01-30 12:30:55 +00:00
|
|
|
#else
|
x86, bitops: make constant-bit set/clear_bit ops faster
On Wed, 18 Jun 2008, Linus Torvalds wrote:
>
> And yes, the "lock andl" should be noticeably faster than the xchgl.
I dunno. Here's a untested (!!) patch that turns constant-bit
set/clear_bit ops into byte mask ops (lock orb/andb).
It's not exactly pretty. The reason for using the byte versions is that a
locked op is serialized in the memory pipeline anyway, so there are no
forwarding issues (that could slow down things when we access things with
different sizes), and the byte ops are a lot smaller than 32-bit and
particularly 64-bit ops (big constants, and the 64-bit ops need the REX
prefix byte too).
[ Side note: I wonder if we should turn the "test_bit()" C version into a
"char *" version too.. It could actually help with alias analysis, since
char pointers can alias anything. So it might be the RightThing(tm) to
do for multiple reasons. I dunno. It's a separate issue. ]
It does actually shrink the kernel image a bit (a couple of hundred bytes
on the text segment for my everything-compiled-in image), and while it's
totally untested the (admittedly few) code generation points I looked at
seemed sane. And "lock orb" should be noticeably faster than "lock bts".
If somebody wants to play with it, go wild. I didn't do "change_bit()",
because nobody sane uses that thing anyway. I guarantee nothing. And if it
breaks, nobody saw me do anything. You can't prove this email wasn't sent
by somebody who is good at forging smtp.
This does require a gcc that is recent enough for "__builtin_constant_p()"
to work in an inline function, but I suspect our kernel requirements are
already higher than that. And if you do have an old gcc that is supported,
the worst that would happen is that the optimization doesn't trigger.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-06-19 04:03:26 +00:00
|
|
|
#define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
|
2008-01-30 12:30:55 +00:00
|
|
|
#endif
|
|
|
|
|
2008-06-20 05:28:24 +00:00
|
|
|
#define ADDR BITOP_ADDR(addr)
|
x86, bitops: make constant-bit set/clear_bit ops faster
On Wed, 18 Jun 2008, Linus Torvalds wrote:
>
> And yes, the "lock andl" should be noticeably faster than the xchgl.
I dunno. Here's a untested (!!) patch that turns constant-bit
set/clear_bit ops into byte mask ops (lock orb/andb).
It's not exactly pretty. The reason for using the byte versions is that a
locked op is serialized in the memory pipeline anyway, so there are no
forwarding issues (that could slow down things when we access things with
different sizes), and the byte ops are a lot smaller than 32-bit and
particularly 64-bit ops (big constants, and the 64-bit ops need the REX
prefix byte too).
[ Side note: I wonder if we should turn the "test_bit()" C version into a
"char *" version too.. It could actually help with alias analysis, since
char pointers can alias anything. So it might be the RightThing(tm) to
do for multiple reasons. I dunno. It's a separate issue. ]
It does actually shrink the kernel image a bit (a couple of hundred bytes
on the text segment for my everything-compiled-in image), and while it's
totally untested the (admittedly few) code generation points I looked at
seemed sane. And "lock orb" should be noticeably faster than "lock bts".
If somebody wants to play with it, go wild. I didn't do "change_bit()",
because nobody sane uses that thing anyway. I guarantee nothing. And if it
breaks, nobody saw me do anything. You can't prove this email wasn't sent
by somebody who is good at forging smtp.
This does require a gcc that is recent enough for "__builtin_constant_p()"
to work in an inline function, but I suspect our kernel requirements are
already higher than that. And if you do have an old gcc that is supported,
the worst that would happen is that the optimization doesn't trigger.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-06-19 04:03:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We do the locked ops that don't return the old value as
|
|
|
|
* a mask operation on a byte.
|
|
|
|
*/
|
2008-06-20 05:28:24 +00:00
|
|
|
#define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
|
|
|
|
#define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
|
|
|
|
#define CONST_MASK(nr) (1 << ((nr) & 7))
|
x86, bitops: make constant-bit set/clear_bit ops faster
On Wed, 18 Jun 2008, Linus Torvalds wrote:
>
> And yes, the "lock andl" should be noticeably faster than the xchgl.
I dunno. Here's a untested (!!) patch that turns constant-bit
set/clear_bit ops into byte mask ops (lock orb/andb).
It's not exactly pretty. The reason for using the byte versions is that a
locked op is serialized in the memory pipeline anyway, so there are no
forwarding issues (that could slow down things when we access things with
different sizes), and the byte ops are a lot smaller than 32-bit and
particularly 64-bit ops (big constants, and the 64-bit ops need the REX
prefix byte too).
[ Side note: I wonder if we should turn the "test_bit()" C version into a
"char *" version too.. It could actually help with alias analysis, since
char pointers can alias anything. So it might be the RightThing(tm) to
do for multiple reasons. I dunno. It's a separate issue. ]
It does actually shrink the kernel image a bit (a couple of hundred bytes
on the text segment for my everything-compiled-in image), and while it's
totally untested the (admittedly few) code generation points I looked at
seemed sane. And "lock orb" should be noticeably faster than "lock bts".
If somebody wants to play with it, go wild. I didn't do "change_bit()",
because nobody sane uses that thing anyway. I guarantee nothing. And if it
breaks, nobody saw me do anything. You can't prove this email wasn't sent
by somebody who is good at forging smtp.
This does require a gcc that is recent enough for "__builtin_constant_p()"
to work in an inline function, but I suspect our kernel requirements are
already higher than that. And if you do have an old gcc that is supported,
the worst that would happen is that the optimization doesn't trigger.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-06-19 04:03:26 +00:00
|
|
|
|
2008-01-30 12:30:55 +00:00
|
|
|
/**
|
|
|
|
* set_bit - Atomically set a bit in memory
|
|
|
|
* @nr: the bit to set
|
|
|
|
* @addr: the address to start counting from
|
|
|
|
*
|
|
|
|
* This function is atomic and may not be reordered. See __set_bit()
|
|
|
|
* if you do not require the atomic guarantees.
|
|
|
|
*
|
|
|
|
* Note: there are no guarantees that this function will not be reordered
|
|
|
|
* on non x86 architectures, so if you are writing portable code,
|
|
|
|
* make sure not to rely on its reordering guarantees.
|
|
|
|
*
|
|
|
|
* Note that @nr may be almost arbitrarily large; this function is not
|
|
|
|
* restricted to acting on a single-word quantity.
|
|
|
|
*/
|
x86, bitops: make constant-bit set/clear_bit ops faster
On Wed, 18 Jun 2008, Linus Torvalds wrote:
>
> And yes, the "lock andl" should be noticeably faster than the xchgl.
I dunno. Here's a untested (!!) patch that turns constant-bit
set/clear_bit ops into byte mask ops (lock orb/andb).
It's not exactly pretty. The reason for using the byte versions is that a
locked op is serialized in the memory pipeline anyway, so there are no
forwarding issues (that could slow down things when we access things with
different sizes), and the byte ops are a lot smaller than 32-bit and
particularly 64-bit ops (big constants, and the 64-bit ops need the REX
prefix byte too).
[ Side note: I wonder if we should turn the "test_bit()" C version into a
"char *" version too.. It could actually help with alias analysis, since
char pointers can alias anything. So it might be the RightThing(tm) to
do for multiple reasons. I dunno. It's a separate issue. ]
It does actually shrink the kernel image a bit (a couple of hundred bytes
on the text segment for my everything-compiled-in image), and while it's
totally untested the (admittedly few) code generation points I looked at
seemed sane. And "lock orb" should be noticeably faster than "lock bts".
If somebody wants to play with it, go wild. I didn't do "change_bit()",
because nobody sane uses that thing anyway. I guarantee nothing. And if it
breaks, nobody saw me do anything. You can't prove this email wasn't sent
by somebody who is good at forging smtp.
This does require a gcc that is recent enough for "__builtin_constant_p()"
to work in an inline function, but I suspect our kernel requirements are
already higher than that. And if you do have an old gcc that is supported,
the worst that would happen is that the optimization doesn't trigger.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-06-19 04:03:26 +00:00
|
|
|
static inline void set_bit(unsigned int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-06-20 05:28:24 +00:00
|
|
|
if (IS_IMMEDIATE(nr)) {
|
|
|
|
asm volatile(LOCK_PREFIX "orb %1,%0"
|
|
|
|
: CONST_MASK_ADDR(nr, addr)
|
2008-06-20 19:50:20 +00:00
|
|
|
: "iq" ((u8)CONST_MASK(nr))
|
2008-06-20 05:28:24 +00:00
|
|
|
: "memory");
|
|
|
|
} else {
|
|
|
|
asm volatile(LOCK_PREFIX "bts %1,%0"
|
|
|
|
: BITOP_ADDR(addr) : "Ir" (nr) : "memory");
|
|
|
|
}
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __set_bit - Set a bit in memory
|
|
|
|
* @nr: the bit to set
|
|
|
|
* @addr: the address to start counting from
|
|
|
|
*
|
|
|
|
* Unlike set_bit(), this function is non-atomic and may be reordered.
|
|
|
|
* If it's called on the same region of memory simultaneously, the effect
|
|
|
|
* may be that only one operation succeeds.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void __set_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-03-23 08:03:07 +00:00
|
|
|
asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clear_bit - Clears a bit in memory
|
|
|
|
* @nr: Bit to clear
|
|
|
|
* @addr: Address to start counting from
|
|
|
|
*
|
|
|
|
* clear_bit() is atomic and may not be reordered. However, it does
|
|
|
|
* not contain a memory barrier, so if it is used for locking purposes,
|
|
|
|
* you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
|
|
|
|
* in order to ensure changes are visible on other processors.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void clear_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-06-20 05:28:24 +00:00
|
|
|
if (IS_IMMEDIATE(nr)) {
|
|
|
|
asm volatile(LOCK_PREFIX "andb %1,%0"
|
|
|
|
: CONST_MASK_ADDR(nr, addr)
|
2008-06-20 19:50:20 +00:00
|
|
|
: "iq" ((u8)~CONST_MASK(nr)));
|
2008-06-20 05:28:24 +00:00
|
|
|
} else {
|
|
|
|
asm volatile(LOCK_PREFIX "btr %1,%0"
|
|
|
|
: BITOP_ADDR(addr)
|
|
|
|
: "Ir" (nr));
|
|
|
|
}
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clear_bit_unlock - Clears a bit in memory
|
|
|
|
* @nr: Bit to clear
|
|
|
|
* @addr: Address to start counting from
|
|
|
|
*
|
|
|
|
* clear_bit() is atomic and implies release semantics before the memory
|
|
|
|
* operation. It can be used for an unlock.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
barrier();
|
|
|
|
clear_bit(nr, addr);
|
|
|
|
}
|
|
|
|
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void __clear_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-05-05 13:45:28 +00:00
|
|
|
asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __clear_bit_unlock - Clears a bit in memory
|
|
|
|
* @nr: Bit to clear
|
|
|
|
* @addr: Address to start counting from
|
|
|
|
*
|
|
|
|
* __clear_bit() is non-atomic and implies release semantics before the memory
|
|
|
|
* operation. It can be used for an unlock if no other CPUs can concurrently
|
|
|
|
* modify other bits in the word.
|
|
|
|
*
|
|
|
|
* No memory barrier is required here, because x86 cannot reorder stores past
|
|
|
|
* older loads. Same principle as spin_unlock.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void __clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
barrier();
|
|
|
|
__clear_bit(nr, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define smp_mb__before_clear_bit() barrier()
|
|
|
|
#define smp_mb__after_clear_bit() barrier()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __change_bit - Toggle a bit in memory
|
|
|
|
* @nr: the bit to change
|
|
|
|
* @addr: the address to start counting from
|
|
|
|
*
|
|
|
|
* Unlike change_bit(), this function is non-atomic and may be reordered.
|
|
|
|
* If it's called on the same region of memory simultaneously, the effect
|
|
|
|
* may be that only one operation succeeds.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void __change_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-05-05 13:45:28 +00:00
|
|
|
asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* change_bit - Toggle a bit in memory
|
|
|
|
* @nr: Bit to change
|
|
|
|
* @addr: Address to start counting from
|
|
|
|
*
|
|
|
|
* change_bit() is atomic and may not be reordered.
|
|
|
|
* Note that @nr may be almost arbitrarily large; this function is not
|
|
|
|
* restricted to acting on a single-word quantity.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline void change_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-10-24 14:53:33 +00:00
|
|
|
if (IS_IMMEDIATE(nr)) {
|
|
|
|
asm volatile(LOCK_PREFIX "xorb %1,%0"
|
|
|
|
: CONST_MASK_ADDR(nr, addr)
|
|
|
|
: "iq" ((u8)CONST_MASK(nr)));
|
|
|
|
} else {
|
|
|
|
asm volatile(LOCK_PREFIX "btc %1,%0"
|
|
|
|
: BITOP_ADDR(addr)
|
|
|
|
: "Ir" (nr));
|
|
|
|
}
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test_and_set_bit - Set a bit and return its old value
|
|
|
|
* @nr: Bit to set
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This operation is atomic and cannot be reordered.
|
|
|
|
* It also implies a memory barrier.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
|
|
|
asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
|
2008-03-23 08:01:45 +00:00
|
|
|
"sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test_and_set_bit_lock - Set a bit and return its old value for lock
|
|
|
|
* @nr: Bit to set
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This is the same as test_and_set_bit on x86.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int test_and_set_bit_lock(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
return test_and_set_bit(nr, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __test_and_set_bit - Set a bit and return its old value
|
|
|
|
* @nr: Bit to set
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This operation is non-atomic and can be reordered.
|
|
|
|
* If two examples of this operation race, one can appear to succeed
|
|
|
|
* but actually fail. You must protect multiple accesses with a lock.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
2008-05-05 13:45:28 +00:00
|
|
|
asm("bts %2,%1\n\t"
|
|
|
|
"sbb %0,%0"
|
|
|
|
: "=r" (oldbit), ADDR
|
|
|
|
: "Ir" (nr));
|
2008-01-30 12:30:55 +00:00
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test_and_clear_bit - Clear a bit and return its old value
|
|
|
|
* @nr: Bit to clear
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This operation is atomic and cannot be reordered.
|
|
|
|
* It also implies a memory barrier.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
|
|
|
asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
|
|
|
|
"sbb %0,%0"
|
2008-03-23 08:01:45 +00:00
|
|
|
: "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __test_and_clear_bit - Clear a bit and return its old value
|
|
|
|
* @nr: Bit to clear
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This operation is non-atomic and can be reordered.
|
|
|
|
* If two examples of this operation race, one can appear to succeed
|
|
|
|
* but actually fail. You must protect multiple accesses with a lock.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
2008-05-05 13:45:28 +00:00
|
|
|
asm volatile("btr %2,%1\n\t"
|
2008-01-30 12:30:55 +00:00
|
|
|
"sbb %0,%0"
|
2008-05-05 13:45:28 +00:00
|
|
|
: "=r" (oldbit), ADDR
|
|
|
|
: "Ir" (nr));
|
2008-01-30 12:30:55 +00:00
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WARNING: non atomic and it can be reordered! */
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
2008-05-05 13:45:28 +00:00
|
|
|
asm volatile("btc %2,%1\n\t"
|
2008-01-30 12:30:55 +00:00
|
|
|
"sbb %0,%0"
|
2008-05-05 13:45:28 +00:00
|
|
|
: "=r" (oldbit), ADDR
|
|
|
|
: "Ir" (nr) : "memory");
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test_and_change_bit - Change a bit and return its old value
|
|
|
|
* @nr: Bit to change
|
|
|
|
* @addr: Address to count from
|
|
|
|
*
|
|
|
|
* This operation is atomic and cannot be reordered.
|
|
|
|
* It also implies a memory barrier.
|
|
|
|
*/
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
|
|
|
asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
|
|
|
|
"sbb %0,%0"
|
2008-03-23 08:01:45 +00:00
|
|
|
: "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int constant_test_bit(int nr, const volatile unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
2008-01-30 12:31:31 +00:00
|
|
|
return ((1UL << (nr % BITS_PER_LONG)) &
|
|
|
|
(((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
|
2008-01-30 12:30:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-14 23:10:41 +00:00
|
|
|
static inline int variable_test_bit(int nr, volatile const unsigned long *addr)
|
2008-01-30 12:30:55 +00:00
|
|
|
{
|
|
|
|
int oldbit;
|
|
|
|
|
2008-05-05 13:45:28 +00:00
|
|
|
asm volatile("bt %2,%1\n\t"
|
2008-01-30 12:30:55 +00:00
|
|
|
"sbb %0,%0"
|
|
|
|
: "=r" (oldbit)
|
2008-05-05 13:45:28 +00:00
|
|
|
: "m" (*(unsigned long *)addr), "Ir" (nr));
|
2008-01-30 12:30:55 +00:00
|
|
|
|
|
|
|
return oldbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 /* Fool kernel-doc since it doesn't do macros yet */
|
|
|
|
/**
|
|
|
|
* test_bit - Determine whether a bit is set
|
|
|
|
* @nr: bit number to test
|
|
|
|
* @addr: Address to start counting from
|
|
|
|
*/
|
|
|
|
static int test_bit(int nr, const volatile unsigned long *addr);
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 08:03:07 +00:00
|
|
|
#define test_bit(nr, addr) \
|
|
|
|
(__builtin_constant_p((nr)) \
|
|
|
|
? constant_test_bit((nr), (addr)) \
|
|
|
|
: variable_test_bit((nr), (addr)))
|
2008-01-30 12:30:55 +00:00
|
|
|
|
2008-03-15 12:04:42 +00:00
|
|
|
/**
|
|
|
|
* __ffs - find first set bit in word
|
|
|
|
* @word: The word to search
|
|
|
|
*
|
|
|
|
* Undefined if no bit exists, so code should check against 0 first.
|
|
|
|
*/
|
|
|
|
static inline unsigned long __ffs(unsigned long word)
|
|
|
|
{
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsf %1,%0"
|
|
|
|
: "=r" (word)
|
|
|
|
: "rm" (word));
|
2008-03-15 12:04:42 +00:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ffz - find first zero bit in word
|
|
|
|
* @word: The word to search
|
|
|
|
*
|
|
|
|
* Undefined if no zero exists, so code should check against ~0UL first.
|
|
|
|
*/
|
|
|
|
static inline unsigned long ffz(unsigned long word)
|
|
|
|
{
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsf %1,%0"
|
|
|
|
: "=r" (word)
|
|
|
|
: "r" (~word));
|
2008-03-15 12:04:42 +00:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __fls: find last set bit in word
|
|
|
|
* @word: The word to search
|
|
|
|
*
|
2008-07-05 17:53:46 +00:00
|
|
|
* Undefined if no set bit exists, so code should check against 0 first.
|
2008-03-15 12:04:42 +00:00
|
|
|
*/
|
|
|
|
static inline unsigned long __fls(unsigned long word)
|
|
|
|
{
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsr %1,%0"
|
|
|
|
: "=r" (word)
|
|
|
|
: "rm" (word));
|
2008-03-15 12:04:42 +00:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
|
|
|
/**
|
|
|
|
* ffs - find first set bit in word
|
|
|
|
* @x: the word to search
|
|
|
|
*
|
|
|
|
* This is defined the same way as the libc and compiler builtin ffs
|
|
|
|
* routines, therefore differs in spirit from the other bitops.
|
|
|
|
*
|
|
|
|
* ffs(value) returns 0 if value is 0 or the position of the first
|
|
|
|
* set bit if value is nonzero. The first (least significant) bit
|
|
|
|
* is at position 1.
|
|
|
|
*/
|
|
|
|
static inline int ffs(int x)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
#ifdef CONFIG_X86_CMOV
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsfl %1,%0\n\t"
|
|
|
|
"cmovzl %2,%0"
|
|
|
|
: "=r" (r) : "rm" (x), "r" (-1));
|
2008-03-15 12:04:42 +00:00
|
|
|
#else
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsfl %1,%0\n\t"
|
|
|
|
"jnz 1f\n\t"
|
|
|
|
"movl $-1,%0\n"
|
|
|
|
"1:" : "=r" (r) : "rm" (x));
|
2008-03-15 12:04:42 +00:00
|
|
|
#endif
|
|
|
|
return r + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fls - find last set bit in word
|
|
|
|
* @x: the word to search
|
|
|
|
*
|
|
|
|
* This is defined in a similar way as the libc and compiler builtin
|
|
|
|
* ffs, but returns the position of the most significant set bit.
|
|
|
|
*
|
|
|
|
* fls(value) returns 0 if value is 0 or the position of the last
|
|
|
|
* set bit if value is nonzero. The last (most significant) bit is
|
|
|
|
* at position 32.
|
|
|
|
*/
|
|
|
|
static inline int fls(int x)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
#ifdef CONFIG_X86_CMOV
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsrl %1,%0\n\t"
|
|
|
|
"cmovzl %2,%0"
|
|
|
|
: "=&r" (r) : "rm" (x), "rm" (-1));
|
2008-03-15 12:04:42 +00:00
|
|
|
#else
|
2008-03-23 08:03:07 +00:00
|
|
|
asm("bsrl %1,%0\n\t"
|
|
|
|
"jnz 1f\n\t"
|
|
|
|
"movl $-1,%0\n"
|
|
|
|
"1:" : "=r" (r) : "rm" (x));
|
2008-03-15 12:04:42 +00:00
|
|
|
#endif
|
|
|
|
return r + 1;
|
|
|
|
}
|
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
|
2008-01-30 12:30:55 +00:00
|
|
|
#undef ADDR
|
|
|
|
|
2008-04-04 18:49:30 +00:00
|
|
|
#ifdef __KERNEL__
|
|
|
|
|
|
|
|
#include <asm-generic/bitops/sched.h>
|
|
|
|
|
|
|
|
#define ARCH_HAS_FAST_MULTIPLIER 1
|
|
|
|
|
|
|
|
#include <asm-generic/bitops/hweight.h>
|
2008-01-30 12:30:55 +00:00
|
|
|
|
2008-04-04 18:49:30 +00:00
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
|
|
|
|
#include <asm-generic/bitops/fls64.h>
|
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
|
|
|
|
|
|
|
#include <asm-generic/bitops/ext2-non-atomic.h>
|
|
|
|
|
|
|
|
#define ext2_set_bit_atomic(lock, nr, addr) \
|
|
|
|
test_and_set_bit((nr), (unsigned long *)(addr))
|
|
|
|
#define ext2_clear_bit_atomic(lock, nr, addr) \
|
|
|
|
test_and_clear_bit((nr), (unsigned long *)(addr))
|
|
|
|
|
|
|
|
#include <asm-generic/bitops/minix.h>
|
|
|
|
|
|
|
|
#endif /* __KERNEL__ */
|
2008-10-23 05:26:29 +00:00
|
|
|
#endif /* _ASM_X86_BITOPS_H */
|