1*4bdff4beSrobert //===----------------------------------------------------------------------===// 2*4bdff4beSrobert // 3*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information. 5*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*4bdff4beSrobert // 7*4bdff4beSrobert //===----------------------------------------------------------------------===// 8*4bdff4beSrobert 9*4bdff4beSrobert #ifndef _LIBCPP___BIT_POPCOUNT_H 10*4bdff4beSrobert #define _LIBCPP___BIT_POPCOUNT_H 11*4bdff4beSrobert 12*4bdff4beSrobert #include <__bit/rotate.h> 13*4bdff4beSrobert #include <__concepts/arithmetic.h> 14*4bdff4beSrobert #include <__config> 15*4bdff4beSrobert #include <limits> 16*4bdff4beSrobert 17*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18*4bdff4beSrobert # pragma GCC system_header 19*4bdff4beSrobert #endif 20*4bdff4beSrobert 21*4bdff4beSrobert _LIBCPP_PUSH_MACROS 22*4bdff4beSrobert #include <__undef_macros> 23*4bdff4beSrobert 24*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD 25*4bdff4beSrobert 26*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __libcpp_popcount(unsigned __x)27*4bdff4beSrobertint __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } 28*4bdff4beSrobert 29*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __libcpp_popcount(unsigned long __x)30*4bdff4beSrobertint __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); } 31*4bdff4beSrobert 32*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __libcpp_popcount(unsigned long long __x)33*4bdff4beSrobertint __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); } 34*4bdff4beSrobert 35*4bdff4beSrobert #if _LIBCPP_STD_VER >= 20 36*4bdff4beSrobert 37*4bdff4beSrobert template <__libcpp_unsigned_integer _Tp> popcount(_Tp __t)38*4bdff4beSrobert_LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept { 39*4bdff4beSrobert if (sizeof(_Tp) <= sizeof(unsigned int)) 40*4bdff4beSrobert return std::__libcpp_popcount(static_cast<unsigned int>(__t)); 41*4bdff4beSrobert else if (sizeof(_Tp) <= sizeof(unsigned long)) 42*4bdff4beSrobert return std::__libcpp_popcount(static_cast<unsigned long>(__t)); 43*4bdff4beSrobert else if (sizeof(_Tp) <= sizeof(unsigned long long)) 44*4bdff4beSrobert return std::__libcpp_popcount(static_cast<unsigned long long>(__t)); 45*4bdff4beSrobert else { 46*4bdff4beSrobert int __ret = 0; 47*4bdff4beSrobert while (__t != 0) { 48*4bdff4beSrobert __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t)); 49*4bdff4beSrobert __t >>= numeric_limits<unsigned long long>::digits; 50*4bdff4beSrobert } 51*4bdff4beSrobert return __ret; 52*4bdff4beSrobert } 53*4bdff4beSrobert } 54*4bdff4beSrobert 55*4bdff4beSrobert #endif // _LIBCPP_STD_VER >= 20 56*4bdff4beSrobert 57*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD 58*4bdff4beSrobert 59*4bdff4beSrobert _LIBCPP_POP_MACROS 60*4bdff4beSrobert 61*4bdff4beSrobert #endif // _LIBCPP___BIT_POPCOUNT_H 62