Coverage Report

Created: 2025-07-11 06:37

/src/abseil-cpp/absl/numeric/internal/bits.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef ABSL_NUMERIC_INTERNAL_BITS_H_
16
#define ABSL_NUMERIC_INTERNAL_BITS_H_
17
18
#include <cstdint>
19
#include <limits>
20
#include <type_traits>
21
22
// Clang on Windows has __builtin_clzll; otherwise we need to use the
23
// windows intrinsic functions.
24
#if defined(_MSC_VER) && !defined(__clang__)
25
#include <intrin.h>
26
#endif
27
28
#include "absl/base/attributes.h"
29
#include "absl/base/config.h"
30
31
#if defined(__GNUC__) && !defined(__clang__)
32
// GCC
33
#define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) 1
34
#else
35
#define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) ABSL_HAVE_BUILTIN(x)
36
#endif
37
38
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountl) && \
39
    ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
40
#define ABSL_INTERNAL_CONSTEXPR_POPCOUNT constexpr
41
#define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 1
42
#else
43
#define ABSL_INTERNAL_CONSTEXPR_POPCOUNT
44
#define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 0
45
#endif
46
47
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz) && \
48
    ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
49
#define ABSL_INTERNAL_CONSTEXPR_CLZ constexpr
50
#define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 1
51
#else
52
#define ABSL_INTERNAL_CONSTEXPR_CLZ
53
#define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 0
54
#endif
55
56
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz) && \
57
    ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
58
#define ABSL_INTERNAL_CONSTEXPR_CTZ constexpr
59
#define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 1
60
#else
61
#define ABSL_INTERNAL_CONSTEXPR_CTZ
62
#define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 0
63
#endif
64
65
namespace absl {
66
ABSL_NAMESPACE_BEGIN
67
namespace numeric_internal {
68
69
0
constexpr bool IsPowerOf2(unsigned int x) noexcept {
70
0
  return x != 0 && (x & (x - 1)) == 0;
71
0
}
72
73
template <class T>
74
[[nodiscard]] ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateRight(
75
    T x, int s) noexcept {
76
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
77
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
78
                "T must have a power-of-2 size");
79
80
  // Rotate by s mod the number of digits to avoid unnecessary rotations.
81
  //
82
  // A negative s represents a left rotation instead of a right rotation.
83
  // We compute it as an equivalent complementary right rotation by leveraging
84
  // its two's complement representation.
85
  //
86
  // For example, suppose we rotate a 3-bit number by -2.
87
  // In that case:
88
  //   * s = 0b11111111111111111111111111111110
89
  //   * n = 8
90
  //   * r = (0b11111111111111111111111111111110 & 0b111) = 0b110
91
  //
92
  // Instead of rotating by 2 to the left, we rotate by 6 to the right, which
93
  // is equivalent.
94
  const int n = std::numeric_limits<T>::digits;
95
  const int r = s & (n - 1);
96
97
  if (r == 0) {
98
    return x;
99
  } else {
100
    return (x >> r) | (x << (n - r));
101
  }
102
}
103
104
template <class T>
105
[[nodiscard]] ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateLeft(
106
    T x, int s) noexcept {
107
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
108
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
109
                "T must have a power-of-2 size");
110
111
  // Rotate by s mod the number of digits to avoid unnecessary rotations.
112
  // See comment in RotateRight for a detailed explanation of the logic below.
113
  const int n = std::numeric_limits<T>::digits;
114
  const int r = s & (n - 1);
115
116
  if (r == 0) {
117
    return x;
118
  } else {
119
    return (x << r) | (x >> (n - r));
120
  }
121
}
122
123
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
124
0
Popcount32(uint32_t x) noexcept {
125
0
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcount)
126
0
  static_assert(sizeof(unsigned int) == sizeof(x),
127
0
                "__builtin_popcount does not take 32-bit arg");
128
0
  return __builtin_popcount(x);
129
0
#else
130
0
  x -= ((x >> 1) & 0x55555555);
131
0
  x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
