From 8bd2239b0b33648a5f918765fe2c655e40a654fe Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 29 Dec 2020 01:56:34 +0200 Subject: [PATCH] Use __builtin_ctz --- 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 98bf517..5324c43 100644 --- a/include/boost/core/bit.hpp +++ b/include/boost/core/bit.hpp @@ -190,6 +190,46 @@ BOOST_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT // countr +#if defined(__GNUC__) || defined(__clang__) + +namespace detail +{ + +BOOST_CONSTEXPR inline int countr_impl( unsigned char x ) BOOST_NOEXCEPT +{ + return x? __builtin_ctz( x ): std::numeric_limits::digits; +} + +BOOST_CONSTEXPR inline int countr_impl( unsigned short x ) BOOST_NOEXCEPT +{ + return x? __builtin_ctz( x ): std::numeric_limits::digits; +} + +BOOST_CONSTEXPR inline int countr_impl( unsigned int x ) BOOST_NOEXCEPT +{ + return x? __builtin_ctz( x ): std::numeric_limits::digits; +} + +BOOST_CONSTEXPR inline int countr_impl( unsigned long x ) BOOST_NOEXCEPT +{ + return x? __builtin_ctzl( x ): std::numeric_limits::digits; +} + +BOOST_CONSTEXPR inline int countr_impl( unsigned long long x ) BOOST_NOEXCEPT +{ + return x? __builtin_ctzll( x ): std::numeric_limits::digits; +} + +} // namespace detail + +template +BOOST_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT +{ + return boost::core::detail::countr_impl( x ); +} + +#else // defined(__GNUC__) || defined(__clang__) + namespace detail { @@ -241,6 +281,8 @@ int countr_zero( T x ) BOOST_NOEXCEPT } } +#endif // defined(__GNUC__) || defined(__clang__) + template BOOST_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT {