__bits 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___BITS
  10. #define _LIBCPP___BITS
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. _LIBCPP_PUSH_MACROS
  16. #include <__undef_macros>
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #ifndef _LIBCPP_COMPILER_MSVC
  19. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  20. int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
  21. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  22. int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
  23. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  24. int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
  25. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  26. int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
  27. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  28. int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
  29. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  30. int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
  31. # ifndef _LIBCPP_HAS_NO_INT128
  32. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  33. int __libcpp_clz(__uint128_t __x) _NOEXCEPT {
  34. // The function is written in this form due to C++ constexpr limitations.
  35. // The algorithm:
  36. // - Test whether any bit in the high 64-bits is set
  37. // - No bits set:
  38. // - The high 64-bits contain 64 leading zeros,
  39. // - Add the result of the low 64-bits.
  40. // - Any bits set:
  41. // - The number of leading zeros of the input is the number of leading
  42. // zeros in the high 64-bits.
  43. return ((__x >> 64) == 0)
  44. ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))
  45. : __builtin_clzll(static_cast<unsigned long long>(__x >> 64));
  46. }
  47. # endif
  48. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  49. int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
  50. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  51. int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
  52. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  53. int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
  54. #else // _LIBCPP_COMPILER_MSVC
  55. // Precondition: __x != 0
  56. inline _LIBCPP_INLINE_VISIBILITY
  57. int __libcpp_ctz(unsigned __x) {
  58. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  59. static_assert(sizeof(unsigned long) == 4, "");
  60. unsigned long __where;
  61. if (_BitScanForward(&__where, __x))
  62. return static_cast<int>(__where);
  63. return 32;
  64. }
  65. inline _LIBCPP_INLINE_VISIBILITY
  66. int __libcpp_ctz(unsigned long __x) {
  67. static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
  68. return __libcpp_ctz(static_cast<unsigned>(__x));
  69. }
  70. inline _LIBCPP_INLINE_VISIBILITY
  71. int __libcpp_ctz(unsigned long long __x) {
  72. unsigned long __where;
  73. #if defined(_LIBCPP_HAS_BITSCAN64)
  74. if (_BitScanForward64(&__where, __x))
  75. return static_cast<int>(__where);
  76. #else
  77. // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
  78. if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
  79. return static_cast<int>(__where);
  80. if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
  81. return static_cast<int>(__where + 32);
  82. #endif
  83. return 64;
  84. }
  85. // Precondition: __x != 0
  86. inline _LIBCPP_INLINE_VISIBILITY
  87. int __libcpp_clz(unsigned __x) {
  88. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  89. static_assert(sizeof(unsigned long) == 4, "");
  90. unsigned long __where;
  91. if (_BitScanReverse(&__where, __x))
  92. return static_cast<int>(31 - __where);
  93. return 32; // Undefined Behavior.
  94. }
  95. inline _LIBCPP_INLINE_VISIBILITY
  96. int __libcpp_clz(unsigned long __x) {
  97. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  98. return __libcpp_clz(static_cast<unsigned>(__x));
  99. }
  100. inline _LIBCPP_INLINE_VISIBILITY
  101. int __libcpp_clz(unsigned long long __x) {
  102. unsigned long __where;
  103. #if defined(_LIBCPP_HAS_BITSCAN64)
  104. if (_BitScanReverse64(&__where, __x))
  105. return static_cast<int>(63 - __where);
  106. #else
  107. // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
  108. if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
  109. return static_cast<int>(63 - (__where + 32));
  110. if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
  111. return static_cast<int>(63 - __where);
  112. #endif
  113. return 64; // Undefined Behavior.
  114. }
  115. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
  116. static_assert(sizeof(unsigned) == 4, "");
  117. return __popcnt(__x);
  118. }
  119. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
  120. static_assert(sizeof(unsigned long) == 4, "");
  121. return __popcnt(__x);
  122. }
  123. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
  124. static_assert(sizeof(unsigned long long) == 8, "");
  125. #if defined(_M_IX86)
  126. return __popcnt(__x) + __popcnt(__x >> 32);
  127. #else
  128. return __popcnt64(__x);
  129. #endif
  130. }
  131. #endif // _LIBCPP_COMPILER_MSVC
  132. _LIBCPP_END_NAMESPACE_STD
  133. _LIBCPP_POP_MACROS
  134. #endif // _LIBCPP___BITS