constrt_uint8_t __lowest_bit_bitmap[]={/* 00 */0,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 10 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 20 */5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 30 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 40 */6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 50 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 60 */5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 70 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 80 */7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* 90 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* A0 */5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* B0 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* C0 */6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* D0 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* E0 */5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,/* F0 */4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};/**
* This function finds the first bit set (beginning with the least significant bit)
* in value and return the index of that bit.
*
* Bits are numbered starting at 1 (the least significant bit). A return value of
* zero from any of these functions means that the argument was zero.
*
* @return return the index of the first bit set. If value is 0, then this function
* shall return 0.
*/int__rt_ffs(int value){if(value ==0)return0;if(value &0xff)return __lowest_bit_bitmap[value &0xff]+1;if(value &0xff00)return __lowest_bit_bitmap[(value &0xff00)>>8]+9;if(value &0xff0000)return __lowest_bit_bitmap[(value &0xff0000)>>16]+17;return __lowest_bit_bitmap[(value &0xff000000)>>24]+25;}
/**
* This function finds the first bit set (beginning with the least significant bit)
* in value and return the index of that bit.
*
* Bits are numbered starting at 1 (the least significant bit). A return value of
* zero from any of these functions means that the argument was zero.
*
* @return return the index of the first bit set. If value is 0, then this function
* shall return 0.
*/#ifdefined(__CC_ARM)||defined(__CLANG_ARM)
__asm int__rt_ffs(int value){// 判断value是否为0,若为0则直接返回
CMP r0, #0x00
BEQ exit
RBIT r0, r0 // 反转value的高低位
CLZ r0, r0 // 计算value低bit位为0的个数
ADDS r0, r0, #0x01// 以上得到的结果+1,则得到为1的最低bit索引
exit
BX lr
}#elifdefined(__IAR_SYSTEMS_ICC__)int__rt_ffs(int value){if(value ==0)return value;asm("RBIT %0, %1":"=r"(value):"r"(value));asm("CLZ %0, %1":"=r"(value):"r"(value));asm("ADDS %0, %1, #0x01":"=r"(value):"r"(value));return value;}#elifdefined(__GNUC__)int__rt_ffs(int value){return__builtin_ffs(value);}#endif