Coverage Report

Created: 2026-09-14 06:45

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