Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/internal/hashtable_control_bytes.h
Line
Count
Source
1
// Copyright 2025 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
// This file contains the implementation of the hashtable control bytes
16
// manipulation.
17
18
#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_CONTROL_BYTES_H_
19
#define ABSL_CONTAINER_INTERNAL_HASHTABLE_CONTROL_BYTES_H_
20
21
#include <cassert>
22
#include <cstddef>
23
#include <cstdint>
24
#include <type_traits>
25
26
#include "absl/base/config.h"
27
#include "absl/base/internal/endian.h"
28
#include "absl/base/optimization.h"
29
#include "absl/numeric/bits.h"
30
31
#ifdef ABSL_INTERNAL_HAVE_SSE2
32
#include <emmintrin.h>
33
#endif
34
35
#ifdef ABSL_INTERNAL_HAVE_SSSE3
36
#include <tmmintrin.h>
37
#endif
38
39
#ifdef _MSC_VER
40
#include <intrin.h>
41
#endif
42
43
#ifdef ABSL_INTERNAL_HAVE_ARM_NEON
44
#include <arm_neon.h>
45
#endif
46
47
namespace absl {
48
ABSL_NAMESPACE_BEGIN
49
namespace container_internal {
50
51
#ifdef ABSL_SWISSTABLE_ASSERT
52
#error ABSL_SWISSTABLE_ASSERT cannot be directly set
53
#else
54
// We use this macro for assertions that users may see when the table is in an
55
// invalid state that sanitizers may help diagnose.
56
#define ABSL_SWISSTABLE_ASSERT(CONDITION) \
57
0
  assert((CONDITION) && "Try enabling sanitizers.")
58
#endif
59
60
61
template <typename T>
62
11.0M
uint32_t TrailingZeros(T x) {
63
11.0M
  ABSL_ASSUME(x != 0);
64
11.0M
  return static_cast<uint32_t>(countr_zero(x));
65
11.0M
}
unsigned int absl::container_internal::TrailingZeros<unsigned long>(unsigned long)
Line
Count
Source
62
285k
uint32_t TrailingZeros(T x) {
63
285k
  ABSL_ASSUME(x != 0);
64
285k
  return static_cast<uint32_t>(countr_zero(x));
65
285k
}
unsigned int absl::container_internal::TrailingZeros<unsigned int>(unsigned int)
Line
Count
Source
62
10.7M
uint32_t TrailingZeros(T x) {
63
10.7M
  ABSL_ASSUME(x != 0);
64
10.7M
  return static_cast<uint32_t>(countr_zero(x));
65
10.7M
}
66
67
// 8 bytes bitmask with most significant bit set for every byte.
68
constexpr uint64_t kMsbs8Bytes = 0x8080808080808080ULL;
69
// 8 kEmpty bytes that is useful for small table initialization.
70
constexpr uint64_t k8EmptyBytes = kMsbs8Bytes;
71
72
// An abstract bitmask, such as that emitted by a SIMD instruction.
73
//
74
// Specifically, this type implements a simple bitset whose representation is
75
// controlled by `SignificantBits` and `Shift`. `SignificantBits` is the number
76
// of abstract bits in the bitset, while `Shift` is the log-base-two of the
77
// width of an abstract bit in the representation.
78
// This mask provides operations for any number of real bits set in an abstract
79
// bit. To add iteration on top of that, implementation must guarantee no more
80
// than the most significant real bit is set in a set abstract bit.
81
template <class T, int SignificantBits, int Shift = 0>
82
class NonIterableBitMask {
83
 public:
84
121k
  explicit NonIterableBitMask(T mask) : mask_(mask) {}
absl::container_internal::NonIterableBitMask<unsigned int, 16, 0>::NonIterableBitMask(unsigned int)
Line
Count
Source
84
121k
  explicit NonIterableBitMask(T mask) : mask_(mask) {}
Unexecuted instantiation: absl::container_internal::NonIterableBitMask<unsigned long, 8, 3>::NonIterableBitMask(unsigned long)
85
86
121k
  explicit operator bool() const { return mask_ != 0; }
87
88
  // Returns the index of the lowest *abstract* bit set in `self`.
89
10.7M
  uint32_t LowestBitSet() const {
90
10.7M
    return container_internal::TrailingZeros(mask_) >> Shift;
91
10.7M
  }
absl::container_internal::NonIterableBitMask<unsigned int, 16, 0>::LowestBitSet() const
Line
Count
Source
89
10.7M
  uint32_t LowestBitSet() const {
90
10.7M
    return container_internal::TrailingZeros(mask_) >> Shift;
91
10.7M
  }
Unexecuted instantiation: absl::container_internal::NonIterableBitMask<unsigned long, 8, 3>::LowestBitSet() const
92
93
  // Returns the number of trailing zero *abstract* bits.
94
0
  uint32_t TrailingZeros() const {
95
0
    return container_internal::TrailingZeros(mask_) >> Shift;
96
0
  }
97
98
  // Returns the number of leading zero *abstract* bits.
99
0
  uint32_t LeadingZeros() const {
100
0
    constexpr int total_significant_bits = SignificantBits << Shift;
101
0
    constexpr int extra_bits = sizeof(T) * 8 - total_significant_bits;
102
0
    return static_cast<uint32_t>(
103
0
               countl_zero(static_cast<T>(mask_ << extra_bits))) >>
104
0
           Shift;
105
0
  }
106
107
  T mask_;
108
};
109
110
// Mask that can be iterable
111
//
112
// For example, when `SignificantBits` is 16 and `Shift` is zero, this is just
113
// an ordinary 16-bit bitset occupying the low 16 bits of `mask`. When
114
// `SignificantBits` is 8 and `Shift` is 3, abstract bits are represented as
115
// the bytes `0x00` and `0x80`, and it occupies all 64 bits of the bitmask.
116
// If NullifyBitsOnIteration is true (only allowed for Shift == 3),
117
// non zero abstract bit is allowed to have additional bits
118
// (e.g., `0xff`, `0x83` and `0x9c` are ok, but `0x6f` is not).
119
//
120
// For example:
121
//   for (int i : BitMask<uint32_t, 16>(0b101)) -> yields 0, 2
122
//   for (int i : BitMask<uint64_t, 8, 3>(0x0000000080800000)) -> yields 2, 3
123
template <class T, int SignificantBits, int Shift = 0,
124
          bool NullifyBitsOnIteration = false>
125
class BitMask : public NonIterableBitMask<T, SignificantBits, Shift> {
126
  using Base = NonIterableBitMask<T, SignificantBits, Shift>;
127
  static_assert(std::is_unsigned_v<T>);
128
  static_assert(Shift == 0 || Shift == 3);
129
  static_assert(!NullifyBitsOnIteration || Shift == 3);
130
131
 public:
132
0
  explicit BitMask(T mask) : Base(mask) {
133
0
    if (Shift == 3 && !NullifyBitsOnIteration) {
134
0
      ABSL_SWISSTABLE_ASSERT(this->mask_ == (this->mask_ & kMsbs8Bytes));
135
0
    }
136
0
  }
Unexecuted instantiation: absl::container_internal::BitMask<unsigned int, 16, 0, false>::BitMask(unsigned int)
Unexecuted instantiation: absl::container_internal::BitMask<unsigned long, 8, 3, false>::BitMask(unsigned long)
137
  // BitMask is an iterator over the indices of its abstract bits.
138
  using value_type = int;
139
  using iterator = BitMask;
140
  using const_iterator = BitMask;
141
142
  BitMask& operator++() {
143
    if (Shift == 3 && NullifyBitsOnIteration) {
144
      this->mask_ &= kMsbs8Bytes;
145
    }
146
    this->mask_ &= (this->mask_ - 1);
147
    return *this;
148
  }
149
150
0
  uint32_t operator*() const { return Base::LowestBitSet(); }
Unexecuted instantiation: absl::container_internal::BitMask<unsigned long, 8, 3, false>::operator*() const
Unexecuted instantiation: absl::container_internal::BitMask<unsigned int, 16, 0, false>::operator*() const
151
152
0
  BitMask begin() const { return *this; }
Unexecuted instantiation: absl::container_internal::BitMask<unsigned long, 8, 3, false>::begin() const
Unexecuted instantiation: absl::container_internal::BitMask<unsigned int, 16, 0, false>::begin() const
153
0
  BitMask end() const { return BitMask(0); }
Unexecuted instantiation: absl::container_internal::BitMask<unsigned long, 8, 3, false>::end() const
Unexecuted instantiation: absl::container_internal::BitMask<unsigned int, 16, 0, false>::end() const
154
155
 private:
156
  friend bool operator==(const BitMask& a, const BitMask& b) {
157
    return a.mask_ == b.mask_;
158
  }
159
0
  friend bool operator!=(const BitMask& a, const BitMask& b) {
160
0
    return a.mask_ != b.mask_;
161
0
  }
Unexecuted instantiation: absl::container_internal::operator!=(absl::container_internal::BitMask<unsigned long, 8, 3, false> const&, absl::container_internal::BitMask<unsigned long, 8, 3, false> const&)
Unexecuted instantiation: absl::container_internal::operator!=(absl::container_internal::BitMask<unsigned int, 16, 0, false> const&, absl::container_internal::BitMask<unsigned int, 16, 0, false> const&)
162
};
163
164
using h2_t = uint8_t;
165
166
// The values here are selected for maximum performance. See the static asserts
167
// below for details.
168
169
// A `ctrl_t` is a single control byte, which can have one of four
170
// states: empty, deleted, full (which has an associated seven-bit h2_t value)
171
// and the sentinel. They have the following bit patterns:
172
//
173
//      empty: 1 0 0 0 0 0 0 0
174
//    deleted: 1 1 1 1 1 1 1 0
175
//       full: 0 h h h h h h h  // h represents the hash bits.
176
//   sentinel: 1 1 1 1 1 1 1 1
177
//
178
// These values are specifically tuned for SSE-flavored SIMD.
179
// The static_asserts below detail the source of these choices.
180
//
181
// We use an enum class so that when strict aliasing is enabled, the compiler
182
// knows ctrl_t doesn't alias other types.
183
enum class ctrl_t : int8_t {
184
  kEmpty = -128,   // 0b10000000
185
  kDeleted = -2,   // 0b11111110
186
  kSentinel = -1,  // 0b11111111
187
  // Special value used in the slow path of resizing.
188
  kMarkedForSlowTransfer = -3,
189
};
190
static_assert(
191
    (static_cast<int8_t>(ctrl_t::kEmpty) &
192
     static_cast<int8_t>(ctrl_t::kDeleted) &
193
     static_cast<int8_t>(ctrl_t::kSentinel) & 0x80) != 0,
194
    "Special markers need to have the MSB to make checking for them efficient");
195
static_assert(
196
    ctrl_t::kEmpty < ctrl_t::kSentinel && ctrl_t::kDeleted < ctrl_t::kSentinel,
197
    "ctrl_t::kEmpty and ctrl_t::kDeleted must be smaller than "
198
    "ctrl_t::kSentinel to make the SIMD test of IsEmptyOrDeleted() efficient");
199
static_assert(
200
    ctrl_t::kSentinel == static_cast<ctrl_t>(-1),
201
    "ctrl_t::kSentinel must be -1 to elide loading it from memory into SIMD "
202
    "registers (pcmpeqd xmm, xmm)");
203
static_assert(ctrl_t::kEmpty == static_cast<ctrl_t>(-128),
204
              "ctrl_t::kEmpty must be -128 to make the SIMD check for its "
205
              "existence efficient (psignb xmm, xmm)");
206
static_assert(
207
    (~static_cast<int8_t>(ctrl_t::kEmpty) &
208
     ~static_cast<int8_t>(ctrl_t::kDeleted) &
209
     static_cast<int8_t>(ctrl_t::kSentinel) & 0x7F) != 0,
210
    "ctrl_t::kEmpty and ctrl_t::kDeleted must share an unset bit that is not "
211
    "shared by ctrl_t::kSentinel to make the scalar test for "
212
    "MaskEmptyOrDeleted() efficient");
213
static_assert(ctrl_t::kDeleted == static_cast<ctrl_t>(-2),
214
              "ctrl_t::kDeleted must be -2 to make the implementation of "
215
              "ConvertSpecialToEmptyAndFullToDeleted efficient");
216
static_assert(ctrl_t::kEmpty == static_cast<ctrl_t>(-128),
217
              "ctrl_t::kEmpty must be -128 to use saturated subtraction in"
218
              " ConvertSpecialToEmptyAndFullToDeleted");
219
220
// Helpers for checking the state of a control byte.
221
193k
inline bool IsEmpty(ctrl_t c) { return c == ctrl_t::kEmpty; }
222
10.9M
inline bool IsFull(ctrl_t c) {
223
  // Cast `c` to the underlying type instead of casting `0` to `ctrl_t` as `0`
224
  // is not a value in the enum. Both ways are equivalent, but this way makes
225
  // linters happier.
226
10.9M
  return static_cast<std::underlying_type_t<ctrl_t>>(c) >= 0;
227
10.9M
}
228
0
inline bool IsDeleted(ctrl_t c) { return c == ctrl_t::kDeleted; }
229
226k
inline bool IsEmptyOrDeleted(ctrl_t c) { return c < ctrl_t::kSentinel; }
230
231
#ifdef ABSL_INTERNAL_HAVE_SSE2
232
// Quick reference guide for intrinsics used below:
233
//
234
// * __m128i: An XMM (128-bit) word.
235
//
236
// * _mm_setzero_si128: Returns a zero vector.
237
// * _mm_set1_epi8:     Returns a vector with the same i8 in each lane.
238
//
239
// * _mm_subs_epi8:    Saturating-subtracts two i8 vectors.
240
// * _mm_and_si128:    Ands two i128s together.
241
// * _mm_or_si128:     Ors two i128s together.
242
// * _mm_andnot_si128: And-nots two i128s together.
243
//
244
// * _mm_cmpeq_epi8: Component-wise compares two i8 vectors for equality,
245
//                   filling each lane with 0x00 or 0xff.
246
// * _mm_cmpgt_epi8: Same as above, but using > rather than ==.
247
//
248
// * _mm_loadu_si128:  Performs an unaligned load of an i128.
249
// * _mm_storeu_si128: Performs an unaligned store of an i128.
250
//
251
// * _mm_sign_epi8:     Retains, negates, or zeroes each i8 lane of the first
252
//                      argument if the corresponding lane of the second
253
//                      argument is positive, negative, or zero, respectively.
254
// * _mm_movemask_epi8: Selects the sign bit out of each i8 lane and produces a
255
//                      bitmask consisting of those bits.
256
// * _mm_shuffle_epi8:  Selects i8s from the first argument, using the low
257
//                      four bits of each i8 lane in the second argument as
258
//                      indices.
259
260
// https://github.com/abseil/abseil-cpp/issues/209
261
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87853
262
// _mm_cmpgt_epi8 is broken under GCC with -funsigned-char
263
// Work around this by using the portable implementation of Group
264
// when using -funsigned-char under GCC.
265
121k
inline __m128i _mm_cmpgt_epi8_fixed(__m128i a, __m128i b) {
266
#if defined(__GNUC__) && !defined(__clang__)
267
  if (std::is_unsigned_v<char>) {
268
    const __m128i mask = _mm_set1_epi8(0x80);
269
    const __m128i diff = _mm_subs_epi8(b, a);
270
    return _mm_cmpeq_epi8(_mm_and_si128(diff, mask), mask);
271
  }
272
#endif
273
121k
  return _mm_cmpgt_epi8(a, b);
274
121k
}
275
276
struct GroupSse2Impl {
277
  static constexpr size_t kWidth = 16;  // the number of slots per group
278
  // There are only 16 bits, but using uint32_t instead of uint16_t allows for
279
  // better codegen. In particular, there is no blsr instruction for a 16 bit
280
  // register, but there is for a 32 bit register (used in BitMask::operator++).
281
  using MaskInt = uint32_t;
282
  using BitMaskType = BitMask<MaskInt, kWidth>;
283
  using NonIterableBitMaskType = NonIterableBitMask<MaskInt, kWidth>;
284
285
121k
  explicit GroupSse2Impl(const ctrl_t* pos) {
286
121k
    ctrl = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pos));
287
121k
  }
288
289
  // Returns a bitmask representing the positions of slots that match hash.
290
0
  BitMaskType Match(h2_t hash) const {
291
0
    auto match = _mm_set1_epi8(static_cast<char>(hash));
292
0
    return BitMaskType(MoveMask(_mm_cmpeq_epi8(match, ctrl)));
293
0
  }
294
295
  // Returns a bitmask representing the positions of empty slots.
296
0
  NonIterableBitMaskType MaskEmpty() const {
297
#ifdef ABSL_INTERNAL_HAVE_SSSE3
298
    // This only works because ctrl_t::kEmpty is -128.
299
    return NonIterableBitMaskType(MoveMask(_mm_sign_epi8(ctrl, ctrl)));
300
#else
301
0
    auto match = _mm_set1_epi8(static_cast<char>(ctrl_t::kEmpty));
302
0
    return NonIterableBitMaskType(MoveMask(_mm_cmpeq_epi8(match, ctrl)));
303
0
#endif
304
0
  }
305
306
  // Returns a bitmask representing the positions of full slots.
307
  // Note: for `is_small()` tables group may contain the "same" slot twice:
308
  // original and mirrored.
309
0
  BitMaskType MaskFull() const { return BitMaskType(MoveMask(ctrl) ^ 0xffff); }
310
311
  // Returns a bitmask representing the positions of non full slots.
312
  // Note: this includes: kEmpty, kDeleted, kSentinel.
313
  // It is useful in contexts when kSentinel is not present.
314
0
  auto MaskNonFull() const { return BitMaskType(MoveMask(ctrl)); }
315
316
  // Returns a bitmask representing the positions of empty or deleted slots.
317
121k
  NonIterableBitMaskType MaskEmptyOrDeleted() const {
318
121k
    auto special = _mm_set1_epi8(static_cast<char>(ctrl_t::kSentinel));
319
121k
    return NonIterableBitMaskType(
320
121k
        MoveMask(_mm_cmpgt_epi8_fixed(special, ctrl)));
321
121k
  }
322
323
  // Returns a bitmask representing the positions of full or sentinel slots.
324
  // Note: for `is_small()` tables group may contain the "same" slot twice:
325
  // original and mirrored.
326
0
  NonIterableBitMaskType MaskFullOrSentinel() const {
327
0
    auto special = _mm_set1_epi8(static_cast<char>(ctrl_t::kSentinel) - 1);
328
0
    return NonIterableBitMaskType(
329
0
        MoveMask(_mm_cmpgt_epi8_fixed(ctrl, special)));
330
0
  }
331
332
0
  void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
333
    // Take advantage of the fact that kEmpty is already the smallest signed
334
    // char value, and using a saturated subtraction will not affect it.
335
    // All special values have the MSB set, so after an AND with MSBS, we
336
    // are left with -128 for special values and 0 for full. After applying
337
    // subs 2, we arrive at the result of -128(kEmpty) for special and
338
    // -2(kDeleted) for full.
339
0
    auto msbs = _mm_set1_epi8(static_cast<char>(-128));
340
0
    auto twos = _mm_set1_epi8(static_cast<char>(2));
341
0
    auto res = _mm_subs_epi8(_mm_and_si128(msbs, ctrl), twos);
342
0
    _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), res);
