bits.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2020 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: bits.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This file contains implementations of C++20's bitwise math functions, as
  20. // defined by:
  21. //
  22. // P0553R4:
  23. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0553r4.html
  24. // P0556R3:
  25. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0556r3.html
  26. // P1355R2:
  27. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1355r2.html
  28. // P1956R1:
  29. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1956r1.pdf
  30. //
  31. // When using a standard library that implements these functions, we use the
  32. // standard library's implementation.
  33. #ifndef ABSL_NUMERIC_BITS_H_
  34. #define ABSL_NUMERIC_BITS_H_
  35. #include <cstdint>
  36. #include <limits>
  37. #include <type_traits>
  38. #if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) || \
  39. (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
  40. #include <bit>
  41. #endif
  42. #include "absl/base/attributes.h"
  43. #include "absl/base/config.h"
  44. #include "absl/numeric/internal/bits.h"
  45. namespace absl {
  46. ABSL_NAMESPACE_BEGIN
  47. #if !(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
  48. // rotating
  49. template <class T>
  50. ABSL_MUST_USE_RESULT constexpr
  51. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  52. rotl(T x, int s) noexcept {
  53. return numeric_internal::RotateLeft(x, s);
  54. }
  55. template <class T>
  56. ABSL_MUST_USE_RESULT constexpr
  57. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  58. rotr(T x, int s) noexcept {
  59. return numeric_internal::RotateRight(x, s);
  60. }
  61. // Counting functions
  62. //
  63. // While these functions are typically constexpr, on some platforms, they may
  64. // not be marked as constexpr due to constraints of the compiler/available
  65. // intrinsics.
  66. template <class T>
  67. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  68. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  69. countl_zero(T x) noexcept {
  70. return numeric_internal::CountLeadingZeroes(x);
  71. }
  72. template <class T>
  73. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  74. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  75. countl_one(T x) noexcept {
  76. // Avoid integer promotion to a wider type
  77. return countl_zero(static_cast<T>(~x));
  78. }
  79. template <class T>
  80. ABSL_INTERNAL_CONSTEXPR_CTZ inline
  81. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  82. countr_zero(T x) noexcept {
  83. return numeric_internal::CountTrailingZeroes(x);
  84. }
  85. template <class T>
  86. ABSL_INTERNAL_CONSTEXPR_CTZ inline
  87. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  88. countr_one(T x) noexcept {
  89. // Avoid integer promotion to a wider type
  90. return countr_zero(static_cast<T>(~x));
  91. }
  92. template <class T>
  93. ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline
  94. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  95. popcount(T x) noexcept {
  96. return numeric_internal::Popcount(x);
  97. }
  98. #else // defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L
  99. using std::countl_one;
  100. using std::countl_zero;
  101. using std::countr_one;
  102. using std::countr_zero;
  103. using std::popcount;
  104. using std::rotl;
  105. using std::rotr;
  106. #endif
  107. #if !(defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
  108. // Returns: true if x is an integral power of two; false otherwise.
  109. template <class T>
  110. constexpr inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type
  111. has_single_bit(T x) noexcept {
  112. return x != 0 && (x & (x - 1)) == 0;
  113. }
  114. // Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any
  115. // fractional part discarded.
  116. template <class T>
  117. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  118. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  119. bit_width(T x) noexcept {
  120. return std::numeric_limits<T>::digits - countl_zero(x);
  121. }
  122. // Returns: If x == 0, 0; otherwise the maximal value y such that
  123. // has_single_bit(y) is true and y <= x.
  124. template <class T>
  125. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  126. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  127. bit_floor(T x) noexcept {
  128. return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
  129. }
  130. // Returns: N, where N is the smallest power of 2 greater than or equal to x.
  131. //
  132. // Preconditions: N is representable as a value of type T.
  133. template <class T>
  134. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  135. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  136. bit_ceil(T x) {
  137. // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
  138. // want to force it to wraparound so that bit_ceil of an invalid value are not
  139. // core constant expressions.
  140. //
  141. // BitCeilNonPowerOf2 triggers an overflow in constexpr contexts if we would
  142. // undergo promotion to unsigned but not fit the result into T without
  143. // truncation.
  144. return has_single_bit(x) ? T{1} << (bit_width(x) - 1)
  145. : numeric_internal::BitCeilNonPowerOf2(x);
  146. }
  147. #else // defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
  148. using std::bit_ceil;
  149. using std::bit_floor;
  150. using std::bit_width;
  151. using std::has_single_bit;
  152. #endif
  153. ABSL_NAMESPACE_END
  154. } // namespace absl
  155. #endif // ABSL_NUMERIC_BITS_H_