A small repository with examples of how one can count set bits in an integer.
If you just want to get a gist of different methods of computing the set bits, just navigate to the code folder and look into .cpp files.
A detailed discussion of the algorithms' performance can be found in this post.
- C++20-capable compiler (mainly because of
std::popcountfunction fromstd, all the rest can be compiled withC++17) - With
AVX512option:avx512vpopcntdq- andpopcnt-capable hardware - With
POPCNToption:popcnt-capable hardware CMakever.>= 3.16
Run CMake to generate project files. You can optionally pass the following arguments:
-DPOPCNT=ON: enables using thepopcntinstruction by the compiler-DAVX512=ON: enables vectorizedpopcnt, also enables the usual one (POPCNTflag is not necessary)
Tested on x64 processor architecture with
gcc-10.3.0clang-13.0.1msvc-19.34.31937.0