initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / x86_64 / lib / bitops.c
blob6060dd9084aae7bf4b74d30b3254835fe25f454e
1 #include <linux/module.h>
2 #include <asm/bitops.h>
4 #undef find_first_zero_bit
5 #undef find_next_zero_bit
6 #undef find_first_bit
7 #undef find_next_bit
9 /**
10 * find_first_zero_bit - find the first zero bit in a memory region
11 * @addr: The address to start the search at
12 * @size: The maximum size to search
14 * Returns the bit-number of the first zero bit, not the number of the byte
15 * containing a bit.
17 inline long find_first_zero_bit(const unsigned long * addr, unsigned long size)
19 long d0, d1, d2;
20 long res;
22 if (!size)
23 return 0;
24 asm volatile(
25 " repe; scasq\n"
26 " je 1f\n"
27 " xorq -8(%%rdi),%%rax\n"
28 " subq $8,%%rdi\n"
29 " bsfq %%rax,%%rdx\n"
30 "1: subq %[addr],%%rdi\n"
31 " shlq $3,%%rdi\n"
32 " addq %%rdi,%%rdx"
33 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
34 :"0" (0ULL), "1" ((size + 63) >> 6), "2" (addr), "3" (-1ULL),
35 [addr] "r" (addr) : "memory");
36 return res;
39 /**
40 * find_next_zero_bit - find the first zero bit in a memory region
41 * @addr: The address to base the search on
42 * @offset: The bitnumber to start searching at
43 * @size: The maximum size to search
45 long find_next_zero_bit (const unsigned long * addr, long size, long offset)
47 unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
48 unsigned long set = 0;
49 unsigned long res, bit = offset&63;
51 if (bit) {
53 * Look for zero in first word
55 asm("bsfq %1,%0\n\t"
56 "cmoveq %2,%0"
57 : "=r" (set)
58 : "r" (~(*p >> bit)), "r"(64L));
59 if (set < (64 - bit))
60 return set + offset;
61 set = 64 - bit;
62 p++;
65 * No zero yet, search remaining full words for a zero
67 res = find_first_zero_bit ((const unsigned long *)p,
68 size - 64 * (p - (unsigned long *) addr));
69 return (offset + set + res);
72 static inline long
73 __find_first_bit(const unsigned long * addr, unsigned long size)
75 long d0, d1;
76 long res;
78 asm volatile(
79 " repe; scasq\n"
80 " jz 1f\n"
81 " subq $8,%%rdi\n"
82 " bsfq (%%rdi),%%rax\n"
83 "1: subq %[addr],%%rdi\n"
84 " shlq $3,%%rdi\n"
85 " addq %%rdi,%%rax"
86 :"=a" (res), "=&c" (d0), "=&D" (d1)
87 :"0" (0ULL),
88 "1" ((size + 63) >> 6), "2" (addr),
89 [addr] "r" (addr) : "memory");
90 return res;
93 /**
94 * find_first_bit - find the first set bit in a memory region
95 * @addr: The address to start the search at
96 * @size: The maximum size to search
98 * Returns the bit-number of the first set bit, not the number of the byte
99 * containing a bit.
101 long find_first_bit(const unsigned long * addr, unsigned long size)
103 return __find_first_bit(addr,size);
107 * find_next_bit - find the first set bit in a memory region
108 * @addr: The address to base the search on
109 * @offset: The bitnumber to start searching at
110 * @size: The maximum size to search
112 long find_next_bit(const unsigned long * addr, long size, long offset)
114 const unsigned long * p = addr + (offset >> 6);
115 unsigned long set = 0, bit = offset & 63, res;
117 if (bit) {
119 * Look for nonzero in the first 64 bits:
121 asm("bsfq %1,%0\n\t"
122 "cmoveq %2,%0\n\t"
123 : "=r" (set)
124 : "r" (*p >> bit), "r" (64L));
125 if (set < (64 - bit))
126 return set + offset;
127 set = 64 - bit;
128 p++;
131 * No set bit yet, search remaining full words for a bit
133 res = __find_first_bit (p, size - 64 * (p - addr));
134 return (offset + set + res);
137 EXPORT_SYMBOL(find_next_bit);
138 EXPORT_SYMBOL(find_first_bit);
139 EXPORT_SYMBOL(find_first_zero_bit);
140 EXPORT_SYMBOL(find_next_zero_bit);