countl.h 3.7 KB

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