Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:23

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 // TODO: __builtin_popcountg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can
0010 //  refactor this code to exclusively use __builtin_popcountg.
0011 
0012 #ifndef _LIBCPP___CXX03___BIT_POPCOUNT_H
0013 #define _LIBCPP___CXX03___BIT_POPCOUNT_H
0014 
0015 #include <__cxx03/__bit/rotate.h>
0016 #include <__cxx03/__concepts/arithmetic.h>
0017 #include <__cxx03/__config>
0018 #include <__cxx03/limits>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_PUSH_MACROS
0025 #include <__cxx03/__undef_macros>
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT {
0030   return __builtin_popcount(__x);
0031 }
0032 
0033 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long __x) _NOEXCEPT {
0034   return __builtin_popcountl(__x);
0035 }
0036 
0037 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long long __x) _NOEXCEPT {
0038   return __builtin_popcountll(__x);
0039 }
0040 
0041 #if _LIBCPP_STD_VER >= 20
0042 
0043 template <__libcpp_unsigned_integer _Tp>
0044 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
0045 #  if __has_builtin(__builtin_popcountg)
0046   return __builtin_popcountg(__t);
0047 #  else  // __has_builtin(__builtin_popcountg)
0048   if (sizeof(_Tp) <= sizeof(unsigned int))
0049     return std::__libcpp_popcount(static_cast<unsigned int>(__t));
0050   else if (sizeof(_Tp) <= sizeof(unsigned long))
0051     return std::__libcpp_popcount(static_cast<unsigned long>(__t));
0052   else if (sizeof(_Tp) <= sizeof(unsigned long long))
0053     return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
0054   else {
0055     int __ret = 0;
0056     while (__t != 0) {
0057       __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
0058       __t >>= numeric_limits<unsigned long long>::digits;
0059     }
0060     return __ret;
0061   }
0062 #  endif // __has_builtin(__builtin_popcountg)
0063 }
0064 
0065 #endif // _LIBCPP_STD_VER >= 20
0066 
0067 _LIBCPP_END_NAMESPACE_STD
0068 
0069 _LIBCPP_POP_MACROS
0070 
0071 #endif // _LIBCPP___CXX03___BIT_POPCOUNT_H