From 919b98d425cb0251806a2c24eba05e38c8c74c3d Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 29 Dec 2020 02:06:56 +0200 Subject: [PATCH] Use __builtin_popcount --- include/boost/core/bit.hpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/boost/core/bit.hpp b/include/boost/core/bit.hpp index 81898d8..dd85b2a 100644 --- a/include/boost/core/bit.hpp +++ b/include/boost/core/bit.hpp @@ -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 +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