132
0
  return static_cast<int>((((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24);
133
0
#endif
134
0
}
135
136
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
137
0
Popcount64(uint64_t x) noexcept {
138
0
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
139
0
  static_assert(sizeof(unsigned long long) == sizeof(x),  // NOLINT(runtime/int)
140
0
                "__builtin_popcount does not take 64-bit arg");
141
0
  return __builtin_popcountll(x);
142
#else
143
  x -= (x >> 1) & 0x5555555555555555ULL;
144
  x = ((x >> 2) & 0x3333333333333333ULL) + (x & 0x3333333333333333ULL);
145
  return static_cast<int>(
146
      (((x + (x >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
147
#endif
148
0
}
149
150
template <class T>
151
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
152
0
Popcount(T x) noexcept {
153
0
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
154
0
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
155
0
                "T must have a power-of-2 size");
156
0
  static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
157
  if constexpr (sizeof(x) <= sizeof(uint32_t)) {
158
    return Popcount32(x);
159
0
  } else {
160
0
    return Popcount64(x);
161
0
  }
162
0
}
163
164
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
165
0
CountLeadingZeroes32(uint32_t x) {
166
0
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz)
167
0
  // Use __builtin_clz, which uses the following instructions:
168
0
  //  x86: bsr, lzcnt
169
0
  //  ARM64: clz
170
0
  //  PPC: cntlzd
171
0
172
0
  static_assert(sizeof(unsigned int) == sizeof(x),
173
0
                "__builtin_clz does not take 32-bit arg");
174
0
  // Handle 0 as a special case because __builtin_clz(0) is undefined.
175
0
  return x == 0 ? 32 : __builtin_clz(x);
176
0
#elif defined(_MSC_VER) && !defined(__clang__)
177
0
  unsigned long result = 0;  // NOLINT(runtime/int)
178
0
  if (_BitScanReverse(&result, x)) {
179
0
    return 31 - result;
180
0
  }
181
0
  return 32;
182
0
#else
183
0
  int zeroes = 28;
184
0
  if (x >> 16) {
185
0
    zeroes -= 16;
186
0
    x >>= 16;
187
0
  }
188
0
  if (x >> 8) {
189
0
    zeroes -= 8;
190
0
    x >>= 8;
191
0
  }
192
0
  if (x >> 4) {
193
0
    zeroes -= 4;
194
0
    x >>= 4;
195
0
  }
196
0
  return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
197
0
#endif
198
0
}
199
200
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
201
0
CountLeadingZeroes16(uint16_t x) {
202
#if ABSL_HAVE_BUILTIN(__builtin_clzg)
203
  return x == 0 ? 16 : __builtin_clzg(x);
204
#elif ABSL_HAVE_BUILTIN(__builtin_clzs)
205
  static_assert(sizeof(unsigned short) == sizeof(x),  // NOLINT(runtime/int)
206
0
                "__builtin_clzs does not take 16-bit arg");
207
0
  return x == 0 ? 16 : __builtin_clzs(x);
208
#else
209
  return CountLeadingZeroes32(x) - 16;
210
#endif
211
0
}
212
213
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
214
0
CountLeadingZeroes64(uint64_t x) {
215
0
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
216
  // Use __builtin_clzll, which uses the following instructions:
217
  //  x86: bsr, lzcnt
218
  //  ARM64: clz
219
  //  PPC: cntlzd
220
0
  static_assert(sizeof(unsigned long long) == sizeof(x),  // NOLINT(runtime/int)
221
0
                "__builtin_clzll does not take 64-bit arg");
222
223
  // Handle 0 as a special case because __builtin_clzll(0) is undefined.
224
0
  return x == 0 ? 64 : __builtin_clzll(x);
225
#elif defined(_MSC_VER) && !defined(__clang__) && \
226
    (defined(_M_X64) || defined(_M_ARM64))
227
  // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
228
  unsigned long result = 0;  // NOLINT(runtime/int)
229
  if (_BitScanReverse64(&result, x)) {
230
    return 63 - result;
231
  }
232
  return 64;
233
#elif defined(_MSC_VER) && !defined(__clang__)
234
  // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
235
  unsigned long result = 0;  // NOLINT(runtime/int)
236
  if ((x >> 32) &&
237
      _BitScanReverse(&result, static_cast<unsigned long>(x >> 32))) {
238
    return 31 - result;
239
  }
240
  if (_BitScanReverse(&result, static_cast<unsigned long>(x))) {
241
    return 63 - result;
242
  }
243
  return 64;
244
#else
245
  int zeroes = 60;
246
  if (x >> 32) {
247
    zeroes -= 32;
248
    x >>= 32;
249
  }
250
  if (x >> 16) {
251
    zeroes -= 16;
252
    x >>= 16;
253
  }
254
  if (x >> 8) {
255
    zeroes -= 8;
256
    x >>= 8;
257
  }
258
  if (x >> 4) {
259
    zeroes -= 4;
260
    x >>= 4;
261
  }
262
  return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
263
#endif
264
0
}
265
266
template <typename T>
267
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
268
0
CountLeadingZeroes(T x) {
269
0
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
270
0
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
271
0
                "T must have a power-of-2 size");
272
0
  static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
273
0
  return sizeof(T) <= sizeof(uint16_t)
274
0
             ? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
275
0
                   (std::numeric_limits<uint16_t>::digits -
276
0
                    std::numeric_limits<T>::digits)
277
0
             : (sizeof(T) <= sizeof(uint32_t)
278
0
                    ? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
279
0
                          (std::numeric_limits<uint32_t>::digits -
280
0
                           std::numeric_limits<T>::digits)
281
0
                    : CountLeadingZeroes64(x));
282
0
}
Unexecuted instantiation: int absl::numeric_internal::CountLeadingZeroes<unsigned long>(unsigned long)
Unexecuted instantiation: int absl::numeric_internal::CountLeadingZeroes<unsigned short>(unsigned short)
283
284
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
285
0
CountTrailingZeroesNonzero32(uint32_t x) {
286
0
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
287
0
  static_assert(sizeof(unsigned int) == sizeof(x),
288
0
                "__builtin_ctz does not take 32-bit arg");
289
0
  return __builtin_ctz(x);
290
#elif defined(_MSC_VER) && !defined(__clang__)
291
  unsigned long result = 0;  // NOLINT(runtime/int)
292
  _BitScanForward(&result, x);
293
  return result;
294
#else
295
  int c = 31;
296
  x &= ~x + 1;
297
  if (x & 0x0000FFFF) c -= 16;
298
  if (x & 0x00FF00FF) c -= 8;
299
  if (x & 0x0F0F0F0F) c -= 4;
300
  if (x & 0x33333333) c -= 2;
301
  if (x & 0x55555555) c -= 1;
302
  return c;
303
#endif
304
0
}
305
306
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
307
870
CountTrailingZeroesNonzero64(uint64_t x) {
308
870
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
309
870
  static_assert(sizeof(unsigned long long) == sizeof(x),  // NOLINT(runtime/int)
310
870
                "__builtin_ctzll does not take 64-bit arg");
311
870
  return __builtin_ctzll(x);
312
#elif defined(_MSC_VER) && !defined(__clang__) && \
313
    (defined(_M_X64) || defined(_M_ARM64))
314
  unsigned long result = 0;  // NOLINT(runtime/int)
315
  _BitScanForward64(&result, x);
316
  return result;
317
#elif defined(_MSC_VER) && !defined(__clang__)
318
  unsigned long result = 0;  // NOLINT(runtime/int)
319
  if (static_cast<uint32_t>(x) == 0) {
320
    _BitScanForward(&result, static_cast<unsigned long>(x >> 32));
321
    return result + 32;
322
  }
323
  _BitScanForward(&result, static_cast<unsigned long>(x));
324
  return result;
325
#else
326
  int c = 63;
327
  x &= ~x + 1;
328
  if (x & 0x00000000FFFFFFFF) c -= 32;
329
  if (x & 0x0000FFFF0000FFFF) c -= 16;
330
  if (x & 0x00FF00FF00FF00FF) c -= 8;
331
  if (x & 0x0F0F0F0F0F0F0F0F) c -= 4;
332
  if (x & 0x3333333333333333) c -= 2;
333
  if (x & 0x5555555555555555) c -= 1;
334
  return c;
335
#endif
336
870
}
337
338
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
339
32.9M
CountTrailingZeroesNonzero16(uint16_t x) {
340
#if ABSL_HAVE_BUILTIN(__builtin_ctzg)
341
  return __builtin_ctzg(x);
342
#elif ABSL_HAVE_BUILTIN(__builtin_ctzs)
343
  static_assert(sizeof(unsigned short) == sizeof(x),  // NOLINT(runtime/int)
344
32.9M
                "__builtin_ctzs does not take 16-bit arg");
345
32.9M
  return __builtin_ctzs(x);
346
#else
347
  return CountTrailingZeroesNonzero32(x);
348
#endif
349
32.9M
}
350
351
template <class T>
352
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
353
32.9M
CountTrailingZeroes(T x) noexcept {
354
32.9M
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
355
32.9M
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
356
32.9M
                "T must have a power-of-2 size");
357
32.9M
  static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
358
32.9M
  return x == 0 ? std::numeric_limits<T>::digits
359
32.9M
                : (sizeof(T) <= sizeof(uint16_t)
360
32.9M
                       ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
361
32.9M
                       : (sizeof(T) <= sizeof(uint32_t)
362
870
                              ? CountTrailingZeroesNonzero32(
363
0
                                    static_cast<uint32_t>(x))
364
870
                              : CountTrailingZeroesNonzero64(x)));
365
32.9M
}
int absl::numeric_internal::CountTrailingZeroes<unsigned short>(unsigned short)
Line
Count
Source
353
32.9M
CountTrailingZeroes(T x) noexcept {
354
32.9M
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
355
32.9M
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
356
32.9M
                "T must have a power-of-2 size");
357
32.9M
  static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
358
32.9M
  return x == 0 ? std::numeric_limits<T>::digits
359
32.9M
                : (sizeof(T) <= sizeof(uint16_t)
360
32.9M
                       ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
361
32.9M
                       : (sizeof(T) <= sizeof(uint32_t)
362
0
                              ? CountTrailingZeroesNonzero32(
363
0
                                    static_cast<uint32_t>(x))
364
0
                              : CountTrailingZeroesNonzero64(x)));
365
32.9M
}
int absl::numeric_internal::CountTrailingZeroes<unsigned long>(unsigned long)
Line
Count
Source
353
870
CountTrailingZeroes(T x) noexcept {
354
870
  static_assert(std::is_unsigned<T>::value, "T must be unsigned");
355
870
  static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
356
870
                "T must have a power-of-2 size");
357
870
  static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
358
870
  return x == 0 ? std::numeric_limits<T>::digits
359
870
                : (sizeof(T) <= sizeof(uint16_t)
360
870
                       ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
361
870
                       : (sizeof(T) <= sizeof(uint32_t)
362
870
                              ? CountTrailingZeroesNonzero32(
363
0
                                    static_cast<uint32_t>(x))
364
870
                              : CountTrailingZeroesNonzero64(x)));
365
870
}
Unexecuted instantiation: int absl::numeric_internal::CountTrailingZeroes<unsigned int>(unsigned int)
366
367
// If T is narrower than unsigned, T{1} << bit_width will be promoted.  We
368
// want to force it to wraparound so that bit_ceil of an invalid value are not
369
// core constant expressions.
370
template <class T>
371
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
372
    typename std::enable_if<std::is_unsigned<T>::value, T>::type
373
    BitCeilPromotionHelper(T x, T promotion) {
374
  return (T{1} << (x + promotion)) >> promotion;
375
}
376
377
template <class T>
378
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
379
    typename std::enable_if<std::is_unsigned<T>::value, T>::type
380
    BitCeilNonPowerOf2(T x) {
381
  // If T is narrower than unsigned, it undergoes promotion to unsigned when we
382
  // shift.  We calculate the number of bits added by the wider type.
383
  return BitCeilPromotionHelper(
384
      static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
385
      T{sizeof(T) >= sizeof(unsigned) ? 0
386
                                      : std::numeric_limits<unsigned>::digits -
387
                                            std::numeric_limits<T>::digits});
388
}
389
390
}  // namespace numeric_internal
391
ABSL_NAMESPACE_END
392
}  // namespace absl
393
394
#endif  // ABSL_NUMERIC_INTERNAL_BITS_H_