/*============================================================================= Copyright (c) 2011-2017 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef SPROUT_BIT_POPCOUNT_HPP #define SPROUT_BIT_POPCOUNT_HPP #include #include #include #include namespace sprout { namespace detail { # if SPROUT_USE_BUILTIN_BIT_OPERATION inline SPROUT_CONSTEXPR int popcount(unsigned n) { return __builtin_popcount(n); } inline SPROUT_CONSTEXPR int popcount(unsigned long n) { return __builtin_popcountl(n); } inline SPROUT_CONSTEXPR int popcount(unsigned long long n) { return __builtin_popcountll(n); } # endif template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_unsigned::value, int >::type popcount(T n) { return n == 0 ? 0 : 1 + sprout::detail::popcount(static_cast(n & (n - 1))) ; } template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_signed::value, int >::type popcount(T n) { return sprout::detail::popcount(static_cast::type>(n)); } } // namespace detail // // popcount // template inline SPROUT_CONSTEXPR typename std::enable_if< std::is_integral::value, int >::type popcount(T n) { return sprout::detail::popcount(n); } } // namespace sprout #endif // #ifndef SPROUT_BIT_POPCOUNT_HPP