1 #include <linux/bitops.h>
3 #undef find_first_zero_bit
4 #undef find_next_zero_bit
9 __find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
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.
30 " xorq -8(%%rdi),%%rax\n"
33 "1: subq %[addr],%%rdi\n"
36 :"=d" (res
), "=&c" (d0
), "=&D" (d1
), "=&a" (d2
)
37 :"0" (0ULL), "1" (size
), "2" (addr
), "3" (-1ULL),
38 [addr
] "S" (addr
) : "memory");
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.
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
52 * Returns the bit-number of the first zero bit, not the number of the byte
55 long find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
57 return __find_first_zero_bit (addr
, size
);
61 * find_next_zero_bit - find the first 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
66 long find_next_zero_bit (const unsigned long * addr
, long size
, long offset
)
68 const unsigned long * p
= addr
+ (offset
>> 6);
69 unsigned long set
= 0;
70 unsigned long res
, bit
= offset
&63;
74 * Look for zero in first word
79 : "r" (~(*p
>> bit
)), "r"(64L));
86 * No zero yet, search remaining full words for a zero
88 res
= __find_first_zero_bit (p
, size
- 64 * (p
- addr
));
90 return (offset
+ set
+ res
);
94 __find_first_bit(const unsigned long * addr
, unsigned long size
)
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.
116 " bsfq (%%rdi),%%rax\n"
117 "1: subq %[addr],%%rdi\n"
120 :"=a" (res
), "=&c" (d0
), "=&D" (d1
)
121 :"0" (0ULL), "1" (size
), "2" (addr
),
122 [addr
] "r" (addr
) : "memory");
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
131 * Returns the bit-number of the first set bit, not the number of the byte
134 long find_first_bit(const unsigned long * addr
, unsigned long size
)
136 return __find_first_bit(addr
,size
);
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
145 long find_next_bit(const unsigned long * addr
, long size
, long offset
)
147 const unsigned long * p
= addr
+ (offset
>> 6);
148 unsigned long set
= 0, bit
= offset
& 63, res
;
152 * Look for nonzero in the first 64 bits:
157 : "r" (*p
>> bit
), "r" (64L));
158 if (set
< (64 - bit
))
164 * No set bit yet, search remaining full words for a bit
166 res
= __find_first_bit (p
, size
- 64 * (p
- addr
));
167 return (offset
+ set
+ res
);
170 #include <linux/module.h>
172 EXPORT_SYMBOL(find_next_bit
);
173 EXPORT_SYMBOL(find_first_bit
);
174 EXPORT_SYMBOL(find_first_zero_bit
);
175 EXPORT_SYMBOL(find_next_zero_bit
);