xref: /freebsd-src/contrib/llvm-project/libcxx/include/__bit/countl.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric 
9*0fca6ea1SDimitry Andric // TODO: __builtin_clzg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can
10*0fca6ea1SDimitry Andric //  refactor this code to exclusively use __builtin_clzg.
11*0fca6ea1SDimitry Andric 
12bdd1243dSDimitry Andric #ifndef _LIBCPP___BIT_COUNTL_H
13bdd1243dSDimitry Andric #define _LIBCPP___BIT_COUNTL_H
14bdd1243dSDimitry Andric 
15bdd1243dSDimitry Andric #include <__bit/rotate.h>
16bdd1243dSDimitry Andric #include <__concepts/arithmetic.h>
17bdd1243dSDimitry Andric #include <__config>
18bdd1243dSDimitry Andric #include <__type_traits/is_unsigned_integer.h>
19bdd1243dSDimitry Andric #include <limits>
20bdd1243dSDimitry Andric 
21bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22bdd1243dSDimitry Andric #  pragma GCC system_header
23bdd1243dSDimitry Andric #endif
24bdd1243dSDimitry Andric 
25bdd1243dSDimitry Andric _LIBCPP_PUSH_MACROS
26bdd1243dSDimitry Andric #include <__undef_macros>
27bdd1243dSDimitry Andric 
28bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
29bdd1243dSDimitry Andric 
30cb14a3feSDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned __x) _NOEXCEPT {
31cb14a3feSDimitry Andric   return __builtin_clz(__x);
32cb14a3feSDimitry Andric }
33bdd1243dSDimitry Andric 
34cb14a3feSDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long __x) _NOEXCEPT {
35cb14a3feSDimitry Andric   return __builtin_clzl(__x);
36cb14a3feSDimitry Andric }
37bdd1243dSDimitry Andric 
38cb14a3feSDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long long __x) _NOEXCEPT {
39cb14a3feSDimitry Andric   return __builtin_clzll(__x);
40cb14a3feSDimitry Andric }
41bdd1243dSDimitry Andric 
42bdd1243dSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
43cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(__uint128_t __x) _NOEXCEPT {
44*0fca6ea1SDimitry Andric #  if __has_builtin(__builtin_clzg)
45*0fca6ea1SDimitry Andric   return __builtin_clzg(__x);
46*0fca6ea1SDimitry Andric #  else
47bdd1243dSDimitry Andric   // The function is written in this form due to C++ constexpr limitations.
48bdd1243dSDimitry Andric   // The algorithm:
49bdd1243dSDimitry Andric   // - Test whether any bit in the high 64-bits is set
50bdd1243dSDimitry Andric   // - No bits set:
51bdd1243dSDimitry Andric   //   - The high 64-bits contain 64 leading zeros,
52bdd1243dSDimitry Andric   //   - Add the result of the low 64-bits.
53bdd1243dSDimitry Andric   // - Any bits set:
54bdd1243dSDimitry Andric   //   - The number of leading zeros of the input is the number of leading
55bdd1243dSDimitry Andric   //     zeros in the high 64-bits.
56cb14a3feSDimitry Andric   return ((__x >> 64) == 0) ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))
57bdd1243dSDimitry Andric                             : __builtin_clzll(static_cast<unsigned long long>(__x >> 64));
58*0fca6ea1SDimitry Andric #  endif
59bdd1243dSDimitry Andric }
60bdd1243dSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
61bdd1243dSDimitry Andric 
62bdd1243dSDimitry Andric template <class _Tp>
63cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _NOEXCEPT {
64bdd1243dSDimitry Andric   static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
65*0fca6ea1SDimitry Andric #if __has_builtin(__builtin_clzg)
66*0fca6ea1SDimitry Andric   return __builtin_clzg(__t, numeric_limits<_Tp>::digits);
67*0fca6ea1SDimitry Andric #else  // __has_builtin(__builtin_clzg)
68bdd1243dSDimitry Andric   if (__t == 0)
69bdd1243dSDimitry Andric     return numeric_limits<_Tp>::digits;
70bdd1243dSDimitry Andric 
71bdd1243dSDimitry Andric   if (sizeof(_Tp) <= sizeof(unsigned int))
72cb14a3feSDimitry Andric     return std::__libcpp_clz(static_cast<unsigned int>(__t)) -
73cb14a3feSDimitry Andric            (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
74bdd1243dSDimitry Andric   else if (sizeof(_Tp) <= sizeof(unsigned long))
75cb14a3feSDimitry Andric     return std::__libcpp_clz(static_cast<unsigned long>(__t)) -
76cb14a3feSDimitry Andric            (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
77bdd1243dSDimitry Andric   else if (sizeof(_Tp) <= sizeof(unsigned long long))
78cb14a3feSDimitry Andric     return std::__libcpp_clz(static_cast<unsigned long long>(__t)) -
79cb14a3feSDimitry Andric            (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
80cb14a3feSDimitry Andric   else {
81bdd1243dSDimitry Andric     int __ret                      = 0;
82bdd1243dSDimitry Andric     int __iter                     = 0;
83bdd1243dSDimitry Andric     const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
84bdd1243dSDimitry Andric     while (true) {
855f757f3fSDimitry Andric       __t = std::__rotl(__t, __ulldigits);
86bdd1243dSDimitry Andric       if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
87bdd1243dSDimitry Andric         break;
88bdd1243dSDimitry Andric       __ret += __iter;
89bdd1243dSDimitry Andric     }
90bdd1243dSDimitry Andric     return __ret + __iter;
91bdd1243dSDimitry Andric   }
92*0fca6ea1SDimitry Andric #endif // __has_builtin(__builtin_clzg)
93bdd1243dSDimitry Andric }
94bdd1243dSDimitry Andric 
95bdd1243dSDimitry Andric #if _LIBCPP_STD_VER >= 20
96bdd1243dSDimitry Andric 
97bdd1243dSDimitry Andric template <__libcpp_unsigned_integer _Tp>
98*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
99bdd1243dSDimitry Andric   return std::__countl_zero(__t);
100bdd1243dSDimitry Andric }
101bdd1243dSDimitry Andric 
102bdd1243dSDimitry Andric template <__libcpp_unsigned_integer _Tp>
103*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
104bdd1243dSDimitry Andric   return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
105bdd1243dSDimitry Andric }
106bdd1243dSDimitry Andric 
107bdd1243dSDimitry Andric #endif // _LIBCPP_STD_VER >= 20
108bdd1243dSDimitry Andric 
109bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
110bdd1243dSDimitry Andric 
111bdd1243dSDimitry Andric _LIBCPP_POP_MACROS
112bdd1243dSDimitry Andric 
113bdd1243dSDimitry Andric #endif // _LIBCPP___BIT_COUNTL_H
114