AT&T汇编语法:
bsfl op1 op2
含义
扫描操作数op1,找到到第1个非0bit位, 把非0bit位的索引下标(从0计算)存入op2. 扫描从低位到高位扫描(也就是从右->左)
举例:
操作数:1 第一个非0bit的位置 0
操作数:2 第一个非0bit的位置 1
操作数:4 第一个非0bit的位置 2
......
适用场景举例1
linux内核在进程调用算法中,使用了数组来维护进程的优先级列表, 数组有140个元素, 每个元素对应了具有相同优先级的进程列表. 为数组维护了一个bitmap,当某个优先级别上有进程被插入列表时,相应的比特位就被置位。
sched_find_first_bit函数将利用bsfl指令找到第一个非空的数组元素, 也就是当前最高优先级的进程所在的链表.
适用场景举例2
dlmalloc在管理小内存块的smallbin数组时, 也是为数组维护了对应的bitmap, 以便加快查找哪个bin中非空.
#define compute_bit2idx(X, I)\
{\
unsigned int J;\
J = __builtin_ctz(X); \
I = (bindex_t)J;\
}
__builtin_ctz(统计低位0的个数)
测试代码:
#include <stdio.h>
int main()
{
int len = 0;
unsigned int num = 1;
for (int i=0; i<32; i++)
{
__asm__ ("bsfl %1, %0":"=r"(len):"r"(num):);
printf("num:%u, first non-0 bit index(from low bit to high bit, start 0):%d\n", num, len);
num <<= 1;
}
// warning
num = 0;
__asm__ ("bsfl %1, %0":"=r"(len):"r"(num):);
printf("num:%u, first non-0 bit index(from low bit to high bit, start 0):%d\n", num, len);
return 0;
}
num:1, first non-0 bit index(from low bit to high bit, start 0):0
num:2, first non-0 bit index(from low bit to high bit, start 0):1
num:4, first non-0 bit index(from low bit to high bit, start 0):2
num:8, first non-0 bit index(from low bit to high bit, start 0):3
num:16, first non-0 bit index(from low bit to high bit, start 0):4
num:32, first non-0 bit index(from low bit to high bit, start 0):5
num:64, first non-0 bit index(from low bit to high bit, start 0):6
num:128, first non-0 bit index(from low bit to high bit, start 0):7
num:256, first non-0 bit index(from low bit to high bit, start 0):8
num:512, first non-0 bit index(from low bit to high bit, start 0):9
num:1024, first non-0 bit index(from low bit to high bit, start 0):10
num:2048, first non-0 bit index(from low bit to high bit, start 0):11
num:4096, first non-0 bit index(from low bit to high bit, start 0):12
num:8192, first non-0 bit index(from low bit to high bit, start 0):13
num:16384, first non-0 bit index(from low bit to high bit, start 0):14
num:32768, first non-0 bit index(from low bit to high bit, start 0):15
num:65536, first non-0 bit index(from low bit to high bit, start 0):16
num:131072, first non-0 bit index(from low bit to high bit, start 0):17
num:262144, first non-0 bit index(from low bit to high bit, start 0):18
num:524288, first non-0 bit index(from low bit to high bit, start 0):19
num:1048576, first non-0 bit index(from low bit to high bit, start 0):20
num:2097152, first non-0 bit index(from low bit to high bit, start 0):21
num:4194304, first non-0 bit index(from low bit to high bit, start 0):22
num:8388608, first non-0 bit index(from low bit to high bit, start 0):23
num:16777216, first non-0 bit index(from low bit to high bit, start 0):24
num:33554432, first non-0 bit index(from low bit to high bit, start 0):25
num:67108864, first non-0 bit index(from low bit to high bit, start 0):26
num:134217728, first non-0 bit index(from low bit to high bit, start 0):27
num:268435456, first non-0 bit index(from low bit to high bit, start 0):28
num:536870912, first non-0 bit index(from low bit to high bit, start 0):29
num:1073741824, first non-0 bit index(from low bit to high bit, start 0):30
num:2147483648, first non-0 bit index(from low bit to high bit, start 0):31
num:0, first non-0 bit index(from low bit to high bit, start 0):0