Use __builtin_popcount

This commit is contained in:
Peter Dimov
2020-12-29 02:06:56 +02:00
parent e8aa0c75b4
commit 919b98d425

View File

@ -327,6 +327,46 @@ BOOST_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT
// popcount
#if defined(__GNUC__) || defined(__clang__)
namespace detail
{
BOOST_CONSTEXPR inline int popcount_impl( unsigned char x ) BOOST_NOEXCEPT
{
return __builtin_popcount( x );
}
BOOST_CONSTEXPR inline int popcount_impl( unsigned short x ) BOOST_NOEXCEPT
{
return __builtin_popcount( x );
}
BOOST_CONSTEXPR inline int popcount_impl( unsigned int x ) BOOST_NOEXCEPT
{
return __builtin_popcount( x );
}
BOOST_CONSTEXPR inline int popcount_impl( unsigned long x ) BOOST_NOEXCEPT
{
return __builtin_popcountl( x );
}
BOOST_CONSTEXPR inline int popcount_impl( unsigned long long x ) BOOST_NOEXCEPT
{
return __builtin_popcountll( x );
}
} // namespace detail
template<class T>
BOOST_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
{
return boost::core::detail::popcount_impl( x );
}
#else // defined(__GNUC__) || defined(__clang__)
namespace detail
{
@ -365,6 +405,8 @@ BOOST_CXX14_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
}
}
#endif // defined(__GNUC__) || defined(__clang__)
// rotating
template<class T>