Skip to content

Commit cd85c8b

Browse files
rostedtLinus Torvalds
authored andcommitted
[PATCH] speed up on find_first_bit for i386 (let compiler do the work)
Avoid using "rep scas", just let the compiler select a sequence of regular instructions. Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 79a8810 commit cd85c8b

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

‎include/asm-i386/bitops.h‎

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
310310
*/
311311
int find_next_zero_bit(const unsigned long *addr, int size, int offset);
312312

313+
/**
314+
* __ffs - find first bit in word.
315+
* @word: The word to search
316+
*
317+
* Undefined if no bit exists, so code should check against 0 first.
318+
*/
319+
static inline unsigned long __ffs(unsigned long word)
320+
{
321+
__asm__("bsfl %1,%0"
322+
:"=r" (word)
323+
:"rm" (word));
324+
return word;
325+
}
326+
313327
/**
314328
* find_first_bit - find the first set bit in a memory region
315329
* @addr: The address to start the search at
@@ -320,22 +334,16 @@ int find_next_zero_bit(const unsigned long *addr, int size, int offset);
320334
*/
321335
static inline int find_first_bit(const unsigned long *addr, unsigned size)
322336
{
323-
int d0, d1;
324-
int res;
325-
326-
/* This looks at memory. Mark it volatile to tell gcc not to move it around */
327-
__asm__ __volatile__(
328-
"xorl %%eax,%%eax\n\t"
329-
"repe; scasl\n\t"
330-
"jz 1f\n\t"
331-
"leal -4(%%edi),%%edi\n\t"
332-
"bsfl (%%edi),%%eax\n"
333-
"1:\tsubl %%ebx,%%edi\n\t"
334-
"shll $3,%%edi\n\t"
335-
"addl %%edi,%%eax"
336-
:"=a" (res), "=&c" (d0), "=&D" (d1)
337-
:"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
338-
return res;
337+
int x = 0;
338+
do {
339+
if (*addr)
340+
return __ffs(*addr) + x;
341+
addr++;
342+
if (x >= size)
343+
break;
344+
x += (sizeof(*addr)<<3);
345+
} while (1);
346+
return x;
339347
}
340348

341349
/**
@@ -360,20 +368,6 @@ static inline unsigned long ffz(unsigned long word)
360368
return word;
361369
}
362370

363-
/**
364-
* __ffs - find first bit in word.
365-
* @word: The word to search
366-
*
367-
* Undefined if no bit exists, so code should check against 0 first.
368-
*/
369-
static inline unsigned long __ffs(unsigned long word)
370-
{
371-
__asm__("bsfl %1,%0"
372-
:"=r" (word)
373-
:"rm" (word));
374-
return word;
375-
}
376-
377371
/*
378372
* fls: find last bit set.
379373
*/

0 commit comments

Comments
 (0)