forked from boostorg/core
Add popcount
This commit is contained in:
@ -100,7 +100,6 @@ BOOST_CORE_BIT_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
BOOST_CORE_BIT_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT
|
BOOST_CORE_BIT_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -164,8 +163,43 @@ BOOST_CORE_BIT_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT
|
|||||||
return boost::core::countr_zero( static_cast<T>( ~x ) );
|
return boost::core::countr_zero( static_cast<T>( ~x ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint32_t x ) BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
x = x - ( ( x >> 1 ) & 0x55555555 );
|
||||||
|
x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
|
||||||
|
x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F;
|
||||||
|
|
||||||
|
return static_cast<unsigned>( ( x * 0x01010101 ) >> 24 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint64_t x ) BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
x = x - ( ( x >> 1 ) & 0x5555555555555555 );
|
||||||
|
x = ( x & 0x3333333333333333 ) + ( ( x >> 2 ) & 0x3333333333333333 );
|
||||||
|
x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F0F0F0F0F;
|
||||||
|
|
||||||
|
return static_cast<unsigned>( ( x * 0x0101010101010101 ) >> 56 );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
BOOST_CORE_BIT_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT;
|
BOOST_CORE_BIT_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) );
|
||||||
|
|
||||||
|
if( sizeof(T) <= sizeof(boost::uint32_t) )
|
||||||
|
{
|
||||||
|
return boost::core::detail::popcount_impl( static_cast<boost::uint32_t>( x ) );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return boost::core::detail::popcount_impl( static_cast<boost::uint64_t>( x ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rotating
|
// rotating
|
||||||
|
|
||||||
|
@ -230,6 +230,7 @@ run bit_width_test.cpp ;
|
|||||||
run has_single_bit_test.cpp ;
|
run has_single_bit_test.cpp ;
|
||||||
run bit_floor_test.cpp ;
|
run bit_floor_test.cpp ;
|
||||||
run bit_ceil_test.cpp ;
|
run bit_ceil_test.cpp ;
|
||||||
|
run bit_popcount_test.cpp ;
|
||||||
|
|
||||||
use-project /boost/core/swap : ./swap ;
|
use-project /boost/core/swap : ./swap ;
|
||||||
build-project ./swap ;
|
build-project ./swap ;
|
||||||
|
46
test/bit_popcount_test.cpp
Normal file
46
test/bit_popcount_test.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Test for boost/core/bit.hpp (bit_ceil)
|
||||||
|
//
|
||||||
|
// Copyright 2020 Peter Dimov
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/core/bit.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/core/detail/splitmix64.hpp>
|
||||||
|
#include <boost/cstdint.hpp>
|
||||||
|
#include <limits>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
template<class T> void test_popcount( T x )
|
||||||
|
{
|
||||||
|
int k = 0;
|
||||||
|
for( T y = x; y; y &= y - 1, ++k );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( boost::core::popcount( x ), k ) || ( std::cerr << "x: " << +x << std::endl );
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
test_popcount( static_cast<unsigned char>( 0 ) );
|
||||||
|
test_popcount( static_cast<unsigned short>( 0 ) );
|
||||||
|
test_popcount( static_cast<unsigned int>( 0 ) );
|
||||||
|
test_popcount( static_cast<unsigned long>( 0 ) );
|
||||||
|
test_popcount( static_cast<unsigned long long>( 0 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::detail::splitmix64 rng;
|
||||||
|
|
||||||
|
for( int i = 0; i < 1000; ++i )
|
||||||
|
{
|
||||||
|
boost::uint64_t x = rng();
|
||||||
|
|
||||||
|
test_popcount( static_cast<unsigned char>( x ) );
|
||||||
|
test_popcount( static_cast<unsigned short>( x ) );
|
||||||
|
test_popcount( static_cast<unsigned int>( x ) );
|
||||||
|
test_popcount( static_cast<unsigned long>( x ) );
|
||||||
|
test_popcount( static_cast<unsigned long long>( x ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
template<class T> void test_single_bit( T x )
|
template<class T> void test_single_bit( T x )
|
||||||
{
|
{
|
||||||
// BOOST_TEST_EQ( boost::core::has_single_bit( x ), boost::core::popcount( x ) == 1 );
|
BOOST_TEST_EQ( boost::core::has_single_bit( x ), boost::core::popcount( x ) == 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
Reference in New Issue
Block a user