#include #include inline bool bittest_gcc(unsigned x, int c) { return x & (1 << c); } inline bool bittest_asm(unsigned x, int c) { bool res; __asm__ __volatile__( "btl %1,%2\n" "setb %0" :"=q"(res) :"r"(c), "r"(x) ); return res; } volatile bool res; int main(void) { for(unsigned n=0; n<32; ++n) if(bittest_gcc(0x12345678,n) != bittest_asm(0x12345678, n)) std::fprintf(stderr, "sanity test fails\n"); { clock_t iclock = std::clock(); unsigned counter = 0; while(std::clock() - iclock < 4*CLOCKS_PER_SEC) { for(unsigned n=0; n<100; ++n) res = bittest_gcc(counter, n&31); ++counter; } std::printf("gcc test: %llu loops\n", counter*100ULL); } { clock_t iclock = std::clock(); unsigned counter = 0; while(std::clock() - iclock < 4*CLOCKS_PER_SEC) { for(unsigned n=0; n<100; ++n) res = bittest_asm(counter, n&31); ++counter; } std::printf("asm test: %llu loops\n", counter*100ULL); } }