#include <stdio.h>
#include <unistd.h>
#define set_bit(nr,addr) ({\
register int res;\
__asm__ __volatile__("btsl %2,%3\n\t setb %%al"\
:"=a"(res) :"0" (0),"r"(nr),"m"(*(addr)));\
res;})
#define clear_bit(nr,addr) ({\
register int res;\
__asm__ __volatile__("btrl %2,%3\n\t setnb %%al"\
:"=a"(res):"0"(0),"r"(nr),"m"(*(addr)));\
res;})
#define find_first_zero(addr) ({\
int __res;\
__asm__ __volatile__("cld\n"\
"1:\tlodsl\n\t"\
"notl %%eax\n\t"\
"bsfl %%eax,%%edx\n\t"\
"je 2f\n\t"\
"addl %%edx,%%ecx\n\t"\
"jmp 3f\n"\
"2:\t addl $32,%%ecx\n\t"\
"cmpl $8192,%%ecx\n\t"\
"jl 1b\n"\
"3:"\
:"=c"(__res):"c"(0),"S"(addr));\
__res;})
int main()
{
unsigned long addr = 8192;
/*test set_bit*/
printf("**********test set_bit:\n");
printf("addr %lu(%x)\n",addr,addr);
printf("%d\n",set_bit(3,&addr));
printf("addr:%lu(%x)\n",addr,addr);
/*test clear_bit*/
printf("***********test clear_bit:\n");
printf("addr %lu(%x)\n",addr,addr);
printf("%d\n",clear_bit(4,&addr));
printf("addr:%lu(%x)\n",addr,addr);
printf("************test find_first_zero:\n");
addr = 0xffff;
printf("addr:%d\n",find_first_zero(&addr));
return 0;
}
|