查找第一位,使用 bsf、blf 查找最后一位程序集
Find first bit, find last bit assembly using bsf, blf
我正在查看这段代码:
http://lxr.free-electrons.com/source/arch/x86/include/asm/bitops.h
static inline unsigned long __ffs(unsigned long word)
{
asm("rep; bsf %1,%0"
: "=r" (word)
: "rm" (word));
return word;
}
为什么bsf指令前面有一个"rep"?为什么 __fls
不是这种情况?
在支持它的处理器上将 bfs
变成 tzcnt
是一种技巧。不过,它肯定需要在代码中添加注释。引用指令集参考:
0F BC /r BSF r32, r/m32
F3 0F BC /r TZCNT r32, r/m32
TZCNT counts the number of trailing least significant zero bits in
source operand (second operand) and returns the result in destination
operand (first operand). TZCNT is an extension of the BSF instruction.
The key difference between TZCNT and BSF instruction is that TZCNT
provides operand size as output when source operand is zero while in
the case of BSF instruction, if source operand is zero, the content of
destination operand are undefined. On processors that do not support
TZCNT, the instruction byte encoding is executed as BSF.
(REP
前缀当然是F3
。)
我正在查看这段代码:
http://lxr.free-electrons.com/source/arch/x86/include/asm/bitops.h
static inline unsigned long __ffs(unsigned long word)
{
asm("rep; bsf %1,%0"
: "=r" (word)
: "rm" (word));
return word;
}
为什么bsf指令前面有一个"rep"?为什么 __fls
不是这种情况?
在支持它的处理器上将 bfs
变成 tzcnt
是一种技巧。不过,它肯定需要在代码中添加注释。引用指令集参考:
0F BC /r BSF r32, r/m32
F3 0F BC /r TZCNT r32, r/m32
TZCNT counts the number of trailing least significant zero bits in source operand (second operand) and returns the result in destination operand (first operand). TZCNT is an extension of the BSF instruction. The key difference between TZCNT and BSF instruction is that TZCNT provides operand size as output when source operand is zero while in the case of BSF instruction, if source operand is zero, the content of destination operand are undefined. On processors that do not support TZCNT, the instruction byte encoding is executed as BSF.
(REP
前缀当然是F3
。)