aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-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
15 files changed, 378 insertions, 341 deletions
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