abseil-cpp/absl/numeric/bits.h
Go to the documentation of this file.
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 
34 #ifndef ABSL_NUMERIC_BITS_H_
35 #define ABSL_NUMERIC_BITS_H_
36 
37 #include <cstdint>
38 #include <limits>
39 #include <type_traits>
40 
41 #if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) || \
42  (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
43 #include <bit>
44 #endif
45 
46 #include "absl/base/attributes.h"
47 #include "absl/base/config.h"
48 #include "absl/numeric/internal/bits.h"
49 
50 namespace absl {
52 
53 #if !(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
54 // rotating
55 template <class T>
56 ABSL_MUST_USE_RESULT constexpr
58  rotl(T x, int s) noexcept {
60 }
61 
62 template <class T>
63 ABSL_MUST_USE_RESULT constexpr
65  rotr(T x, int s) noexcept {
67 }
68 
69 // Counting functions
70 //
71 // While these functions are typically constexpr, on some platforms, they may
72 // not be marked as constexpr due to constraints of the compiler/available
73 // intrinsics.
74 template <class T>
77  countl_zero(T x) noexcept {
79 }
80 
81 template <class T>
84  countl_one(T x) noexcept {
85  // Avoid integer promotion to a wider type
86  return countl_zero(static_cast<T>(~x));
87 }
88 
89 template <class T>
92  countr_zero(T x) noexcept {
94 }
95 
96 template <class T>
99  countr_one(T x) noexcept {
100  // Avoid integer promotion to a wider type
101  return countr_zero(static_cast<T>(~x));
102 }
103 
104 template <class T>
107  popcount(T x) noexcept {
109 }
110 #else // defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L
111 
112 using std::countl_one;
113 using std::countl_zero;
114 using std::countr_one;
115 using std::countr_zero;
116 using std::popcount;
117 using std::rotl;
118 using std::rotr;
119 
120 #endif
121 
122 #if !(defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
123 // Returns: true if x is an integral power of two; false otherwise.
124 template <class T>
125 constexpr inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type
126 has_single_bit(T x) noexcept {
127  return x != 0 && (x & (x - 1)) == 0;
128 }
129 
130 // Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any
131 // fractional part discarded.
132 template <class T>
135  bit_width(T x) noexcept {
136  return std::numeric_limits<T>::digits -
137  static_cast<unsigned int>(countl_zero(x));
138 }
139 
140 // Returns: If x == 0, 0; otherwise the maximal value y such that
141 // has_single_bit(y) is true and y <= x.
142 template <class T>
145  bit_floor(T x) noexcept {
146  return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
147 }
148 
149 // Returns: N, where N is the smallest power of 2 greater than or equal to x.
150 //
151 // Preconditions: N is representable as a value of type T.
152 template <class T>
155  bit_ceil(T x) {
156  // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
157  // want to force it to wraparound so that bit_ceil of an invalid value are not
158  // core constant expressions.
159  //
160  // BitCeilNonPowerOf2 triggers an overflow in constexpr contexts if we would
161  // undergo promotion to unsigned but not fit the result into T without
162  // truncation.
163  return has_single_bit(x) ? T{1} << (bit_width(x) - 1)
165 }
166 #else // defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
167 
168 using std::bit_ceil;
169 using std::bit_floor;
170 using std::bit_width;
171 using std::has_single_bit;
172 
173 #endif
174 
176 } // namespace absl
177 
178 #endif // ABSL_NUMERIC_BITS_H_
absl::countr_one
ABSL_INTERNAL_CONSTEXPR_CTZ std::enable_if< std::is_unsigned< T >::value, int >::type countr_one(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:99
absl::FormatConversionChar::s
@ s
absl::rotl
ABSL_NAMESPACE_BEGIN constexpr ABSL_MUST_USE_RESULT std::enable_if< std::is_unsigned< T >::value, T >::type rotl(T x, int s) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:58
absl::numeric_internal::CountTrailingZeroes
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ int CountTrailingZeroes(T x) noexcept
Definition: abseil-cpp/absl/numeric/internal/bits.h:317
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
T
#define T(upbtypeconst, upbtype, ctype, default_value)
ABSL_INTERNAL_CONSTEXPR_CTZ
#define ABSL_INTERNAL_CONSTEXPR_CTZ
Definition: abseil-cpp/absl/numeric/internal/bits.h:61
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::numeric_internal::RotateLeft
ABSL_MUST_USE_RESULT constexpr ABSL_ATTRIBUTE_ALWAYS_INLINE T RotateLeft(T x, int s) noexcept
Definition: abseil-cpp/absl/numeric/internal/bits.h:85
absl::numeric_internal::BitCeilNonPowerOf2
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, T >::type BitCeilNonPowerOf2(T x)
Definition: abseil-cpp/absl/numeric/internal/bits.h:344
absl::numeric_internal::CountLeadingZeroes
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ int CountLeadingZeroes(T x)
Definition: abseil-cpp/absl/numeric/internal/bits.h:234
absl::numeric_internal::RotateRight
ABSL_MUST_USE_RESULT constexpr ABSL_ATTRIBUTE_ALWAYS_INLINE T RotateRight(T x, int s) noexcept
Definition: abseil-cpp/absl/numeric/internal/bits.h:74
ABSL_INTERNAL_CONSTEXPR_POPCOUNT
#define ABSL_INTERNAL_CONSTEXPR_POPCOUNT
Definition: abseil-cpp/absl/numeric/internal/bits.h:43
absl::countr_zero
ABSL_INTERNAL_CONSTEXPR_CTZ std::enable_if< std::is_unsigned< T >::value, int >::type countr_zero(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:92
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::has_single_bit
constexpr std::enable_if< std::is_unsigned< T >::value, bool >::type has_single_bit(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:126
absl::countl_one
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, int >::type countl_one(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:84
absl::bit_width
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, T >::type bit_width(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:135
absl::countl_zero
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, int >::type countl_zero(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:77
value
const char * value
Definition: hpack_parser_table.cc:165
absl::popcount
ABSL_INTERNAL_CONSTEXPR_POPCOUNT std::enable_if< std::is_unsigned< T >::value, int >::type popcount(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:107
ABSL_INTERNAL_CONSTEXPR_CLZ
#define ABSL_INTERNAL_CONSTEXPR_CLZ
Definition: abseil-cpp/absl/numeric/internal/bits.h:52
absl::bit_floor
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, T >::type bit_floor(T x) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:145
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::rotr
constexpr ABSL_MUST_USE_RESULT std::enable_if< std::is_unsigned< T >::value, T >::type rotr(T x, int s) noexcept
Definition: abseil-cpp/absl/numeric/bits.h:65
absl::bit_ceil
ABSL_INTERNAL_CONSTEXPR_CLZ std::enable_if< std::is_unsigned< T >::value, T >::type bit_ceil(T x)
Definition: abseil-cpp/absl/numeric/bits.h:155
absl::numeric_internal::Popcount
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT int Popcount(T x) noexcept
Definition: abseil-cpp/absl/numeric/internal/bits.h:124


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:38