343
0
  }
344
345
121k
  static MaskInt MoveMask(__m128i xmm) {
346
121k
    auto mask = static_cast<MaskInt>(_mm_movemask_epi8(xmm));
347
121k
#ifdef __clang__
348
    // TODO(b/472522597): Without the inline asm, clang ends up generating an
349
    // unnecessary movzx to zero the upper bits of the output, but those bits
350
    // are already zero. See https://godbolt.org/z/G6xW1Ecbx.
351
121k
    asm("" : "+r"(mask));  // NOLINT
352
121k
#endif
353
121k
    return mask;
354
121k
  }
355
356
  __m128i ctrl;
357
};
358
#endif  // ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
359
360
#if defined(ABSL_INTERNAL_HAVE_ARM_NEON) && defined(ABSL_IS_LITTLE_ENDIAN)
361
struct GroupAArch64Impl {
362
  static constexpr size_t kWidth = 8;
363
  using BitMaskType = BitMask<uint64_t, kWidth, /*Shift=*/3,
364
                              /*NullifyBitsOnIteration=*/true>;
365
  using NonIterableBitMaskType =
366
      NonIterableBitMask<uint64_t, kWidth, /*Shift=*/3>;
367
368
  explicit GroupAArch64Impl(const ctrl_t* pos) {
369
    ctrl = vld1_u8(reinterpret_cast<const uint8_t*>(pos));
370
  }
371
372
  auto Match(h2_t hash) const {
373
    uint8x8_t dup = vdup_n_u8(hash);
374
    auto mask = vceq_u8(ctrl, dup);
375
    return BitMaskType(vget_lane_u64(vreinterpret_u64_u8(mask), 0));
376
  }
377
378
  auto MaskEmpty() const {
379
    uint64_t mask =
380
        vget_lane_u64(vreinterpret_u64_u8(vceq_s8(
381
                          vdup_n_s8(static_cast<int8_t>(ctrl_t::kEmpty)),
382
                          vreinterpret_s8_u8(ctrl))),
383
                      0);
384
    return NonIterableBitMaskType(mask);
385
  }
386
387
  // Returns a bitmask representing the positions of full slots.
388
  // Note: for `is_small()` tables group may contain the "same" slot twice:
389
  // original and mirrored.
390
  auto MaskFull() const {
391
    uint64_t mask = vget_lane_u64(
392
        vreinterpret_u64_u8(vcge_s8(vreinterpret_s8_u8(ctrl),
393
                                    vdup_n_s8(static_cast<int8_t>(0)))),
394
        0);
395
    return BitMaskType(mask);
396
  }
397
398
  // Returns a bitmask representing the positions of non full slots.
399
  // Note: this includes: kEmpty, kDeleted, kSentinel.
400
  // It is useful in contexts when kSentinel is not present.
401
  auto MaskNonFull() const {
402
    uint64_t mask = vget_lane_u64(
403
        vreinterpret_u64_u8(vclt_s8(vreinterpret_s8_u8(ctrl),
404
                                    vdup_n_s8(static_cast<int8_t>(0)))),
405
        0);
406
    return BitMaskType(mask);
407
  }
408
409
  auto MaskEmptyOrDeleted() const {
410
    uint64_t mask =
411
        vget_lane_u64(vreinterpret_u64_u8(vcgt_s8(
412
                          vdup_n_s8(static_cast<int8_t>(ctrl_t::kSentinel)),
413
                          vreinterpret_s8_u8(ctrl))),
414
                      0);
415
    return NonIterableBitMaskType(mask);
416
  }
417
418
  NonIterableBitMaskType MaskFullOrSentinel() const {
419
    uint64_t mask = vget_lane_u64(
420
        vreinterpret_u64_u8(
421
            vcgt_s8(vreinterpret_s8_u8(ctrl),
422
                    vdup_n_s8(static_cast<int8_t>(ctrl_t::kSentinel) - 1))),
423
        0);
424
    return NonIterableBitMaskType(mask);
425
  }
426
427
  void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
428
    uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(ctrl), 0);
