Best method found: Bit group moving (about 39 cycles on superscalar processors):
x = (x & 0x20200000) | ((x & 0x00000011) << 1) | ((x & 0x00000008) << 3) | ((x & 0x00004000) << 5) | ((x & 0x00020000) << 6) | ((x & 0x00000800) << 9) | ((x & 0x00400000) >> 19) | ((x & 0x00000002) << 14) | ((x & 0x01000000) >> 17) | rol(x & 0x84000100, 17) | ((x & 0x00003000) << 18) | ((x & 0x40000000) >> 12) | rol(x & 0x08180040, 22) | ((x & 0x00000004) << 24) | ((x & 0x00000080) >> 7) | ((x & 0x00000400) >> 6) | ((x & 0x00040000) >> 5) | ((x & 0x00010000) >> 4) | ((x & 0x00000020) >> 3) | ((x & 0x12808200) >> 1);
See documentation to
pext and pdep can be emulated with compress_right and expand_right.
This result is not necessarily the best possible, but at least several methods have been challenged. The given cycles are only estimated and may vary significantly depending on the used processor. Thus, the selected method might not be the best one for your application. You can however influence the choice by using the options above.
See also some notes on the inner workings.
There is an even better calculator calcperm.* which is usable for various word sizes (Pascal and C++ sources).
Error reports, comments or questions? E-mail: email@example.com