xref: /openbsd-src/gnu/llvm/libcxx/include/__bit/countr.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
2*4bdff4beSrobert //
3*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
5*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4bdff4beSrobert //
7*4bdff4beSrobert //===----------------------------------------------------------------------===//
8*4bdff4beSrobert 
9*4bdff4beSrobert #ifndef _LIBCPP___BIT_COUNTR_H
10*4bdff4beSrobert #define _LIBCPP___BIT_COUNTR_H
11*4bdff4beSrobert 
12*4bdff4beSrobert #include <__bit/rotate.h>
13*4bdff4beSrobert #include <__concepts/arithmetic.h>
14*4bdff4beSrobert #include <__config>
15*4bdff4beSrobert #include <limits>
16*4bdff4beSrobert 
17*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18*4bdff4beSrobert #  pragma GCC system_header
19*4bdff4beSrobert #endif
20*4bdff4beSrobert 
21*4bdff4beSrobert _LIBCPP_PUSH_MACROS
22*4bdff4beSrobert #include <__undef_macros>
23*4bdff4beSrobert 
24*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
25*4bdff4beSrobert 
26*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
__libcpp_ctz(unsigned __x)27*4bdff4beSrobert int __libcpp_ctz(unsigned __x)           _NOEXCEPT { return __builtin_ctz(__x); }
28*4bdff4beSrobert 
29*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
__libcpp_ctz(unsigned long __x)30*4bdff4beSrobert int __libcpp_ctz(unsigned long __x)      _NOEXCEPT { return __builtin_ctzl(__x); }
31*4bdff4beSrobert 
32*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
__libcpp_ctz(unsigned long long __x)33*4bdff4beSrobert int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
34*4bdff4beSrobert 
35*4bdff4beSrobert #if _LIBCPP_STD_VER >= 20
36*4bdff4beSrobert 
37*4bdff4beSrobert template <__libcpp_unsigned_integer _Tp>
countr_zero(_Tp __t)38*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
39*4bdff4beSrobert   if (__t == 0)
40*4bdff4beSrobert     return numeric_limits<_Tp>::digits;
41*4bdff4beSrobert 
42*4bdff4beSrobert   if (sizeof(_Tp) <= sizeof(unsigned int))
43*4bdff4beSrobert     return std::__libcpp_ctz(static_cast<unsigned int>(__t));
44*4bdff4beSrobert   else if (sizeof(_Tp) <= sizeof(unsigned long))
45*4bdff4beSrobert     return std::__libcpp_ctz(static_cast<unsigned long>(__t));
46*4bdff4beSrobert   else if (sizeof(_Tp) <= sizeof(unsigned long long))
47*4bdff4beSrobert     return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
48*4bdff4beSrobert   else {
49*4bdff4beSrobert     int __ret = 0;
50*4bdff4beSrobert     const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
51*4bdff4beSrobert     while (static_cast<unsigned long long>(__t) == 0uLL) {
52*4bdff4beSrobert       __ret += __ulldigits;
53*4bdff4beSrobert       __t >>= __ulldigits;
54*4bdff4beSrobert     }
55*4bdff4beSrobert     return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
56*4bdff4beSrobert   }
57*4bdff4beSrobert }
58*4bdff4beSrobert 
59*4bdff4beSrobert template <__libcpp_unsigned_integer _Tp>
countr_one(_Tp __t)60*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
61*4bdff4beSrobert   return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
62*4bdff4beSrobert }
63*4bdff4beSrobert 
64*4bdff4beSrobert #endif // _LIBCPP_STD_VER >= 20
65*4bdff4beSrobert 
66*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
67*4bdff4beSrobert 
68*4bdff4beSrobert _LIBCPP_POP_MACROS
69*4bdff4beSrobert 
70*4bdff4beSrobert #endif // _LIBCPP___BIT_COUNTR_H
71