Coverage Report

Created: 2026-07-16 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/numeric/bits.h
Line
Count
Source
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
// P0463R1
31
//  https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0463r1.html
32
// P1272R4
33
//  https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1272r4.html
34
//
35
// When using a standard library that implements these functions, we use the
36
// standard library's implementation.
37
38
#ifndef ABSL_NUMERIC_BITS_H_
39
#define ABSL_NUMERIC_BITS_H_
40
41
#include <cstdint>
42
#include <limits>
43
#include <type_traits>
44
45
#include "absl/base/config.h"
46
47
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
48
#include <bit>
49
#endif
50
51
#include "absl/base/attributes.h"
52
#include "absl/base/internal/endian.h"
53
#include "absl/numeric/internal/bits.h"
54
55
namespace absl {
56
ABSL_NAMESPACE_BEGIN
57
58
// https://github.com/llvm/llvm-project/issues/64544
59
// libc++ had the wrong signature for std::rotl and std::rotr
60
// prior to libc++ 18.0.
61
//
62
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L) &&     \
63
    (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 180000)
64
using std::rotl;
65
using std::rotr;
66
67
#else
68
69
// Rotating functions
70
template <class T>
71
[[nodiscard]] constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotl(
72
    T x, int s) noexcept {
73
  return numeric_internal::RotateLeft(x, s);
74
}
75
76
template <class T>
77
[[nodiscard]] constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotr(
78
    T x, int s) noexcept {
79
  return numeric_internal::RotateRight(x, s);
80
}
81
82
#endif
83
84
// https://github.com/llvm/llvm-project/issues/64544
85
// libc++ had the wrong signature for std::rotl and std::rotr
86
// prior to libc++ 18.0.
87
//
88
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
89
90
using std::countl_one;
91
using std::countl_zero;
92
using std::countr_one;
93
using std::countr_zero;
94
using std::popcount;
95
96
#else
97
98
// Counting functions
99
//
100
// While these functions are typically constexpr, on some platforms, they may
101
// not be marked as constexpr due to constraints of the compiler/available
102
// intrinsics.
103
template <class T>
104
ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, int>
105
0
countl_zero(T x) noexcept {
106
0
  return numeric_internal::CountLeadingZeroes(x);
107
0
}
Unexecuted instantiation: _ZN4absl11countl_zeroImEENSt3__19enable_ifIXsr3stdE13is_unsigned_vIT_EEiE4typeES3_
Unexecuted instantiation: _ZN4absl11countl_zeroIjEENSt3__19enable_ifIXsr3stdE13is_unsigned_vIT_EEiE4typeES3_
108
109
template <class T>
110
ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, int>
111
countl_one(T x) noexcept {
112
  // Avoid integer promotion to a wider type
113
  return countl_zero(static_cast<T>(~x));
114
}
115
116
template <class T>
117
ABSL_INTERNAL_CONSTEXPR_CTZ inline std::enable_if_t<std::is_unsigned_v<T>, int>
118
9.32M
countr_zero(T x) noexcept {
119
9.32M
  return numeric_internal::CountTrailingZeroes(x);
120
9.32M
}
_ZN4absl11countr_zeroImEENSt3__19enable_ifIXsr3stdE13is_unsigned_vIT_EEiE4typeES3_
Line
Count
Source
118
257k
countr_zero(T x) noexcept {
119
257k
  return numeric_internal::CountTrailingZeroes(x);
120
257k
}
_ZN4absl11countr_zeroIjEENSt3__19enable_ifIXsr3stdE13is_unsigned_vIT_EEiE4typeES3_
Line
Count
Source
118
9.07M
countr_zero(T x) noexcept {
119
9.07M
  return numeric_internal::CountTrailingZeroes(x);
120
9.07M
}
121
122
template <class T>
123
ABSL_INTERNAL_CONSTEXPR_CTZ inline std::enable_if_t<std::is_unsigned_v<T>, int>
124
countr_one(T x) noexcept {
125
  // Avoid integer promotion to a wider type
126
  return countr_zero(static_cast<T>(~x));
127
}
128
129
template <class T>
130
ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline std::enable_if_t<std::is_unsigned_v<T>,
131
                                                         int>
