1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi"). 10 #include <type_traits> 20 {
return bits.count(); }
23 template<
class Un
signed >
27 static_assert( std::is_unsigned_v<Unsigned> );
28 return bitset<bits_per_<Unsigned>>( x ).count();
31 #if 0 // Possible optimizations, probably not needed 32 #if defined( _MSC_VER ) 34 inline auto sum_of_bits(
const uint16_t x ) noexcept
36 {
return __popcnt16( x ); }
38 inline auto sum_of_bits(
const uint32_t x ) noexcept
40 {
return __popcnt( x ); }
42 inline auto sum_of_bits(
const uint64_t x ) noexcept
44 {
return __popcnt64( x ); }
46 #elif defined( __GNUC__ ) 51 template<
class Un
signed >
52 inline auto gcc_sum_of_bits(
const Unsigned x ) noexcept -> int;
54 template<>
inline auto gcc_sum_of_bits<unsigned>(
const unsigned x ) noexcept
56 {
return __builtin_popcount( x ); }
58 template<>
inline auto gcc_sum_of_bits<unsigned long>(
const unsigned long x ) noexcept
60 {
return __builtin_popcountl( x ); }
62 template<>
inline auto gcc_sum_of_bits<unsigned long long>(
const unsigned long long x ) noexcept
64 {
return __builtin_popcountll( x ); }
68 inline auto sum_of_bits(
const uint16_t x ) noexcept
70 {
return impl::gcc_sum_of_bits<uint32_t>( x ); }
72 inline auto sum_of_bits(
const uint32_t x ) noexcept
74 {
return impl::gcc_sum_of_bits( x ); }
76 inline auto sum_of_bits(
const uint64_t x ) noexcept
78 {
return impl::gcc_sum_of_bits( x ); }
auto sum_of_bits(const bitset< n > &bits) noexcept -> int
The number of 1-bits in a std::bitset.
CPPX_USE_CPPX(bits_per_, magnitude_bits_per_)
bits_per_ and magnitude_bits_per_, plus the for-readability constant bits_per_byte.
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...