1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // TODO: __builtin_clzg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can
10 // refactor this code to exclusively use __builtin_clzg.
12 #ifndef _LIBCPP___BIT_COUNTL_H
13 #define _LIBCPP___BIT_COUNTL_H
15 #include <__cxx03/__bit/rotate.h>
16 #include <__cxx03/__concepts/arithmetic.h>
17 #include <__cxx03/__config>
18 #include <__cxx03/__type_traits/is_unsigned_integer.h>
19 #include <__cxx03/limits>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
26 #include <__cxx03/__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 _LIBCPP_NODISCARD
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
int __libcpp_clz(unsigned __x
) _NOEXCEPT
{
31 return __builtin_clz(__x
);
34 _LIBCPP_NODISCARD
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
int __libcpp_clz(unsigned long __x
) _NOEXCEPT
{
35 return __builtin_clzl(__x
);
38 _LIBCPP_NODISCARD
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
int __libcpp_clz(unsigned long long __x
) _NOEXCEPT
{
39 return __builtin_clzll(__x
);
42 #ifndef _LIBCPP_HAS_NO_INT128
43 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
int __libcpp_clz(__uint128_t __x
) _NOEXCEPT
{
44 # if __has_builtin(__builtin_clzg)
45 return __builtin_clzg(__x
);
47 // The function is written in this form due to C++ constexpr limitations.
49 // - Test whether any bit in the high 64-bits is set
51 // - The high 64-bits contain 64 leading zeros,
52 // - Add the result of the low 64-bits.
54 // - The number of leading zeros of the input is the number of leading
55 // zeros in the high 64-bits.
56 return ((__x
>> 64) == 0) ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x
)))
57 : __builtin_clzll(static_cast<unsigned long long>(__x
>> 64));
60 #endif // _LIBCPP_HAS_NO_INT128
63 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
int __countl_zero(_Tp __t
) _NOEXCEPT
{
64 static_assert(__libcpp_is_unsigned_integer
<_Tp
>::value
, "__countl_zero requires an unsigned integer type");
65 #if __has_builtin(__builtin_clzg)
66 return __builtin_clzg(__t
, numeric_limits
<_Tp
>::digits
);
67 #else // __has_builtin(__builtin_clzg)
69 return numeric_limits
<_Tp
>::digits
;
71 if (sizeof(_Tp
) <= sizeof(unsigned int))
72 return std::__libcpp_clz(static_cast<unsigned int>(__t
)) -
73 (numeric_limits
<unsigned int>::digits
- numeric_limits
<_Tp
>::digits
);
74 else if (sizeof(_Tp
) <= sizeof(unsigned long))
75 return std::__libcpp_clz(static_cast<unsigned long>(__t
)) -
76 (numeric_limits
<unsigned long>::digits
- numeric_limits
<_Tp
>::digits
);
77 else if (sizeof(_Tp
) <= sizeof(unsigned long long))
78 return std::__libcpp_clz(static_cast<unsigned long long>(__t
)) -
79 (numeric_limits
<unsigned long long>::digits
- numeric_limits
<_Tp
>::digits
);
83 const unsigned int __ulldigits
= numeric_limits
<unsigned long long>::digits
;
85 __t
= std::__rotl(__t
, __ulldigits
);
86 if ((__iter
= std::__countl_zero(static_cast<unsigned long long>(__t
))) != __ulldigits
)
90 return __ret
+ __iter
;
92 #endif // __has_builtin(__builtin_clzg)
95 #if _LIBCPP_STD_VER >= 20
97 template <__libcpp_unsigned_integer _Tp
>
98 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr int countl_zero(_Tp __t
) noexcept
{
99 return std::__countl_zero(__t
);
102 template <__libcpp_unsigned_integer _Tp
>
103 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr int countl_one(_Tp __t
) noexcept
{
104 return __t
!= numeric_limits
<_Tp
>::max() ? std::countl_zero(static_cast<_Tp
>(~__t
)) : numeric_limits
<_Tp
>::digits
;
107 #endif // _LIBCPP_STD_VER >= 20
109 _LIBCPP_END_NAMESPACE_STD
113 #endif // _LIBCPP___BIT_COUNTL_H