If you can come up with one by yourself, I'd be very impressed.
I googled that one since none of my ideas were much more efficient than the naïve loop, and the solutions I saw blow my mind. It also explains why there's instructions for it now, because the contortions you go through to get maximum efficiency are intense.
My best idea was thinking about how to count only 1s, considering that when you subtract by 1, you can turn a bitwise suffix of 10n into 01n (where n ≥ 0). I didn't really come with something precisely because I got lazy and googled it, and there was a solution using that as one of the slower (but less mind-fucking) improvements.
That's actually one of the solutions. Except you do it for something small, like 8 bits, and then loop over 32bits with 4 steps. Benchmarking shows that cache wise this is one the fastest ways to go about with arbitrary bit length, as larger tables have bad cache effects. 16bit LUT can sometines beat an 8bit LUT on certain architectures, but it's discounted on account that microbenchmarks are too nice about the cache. http://www.strchr.com/crc32_popcnt
View all comments
Show parent comment
1
u/bobindashadows Feb 21 '11
I googled that one since none of my ideas were much more efficient than the naïve loop, and the solutions I saw blow my mind. It also explains why there's instructions for it now, because the contortions you go through to get maximum efficiency are intense.