彙編:bsfl 指令

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

注意, 操作數位0時, 得到的結果0是沒有意義的. 也就是不應該對操作數0作用bsfl指令

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章