File indexing completed on 2026-05-03 08:13:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef _LIBCPP___CXX03___BIT_COUNTR_H
0013 #define _LIBCPP___CXX03___BIT_COUNTR_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 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned __x) _NOEXCEPT {
0030 return __builtin_ctz(__x);
0031 }
0032
0033 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned long __x) _NOEXCEPT {
0034 return __builtin_ctzl(__x);
0035 }
0036
0037 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned long long __x) _NOEXCEPT {
0038 return __builtin_ctzll(__x);
0039 }
0040
0041 template <class _Tp>
0042 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero(_Tp __t) _NOEXCEPT {
0043 #if __has_builtin(__builtin_ctzg)
0044 return __builtin_ctzg(__t, numeric_limits<_Tp>::digits);
0045 #else
0046 if (__t == 0)
0047 return numeric_limits<_Tp>::digits;
0048 if (sizeof(_Tp) <= sizeof(unsigned int))
0049 return std::__libcpp_ctz(static_cast<unsigned int>(__t));
0050 else if (sizeof(_Tp) <= sizeof(unsigned long))
0051 return std::__libcpp_ctz(static_cast<unsigned long>(__t));
0052 else if (sizeof(_Tp) <= sizeof(unsigned long long))
0053 return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
0054 else {
0055 int __ret = 0;
0056 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
0057 while (static_cast<unsigned long long>(__t) == 0uLL) {
0058 __ret += __ulldigits;
0059 __t >>= __ulldigits;
0060 }
0061 return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
0062 }
0063 #endif
0064 }
0065
0066 #if _LIBCPP_STD_VER >= 20
0067
0068 template <__libcpp_unsigned_integer _Tp>
0069 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
0070 return std::__countr_zero(__t);
0071 }
0072
0073 template <__libcpp_unsigned_integer _Tp>
0074 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
0075 return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
0076 }
0077
0078 #endif
0079
0080 _LIBCPP_END_NAMESPACE_STD
0081
0082 _LIBCPP_POP_MACROS
0083
0084 #endif