aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-04-26 16:46:11 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-04-26 16:46:11 -0400
commit9b79ed952bd7344d40152f8a560ad8a0d93f3886 (patch)
tree0cdf72321a9eeb2a766b7b98d5a87ad3d46ad620
parenta52b0d25a722e84da999005b75f972aa4824253c (diff)
parent19870def587554c4055df3e74a21508e3647fb7e (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/x86/linux-2.6-generic-bitops-v3
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/x86/linux-2.6-generic-bitops-v3: x86, bitops: select the generic bitmap search functions x86: include/asm-x86/pgalloc.h/bitops.h: checkpatch cleanups - formatting only x86: finalize bitops unification x86, UML: remove x86-specific implementations of find_first_bit x86: optimize find_first_bit for small bitmaps x86: switch 64-bit to generic find_first_bit x86: generic versions of find_first_(zero_)bit, convert i386 bitops: use __fls for fls64 on 64-bit archs generic: implement __fls on all 64-bit archs generic: introduce a generic __fls implementation x86: merge the simple bitops and move them to bitops.h x86, generic: optimize find_next_(zero_)bit for small constant-size bitmaps x86, uml: fix uml with generic find_next_bit for x86 x86: change x86 to use generic find_next_bit uml: Kconfig cleanup uml: fix build error
-rw-r--r--arch/um/Kconfig.x86_647
-rw-r--r--arch/um/os-Linux/helper.c1
-rw-r--r--arch/um/sys-i386/Makefile2
-rw-r--r--arch/um/sys-x86_64/Makefile2
-rw-r--r--arch/x86/Kconfig.cpu7
-rw-r--r--arch/x86/lib/Makefile3
-rw-r--r--arch/x86/lib/bitops_32.c70
-rw-r--r--arch/x86/lib/bitops_64.c175
-rw-r--r--include/asm-alpha/bitops.h5
-rw-r--r--include/asm-generic/bitops/__fls.h43
-rw-r--r--include/asm-generic/bitops/find.h2
-rw-r--r--include/asm-generic/bitops/fls64.h22
-rw-r--r--include/asm-ia64/bitops.h16
-rw-r--r--include/asm-mips/bitops.h5
-rw-r--r--include/asm-parisc/bitops.h1
-rw-r--r--include/asm-powerpc/bitops.h5
-rw-r--r--include/asm-s390/bitops.h1
-rw-r--r--include/asm-sh/bitops.h1
-rw-r--r--include/asm-sparc64/bitops.h1
-rw-r--r--include/asm-x86/bitops.h149
-rw-r--r--include/asm-x86/bitops_32.h166
-rw-r--r--include/asm-x86/bitops_64.h162
-rw-r--r--include/linux/bitops.h140
-rw-r--r--lib/Kconfig6
-rw-r--r--lib/Makefile1
-rw-r--r--lib/find_next_bit.c77
26 files changed, 467 insertions, 603 deletions
diff --git a/arch/um/Kconfig.x86_64 b/arch/um/Kconfig.x86_64
index 3fbe69e359ed..5696e7b374b3 100644
--- a/arch/um/Kconfig.x86_64
+++ b/arch/um/Kconfig.x86_64
@@ -1,3 +1,10 @@
1
2menu "Host processor type and features"
3
4source "arch/x86/Kconfig.cpu"
5
6endmenu
7
1config UML_X86 8config UML_X86
2 bool 9 bool
3 default y 10 default y
diff --git a/arch/um/os-Linux/helper.c b/arch/um/os-Linux/helper.c
index f4bd349d4412..f25c29a12d00 100644
--- a/arch/um/os-Linux/helper.c
+++ b/arch/um/os-Linux/helper.c
@@ -14,6 +14,7 @@
14#include "os.h" 14#include "os.h"
15#include "um_malloc.h" 15#include "um_malloc.h"
16#include "user.h" 16#include "user.h"
17#include <linux/limits.h>
17 18
18struct helper_data { 19struct helper_data {
19 void (*pre_exec)(void*); 20 void (*pre_exec)(void*);
diff --git a/arch/um/sys-i386/Makefile b/arch/um/sys-i386/Makefile
index 964dc1a04c37..598b5c1903af 100644
--- a/arch/um/sys-i386/Makefile
+++ b/arch/um/sys-i386/Makefile
@@ -6,7 +6,7 @@ obj-y = bug.o bugs.o checksum.o delay.o fault.o ksyms.o ldt.o ptrace.o \
6 ptrace_user.o setjmp.o signal.o stub.o stub_segv.o syscalls.o sysrq.o \ 6 ptrace_user.o setjmp.o signal.o stub.o stub_segv.o syscalls.o sysrq.o \
7 sys_call_table.o tls.o 7 sys_call_table.o tls.o
8 8
9subarch-obj-y = lib/bitops_32.o lib/semaphore_32.o lib/string_32.o 9subarch-obj-y = lib/semaphore_32.o lib/string_32.o
10subarch-obj-$(CONFIG_HIGHMEM) += mm/highmem_32.o 10subarch-obj-$(CONFIG_HIGHMEM) += mm/highmem_32.o
11subarch-obj-$(CONFIG_MODULES) += kernel/module_32.o 11subarch-obj-$(CONFIG_MODULES) += kernel/module_32.o
12 12
diff --git a/arch/um/sys-x86_64/Makefile b/arch/um/sys-x86_64/Makefile
index 3c22de532088..c8b4cce9cfe1 100644
--- a/arch/um/sys-x86_64/Makefile
+++ b/arch/um/sys-x86_64/Makefile
@@ -10,7 +10,7 @@ obj-y = bug.o bugs.o delay.o fault.o ldt.o mem.o ptrace.o ptrace_user.o \
10 10
11obj-$(CONFIG_MODULES) += um_module.o 11obj-$(CONFIG_MODULES) += um_module.o
12 12
13subarch-obj-y = lib/bitops_64.o lib/csum-partial_64.o lib/memcpy_64.o lib/thunk_64.o 13subarch-obj-y = lib/csum-partial_64.o lib/memcpy_64.o lib/thunk_64.o
14subarch-obj-$(CONFIG_MODULES) += kernel/module_64.o 14subarch-obj-$(CONFIG_MODULES) += kernel/module_64.o
15 15
16ldt-y = ../sys-i386/ldt.o 16ldt-y = ../sys-i386/ldt.o
diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
index 4da3cdb9c1b1..7ef18b01f0bc 100644
--- a/arch/x86/Kconfig.cpu
+++ b/arch/x86/Kconfig.cpu
@@ -278,6 +278,11 @@ config GENERIC_CPU
278 278
279endchoice 279endchoice
280 280
281config X86_CPU
282 def_bool y
283 select GENERIC_FIND_FIRST_BIT
284 select GENERIC_FIND_NEXT_BIT
285
281config X86_GENERIC 286config X86_GENERIC
282 bool "Generic x86 support" 287 bool "Generic x86 support"
283 depends on X86_32 288 depends on X86_32
@@ -398,7 +403,7 @@ config X86_TSC
398# generates cmov. 403# generates cmov.
399config X86_CMOV 404config X86_CMOV
400 def_bool y 405 def_bool y
401 depends on (MK7 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7) 406 depends on (MK7 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7 || X86_64)
402 407
403config X86_MINIMUM_CPU_FAMILY 408config X86_MINIMUM_CPU_FAMILY
404 int 409 int
diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 25df1c1989fe..76f60f52a885 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -11,7 +11,7 @@ lib-y += memcpy_$(BITS).o
11ifeq ($(CONFIG_X86_32),y) 11ifeq ($(CONFIG_X86_32),y)
12 lib-y += checksum_32.o 12 lib-y += checksum_32.o
13 lib-y += strstr_32.o 13 lib-y += strstr_32.o
14 lib-y += bitops_32.o semaphore_32.o string_32.o 14 lib-y += semaphore_32.o string_32.o
15 15
16 lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o 16 lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o
17else 17else
@@ -21,7 +21,6 @@ else
21 21
22 lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o 22 lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o
23 lib-y += thunk_64.o clear_page_64.o copy_page_64.o 23 lib-y += thunk_64.o clear_page_64.o copy_page_64.o
24 lib-y += bitops_64.o
25 lib-y += memmove_64.o memset_64.o 24 lib-y += memmove_64.o memset_64.o
26 lib-y += copy_user_64.o rwlock_64.o copy_user_nocache_64.o 25 lib-y += copy_user_64.o rwlock_64.o copy_user_nocache_64.o
27endif 26endif
diff --git a/arch/x86/lib/bitops_32.c b/arch/x86/lib/bitops_32.c
deleted file mode 100644
index b65440459859..000000000000
--- a/arch/x86/lib/bitops_32.c
+++ /dev/null
@@ -1,70 +0,0 @@
1#include <linux/bitops.h>
2#include <linux/module.h>
3
4/**
5 * find_next_bit - find the next set bit in a memory region
6 * @addr: The address to base the search on
7 * @offset: The bitnumber to start searching at
8 * @size: The maximum size to search
9 */
10int find_next_bit(const unsigned long *addr, int size, int offset)
11{
12 const unsigned long *p = addr + (offset >> 5);
13 int set = 0, bit = offset & 31, res;
14
15 if (bit) {
16 /*
17 * Look for nonzero in the first 32 bits:
18 */
19 __asm__("bsfl %1,%0\n\t"
20 "jne 1f\n\t"
21 "movl $32, %0\n"
22 "1:"
23 : "=r" (set)
24 : "r" (*p >> bit));
25 if (set < (32 - bit))
26 return set + offset;
27 set = 32 - bit;
28 p++;
29 }
30 /*
31 * No set bit yet, search remaining full words for a bit
32 */
33 res = find_first_bit (p, size - 32 * (p - addr));
34 return (offset + set + res);
35}
36EXPORT_SYMBOL(find_next_bit);
37
38/**
39 * find_next_zero_bit - find the first zero bit in a memory region
40 * @addr: The address to base the search on
41 * @offset: The bitnumber to start searching at
42 * @size: The maximum size to search
43 */
44int find_next_zero_bit(const unsigned long *addr, int size, int offset)
45{
46 const unsigned long *p = addr + (offset >> 5);
47 int set = 0, bit = offset & 31, res;
48
49 if (bit) {
50 /*
51 * Look for zero in the first 32 bits.
52 */
53 __asm__("bsfl %1,%0\n\t"
54 "jne 1f\n\t"
55 "movl $32, %0\n"
56 "1:"
57 : "=r" (set)
58 : "r" (~(*p >> bit)));
59 if (set < (32 - bit))
60 return set + offset;
61 set = 32 - bit;
62 p++;
63 }
64 /*
65 * No zero yet, search remaining full bytes for a zero
66 */
67 res = find_first_zero_bit(p, size - 32 * (p - addr));
68 return (offset + set + res);
69}
70EXPORT_SYMBOL(find_next_zero_bit);
diff --git a/arch/x86/lib/bitops_64.c b/arch/x86/lib/bitops_64.c
deleted file mode 100644
index 0e8f491e6ccc..000000000000
--- a/arch/x86/lib/bitops_64.c
+++ /dev/null
@@ -1,175 +0,0 @@
1#include <linux/bitops.h>
2
3#undef find_first_zero_bit
4#undef find_next_zero_bit
5#undef find_first_bit
6#undef find_next_bit
7
8static inline long
9__find_first_zero_bit(const unsigned long * addr, unsigned long size)
10{
11 long d0, d1, d2;
12 long res;
13
14 /*
15 * We must test the size in words, not in bits, because
16 * otherwise incoming sizes in the range -63..-1 will not run
17 * any scasq instructions, and then the flags used by the je
18 * instruction will have whatever random value was in place
19 * before. Nobody should call us like that, but
20 * find_next_zero_bit() does when offset and size are at the
21 * same word and it fails to find a zero itself.
22 */
23 size += 63;
24 size >>= 6;
25 if (!size)
26 return 0;
27 asm volatile(
28 " repe; scasq\n"
29 " je 1f\n"
30 " xorq -8(%%rdi),%%rax\n"
31 " subq $8,%%rdi\n"
32 " bsfq %%rax,%%rdx\n"
33 "1: subq %[addr],%%rdi\n"
34 " shlq $3,%%rdi\n"
35 " addq %%rdi,%%rdx"
36 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
37 :"0" (0ULL), "1" (size), "2" (addr), "3" (-1ULL),
38 [addr] "S" (addr) : "memory");
39 /*
40 * Any register would do for [addr] above, but GCC tends to
41 * prefer rbx over rsi, even though rsi is readily available
42 * and doesn't have to be saved.
43 */
44 return res;
45}
46
47/**
48 * find_first_zero_bit - find the first zero bit in a memory region
49 * @addr: The address to start the search at
50 * @size: The maximum size to search
51 *
52 * Returns the bit-number of the first zero bit, not the number of the byte
53 * containing a bit.
54 */
55long find_first_zero_bit(const unsigned long * addr, unsigned long size)
56{
57 return __find_first_zero_bit (addr, size);
58}
59
60/**
61 * find_next_zero_bit - find the next zero bit in a memory region
62 * @addr: The address to base the search on
63 * @offset: The bitnumber to start searching at
64 * @size: The maximum size to search
65 */
66long find_next_zero_bit (const unsigned long * addr, long size, long offset)
67{
68 const unsigned long * p = addr + (offset >> 6);
69 unsigned long set = 0;
70 unsigned long res, bit = offset&63;
71
72 if (bit) {
73 /*
74 * Look for zero in first word
75 */
76 asm("bsfq %1,%0\n\t"
77 "cmoveq %2,%0"
78 : "=r" (set)
79 : "r" (~(*p >> bit)), "r"(64L));
80 if (set < (64 - bit))
81 return set + offset;
82 set = 64 - bit;
83 p++;
84 }
85 /*
86 * No zero yet, search remaining full words for a zero
87 */
88 res = __find_first_zero_bit (p, size - 64 * (p - addr));
89
90 return (offset + set + res);
91}
92
93static inline long
94__find_first_bit(const unsigned long * addr, unsigned long size)
95{
96 long d0, d1;
97 long res;
98
99 /*
100 * We must test the size in words, not in bits, because
101 * otherwise incoming sizes in the range -63..-1 will not run
102 * any scasq instructions, and then the flags used by the jz
103 * instruction will have whatever random value was in place
104 * before. Nobody should call us like that, but
105 * find_next_bit() does when offset and size are at the same
106 * word and it fails to find a one itself.
107 */
108 size += 63;
109 size >>= 6;
110 if (!size)
111 return 0;
112 asm volatile(
113 " repe; scasq\n"
114 " jz 1f\n"
115 " subq $8,%%rdi\n"
116 " bsfq (%%rdi),%%rax\n"
117 "1: subq %[addr],%%rdi\n"
118 " shlq $3,%%rdi\n"
119 " addq %%rdi,%%rax"
120 :"=a" (res), "=&c" (d0), "=&D" (d1)
121 :"0" (0ULL), "1" (size), "2" (addr),
122 [addr] "r" (addr) : "memory");
123 return res;
124}
125
126/**
127 * find_first_bit - find the first set bit in a memory region
128 * @addr: The address to start the search at
129 * @size: The maximum size to search
130 *
131 * Returns the bit-number of the first set bit, not the number of the byte
132 * containing a bit.
133 */
134long find_first_bit(const unsigned long * addr, unsigned long size)
135{
136 return __find_first_bit(addr,size);
137}
138
139/**
140 * find_next_bit - find the first set bit in a memory region
141 * @addr: The address to base the search on
142 * @offset: The bitnumber to start searching at
143 * @size: The maximum size to search
144 */
145long find_next_bit(const unsigned long * addr, long size, long offset)
146{
147 const unsigned long * p = addr + (offset >> 6);
148 unsigned long set = 0, bit = offset & 63, res;
149
150 if (bit) {
151 /*
152 * Look for nonzero in the first 64 bits:
153 */
154 asm("bsfq %1,%0\n\t"
155 "cmoveq %2,%0\n\t"
156 : "=r" (set)
157 : "r" (*p >> bit), "r" (64L));
158 if (set < (64 - bit))
159 return set + offset;
160 set = 64 - bit;
161 p++;
162 }
163 /*
164 * No set bit yet, search remaining full words for a bit
165 */
166 res = __find_first_bit (p, size - 64 * (p - addr));
167 return (offset + set + res);
168}
169
170#include <linux/module.h>
171
172EXPORT_SYMBOL(find_next_bit);
173EXPORT_SYMBOL(find_first_bit);
174EXPORT_SYMBOL(find_first_zero_bit);
175EXPORT_SYMBOL(find_next_zero_bit);
diff --git a/include/asm-alpha/bitops.h b/include/asm-alpha/bitops.h
index 9e19a704d484..15f3ae25c511 100644
--- a/include/asm-alpha/bitops.h
+++ b/include/asm-alpha/bitops.h
@@ -388,6 +388,11 @@ static inline int fls64(unsigned long x)
388} 388}
389#endif 389#endif
390 390
391static inline unsigned long __fls(unsigned long x)
392{
393 return fls64(x) - 1;
394}
395
391static inline int fls(int x) 396static inline int fls(int x)
392{ 397{
393 return fls64((unsigned int) x); 398 return fls64((unsigned int) x);
diff --git a/include/asm-generic/bitops/__fls.h b/include/asm-generic/bitops/__fls.h
new file mode 100644
index 000000000000..be24465403d6
--- /dev/null
+++ b/include/asm-generic/bitops/__fls.h
@@ -0,0 +1,43 @@
1#ifndef _ASM_GENERIC_BITOPS___FLS_H_
2#define _ASM_GENERIC_BITOPS___FLS_H_
3
4#include <asm/types.h>
5
6/**
7 * __fls - find last (most-significant) set bit in a long word
8 * @word: the word to search
9 *
10 * Undefined if no set bit exists, so code should check against 0 first.
11 */
12static inline unsigned long __fls(unsigned long word)
13{
14 int num = BITS_PER_LONG - 1;
15
16#if BITS_PER_LONG == 64
17 if (!(word & (~0ul << 32))) {
18 num -= 32;
19 word <<= 32;
20 }
21#endif
22 if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
23 num -= 16;
24 word <<= 16;
25 }
26 if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
27 num -= 8;
28 word <<= 8;
29 }
30 if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
31 num -= 4;
32 word <<= 4;
33 }
34 if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
35 num -= 2;
36 word <<= 2;
37 }
38 if (!(word & (~0ul << (BITS_PER_LONG-1))))
39 num -= 1;
40 return num;
41}
42
43#endif /* _ASM_GENERIC_BITOPS___FLS_H_ */
diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h
index 72a51e5a12ef..1914e9742512 100644
--- a/include/asm-generic/bitops/find.h
+++ b/include/asm-generic/bitops/find.h
@@ -1,11 +1,13 @@
1#ifndef _ASM_GENERIC_BITOPS_FIND_H_ 1#ifndef _ASM_GENERIC_BITOPS_FIND_H_
2#define _ASM_GENERIC_BITOPS_FIND_H_ 2#define _ASM_GENERIC_BITOPS_FIND_H_
3 3
4#ifndef CONFIG_GENERIC_FIND_NEXT_BIT
4extern unsigned long find_next_bit(const unsigned long *addr, unsigned long 5extern unsigned long find_next_bit(const unsigned long *addr, unsigned long
5 size, unsigned long offset); 6 size, unsigned long offset);
6 7
7extern unsigned long find_next_zero_bit(const unsigned long *addr, unsigned 8extern unsigned long find_next_zero_bit(const unsigned long *addr, unsigned
8 long size, unsigned long offset); 9 long size, unsigned long offset);
10#endif
9 11
10#define find_first_bit(addr, size) find_next_bit((addr), (size), 0) 12#define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
11#define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0) 13#define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
diff --git a/include/asm-generic/bitops/fls64.h b/include/asm-generic/bitops/fls64.h
index 1b6b17ce2428..86d403f8b256 100644
--- a/include/asm-generic/bitops/fls64.h
+++ b/include/asm-generic/bitops/fls64.h
@@ -3,6 +3,18 @@
3 3
4#include <asm/types.h> 4#include <asm/types.h>
5 5
6/**
7 * fls64 - find last set bit in a 64-bit word
8 * @x: the word to search
9 *
10 * This is defined in a similar way as the libc and compiler builtin
11 * ffsll, but returns the position of the most significant set bit.
12 *
13 * fls64(value) returns 0 if value is 0 or the position of the last
14 * set bit if value is nonzero. The last (most significant) bit is
15 * at position 64.
16 */
17#if BITS_PER_LONG == 32
6static inline int fls64(__u64 x) 18static inline int fls64(__u64 x)
7{ 19{
8 __u32 h = x >> 32; 20 __u32 h = x >> 32;
@@ -10,5 +22,15 @@ static inline int fls64(__u64 x)
10 return fls(h) + 32; 22 return fls(h) + 32;
11 return fls(x); 23 return fls(x);
12} 24}
25#elif BITS_PER_LONG == 64
26static inline int fls64(__u64 x)
27{
28 if (x == 0)
29 return 0;
30 return __fls(x) + 1;
31}
32#else
33#error BITS_PER_LONG not 32 or 64
34#endif
13 35
14#endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */ 36#endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */
diff --git a/include/asm-ia64/bitops.h b/include/asm-ia64/bitops.h
index 953d3df9dd22..e2ca80037335 100644
--- a/include/asm-ia64/bitops.h
+++ b/include/asm-ia64/bitops.h
@@ -407,6 +407,22 @@ fls (int t)
407 return ia64_popcnt(x); 407 return ia64_popcnt(x);
408} 408}
409 409
410/*
411 * Find the last (most significant) bit set. Undefined for x==0.
412 * Bits are numbered from 0..63 (e.g., __fls(9) == 3).
413 */
414static inline unsigned long
415__fls (unsigned long x)
416{
417 x |= x >> 1;
418 x |= x >> 2;
419 x |= x >> 4;
420 x |= x >> 8;
421 x |= x >> 16;
422 x |= x >> 32;
423 return ia64_popcnt(x) - 1;
424}
425
410#include <asm-generic/bitops/fls64.h> 426#include <asm-generic/bitops/fls64.h>
411 427
412/* 428/*
diff --git a/include/asm-mips/bitops.h b/include/asm-mips/bitops.h
index ec75ce4cdb8c..c2bd126c3b4e 100644
--- a/include/asm-mips/bitops.h
+++ b/include/asm-mips/bitops.h
@@ -591,6 +591,11 @@ static inline int __ilog2(unsigned long x)
591 return 63 - lz; 591 return 63 - lz;
592} 592}
593 593
594static inline unsigned long __fls(unsigned long x)
595{
596 return __ilog2(x);
597}
598
594#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) 599#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
595 600
596/* 601/*
diff --git a/include/asm-parisc/bitops.h b/include/asm-parisc/bitops.h
index f8eebcbad01f..7a6ea10bd231 100644
--- a/include/asm-parisc/bitops.h
+++ b/include/asm-parisc/bitops.h
@@ -210,6 +210,7 @@ static __inline__ int fls(int x)
210 return ret; 210 return ret;
211} 211}
212 212
213#include <asm-generic/bitops/__fls.h>
213#include <asm-generic/bitops/fls64.h> 214#include <asm-generic/bitops/fls64.h>
214#include <asm-generic/bitops/hweight.h> 215#include <asm-generic/bitops/hweight.h>
215#include <asm-generic/bitops/lock.h> 216#include <asm-generic/bitops/lock.h>
diff --git a/include/asm-powerpc/bitops.h b/include/asm-powerpc/bitops.h
index a99a74929475..897eade3afbe 100644
--- a/include/asm-powerpc/bitops.h
+++ b/include/asm-powerpc/bitops.h
@@ -313,6 +313,11 @@ static __inline__ int fls(unsigned int x)
313 return 32 - lz; 313 return 32 - lz;
314} 314}
315 315
316static __inline__ unsigned long __fls(unsigned long x)
317{
318 return __ilog2(x);
319}
320
316/* 321/*
317 * 64-bit can do this using one cntlzd (count leading zeroes doubleword) 322 * 64-bit can do this using one cntlzd (count leading zeroes doubleword)
318 * instruction; for 32-bit we use the generic version, which does two 323 * instruction; for 32-bit we use the generic version, which does two
diff --git a/include/asm-s390/bitops.h b/include/asm-s390/bitops.h
index 965394e69452..b4eb24ab5af9 100644
--- a/include/asm-s390/bitops.h
+++ b/include/asm-s390/bitops.h
@@ -769,6 +769,7 @@ static inline int sched_find_first_bit(unsigned long *b)
769} 769}
770 770
771#include <asm-generic/bitops/fls.h> 771#include <asm-generic/bitops/fls.h>
772#include <asm-generic/bitops/__fls.h>
772#include <asm-generic/bitops/fls64.h> 773#include <asm-generic/bitops/fls64.h>
773 774
774#include <asm-generic/bitops/hweight.h> 775#include <asm-generic/bitops/hweight.h>
diff --git a/include/asm-sh/bitops.h b/include/asm-sh/bitops.h
index b6ba5a60dec2..d7d382f63ee5 100644
--- a/include/asm-sh/bitops.h
+++ b/include/asm-sh/bitops.h
@@ -95,6 +95,7 @@ static inline unsigned long ffz(unsigned long word)
95#include <asm-generic/bitops/ext2-atomic.h> 95#include <asm-generic/bitops/ext2-atomic.h>
96#include <asm-generic/bitops/minix.h> 96#include <asm-generic/bitops/minix.h>
97#include <asm-generic/bitops/fls.h> 97#include <asm-generic/bitops/fls.h>
98#include <asm-generic/bitops/__fls.h>
98#include <asm-generic/bitops/fls64.h> 99#include <asm-generic/bitops/fls64.h>
99 100
100#endif /* __KERNEL__ */ 101#endif /* __KERNEL__ */
diff --git a/include/asm-sparc64/bitops.h b/include/asm-sparc64/bitops.h
index 982ce8992b91..11f9d8146cdf 100644
--- a/include/asm-sparc64/bitops.h
+++ b/include/asm-sparc64/bitops.h
@@ -34,6 +34,7 @@ extern void change_bit(unsigned long nr, volatile unsigned long *addr);
34#include <asm-generic/bitops/ffz.h> 34#include <asm-generic/bitops/ffz.h>
35#include <asm-generic/bitops/__ffs.h> 35#include <asm-generic/bitops/__ffs.h>
36#include <asm-generic/bitops/fls.h> 36#include <asm-generic/bitops/fls.h>
37#include <asm-generic/bitops/__fls.h>
37#include <asm-generic/bitops/fls64.h> 38#include <asm-generic/bitops/fls64.h>
38 39
39#ifdef __KERNEL__ 40#ifdef __KERNEL__
diff --git a/include/asm-x86/bitops.h b/include/asm-x86/bitops.h
index 1ae7b270a1ef..b81a4d4d3337 100644
--- a/include/asm-x86/bitops.h
+++ b/include/asm-x86/bitops.h
@@ -62,12 +62,9 @@ static inline void set_bit(int nr, volatile void *addr)
62 */ 62 */
63static inline void __set_bit(int nr, volatile void *addr) 63static inline void __set_bit(int nr, volatile void *addr)
64{ 64{
65 asm volatile("bts %1,%0" 65 asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
66 : ADDR
67 : "Ir" (nr) : "memory");
68} 66}
69 67
70
71/** 68/**
72 * clear_bit - Clears a bit in memory 69 * clear_bit - Clears a bit in memory
73 * @nr: Bit to clear 70 * @nr: Bit to clear
@@ -297,19 +294,145 @@ static inline int variable_test_bit(int nr, volatile const void *addr)
297static int test_bit(int nr, const volatile unsigned long *addr); 294static int test_bit(int nr, const volatile unsigned long *addr);
298#endif 295#endif
299 296
300#define test_bit(nr,addr) \ 297#define test_bit(nr, addr) \
301 (__builtin_constant_p(nr) ? \ 298 (__builtin_constant_p((nr)) \
302 constant_test_bit((nr),(addr)) : \ 299 ? constant_test_bit((nr), (addr)) \
303 variable_test_bit((nr),(addr))) 300 : variable_test_bit((nr), (addr)))
301
302/**
303 * __ffs - find first set bit in word
304 * @word: The word to search
305 *
306 * Undefined if no bit exists, so code should check against 0 first.
307 */
308static inline unsigned long __ffs(unsigned long word)
309{
310 asm("bsf %1,%0"
311 : "=r" (word)
312 : "rm" (word));
313 return word;
314}
315
316/**
317 * ffz - find first zero bit in word
318 * @word: The word to search
319 *
320 * Undefined if no zero exists, so code should check against ~0UL first.
321 */
322static inline unsigned long ffz(unsigned long word)
323{
324 asm("bsf %1,%0"
325 : "=r" (word)
326 : "r" (~word));
327 return word;
328}
329
330/*
331 * __fls: find last set bit in word
332 * @word: The word to search
333 *
334 * Undefined if no zero exists, so code should check against ~0UL first.
335 */
336static inline unsigned long __fls(unsigned long word)
337{
338 asm("bsr %1,%0"
339 : "=r" (word)
340 : "rm" (word));
341 return word;
342}
343
344#ifdef __KERNEL__
345/**
346 * ffs - find first set bit in word
347 * @x: the word to search
348 *
349 * This is defined the same way as the libc and compiler builtin ffs
350 * routines, therefore differs in spirit from the other bitops.
351 *
352 * ffs(value) returns 0 if value is 0 or the position of the first
353 * set bit if value is nonzero. The first (least significant) bit
354 * is at position 1.
355 */
356static inline int ffs(int x)
357{
358 int r;
359#ifdef CONFIG_X86_CMOV
360 asm("bsfl %1,%0\n\t"
361 "cmovzl %2,%0"
362 : "=r" (r) : "rm" (x), "r" (-1));
363#else
364 asm("bsfl %1,%0\n\t"
365 "jnz 1f\n\t"
366 "movl $-1,%0\n"
367 "1:" : "=r" (r) : "rm" (x));
368#endif
369 return r + 1;
370}
371
372/**
373 * fls - find last set bit in word
374 * @x: the word to search
375 *
376 * This is defined in a similar way as the libc and compiler builtin
377 * ffs, but returns the position of the most significant set bit.
378 *
379 * fls(value) returns 0 if value is 0 or the position of the last
380 * set bit if value is nonzero. The last (most significant) bit is
381 * at position 32.
382 */
383static inline int fls(int x)
384{
385 int r;
386#ifdef CONFIG_X86_CMOV
387 asm("bsrl %1,%0\n\t"
388 "cmovzl %2,%0"
389 : "=&r" (r) : "rm" (x), "rm" (-1));
390#else
391 asm("bsrl %1,%0\n\t"
392 "jnz 1f\n\t"
393 "movl $-1,%0\n"
394 "1:" : "=r" (r) : "rm" (x));
395#endif
396 return r + 1;
397}
398#endif /* __KERNEL__ */
304 399
305#undef BASE_ADDR 400#undef BASE_ADDR
306#undef BIT_ADDR 401#undef BIT_ADDR
307#undef ADDR 402#undef ADDR
308 403
309#ifdef CONFIG_X86_32 404static inline void set_bit_string(unsigned long *bitmap,
310# include "bitops_32.h" 405 unsigned long i, int len)
311#else 406{
312# include "bitops_64.h" 407 unsigned long end = i + len;
313#endif 408 while (i < end) {
409 __set_bit(i, bitmap);
410 i++;
411 }
412}
413
414#ifdef __KERNEL__
415
416#include <asm-generic/bitops/sched.h>
417
418#define ARCH_HAS_FAST_MULTIPLIER 1
419
420#include <asm-generic/bitops/hweight.h>
421
422#endif /* __KERNEL__ */
423
424#include <asm-generic/bitops/fls64.h>
425
426#ifdef __KERNEL__
427
428#include <asm-generic/bitops/ext2-non-atomic.h>
429
430#define ext2_set_bit_atomic(lock, nr, addr) \
431 test_and_set_bit((nr), (unsigned long *)(addr))
432#define ext2_clear_bit_atomic(lock, nr, addr) \
433 test_and_clear_bit((nr), (unsigned long *)(addr))
434
435#include <asm-generic/bitops/minix.h>
314 436
437#endif /* __KERNEL__ */
315#endif /* _ASM_X86_BITOPS_H */ 438#endif /* _ASM_X86_BITOPS_H */
diff --git a/include/asm-x86/bitops_32.h b/include/asm-x86/bitops_32.h
deleted file mode 100644
index 2513a81f82aa..000000000000
--- a/include/asm-x86/bitops_32.h
+++ /dev/null
@@ -1,166 +0,0 @@
1#ifndef _I386_BITOPS_H
2#define _I386_BITOPS_H
3
4/*
5 * Copyright 1992, Linus Torvalds.
6 */
7
8/**
9 * find_first_zero_bit - find the first zero bit in a memory region
10 * @addr: The address to start the search at
11 * @size: The maximum size to search
12 *
13 * Returns the bit number of the first zero bit, not the number of the byte
14 * containing a bit.
15 */
16static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
17{
18 int d0, d1, d2;
19 int res;
20
21 if (!size)
22 return 0;
23 /* This looks at memory.
24 * Mark it volatile to tell gcc not to move it around
25 */
26 asm volatile("movl $-1,%%eax\n\t"
27 "xorl %%edx,%%edx\n\t"
28 "repe; scasl\n\t"
29 "je 1f\n\t"
30 "xorl -4(%%edi),%%eax\n\t"
31 "subl $4,%%edi\n\t"
32 "bsfl %%eax,%%edx\n"
33 "1:\tsubl %%ebx,%%edi\n\t"
34 "shll $3,%%edi\n\t"
35 "addl %%edi,%%edx"
36 : "=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
37 : "1" ((size + 31) >> 5), "2" (addr),
38 "b" (addr) : "memory");
39 return res;
40}
41
42/**
43 * find_next_zero_bit - find the first zero bit in a memory region
44 * @addr: The address to base the search on
45 * @offset: The bit number to start searching at
46 * @size: The maximum size to search
47 */
48int find_next_zero_bit(const unsigned long *addr, int size, int offset);
49
50/**
51 * __ffs - find first bit in word.
52 * @word: The word to search
53 *
54 * Undefined if no bit exists, so code should check against 0 first.
55 */
56static inline unsigned long __ffs(unsigned long word)
57{
58 __asm__("bsfl %1,%0"
59 :"=r" (word)
60 :"rm" (word));
61 return word;
62}
63
64/**
65 * find_first_bit - find the first set bit in a memory region
66 * @addr: The address to start the search at
67 * @size: The maximum size to search
68 *
69 * Returns the bit number of the first set bit, not the number of the byte
70 * containing a bit.
71 */
72static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
73{
74 unsigned x = 0;
75
76 while (x < size) {
77 unsigned long val = *addr++;
78 if (val)
79 return __ffs(val) + x;
80 x += sizeof(*addr) << 3;
81 }
82 return x;
83}
84
85/**
86 * find_next_bit - find the first set bit in a memory region
87 * @addr: The address to base the search on
88 * @offset: The bit number to start searching at
89 * @size: The maximum size to search
90 */
91int find_next_bit(const unsigned long *addr, int size, int offset);
92
93/**
94 * ffz - find first zero in word.
95 * @word: The word to search
96 *
97 * Undefined if no zero exists, so code should check against ~0UL first.
98 */
99static inline unsigned long ffz(unsigned long word)
100{
101 __asm__("bsfl %1,%0"
102 :"=r" (word)
103 :"r" (~word));
104 return word;
105}
106
107#ifdef __KERNEL__
108
109#include <asm-generic/bitops/sched.h>
110
111/**
112 * ffs - find first bit set
113 * @x: the word to search
114 *
115 * This is defined the same way as
116 * the libc and compiler builtin ffs routines, therefore
117 * differs in spirit from the above ffz() (man ffs).
118 */
119static inline int ffs(int x)
120{
121 int r;
122
123 __asm__("bsfl %1,%0\n\t"
124 "jnz 1f\n\t"
125 "movl $-1,%0\n"
126 "1:" : "=r" (r) : "rm" (x));
127 return r+1;
128}
129
130/**
131 * fls - find last bit set
132 * @x: the word to search
133 *
134 * This is defined the same way as ffs().
135 */
136static inline int fls(int x)
137{
138 int r;
139
140 __asm__("bsrl %1,%0\n\t"
141 "jnz 1f\n\t"
142 "movl $-1,%0\n"
143 "1:" : "=r" (r) : "rm" (x));
144 return r+1;
145}
146
147#include <asm-generic/bitops/hweight.h>
148
149#endif /* __KERNEL__ */
150
151#include <asm-generic/bitops/fls64.h>
152
153#ifdef __KERNEL__
154
155#include <asm-generic/bitops/ext2-non-atomic.h>
156
157#define ext2_set_bit_atomic(lock, nr, addr) \
158 test_and_set_bit((nr), (unsigned long *)(addr))
159#define ext2_clear_bit_atomic(lock, nr, addr) \
160 test_and_clear_bit((nr), (unsigned long *)(addr))
161
162#include <asm-generic/bitops/minix.h>
163
164#endif /* __KERNEL__ */
165
166#endif /* _I386_BITOPS_H */
diff --git a/include/asm-x86/bitops_64.h b/include/asm-x86/bitops_64.h
deleted file mode 100644
index 365f8207ea59..000000000000
--- a/include/asm-x86/bitops_64.h
+++ /dev/null
@@ -1,162 +0,0 @@
1#ifndef _X86_64_BITOPS_H
2#define _X86_64_BITOPS_H
3
4/*
5 * Copyright 1992, Linus Torvalds.
6 */
7
8extern long find_first_zero_bit(const unsigned long *addr, unsigned long size);
9extern long find_next_zero_bit(const unsigned long *addr, long size, long offset);
10extern long find_first_bit(const unsigned long *addr, unsigned long size);
11extern long find_next_bit(const unsigned long *addr, long size, long offset);
12
13/* return index of first bet set in val or max when no bit is set */
14static inline long __scanbit(unsigned long val, unsigned long max)
15{
16 asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val) : "r" (val), "r" (max));
17 return val;
18}
19
20#define find_next_bit(addr,size,off) \
21((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
22 ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
23 find_next_bit(addr,size,off)))
24
25#define find_next_zero_bit(addr,size,off) \
26((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
27 ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
28 find_next_zero_bit(addr,size,off)))
29
30#define find_first_bit(addr, size) \
31 ((__builtin_constant_p((size)) && (size) <= BITS_PER_LONG \
32 ? (__scanbit(*(unsigned long *)(addr), (size))) \
33 : find_first_bit((addr), (size))))
34
35#define find_first_zero_bit(addr, size) \
36 ((__builtin_constant_p((size)) && (size) <= BITS_PER_LONG \
37 ? (__scanbit(~*(unsigned long *)(addr), (size))) \
38 : find_first_zero_bit((addr), (size))))
39
40static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
41 int len)
42{
43 unsigned long end = i + len;
44 while (i < end) {
45 __set_bit(i, bitmap);
46 i++;
47 }
48}
49
50/**
51 * ffz - find first zero in word.
52 * @word: The word to search
53 *
54 * Undefined if no zero exists, so code should check against ~0UL first.
55 */
56static inline unsigned long ffz(unsigned long word)
57{
58 __asm__("bsfq %1,%0"
59 :"=r" (word)
60 :"r" (~word));
61 return word;
62}
63
64/**
65 * __ffs - find first bit in word.
66 * @word: The word to search
67 *
68 * Undefined if no bit exists, so code should check against 0 first.
69 */
70static inline unsigned long __ffs(unsigned long word)
71{
72 __asm__("bsfq %1,%0"
73 :"=r" (word)
74 :"rm" (word));
75 return word;
76}
77
78/*
79 * __fls: find last bit set.
80 * @word: The word to search
81 *
82 * Undefined if no zero exists, so code should check against ~0UL first.
83 */
84static inline unsigned long __fls(unsigned long word)
85{
86 __asm__("bsrq %1,%0"
87 :"=r" (word)
88 :"rm" (word));
89 return word;
90}
91
92#ifdef __KERNEL__
93
94#include <asm-generic/bitops/sched.h>
95
96/**
97 * ffs - find first bit set
98 * @x: the word to search
99 *
100 * This is defined the same way as
101 * the libc and compiler builtin ffs routines, therefore
102 * differs in spirit from the above ffz (man ffs).
103 */
104static inline int ffs(int x)
105{
106 int r;
107
108 __asm__("bsfl %1,%0\n\t"
109 "cmovzl %2,%0"
110 : "=r" (r) : "rm" (x), "r" (-1));
111 return r+1;
112}
113
114/**
115 * fls64 - find last bit set in 64 bit word
116 * @x: the word to search
117 *
118 * This is defined the same way as fls.
119 */
120static inline int fls64(__u64 x)
121{
122 if (x == 0)
123 return 0;
124 return __fls(x) + 1;
125}
126
127/**
128 * fls - find last bit set
129 * @x: the word to search
130 *
131 * This is defined the same way as ffs.
132 */
133static inline int fls(int x)
134{
135 int r;
136
137 __asm__("bsrl %1,%0\n\t"
138 "cmovzl %2,%0"
139 : "=&r" (r) : "rm" (x), "rm" (-1));
140 return r+1;
141}
142
143#define ARCH_HAS_FAST_MULTIPLIER 1
144
145#include <asm-generic/bitops/hweight.h>
146
147#endif /* __KERNEL__ */
148
149#ifdef __KERNEL__
150
151#include <asm-generic/bitops/ext2-non-atomic.h>
152
153#define ext2_set_bit_atomic(lock, nr, addr) \
154 test_and_set_bit((nr), (unsigned long *)(addr))
155#define ext2_clear_bit_atomic(lock, nr, addr) \
156 test_and_clear_bit((nr), (unsigned long *)(addr))
157
158#include <asm-generic/bitops/minix.h>
159
160#endif /* __KERNEL__ */
161
162#endif /* _X86_64_BITOPS_H */
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 40d54731de7e..48bde600a2db 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -112,4 +112,144 @@ static inline unsigned fls_long(unsigned long l)
112 return fls64(l); 112 return fls64(l);
113} 113}
114 114
115#ifdef __KERNEL__
116#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
117extern unsigned long __find_first_bit(const unsigned long *addr,
118 unsigned long size);
119
120/**
121 * find_first_bit - find the first set bit in a memory region
122 * @addr: The address to start the search at
123 * @size: The maximum size to search
124 *
125 * Returns the bit number of the first set bit.
126 */
127static __always_inline unsigned long
128find_first_bit(const unsigned long *addr, unsigned long size)
129{
130 /* Avoid a function call if the bitmap size is a constant */
131 /* and not bigger than BITS_PER_LONG. */
132
133 /* insert a sentinel so that __ffs returns size if there */
134 /* are no set bits in the bitmap */
135 if (__builtin_constant_p(size) && (size < BITS_PER_LONG))
136 return __ffs((*addr) | (1ul << size));
137
138 /* the result of __ffs(0) is undefined, so it needs to be */
139 /* handled separately */
140 if (__builtin_constant_p(size) && (size == BITS_PER_LONG))
141 return ((*addr) == 0) ? BITS_PER_LONG : __ffs(*addr);
142
143 /* size is not constant or too big */
144 return __find_first_bit(addr, size);
145}
146
147extern unsigned long __find_first_zero_bit(const unsigned long *addr,
148 unsigned long size);
149
150/**
151 * find_first_zero_bit - find the first cleared bit in a memory region
152 * @addr: The address to start the search at
153 * @size: The maximum size to search
154 *
155 * Returns the bit number of the first cleared bit.
156 */
157static __always_inline unsigned long
158find_first_zero_bit(const unsigned long *addr, unsigned long size)
159{
160 /* Avoid a function call if the bitmap size is a constant */
161 /* and not bigger than BITS_PER_LONG. */
162
163 /* insert a sentinel so that __ffs returns size if there */
164 /* are no set bits in the bitmap */
165 if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
166 return __ffs(~(*addr) | (1ul << size));
167 }
168
169 /* the result of __ffs(0) is undefined, so it needs to be */
170 /* handled separately */
171 if (__builtin_constant_p(size) && (size == BITS_PER_LONG))
172 return (~(*addr) == 0) ? BITS_PER_LONG : __ffs(~(*addr));
173
174 /* size is not constant or too big */
175 return __find_first_zero_bit(addr, size);
176}
177#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
178
179#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
180extern unsigned long __find_next_bit(const unsigned long *addr,
181 unsigned long size, unsigned long offset);
182
183/**
184 * find_next_bit - find the next set bit in a memory region
185 * @addr: The address to base the search on
186 * @offset: The bitnumber to start searching at
187 * @size: The bitmap size in bits
188 */
189static __always_inline unsigned long
190find_next_bit(const unsigned long *addr, unsigned long size,
191 unsigned long offset)
192{
193 unsigned long value;
194
195 /* Avoid a function call if the bitmap size is a constant */
196 /* and not bigger than BITS_PER_LONG. */
197
198 /* insert a sentinel so that __ffs returns size if there */
199 /* are no set bits in the bitmap */
200 if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
201 value = (*addr) & ((~0ul) << offset);
202 value |= (1ul << size);
203 return __ffs(value);
204 }
205
206 /* the result of __ffs(0) is undefined, so it needs to be */
207 /* handled separately */
208 if (__builtin_constant_p(size) && (size == BITS_PER_LONG)) {
209 value = (*addr) & ((~0ul) << offset);
210 return (value == 0) ? BITS_PER_LONG : __ffs(value);
211 }
212
213 /* size is not constant or too big */
214 return __find_next_bit(addr, size, offset);
215}
216
217extern unsigned long __find_next_zero_bit(const unsigned long *addr,
218 unsigned long size, unsigned long offset);
219
220/**
221 * find_next_zero_bit - find the next cleared bit in a memory region
222 * @addr: The address to base the search on
223 * @offset: The bitnumber to start searching at
224 * @size: The bitmap size in bits
225 */
226static __always_inline unsigned long
227find_next_zero_bit(const unsigned long *addr, unsigned long size,
228 unsigned long offset)
229{
230 unsigned long value;
231
232 /* Avoid a function call if the bitmap size is a constant */
233 /* and not bigger than BITS_PER_LONG. */
234
235 /* insert a sentinel so that __ffs returns size if there */
236 /* are no set bits in the bitmap */
237 if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
238 value = (~(*addr)) & ((~0ul) << offset);
239 value |= (1ul << size);
240 return __ffs(value);
241 }
242
243 /* the result of __ffs(0) is undefined, so it needs to be */
244 /* handled separately */
245 if (__builtin_constant_p(size) && (size == BITS_PER_LONG)) {
246 value = (~(*addr)) & ((~0ul) << offset);
247 return (value == 0) ? BITS_PER_LONG : __ffs(value);
248 }
249
250 /* size is not constant or too big */
251 return __find_next_zero_bit(addr, size, offset);
252}
253#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
254#endif /* __KERNEL__ */
115#endif 255#endif
diff --git a/lib/Kconfig b/lib/Kconfig
index 2d53dc092e8b..8cc8e8722a3f 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -7,6 +7,12 @@ menu "Library routines"
7config BITREVERSE 7config BITREVERSE
8 tristate 8 tristate
9 9
10config GENERIC_FIND_FIRST_BIT
11 def_bool n
12
13config GENERIC_FIND_NEXT_BIT
14 def_bool n
15
10config CRC_CCITT 16config CRC_CCITT
11 tristate "CRC-CCITT functions" 17 tristate "CRC-CCITT functions"
12 help 18 help
diff --git a/lib/Makefile b/lib/Makefile
index bf8000fc7d48..2d7001b7f5a4 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) += locking-selftest.o
29obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o 29obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
30lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o 30lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
31lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o 31lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
32lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o
32lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o 33lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
33obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o 34obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
34obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o 35obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
diff --git a/lib/find_next_bit.c b/lib/find_next_bit.c
index 78ccd73a8841..d3f5784807b4 100644
--- a/lib/find_next_bit.c
+++ b/lib/find_next_bit.c
@@ -16,14 +16,12 @@
16 16
17#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) 17#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
18 18
19/** 19#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
20 * find_next_bit - find the next set bit in a memory region 20/*
21 * @addr: The address to base the search on 21 * Find the next set bit in a memory region.
22 * @offset: The bitnumber to start searching at
23 * @size: The maximum size to search
24 */ 22 */
25unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 23unsigned long __find_next_bit(const unsigned long *addr,
26 unsigned long offset) 24 unsigned long size, unsigned long offset)
27{ 25{
28 const unsigned long *p = addr + BITOP_WORD(offset); 26 const unsigned long *p = addr + BITOP_WORD(offset);
29 unsigned long result = offset & ~(BITS_PER_LONG-1); 27 unsigned long result = offset & ~(BITS_PER_LONG-1);
@@ -60,15 +58,14 @@ found_first:
60found_middle: 58found_middle:
61 return result + __ffs(tmp); 59 return result + __ffs(tmp);
62} 60}
63 61EXPORT_SYMBOL(__find_next_bit);
64EXPORT_SYMBOL(find_next_bit);
65 62
66/* 63/*
67 * This implementation of find_{first,next}_zero_bit was stolen from 64 * This implementation of find_{first,next}_zero_bit was stolen from
68 * Linus' asm-alpha/bitops.h. 65 * Linus' asm-alpha/bitops.h.
69 */ 66 */
70unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 67unsigned long __find_next_zero_bit(const unsigned long *addr,
71 unsigned long offset) 68 unsigned long size, unsigned long offset)
72{ 69{
73 const unsigned long *p = addr + BITOP_WORD(offset); 70 const unsigned long *p = addr + BITOP_WORD(offset);
74 unsigned long result = offset & ~(BITS_PER_LONG-1); 71 unsigned long result = offset & ~(BITS_PER_LONG-1);
@@ -105,8 +102,64 @@ found_first:
105found_middle: 102found_middle:
106 return result + ffz(tmp); 103 return result + ffz(tmp);
107} 104}
105EXPORT_SYMBOL(__find_next_zero_bit);
106#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
107
108#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
109/*
110 * Find the first set bit in a memory region.
111 */
112unsigned long __find_first_bit(const unsigned long *addr,
113 unsigned long size)
114{
115 const unsigned long *p = addr;
116 unsigned long result = 0;
117 unsigned long tmp;
108 118
109EXPORT_SYMBOL(find_next_zero_bit); 119 while (size & ~(BITS_PER_LONG-1)) {
120 if ((tmp = *(p++)))
121 goto found;
122 result += BITS_PER_LONG;
123 size -= BITS_PER_LONG;
124 }
125 if (!size)
126 return result;
127
128 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
129 if (tmp == 0UL) /* Are any bits set? */
130 return result + size; /* Nope. */
131found:
132 return result + __ffs(tmp);
133}
134EXPORT_SYMBOL(__find_first_bit);
135
136/*
137 * Find the first cleared bit in a memory region.
138 */
139unsigned long __find_first_zero_bit(const unsigned long *addr,
140 unsigned long size)
141{
142 const unsigned long *p = addr;
143 unsigned long result = 0;
144 unsigned long tmp;
145
146 while (size & ~(BITS_PER_LONG-1)) {
147 if (~(tmp = *(p++)))
148 goto found;
149 result += BITS_PER_LONG;
150 size -= BITS_PER_LONG;
151 }
152 if (!size)
153 return result;
154
155 tmp = (*p) | (~0UL << size);
156 if (tmp == ~0UL) /* Are any bits zero? */
157 return result + size; /* Nope. */
158found:
159 return result + ffz(tmp);
160}
161EXPORT_SYMBOL(__find_first_zero_bit);
162#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
110 163
111#ifdef __BIG_ENDIAN 164#ifdef __BIG_ENDIAN
112 165