彙編:bsfl 指令

來源:互聯網
上載者:User

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指令

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.