429
    constexpr uint64_t slsbs = 0x0202020202020202ULL;
430
    constexpr uint64_t midbs = 0x7e7e7e7e7e7e7e7eULL;
431
    auto x = slsbs & (mask >> 6);
432
    auto res = (x + midbs) | kMsbs8Bytes;
433
    little_endian::Store64(dst, res);
434
  }
435
436
  uint8x8_t ctrl;
437
};
438
#endif  // ABSL_INTERNAL_HAVE_ARM_NEON && ABSL_IS_LITTLE_ENDIAN
439
440
struct GroupPortableImpl {
441
  static constexpr size_t kWidth = 8;
442
  using BitMaskType = BitMask<uint64_t, kWidth, /*Shift=*/3,
443
                              /*NullifyBitsOnIteration=*/false>;
444
  using NonIterableBitMaskType =
445
      NonIterableBitMask<uint64_t, kWidth, /*Shift=*/3>;
446
447
  explicit GroupPortableImpl(const ctrl_t* pos)
448
0
      : ctrl(little_endian::Load64(pos)) {}
449
450
0
  BitMaskType Match(h2_t hash) const {
451
0
    // For the technique, see:
452
0
    // http://graphics.stanford.edu/~seander/bithacks.html##ValueInWord
453
0
    // (Determine if a word has a byte equal to n).
454
0
    //
455
0
    // Caveat: there are false positives but:
456
0
    // - they only occur if there is a real match
457
0
    // - they never occur on ctrl_t::kEmpty, ctrl_t::kDeleted, ctrl_t::kSentinel
458
0
    // - they will be handled gracefully by subsequent checks in code
459
0
    //
460
0
    // Example:
461
0
    //   v = 0x1716151413121110
462
0
    //   hash = 0x12
463
0
    //   retval = (v - lsbs) & ~v & msbs = 0x0000000080800000
464
0
    constexpr uint64_t lsbs = 0x0101010101010101ULL;
465
0
    auto x = ctrl ^ (lsbs * hash);
466
0
    return BitMaskType((x - lsbs) & ~x & kMsbs8Bytes);
467
0
  }
468
469
0
  auto MaskEmpty() const {
470
0
    return NonIterableBitMaskType((ctrl & ~(ctrl << 6)) & kMsbs8Bytes);
471
0
  }
472
473
  // Returns a bitmask representing the positions of full slots.
474
  // Note: for `is_small()` tables group may contain the "same" slot twice:
475
  // original and mirrored.
476
0
  auto MaskFull() const {
477
0
    return BitMaskType((ctrl ^ kMsbs8Bytes) & kMsbs8Bytes);
478
0
  }
479
480
  // Returns a bitmask representing the positions of non full slots.
481
  // Note: this includes: kEmpty, kDeleted, kSentinel.
482
  // It is useful in contexts when kSentinel is not present.
483
0
  auto MaskNonFull() const { return BitMaskType(ctrl & kMsbs8Bytes); }
484
485
0
  auto MaskEmptyOrDeleted() const {
486
0
    return NonIterableBitMaskType((ctrl & ~(ctrl << 7)) & kMsbs8Bytes);
487
0
  }
488
489
0
  auto MaskFullOrSentinel() const {
490
0
    return NonIterableBitMaskType((~ctrl | (ctrl << 7)) & kMsbs8Bytes);
491
0
  }
492
493
0
  void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
494
0
    constexpr uint64_t lsbs = 0x0101010101010101ULL;
495
0
    auto x = ctrl & kMsbs8Bytes;
496
0
    auto res = (~x + (x >> 7)) & ~lsbs;
497
0
    little_endian::Store64(dst, res);
498
0
  }
