typedef unsigned long int li; // Reversing byte order in a 64 bit word. li rev(li x){li a = x<<32 | x>>32, m = 0xffff0000ffffL, b = (a&m)<<16 | (a&~m)>>16, n = 0x00ff00ff00ff00ffL; return (b&n)<<8 | (b&~n)>>8;} // gcc thinks that instruction bswapq does the whole job. // Now (2017 Sept) clang thinks so too. // Indeed it seems to! /* demo & test */ #include li ran64(){return (li)random() ^ ((li)random()<<16) ^ ((li)random()<<33);} #include int main(){printf("%016lX\n", rev(0x2201020304050607L)); {int j=100000000; while(j--){li o = ran64(); if(rev(o) != __builtin_bswap64(o)) printf("%016lX\n", o);}} return 0;} // 2.4 sec per 10^8 with clang -O3 // 1.9 sec per 10^8 with gcc -O3 // -> 0706050403020122