Coverage Report

Created: 2026-07-16 06:43

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