#include <cstdio>
#include <array>
#include <cstdint>
#include <utility>
#include <limits>
namespace detail {
constexpr std::uint8_t popcnt_8(std::uint8_t number) {
number -= ((number >> 1) & 0x55);
number = (number & 0x33) + ((number >> 2) & 0x33);
return ((number + (number >> 4)) & 0x0F);
}
template<std::size_t... Indices>
constexpr std::array<std::uint8_t, sizeof...(Indices)>
make_lookup_impl(std::index_sequence<Indices...>) {
return { popcnt_8(Indices)... };
}
} /* detail */
constexpr decltype(auto) make_lookup() {
constexpr std::size_t uint8_max = std::numeric_limits<std::uint8_t>::max();
return detail::make_lookup_impl(std::make_index_sequence<uint8_max + 1>{});
}
int main() {
const auto lookup = make_lookup();
const auto test = std::numeric_limits<std::uint64_t>::max();
std::size_t popcount = 0;
for(std::size_t i = 0; i < sizeof(test); ++i)
popcount += lookup.at(*(reinterpret_cast<const std::uint8_t*>(&test) + i));
printf("-- %d\n", popcount);
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGFycmF5PgojaW5jbHVkZSA8Y3N0ZGludD4KI2luY2x1ZGUgPHV0aWxpdHk+CiNpbmNsdWRlIDxsaW1pdHM+CgpuYW1lc3BhY2UgZGV0YWlsIHsKY29uc3RleHByIHN0ZDo6dWludDhfdCBwb3BjbnRfOChzdGQ6OnVpbnQ4X3QgbnVtYmVyKSB7CiAgICBudW1iZXIgLT0gKChudW1iZXIgPj4gMSkgJiAweDU1KTsKICAgIG51bWJlciA9IChudW1iZXIgJiAweDMzKSArICgobnVtYmVyID4+IDIpICYgMHgzMyk7CiAgICByZXR1cm4gKChudW1iZXIgKyAobnVtYmVyID4+IDQpKSAmIDB4MEYpOwp9Cgp0ZW1wbGF0ZTxzdGQ6OnNpemVfdC4uLiBJbmRpY2VzPgpjb25zdGV4cHIgc3RkOjphcnJheTxzdGQ6OnVpbnQ4X3QsIHNpemVvZi4uLihJbmRpY2VzKT4KbWFrZV9sb29rdXBfaW1wbChzdGQ6OmluZGV4X3NlcXVlbmNlPEluZGljZXMuLi4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