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):0num:2, first non-0 bit index(from low bit to high bit, start 0):1num:4, first non-0 bit index(from low bit to high bit, start 0):2num:8, first non-0 bit index(from low bit to high bit, start 0):3num:16, first non-0 bit index(from low bit to high bit, start 0):4num:32, first non-0 bit index(from low bit to high bit, start 0):5num:64, first non-0 bit index(from low bit to high bit, start 0):6num:128, first non-0 bit index(from low bit to high bit, start 0):7num:256, first non-0 bit index(from low bit to high bit, start 0):8num:512, first non-0 bit index(from low bit to high bit, start 0):9num:1024, first non-0 bit index(from low bit to high bit, start 0):10num:2048, first non-0 bit index(from low bit to high bit, start 0):11num:4096, first non-0 bit index(from low bit to high bit, start 0):12num:8192, first non-0 bit index(from low bit to high bit, start 0):13num:16384, first non-0 bit index(from low bit to high bit, start 0):14num:32768, first non-0 bit index(from low bit to high bit, start 0):15num:65536, first non-0 bit index(from low bit to high bit, start 0):16num:131072, first non-0 bit index(from low bit to high bit, start 0):17num:262144, first non-0 bit index(from low bit to high bit, start 0):18num:524288, first non-0 bit index(from low bit to high bit, start 0):19num:1048576, first non-0 bit index(from low bit to high bit, start 0):20num:2097152, first non-0 bit index(from low bit to high bit, start 0):21num:4194304, first non-0 bit index(from low bit to high bit, start 0):22num:8388608, first non-0 bit index(from low bit to high bit, start 0):23num:16777216, first non-0 bit index(from low bit to high bit, start 0):24num:33554432, first non-0 bit index(from low bit to high bit, start 0):25num:67108864, first non-0 bit index(from low bit to high bit, start 0):26num:134217728, first non-0 bit index(from low bit to high bit, start 0):27num:268435456, first non-0 bit index(from low bit to high bit, start 0):28num:536870912, first non-0 bit index(from low bit to high bit, start 0):29num:1073741824, first non-0 bit index(from low bit to high bit, start 0):30num:2147483648, first non-0 bit index(from low bit to high bit, start 0):31num:0, first non-0 bit index(from low bit to high bit, start 0):0
注意, 運算元位0時, 得到的結果0是沒有意義的. 也就是不應該對運算元0作用bsfl指令