You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bits.h 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. {
  47. ABSL_NAMESPACE_BEGIN
  48. #if !(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
  49. // rotating
  50. template<class T>
  51. ABSL_MUST_USE_RESULT constexpr
  52. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  53. rotl(T x, int s) noexcept
  54. {
  55. return numeric_internal::RotateLeft(x, s);
  56. }
  57. template<class T>
  58. ABSL_MUST_USE_RESULT constexpr
  59. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  60. rotr(T x, int s) noexcept
  61. {
  62. return numeric_internal::RotateRight(x, s);
  63. }
  64. // Counting functions
  65. //
  66. // While these functions are typically constexpr, on some platforms, they may
  67. // not be marked as constexpr due to constraints of the compiler/available
  68. // intrinsics.
  69. template<class T>
  70. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  71. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  72. countl_zero(T x) noexcept
  73. {
  74. return numeric_internal::CountLeadingZeroes(x);
  75. }
  76. template<class T>
  77. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  78. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  79. countl_one(T x) noexcept
  80. {
  81. // Avoid integer promotion to a wider type
  82. return countl_zero(static_cast<T>(~x));
  83. }
  84. template<class T>
  85. ABSL_INTERNAL_CONSTEXPR_CTZ inline
  86. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  87. countr_zero(T x) noexcept
  88. {
  89. return numeric_internal::CountTrailingZeroes(x);
  90. }
  91. template<class T>
  92. ABSL_INTERNAL_CONSTEXPR_CTZ inline
  93. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  94. countr_one(T x) noexcept
  95. {
  96. // Avoid integer promotion to a wider type
  97. return countr_zero(static_cast<T>(~x));
  98. }
  99. template<class T>
  100. ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline
  101. typename std::enable_if<std::is_unsigned<T>::value, int>::type
  102. popcount(T x) noexcept
  103. {
  104. return numeric_internal::Popcount(x);
  105. }
  106. #else // defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L
  107. using std::countl_one;
  108. using std::countl_zero;
  109. using std::countr_one;
  110. using std::countr_zero;
  111. using std::popcount;
  112. using std::rotl;
  113. using std::rotr;
  114. #endif
  115. #if !(defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
  116. // Returns: true if x is an integral power of two; false otherwise.
  117. template<class T>
  118. constexpr inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type
  119. has_single_bit(T x) noexcept
  120. {
  121. return x != 0 && (x & (x - 1)) == 0;
  122. }
  123. // Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any
  124. // fractional part discarded.
  125. template<class T>
  126. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  127. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  128. bit_width(T x) noexcept
  129. {
  130. return std::numeric_limits<T>::digits -
  131. static_cast<unsigned int>(countl_zero(x));
  132. }
  133. // Returns: If x == 0, 0; otherwise the maximal value y such that
  134. // has_single_bit(y) is true and y <= x.
  135. template<class T>
  136. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  137. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  138. bit_floor(T x) noexcept
  139. {
  140. return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
  141. }
  142. // Returns: N, where N is the smallest power of 2 greater than or equal to x.
  143. //
  144. // Preconditions: N is representable as a value of type T.
  145. template<class T>
  146. ABSL_INTERNAL_CONSTEXPR_CLZ inline
  147. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  148. bit_ceil(T x)
  149. {
  150. // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
  151. // want to force it to wraparound so that bit_ceil of an invalid value are not
  152. // core constant expressions.
  153. //
  154. // BitCeilNonPowerOf2 triggers an overflow in constexpr contexts if we would
  155. // undergo promotion to unsigned but not fit the result into T without
  156. // truncation.
  157. return has_single_bit(x) ? T{1} << (bit_width(x) - 1) : numeric_internal::BitCeilNonPowerOf2(x);
  158. }
  159. #else // defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
  160. using std::bit_ceil;
  161. using std::bit_floor;
  162. using std::bit_width;
  163. using std::has_single_bit;
  164. #endif
  165. ABSL_NAMESPACE_END
  166. } // namespace absl
  167. #endif // ABSL_NUMERIC_BITS_H_