132
0
popcount(T x) noexcept {
133
0
  return numeric_internal::Popcount(x);
134
0
}
135
136
#endif
137
138
#if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
139
140
using std::bit_ceil;
141
using std::bit_floor;
142
using std::bit_width;
143
using std::has_single_bit;
144
145
#else
146
147
// Returns: true if x is an integral power of two; false otherwise.
148
template <class T>
149
constexpr inline std::enable_if_t<std::is_unsigned_v<T>, bool> has_single_bit(
150
0
    T x) noexcept {
151
0
  return x != 0 && (x & (x - 1)) == 0;
152
0
}
153
154
// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any
155
// fractional part discarded.
156
template <class T>
157
ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, int>
158
0
bit_width(T x) noexcept {
159
0
  return std::numeric_limits<T>::digits - countl_zero(x);
160
0
}
161
162
// Returns: If x == 0, 0; otherwise the maximal value y such that
163
// has_single_bit(y) is true and y <= x.
164
template <class T>
165
ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, T>
166
bit_floor(T x) noexcept {
167
  return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
168
}
169
170
// Returns: N, where N is the smallest power of 2 greater than or equal to x.
171
//
172
// Preconditions: N is representable as a value of type T.
173
template <class T>
174
ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, T>
175
bit_ceil(T x) {
176
  // If T is narrower than unsigned, T{1} << bit_width will be promoted.  We
177
  // want to force it to wraparound so that bit_ceil of an invalid value are not
178
  // core constant expressions.
179
  //
180
  // BitCeilNonPowerOf2 triggers an overflow in constexpr contexts if we would
181
  // undergo promotion to unsigned but not fit the result into T without
182
  // truncation.
183
  return has_single_bit(x) ? T{1} << (bit_width(x) - 1)
184
                           : numeric_internal::BitCeilNonPowerOf2(x);
185
}
186
187
#endif
188
189
#if defined(__cpp_lib_endian) && __cpp_lib_endian >= 201907L
190
191
// https://en.cppreference.com/w/cpp/types/endian
192
//
193
// Indicates the endianness of all scalar types:
194
//   * If all scalar types are little-endian, `absl::endian::native` equals
195
//     absl::endian::little.
196
//   * If all scalar types are big-endian, `absl::endian::native` equals
197
//     `absl::endian::big`.
198
//   * Platforms that use anything else are unsupported.
199
using std::endian;
200
201
#else
202
203
enum class endian {
204
  little,
205
  big,
206
#if defined(ABSL_IS_LITTLE_ENDIAN)
207
  native = little
208
#elif defined(ABSL_IS_BIG_ENDIAN)
209
  native = big
210
#else
211
#error "Endian detection needs to be set up for this platform"
212
#endif
213
};
214
215
#endif  // defined(__cpp_lib_endian) && __cpp_lib_endian >= 201907L
216
217
#if defined(__cpp_lib_byteswap) && __cpp_lib_byteswap >= 202110L
218
219
// https://en.cppreference.com/w/cpp/numeric/byteswap
220
//
221
// Reverses the bytes in the given integer value `x`.
222
//
223
// `absl::byteswap` participates in overload resolution only if `T` satisfies
224
// integral, i.e., `T` is an integer type. The program is ill-formed if `T` has
225
// padding bits.
226
using std::byteswap;
227
228
#else
229
230
template <class T>
231
[[nodiscard]] constexpr T byteswap(T x) noexcept {
232
  static_assert(std::is_integral_v<T>,
233
                "byteswap requires an integral argument");
234
  static_assert(
235
      sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
236
      "byteswap works only with 8, 16, 32, or 64-bit integers");
237
  if constexpr (sizeof(T) == 1) {
238
    return x;
239
  } else if constexpr (sizeof(T) == 2) {
240
    return static_cast<T>(gbswap_16(static_cast<uint16_t>(x)));
241
  } else if constexpr (sizeof(T) == 4) {
242
    return static_cast<T>(gbswap_32(static_cast<uint32_t>(x)));
243
  } else if constexpr (sizeof(T) == 8) {
244
    return static_cast<T>(gbswap_64(static_cast<uint64_t>(x)));
245
  }
246
}
247
248
#endif  // defined(__cpp_lib_byteswap) && __cpp_lib_byteswap >= 202110L
249
250
ABSL_NAMESPACE_END
251
}  // namespace absl
252
253
#endif  // ABSL_NUMERIC_BITS_H_