Hasty Briefsbeta

Pop Goes the Population Count?

2 days ago
  • #compiler-optimizations
  • #bit-manipulation
  • #performance
  • The 'population count' operation counts the number of one bits in a number and is useful in various fields like data compression, cryptography, and chess.
  • A simple C implementation can loop through each bit or use a trick to clear the bottom set bit by ANDing the value with itself decremented.
  • Compilers can optimize the population count loop into a single instruction (popcnt) when targeting modern architectures like Westmere.
  • Using standard C++ routines like std::popcount ensures optimal performance and clarity of intent.
  • Some architectures require workarounds for performance bugs, like inserting a redundant XOR before popcnt on Sandy Bridge.
  • ARM and RISC-V also have similar instructions for population count (popcount and cpop).