Warning, file /include/absl/numeric/bits.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifndef ABSL_NUMERIC_BITS_H_
0035 #define ABSL_NUMERIC_BITS_H_
0036
0037 #include <cstdint>
0038 #include <limits>
0039 #include <type_traits>
0040
0041 #include "absl/base/config.h"
0042
0043 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
0044 #include <bit>
0045 #endif
0046
0047 #include "absl/base/attributes.h"
0048 #include "absl/numeric/internal/bits.h"
0049
0050 namespace absl {
0051 ABSL_NAMESPACE_BEGIN
0052
0053
0054
0055
0056
0057 #if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L) && \
0058 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 180000)
0059 using std::rotl;
0060 using std::rotr;
0061
0062 #else
0063
0064
0065 template <class T>
0066 ABSL_MUST_USE_RESULT constexpr
0067 typename std::enable_if<std::is_unsigned<T>::value, T>::type
0068 rotl(T x, int s) noexcept {
0069 return numeric_internal::RotateLeft(x, s);
0070 }
0071
0072 template <class T>
0073 ABSL_MUST_USE_RESULT constexpr
0074 typename std::enable_if<std::is_unsigned<T>::value, T>::type
0075 rotr(T x, int s) noexcept {
0076 return numeric_internal::RotateRight(x, s);
0077 }
0078
0079 #endif
0080
0081
0082
0083
0084
0085 #if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
0086
0087 using std::countl_one;
0088 using std::countl_zero;
0089 using std::countr_one;
0090 using std::countr_zero;
0091 using std::popcount;
0092
0093 #else
0094
0095
0096
0097
0098
0099
0100 template <class T>
0101 ABSL_INTERNAL_CONSTEXPR_CLZ inline
0102 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0103 countl_zero(T x) noexcept {
0104 return numeric_internal::CountLeadingZeroes(x);
0105 }
0106
0107 template <class T>
0108 ABSL_INTERNAL_CONSTEXPR_CLZ inline
0109 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0110 countl_one(T x) noexcept {
0111
0112 return countl_zero(static_cast<T>(~x));
0113 }
0114
0115 template <class T>
0116 ABSL_INTERNAL_CONSTEXPR_CTZ inline
0117 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0118 countr_zero(T x) noexcept {
0119 return numeric_internal::CountTrailingZeroes(x);
0120 }
0121
0122 template <class T>
0123 ABSL_INTERNAL_CONSTEXPR_CTZ inline
0124 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0125 countr_one(T x) noexcept {
0126
0127 return countr_zero(static_cast<T>(~x));
0128 }
0129
0130 template <class T>
0131 ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline
0132 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0133 popcount(T x) noexcept {
0134 return numeric_internal::Popcount(x);
0135 }
0136
0137 #endif
0138
0139 #if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
0140
0141 using std::bit_ceil;
0142 using std::bit_floor;
0143 using std::bit_width;
0144 using std::has_single_bit;
0145
0146 #else
0147
0148
0149 template <class T>
0150 constexpr inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type
0151 has_single_bit(T x) noexcept {
0152 return x != 0 && (x & (x - 1)) == 0;
0153 }
0154
0155
0156
0157 template <class T>
0158 ABSL_INTERNAL_CONSTEXPR_CLZ inline
0159 typename std::enable_if<std::is_unsigned<T>::value, int>::type
0160 bit_width(T x) noexcept {
0161 return std::numeric_limits<T>::digits - countl_zero(x);
0162 }
0163
0164
0165
0166 template <class T>
0167 ABSL_INTERNAL_CONSTEXPR_CLZ inline
0168 typename std::enable_if<std::is_unsigned<T>::value, T>::type
0169 bit_floor(T x) noexcept {
0170 return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
0171 }
0172
0173
0174
0175
0176 template <class T>
0177 ABSL_INTERNAL_CONSTEXPR_CLZ inline
0178 typename std::enable_if<std::is_unsigned<T>::value, T>::type
0179 bit_ceil(T x) {
0180
0181
0182
0183
0184
0185
0186
0187 return has_single_bit(x) ? T{1} << (bit_width(x) - 1)
0188 : numeric_internal::BitCeilNonPowerOf2(x);
0189 }
0190
0191 #endif
0192
0193 ABSL_NAMESPACE_END
0194 }
0195
0196 #endif