499
500
  uint64_t ctrl;
501
};
502
503
#ifdef ABSL_INTERNAL_HAVE_SSE2
504
using Group = GroupSse2Impl;
505
using GroupFullEmptyOrDeleted = GroupSse2Impl;
506
#elif defined(ABSL_INTERNAL_HAVE_ARM_NEON) && defined(ABSL_IS_LITTLE_ENDIAN)
507
using Group = GroupAArch64Impl;
508
// For Aarch64, we use the portable implementation for counting and masking
509
// full, empty or deleted group elements. This is to avoid the latency of moving
510
// between data GPRs and Neon registers when it does not provide a benefit.
511
// Using Neon is profitable when we call Match(), but is not when we don't,
512
// which is the case when we do *EmptyOrDeleted and MaskFull operations.
513
// It is difficult to make a similar approach beneficial on other architectures
514
// such as x86 since they have much lower GPR <-> vector register transfer
515
// latency and 16-wide Groups.
516
using GroupFullEmptyOrDeleted = GroupPortableImpl;
517
#else
518
using Group = GroupPortableImpl;
519
using GroupFullEmptyOrDeleted = GroupPortableImpl;
520
#endif
521
522
}  // namespace container_internal
523
ABSL_NAMESPACE_END
524
}  // namespace absl
525
526
#undef ABSL_SWISSTABLE_ASSERT
527
528
#endif  // ABSL_CONTAINER_INTERNAL_HASHTABLE_CONTROL_BYTES_H_