xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__bit/countl.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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.
11 
12 #ifndef _LIBCPP___CXX03___BIT_COUNTL_H
13 #define _LIBCPP___CXX03___BIT_COUNTL_H
14 
15 #include <__cxx03/__bit/rotate.h>
16 #include <__cxx03/__config>
17 #include <__cxx03/__type_traits/is_unsigned_integer.h>
18 #include <__cxx03/limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__cxx03/__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
__libcpp_clz(unsigned __x)29 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
30 
__libcpp_clz(unsigned long __x)31 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int __libcpp_clz(unsigned long __x) _NOEXCEPT {
32   return __builtin_clzl(__x);
33 }
34 
__libcpp_clz(unsigned long long __x)35 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int __libcpp_clz(unsigned long long __x) _NOEXCEPT {
36   return __builtin_clzll(__x);
37 }
38 
39 #ifndef _LIBCPP_HAS_NO_INT128
__libcpp_clz(__uint128_t __x)40 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_clz(__uint128_t __x) _NOEXCEPT {
41 #  if __has_builtin(__builtin_clzg)
42   return __builtin_clzg(__x);
43 #  else
44   // The function is written in this form due to C++ constexpr limitations.
45   // The algorithm:
46   // - Test whether any bit in the high 64-bits is set
47   // - No bits set:
48   //   - The high 64-bits contain 64 leading zeros,
49   //   - Add the result of the low 64-bits.
50   // - Any bits set:
51   //   - The number of leading zeros of the input is the number of leading
52   //     zeros in the high 64-bits.
53   return ((__x >> 64) == 0) ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))
54                             : __builtin_clzll(static_cast<unsigned long long>(__x >> 64));
55 #  endif
56 }
57 #endif // _LIBCPP_HAS_NO_INT128
58 
59 template <class _Tp>
__countl_zero(_Tp __t)60 _LIBCPP_HIDE_FROM_ABI int __countl_zero(_Tp __t) _NOEXCEPT {
61   static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
62 #if __has_builtin(__builtin_clzg)
63   return __builtin_clzg(__t, numeric_limits<_Tp>::digits);
64 #else  // __has_builtin(__builtin_clzg)
65   if (__t == 0)
66     return numeric_limits<_Tp>::digits;
67 
68   if (sizeof(_Tp) <= sizeof(unsigned int))
69     return std::__libcpp_clz(static_cast<unsigned int>(__t)) -
70            (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
71   else if (sizeof(_Tp) <= sizeof(unsigned long))
72     return std::__libcpp_clz(static_cast<unsigned long>(__t)) -
73            (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
74   else if (sizeof(_Tp) <= sizeof(unsigned long long))
75     return std::__libcpp_clz(static_cast<unsigned long long>(__t)) -
76            (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
77   else {
78     int __ret                      = 0;
79     int __iter                     = 0;
80     const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
81     while (true) {
82       __t = std::__rotl(__t, __ulldigits);
83       if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
84         break;
85       __ret += __iter;
86     }
87     return __ret + __iter;
88   }
89 #endif // __has_builtin(__builtin_clzg)
90 }
91 
92 _LIBCPP_END_NAMESPACE_STD
93 
94 _LIBCPP_POP_MACROS
95 
96 #endif // _LIBCPP___CXX03___BIT_COUNTL_H
97