__bits 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  32. int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
  33. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  34. int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
  35. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  36. int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
  37. #else // _LIBCPP_COMPILER_MSVC
  38. // Precondition: __x != 0
  39. inline _LIBCPP_INLINE_VISIBILITY
  40. int __libcpp_ctz(unsigned __x) {
  41. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  42. static_assert(sizeof(unsigned long) == 4, "");
  43. unsigned long __where;
  44. if (_BitScanForward(&__where, __x))
  45. return static_cast<int>(__where);
  46. return 32;
  47. }
  48. inline _LIBCPP_INLINE_VISIBILITY
  49. int __libcpp_ctz(unsigned long __x) {
  50. static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
  51. return __libcpp_ctz(static_cast<unsigned>(__x));
  52. }
  53. inline _LIBCPP_INLINE_VISIBILITY
  54. int __libcpp_ctz(unsigned long long __x) {
  55. unsigned long __where;
  56. #if defined(_LIBCPP_HAS_BITSCAN64)
  57. if (_BitScanForward64(&__where, __x))
  58. return static_cast<int>(__where);
  59. #else
  60. // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
  61. if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
  62. return static_cast<int>(__where);
  63. if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
  64. return static_cast<int>(__where + 32);
  65. #endif
  66. return 64;
  67. }
  68. // Precondition: __x != 0
  69. inline _LIBCPP_INLINE_VISIBILITY
  70. int __libcpp_clz(unsigned __x) {
  71. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  72. static_assert(sizeof(unsigned long) == 4, "");
  73. unsigned long __where;
  74. if (_BitScanReverse(&__where, __x))
  75. return static_cast<int>(31 - __where);
  76. return 32; // Undefined Behavior.
  77. }
  78. inline _LIBCPP_INLINE_VISIBILITY
  79. int __libcpp_clz(unsigned long __x) {
  80. static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
  81. return __libcpp_clz(static_cast<unsigned>(__x));
  82. }
  83. inline _LIBCPP_INLINE_VISIBILITY
  84. int __libcpp_clz(unsigned long long __x) {
  85. unsigned long __where;
  86. #if defined(_LIBCPP_HAS_BITSCAN64)
  87. if (_BitScanReverse64(&__where, __x))
  88. return static_cast<int>(63 - __where);
  89. #else
  90. // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
  91. if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
  92. return static_cast<int>(63 - (__where + 32));
  93. if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
  94. return static_cast<int>(63 - __where);
  95. #endif
  96. return 64; // Undefined Behavior.
  97. }
  98. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
  99. static_assert(sizeof(unsigned) == 4, "");
  100. return __popcnt(__x);
  101. }
  102. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
  103. static_assert(sizeof(unsigned long) == 4, "");
  104. return __popcnt(__x);
  105. }
  106. inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
  107. static_assert(sizeof(unsigned long long) == 8, "");
  108. #if defined(_M_IX86)
  109. return __popcnt(__x) + __popcnt(__x >> 32);
  110. #else
  111. return __popcnt64(__x);
  112. #endif
  113. }
  114. #endif // _LIBCPP_COMPILER_MSVC
  115. _LIBCPP_END_NAMESPACE_STD
  116. _LIBCPP_POP_MACROS
  117. #endif // _LIBCPP___BITS