Coverage Report

Created: 2026-07-24 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/enum_set.h
Line
Count
Source
1
// Copyright (c) 2023 Google Inc.
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
//     http://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
#include <stddef.h>
16
17
#include <algorithm>
18
#include <cassert>
19
#include <cstdint>
20
#include <functional>
21
#include <initializer_list>
22
#include <iterator>
23
#include <limits>
24
#include <type_traits>
25
#include <vector>
26
27
#ifndef SOURCE_ENUM_SET_H_
28
#define SOURCE_ENUM_SET_H_
29
30
#include "source/latest_version_spirv_header.h"
31
32
namespace spvtools {
33
34
// This container is optimized to store and retrieve unsigned enum values.
35
// The base model for this implementation is an open-addressing hashtable with
36
// linear probing. For small enums (max index < 64), all operations are O(1).
37
//
38
// - Enums are stored in buckets (64 contiguous values max per bucket)
39
// - Buckets ranges don't overlap, but don't have to be contiguous.
40
// - Enums are packed into 64-bits buckets, using 1 bit per enum value.
41
//
42
// Example:
43
//  - MyEnum { A = 0, B = 1, C = 64, D = 65 }
44
//  - 2 buckets are required:
45
//      - bucket 0, storing values in the range [ 0;  64[
46
//      - bucket 1, storing values in the range [64; 128[
47
//
48
// - Buckets are stored in a sorted vector (sorted by bucket range).
49
// - Retrieval is done by computing the theoretical bucket index using the enum
50
// value, and
51
//   doing a linear scan from this position.
52
// - Insertion is done by retrieving the bucket and either:
53
//   - inserting a new bucket in the sorted vector when no buckets has a
54
//   compatible range.
55
//   - setting the corresponding bit in the bucket.
56
//   This means insertion in the middle/beginning can cause a memmove when no
57
//   bucket is available. In our case, this happens at most 23 times for the
58
//   largest enum we have (Opcodes).
59
template <typename T>
60
class EnumSet {
61
 private:
62
  using BucketType = uint64_t;
63
  using ElementType = std::underlying_type_t<T>;
64
  static_assert(std::is_enum_v<T>, "EnumSets only works with enums.");
65
  static_assert(std::is_signed_v<ElementType> == false,
66
                "EnumSet doesn't supports signed enums.");
67
68
  // Each bucket can hold up to `kBucketSize` distinct, contiguous enum values.
69
  // The first value a bucket can hold must be aligned on `kBucketSize`.
70
  struct Bucket {
71
    // bit mask to store `kBucketSize` enums.
72
    BucketType data;
73
    // 1st enum this bucket can represent.
74
    T start;
75
76
0
    bool operator==(const Bucket& other) const {
77
0
      return start == other.start && data == other.data;
78
0
    }
Unexecuted instantiation: spvtools::EnumSet<spv::Capability>::Bucket::operator==(spvtools::EnumSet<spv::Capability>::Bucket const&) const
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::Bucket::operator==(spvtools::EnumSet<spvtools::Extension>::Bucket const&) const
79
  };
80
81
  // How many distinct values can a bucket hold? 1 bit per value.
82
  static constexpr size_t kBucketSize = sizeof(BucketType) * 8ULL;
83
84
 public:
85
  class Iterator {
86
   public:
87
    typedef Iterator self_type;
88
    typedef T value_type;
89
    typedef T& reference;
90
    typedef T* pointer;
91
    typedef std::forward_iterator_tag iterator_category;
92
    typedef size_t difference_type;
93
94
    Iterator(const Iterator& other)
95
12.9k
        : set_(other.set_),
96
12.9k
          bucketIndex_(other.bucketIndex_),
97
12.9k
          bucketOffset_(other.bucketOffset_) {}
spvtools::EnumSet<spv::Capability>::Iterator::Iterator(spvtools::EnumSet<spv::Capability>::Iterator const&)
Line
Count
Source
95
11.8k
        : set_(other.set_),
96
11.8k
          bucketIndex_(other.bucketIndex_),
97
11.8k
          bucketOffset_(other.bucketOffset_) {}
spvtools::EnumSet<spvtools::Extension>::Iterator::Iterator(spvtools::EnumSet<spvtools::Extension>::Iterator const&)
Line
Count
Source
95
1.11k
        : set_(other.set_),
96
1.11k
          bucketIndex_(other.bucketIndex_),
97
1.11k
          bucketOffset_(other.bucketOffset_) {}
98
99
2.94k
    Iterator& operator++() {
100
93.1k
      do {
101
93.1k
        if (bucketIndex_ >= set_->buckets_.size()) {
102
0
          bucketIndex_ = set_->buckets_.size();
103
0
          bucketOffset_ = 0;
104
0
          break;
105
0
        }
106
107
93.1k
        if (bucketOffset_ + 1 == kBucketSize) {
108
1.45k
          bucketOffset_ = 0;
109
1.45k
          ++bucketIndex_;
110
91.7k
        } else {
111
91.7k
          ++bucketOffset_;
112
91.7k
        }
113
114
93.1k
      } while (bucketIndex_ < set_->buckets_.size() &&
115
92.0k
               !set_->HasEnumAt(bucketIndex_, bucketOffset_));
116
2.94k
      return *this;
117
2.94k
    }
spvtools::EnumSet<spv::Capability>::Iterator::operator++()
Line
Count
Source
99
2.94k
    Iterator& operator++() {
100
93.1k
      do {
101
93.1k
        if (bucketIndex_ >= set_->buckets_.size()) {
102
0
          bucketIndex_ = set_->buckets_.size();
103
0
          bucketOffset_ = 0;
104
0
          break;
105
0
        }
106
107
93.1k
        if (bucketOffset_ + 1 == kBucketSize) {
108
1.45k
          bucketOffset_ = 0;
109
1.45k
          ++bucketIndex_;
110
91.7k
        } else {
111
91.7k
          ++bucketOffset_;
112
91.7k
        }
113
114
93.1k
      } while (bucketIndex_ < set_->buckets_.size() &&
115
92.0k
               !set_->HasEnumAt(bucketIndex_, bucketOffset_));
116
2.94k
      return *this;
117
2.94k
    }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::Iterator::operator++()
118
119
    Iterator operator++(int) {
120
      Iterator old = *this;
121
      operator++();
122
      return old;
123
    }
124
125
2.60k
    T operator*() const {
126
2.60k
      assert(set_->HasEnumAt(bucketIndex_, bucketOffset_) &&
127
2.60k
             "operator*() called on an invalid iterator.");
128
2.60k
      return GetValueFromBucket(set_->buckets_[bucketIndex_], bucketOffset_);
129
2.60k
    }
spvtools::EnumSet<spv::Capability>::Iterator::operator*() const
Line
Count
Source
125
2.60k
    T operator*() const {
126
      assert(set_->HasEnumAt(bucketIndex_, bucketOffset_) &&
127
2.60k
             "operator*() called on an invalid iterator.");
128
2.60k
      return GetValueFromBucket(set_->buckets_[bucketIndex_], bucketOffset_);
129
2.60k
    }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::Iterator::operator*() const
130
131
4.53k
    bool operator!=(const Iterator& other) const {
132
4.53k
      return set_ != other.set_ || bucketOffset_ != other.bucketOffset_ ||
133
2.76k
             bucketIndex_ != other.bucketIndex_;
134
4.53k
    }
spvtools::EnumSet<spv::Capability>::Iterator::operator!=(spvtools::EnumSet<spv::Capability>::Iterator const&) const
Line
Count
Source
131
4.53k
    bool operator!=(const Iterator& other) const {
132
4.53k
      return set_ != other.set_ || bucketOffset_ != other.bucketOffset_ ||
133
2.76k
             bucketIndex_ != other.bucketIndex_;
134
4.53k
    }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::Iterator::operator!=(spvtools::EnumSet<spvtools::Extension>::Iterator const&) const
135
136
    bool operator==(const Iterator& other) const {
137
      return !(operator!=(other));
138
    }
139
140
    Iterator& operator=(const Iterator& other) {
141
      set_ = other.set_;
142
      bucketIndex_ = other.bucketIndex_;
143
      bucketOffset_ = other.bucketOffset_;
144
      return *this;
145
    }
146
147
   private:
148
    Iterator(const EnumSet* set, size_t bucketIndex, ElementType bucketOffset)
149
16.8k
        : set_(set), bucketIndex_(bucketIndex), bucketOffset_(bucketOffset) {}
spvtools::EnumSet<spv::Capability>::Iterator::Iterator(spvtools::EnumSet<spv::Capability> const*, unsigned long, unsigned int)
Line
Count
Source
149
15.7k
        : set_(set), bucketIndex_(bucketIndex), bucketOffset_(bucketOffset) {}
spvtools::EnumSet<spvtools::Extension>::Iterator::Iterator(spvtools::EnumSet<spvtools::Extension> const*, unsigned long, unsigned int)
Line
Count
Source
149
1.11k
        : set_(set), bucketIndex_(bucketIndex), bucketOffset_(bucketOffset) {}
150
151
   private:
152
    const EnumSet* set_ = nullptr;
153
    // Index of the bucket in the vector.
154
    size_t bucketIndex_ = 0;
155
    // Offset in bits in the current bucket.
156
    ElementType bucketOffset_ = 0;
157
158
    friend class EnumSet;
159
  };
160
161
  // Required to allow the use of std::inserter.
162
  using value_type = T;
163
  using const_iterator = Iterator;
164
  using iterator = Iterator;
165
166
 public:
167
1.92k
  iterator cbegin() const noexcept {
168
1.92k
    auto it = iterator(this, /* bucketIndex= */ 0, /* bucketOffset= */ 0);
169
1.92k
    if (buckets_.size() == 0) {
170
760
      return it;
171
760
    }
172
173
    // The iterator has the logic to find the next valid bit. If the value 0
174
    // is not stored, use it to find the next valid bit.
175
1.16k
    if (!HasEnumAt(it.bucketIndex_, it.bucketOffset_)) {
176
336
      ++it;
177
336
    }
178
179
1.16k
    return it;
180
1.92k
  }
spvtools::EnumSet<spv::Capability>::cbegin() const
Line
Count
Source
167
1.92k
  iterator cbegin() const noexcept {
168
1.92k
    auto it = iterator(this, /* bucketIndex= */ 0, /* bucketOffset= */ 0);
169
1.92k
    if (buckets_.size() == 0) {
170
760
      return it;
171
760
    }
172
173
    // The iterator has the logic to find the next valid bit. If the value 0
174
    // is not stored, use it to find the next valid bit.
175
1.16k
    if (!HasEnumAt(it.bucketIndex_, it.bucketOffset_)) {
176
336
      ++it;
177
336
    }
178
179
1.16k
    return it;
180
1.92k
  }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::cbegin() const
181
182
1.92k
  iterator begin() const noexcept { return cbegin(); }
spvtools::EnumSet<spv::Capability>::begin() const
Line
Count
Source
182
1.92k
  iterator begin() const noexcept { return cbegin(); }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::begin() const
183
184
1.92k
  iterator cend() const noexcept {
185
1.92k
    return iterator(this, buckets_.size(), /* bucketOffset= */ 0);
186
1.92k
  }
spvtools::EnumSet<spv::Capability>::cend() const
Line
Count
Source
184
1.92k
  iterator cend() const noexcept {
185
1.92k
    return iterator(this, buckets_.size(), /* bucketOffset= */ 0);
186
1.92k
  }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::cend() const
187
188
1.92k
  iterator end() const noexcept { return cend(); }
spvtools::EnumSet<spv::Capability>::end() const
Line
Count
Source
188
1.92k
  iterator end() const noexcept { return cend(); }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::end() const
189
190
  // Creates an empty set.
191
127k
  EnumSet() : buckets_(0), size_(0) {}
spvtools::EnumSet<spv::Capability>::EnumSet()
Line
Count
Source
191
83.7k
  EnumSet() : buckets_(0), size_(0) {}
spvtools::EnumSet<spvtools::Extension>::EnumSet()
Line
Count
Source
191
44.1k
  EnumSet() : buckets_(0), size_(0) {}
192
193
  // Creates a set and store `value` in it.
194
  EnumSet(T value) : EnumSet() { insert(value); }
195
196
  // Creates a set and stores each `values` in it.
197
  EnumSet(std::initializer_list<T> values) : EnumSet() {
198
    for (auto item : values) {
199
      insert(item);
200
    }
201
  }
202
203
  // Creates a set, and insert `count` enum values pointed by `array` in it.
204
2.09k
  EnumSet(ElementType count, const T* array) : EnumSet() {
205
3.58k
    for (ElementType i = 0; i < count; i++) {
206
1.49k
      insert(array[i]);
207
1.49k
    }
208
2.09k
  }
spvtools::EnumSet<spvtools::Extension>::EnumSet(unsigned int, spvtools::Extension const*)
Line
Count
Source
204
682
  EnumSet(ElementType count, const T* array) : EnumSet() {
205
1.51k
    for (ElementType i = 0; i < count; i++) {
206
836
      insert(array[i]);
207
836
    }
208
682
  }
spvtools::EnumSet<spv::Capability>::EnumSet(unsigned int, spv::Capability const*)
Line
Count
Source
204
1.41k
  EnumSet(ElementType count, const T* array) : EnumSet() {
205
2.06k
    for (ElementType i = 0; i < count; i++) {
206
654
      insert(array[i]);
207
654
    }
208
1.41k
  }
209
210
  // Creates a set initialized with the content of the range [begin; end[.
211
  template <class InputIt>
212
43.1k
  EnumSet(InputIt begin, InputIt end) : EnumSet() {
213
43.1k
    for (; begin != end; ++begin) {
214
0
      insert(*begin);
215
0
    }
216
43.1k
  }
spvtools::EnumSet<spvtools::Extension>::EnumSet<spvtools::Extension const*>(spvtools::Extension const*, spvtools::Extension const*)
Line
Count
Source
212
43.1k
  EnumSet(InputIt begin, InputIt end) : EnumSet() {
213
43.1k
    for (; begin != end; ++begin) {
214
0
      insert(*begin);
215
0
    }
216
43.1k
  }
Unexecuted instantiation: spvtools::EnumSet<spv::Capability>::EnumSet<spv::Capability const*>(spv::Capability const*, spv::Capability const*)
217
218
  // Copies the EnumSet `other` into a new EnumSet.
219
  EnumSet(const EnumSet& other)
220
      : buckets_(other.buckets_), size_(other.size_) {}
221
222
  // Moves the EnumSet `other` into a new EnumSet.
223
  EnumSet(EnumSet&& other)
224
0
      : buckets_(std::move(other.buckets_)), size_(other.size_) {}
Unexecuted instantiation: spvtools::EnumSet<spv::Capability>::EnumSet(spvtools::EnumSet<spv::Capability>&&)
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::EnumSet(spvtools::EnumSet<spvtools::Extension>&&)
225
226
  // Deep-copies the EnumSet `other` into this EnumSet.
227
9.60k
  EnumSet& operator=(const EnumSet& other) {
228
9.60k
    buckets_ = other.buckets_;
229
9.60k
    size_ = other.size_;
230
9.60k
    return *this;
231
9.60k
  }
232
233
  // Matches std::unordered_set::insert behavior.
234
12.9k
  std::pair<iterator, bool> insert(const T& value) {
235
12.9k
    const size_t index = FindBucketForValue(value);
236
12.9k
    const ElementType offset = ComputeBucketOffset(value);
237
238
12.9k
    if (index >= buckets_.size() ||
239
10.9k
        buckets_[index].start != ComputeBucketStart(value)) {
240
10.9k
      size_ += 1;
241
10.9k
      InsertBucketFor(index, value);
242
10.9k
      return std::make_pair(Iterator(this, index, offset), true);
243
10.9k
    }
244
245
2.02k
    auto& bucket = buckets_[index];
246
2.02k
    const auto mask = ComputeMaskForValue(value);
247
2.02k
    if (bucket.data & mask) {
248
0
      return std::make_pair(Iterator(this, index, offset), false);
249
0
    }
250
251
2.02k
    size_ += 1;
252
2.02k
    bucket.data |= ComputeMaskForValue(value);
253
2.02k
    return std::make_pair(Iterator(this, index, offset), true);
254
2.02k
  }
spvtools::EnumSet<spv::Capability>::insert(spv::Capability const&)
Line
Count
Source
234
11.8k
  std::pair<iterator, bool> insert(const T& value) {
235
11.8k
    const size_t index = FindBucketForValue(value);
236
11.8k
    const ElementType offset = ComputeBucketOffset(value);
237
238
11.8k
    if (index >= buckets_.size() ||
239
9.88k
        buckets_[index].start != ComputeBucketStart(value)) {
240
9.88k
      size_ += 1;
241
9.88k
      InsertBucketFor(index, value);
242
9.88k
      return std::make_pair(Iterator(this, index, offset), true);
243
9.88k
    }
244
245
1.98k
    auto& bucket = buckets_[index];
246
1.98k
    const auto mask = ComputeMaskForValue(value);
247
1.98k
    if (bucket.data & mask) {
248
0
      return std::make_pair(Iterator(this, index, offset), false);
249
0
    }
250
251
1.98k
    size_ += 1;
252
1.98k
    bucket.data |= ComputeMaskForValue(value);
253
1.98k
    return std::make_pair(Iterator(this, index, offset), true);
254
1.98k
  }
spvtools::EnumSet<spvtools::Extension>::insert(spvtools::Extension const&)
Line
Count
Source
234
1.11k
  std::pair<iterator, bool> insert(const T& value) {
235
1.11k
    const size_t index = FindBucketForValue(value);
236
1.11k
    const ElementType offset = ComputeBucketOffset(value);
237
238
1.11k
    if (index >= buckets_.size() ||
239
1.07k
        buckets_[index].start != ComputeBucketStart(value)) {
240
1.07k
      size_ += 1;
241
1.07k
      InsertBucketFor(index, value);
242
1.07k
      return std::make_pair(Iterator(this, index, offset), true);
243
1.07k
    }
244
245
44
    auto& bucket = buckets_[index];
246
44
    const auto mask = ComputeMaskForValue(value);
247
44
    if (bucket.data & mask) {
248
0
      return std::make_pair(Iterator(this, index, offset), false);
249
0
    }
250
251
44
    size_ += 1;
252
44
    bucket.data |= ComputeMaskForValue(value);
253
44
    return std::make_pair(Iterator(this, index, offset), true);
254
44
  }
255
256
  // Inserts `value` in the set if possible.
257
  // Similar to `std::unordered_set::insert`, except the hint is ignored.
258
  // Returns an iterator to the inserted element, or the element preventing
259
  // insertion.
260
  iterator insert(const_iterator, const T& value) {
261
    return insert(value).first;
262
  }
263
264
  // Inserts `value` in the set if possible.
265
  // Similar to `std::unordered_set::insert`, except the hint is ignored.
266
  // Returns an iterator to the inserted element, or the element preventing
267
  // insertion.
268
  iterator insert(const_iterator, T&& value) { return insert(value).first; }
269
270
  // Inserts all the values in the range [`first`; `last[.
271
  // Similar to `std::unordered_set::insert`.
272
  template <class InputIt>
273
0
  void insert(InputIt first, InputIt last) {
274
0
    for (auto it = first; it != last; ++it) {
275
0
      insert(*it);
276
0
    }
277
0
  }
278
279
  // Removes the value `value` into the set.
280
  // Similar to `std::unordered_set::erase`.
281
  // Returns the number of erased elements.
282
0
  size_t erase(const T& value) {
283
0
    const size_t index = FindBucketForValue(value);
284
0
    if (index >= buckets_.size() ||
285
0
        buckets_[index].start != ComputeBucketStart(value)) {
286
0
      return 0;
287
0
    }
288
289
0
    auto& bucket = buckets_[index];
290
0
    const auto mask = ComputeMaskForValue(value);
291
0
    if (!(bucket.data & mask)) {
292
0
      return 0;
293
0
    }
294
295
0
    size_ -= 1;
296
0
    bucket.data &= ~mask;
297
0
    if (bucket.data == 0) {
298
0
      buckets_.erase(buckets_.cbegin() + index);
299
0
    }
300
0
    return 1;
301
0
  }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::erase(spvtools::Extension const&)
Unexecuted instantiation: spvtools::EnumSet<spv::Capability>::erase(spv::Capability const&)
302
303
  // Returns true if `value` is present in the set.
304
442k
  bool contains(T value) const {
305
442k
    const size_t index = FindBucketForValue(value);
306
442k
    if (index >= buckets_.size() ||
307
326k
        buckets_[index].start != ComputeBucketStart(value)) {
308
127k
      return false;
309
127k
    }
310
315k
    auto& bucket = buckets_[index];
311
315k
    return bucket.data & ComputeMaskForValue(value);
312
442k
  }
spvtools::EnumSet<spv::Capability>::contains(spv::Capability) const
Line
Count
Source
304
438k
  bool contains(T value) const {
305
438k
    const size_t index = FindBucketForValue(value);
306
438k
    if (index >= buckets_.size() ||
307
326k
        buckets_[index].start != ComputeBucketStart(value)) {
308
123k
      return false;
309
123k
    }
310
315k
    auto& bucket = buckets_[index];
311
315k
    return bucket.data & ComputeMaskForValue(value);
312
438k
  }
spvtools::EnumSet<spvtools::Extension>::contains(spvtools::Extension) const
Line
Count
Source
304
4.10k
  bool contains(T value) const {
305
4.10k
    const size_t index = FindBucketForValue(value);
306
4.10k
    if (index >= buckets_.size() ||
307
3.96k
        buckets_[index].start != ComputeBucketStart(value)) {
308
3.96k
      return false;
309
3.96k
    }
310
140
    auto& bucket = buckets_[index];
311
140
    return bucket.data & ComputeMaskForValue(value);
312
4.10k
  }
313
314
  // Returns the 1 if `value` is present in the set, `0` otherwise.
315
  inline size_t count(T value) const { return contains(value) ? 1 : 0; }
316
317
  // Returns true if the set is holds no values.
318
107k
  inline bool empty() const { return size_ == 0; }
spvtools::EnumSet<spvtools::Extension>::empty() const
Line
Count
Source
318
44.0k
  inline bool empty() const { return size_ == 0; }
spvtools::EnumSet<spv::Capability>::empty() const
Line
Count
Source
318
63.6k
  inline bool empty() const { return size_ == 0; }
319
320
  // Returns the number of enums stored in this set.
321
0
  size_t size() const { return size_; }
322
323
  // Returns true if this set contains at least one value contained in `in_set`.
324
  // Note: If `in_set` is empty, this function returns true.
325
55.2k
  bool HasAnyOf(const EnumSet<T>& in_set) const {
326
55.2k
    if (in_set.empty()) {
327
47.6k
      return true;
328
47.6k
    }
329
330
7.63k
    auto lhs = buckets_.cbegin();
331
7.63k
    auto rhs = in_set.buckets_.cbegin();
332
333
10.7k
    while (lhs != buckets_.cend() && rhs != in_set.buckets_.cend()) {
334
10.7k
      if (lhs->start == rhs->start) {
335
7.81k
        if (lhs->data & rhs->data) {
336
          // At least 1 bit is shared. Early return.
337
7.63k
          return true;
338
7.63k
        }
339
340
180
        lhs++;
341
180
        rhs++;
342
180
        continue;
343
7.81k
      }
344
345
      // LHS bucket is smaller than the current RHS bucket. Catching up on RHS.
346
2.95k
      if (lhs->start < rhs->start) {
347
2.80k
        lhs++;
348
2.80k
        continue;
349
2.80k
      }
350
351
      // Otherwise, RHS needs to catch up on LHS.
352
148
      rhs++;
353
148
    }
354
355
0
    return false;
356
7.63k
  }
spvtools::EnumSet<spv::Capability>::HasAnyOf(spvtools::EnumSet<spv::Capability> const&) const
Line
Count
Source
325
54.6k
  bool HasAnyOf(const EnumSet<T>& in_set) const {
326
54.6k
    if (in_set.empty()) {
327
47.6k
      return true;
328
47.6k
    }
329
330
6.95k
    auto lhs = buckets_.cbegin();
331
6.95k
    auto rhs = in_set.buckets_.cbegin();
332
333
9.86k
    while (lhs != buckets_.cend() && rhs != in_set.buckets_.cend()) {
334
9.86k
      if (lhs->start == rhs->start) {
335
7.13k
        if (lhs->data & rhs->data) {
336
          // At least 1 bit is shared. Early return.
337
6.95k
          return true;
338
6.95k
        }
339
340
180
        lhs++;
341
180
        rhs++;
342
180
        continue;
343
7.13k
      }
344
345
      // LHS bucket is smaller than the current RHS bucket. Catching up on RHS.
346
2.72k
      if (lhs->start < rhs->start) {
347
2.72k
        lhs++;
348
2.72k
        continue;
349
2.72k
      }
350
351
      // Otherwise, RHS needs to catch up on LHS.
352
0
      rhs++;
353
0
    }
354
355
0
    return false;
356
6.95k
  }
spvtools::EnumSet<spvtools::Extension>::HasAnyOf(spvtools::EnumSet<spvtools::Extension> const&) const
Line
Count
Source
325
676
  bool HasAnyOf(const EnumSet<T>& in_set) const {
326
676
    if (in_set.empty()) {
327
0
      return true;
328
0
    }
329
330
676
    auto lhs = buckets_.cbegin();
331
676
    auto rhs = in_set.buckets_.cbegin();
332
333
904
    while (lhs != buckets_.cend() && rhs != in_set.buckets_.cend()) {
334
904
      if (lhs->start == rhs->start) {
335
676
        if (lhs->data & rhs->data) {
336
          // At least 1 bit is shared. Early return.
337
676
          return true;
338
676
        }
339
340
0
        lhs++;
341
0
        rhs++;
342
0
        continue;
343
676
      }
344
345
      // LHS bucket is smaller than the current RHS bucket. Catching up on RHS.
346
228
      if (lhs->start < rhs->start) {
347
80
        lhs++;
348
80
        continue;
349
80
      }
350
351
      // Otherwise, RHS needs to catch up on LHS.
352
148
      rhs++;
353
148
    }
354
355
0
    return false;
356
676
  }
357
358
 private:
359
  // Returns the index of the last bucket in which `value` could be stored.
360
1.22M
  static constexpr inline size_t ComputeLargestPossibleBucketIndexFor(T value) {
361
1.22M
    return static_cast<size_t>(value) / kBucketSize;
362
1.22M
  }
spvtools::EnumSet<spv::Capability>::ComputeLargestPossibleBucketIndexFor(spv::Capability)
Line
Count
Source
360
1.22M
  static constexpr inline size_t ComputeLargestPossibleBucketIndexFor(T value) {
361
1.22M
    return static_cast<size_t>(value) / kBucketSize;
362
1.22M
  }
spvtools::EnumSet<spvtools::Extension>::ComputeLargestPossibleBucketIndexFor(spvtools::Extension)
Line
Count
Source
360
7.21k
  static constexpr inline size_t ComputeLargestPossibleBucketIndexFor(T value) {
361
7.21k
    return static_cast<size_t>(value) / kBucketSize;
362
7.21k
  }
363
364
  // Returns the smallest enum value that could be contained in the same bucket
365
  // as `value`.
366
784k
  static constexpr inline T ComputeBucketStart(T value) {
367
784k
    return static_cast<T>(kBucketSize *
368
784k
                          ComputeLargestPossibleBucketIndexFor(value));
369
784k
  }
spvtools::EnumSet<spv::Capability>::ComputeBucketStart(spv::Capability)
Line
Count
Source
366
780k
  static constexpr inline T ComputeBucketStart(T value) {
367
780k
    return static_cast<T>(kBucketSize *
368
780k
                          ComputeLargestPossibleBucketIndexFor(value));
369
780k
  }
spvtools::EnumSet<spvtools::Extension>::ComputeBucketStart(spvtools::Extension)
Line
Count
Source
366
4.23k
  static constexpr inline T ComputeBucketStart(T value) {
367
4.23k
    return static_cast<T>(kBucketSize *
368
4.23k
                          ComputeLargestPossibleBucketIndexFor(value));
369
4.23k
  }
370
371
  //  Returns the index of the bit that corresponds to `value` in the bucket.
372
343k
  static constexpr inline ElementType ComputeBucketOffset(T value) {
373
343k
    return static_cast<ElementType>(value) % kBucketSize;
374
343k
  }
spvtools::EnumSet<spv::Capability>::ComputeBucketOffset(spv::Capability)
Line
Count
Source
372
340k
  static constexpr inline ElementType ComputeBucketOffset(T value) {
373
340k
    return static_cast<ElementType>(value) % kBucketSize;
374
340k
  }
spvtools::EnumSet<spvtools::Extension>::ComputeBucketOffset(spvtools::Extension)
Line
Count
Source
372
2.42k
  static constexpr inline ElementType ComputeBucketOffset(T value) {
373
2.42k
    return static_cast<ElementType>(value) % kBucketSize;
374
2.42k
  }
375
376
  // Returns the bitmask used to represent the enum `value` in its bucket.
377
319k
  static constexpr inline BucketType ComputeMaskForValue(T value) {
378
319k
    return 1ULL << ComputeBucketOffset(value);
379
319k
  }
spvtools::EnumSet<spv::Capability>::ComputeMaskForValue(spv::Capability)
Line
Count
Source
377
319k
  static constexpr inline BucketType ComputeMaskForValue(T value) {
378
319k
    return 1ULL << ComputeBucketOffset(value);
379
319k
  }
spvtools::EnumSet<spvtools::Extension>::ComputeMaskForValue(spvtools::Extension)
Line
Count
Source
377
228
  static constexpr inline BucketType ComputeMaskForValue(T value) {
378
228
    return 1ULL << ComputeBucketOffset(value);
379
228
  }
380
381
  // Returns the `enum` stored in `bucket` at `offset`.
382
  // `offset` is the bit-offset in the bucket storage.
383
  static constexpr inline T GetValueFromBucket(const Bucket& bucket,
384
2.60k
                                               BucketType offset) {
385
2.60k
    return static_cast<T>(static_cast<ElementType>(bucket.start) + offset);
386
2.60k
  }
spvtools::EnumSet<spv::Capability>::GetValueFromBucket(spvtools::EnumSet<spv::Capability>::Bucket const&, unsigned long)
Line
Count
Source
384
2.60k
                                               BucketType offset) {
385
2.60k
    return static_cast<T>(static_cast<ElementType>(bucket.start) + offset);
386
2.60k
  }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::GetValueFromBucket(spvtools::EnumSet<spvtools::Extension>::Bucket const&, unsigned long)
387
388
  // For a given enum `value`, finds the bucket index that could contain this
389
  // value. If no such bucket is found, the index at which the new bucket should
390
  // be inserted is returned.
391
455k
  size_t FindBucketForValue(T value) const {
392
    // Set is empty, insert at 0.
393
455k
    if (buckets_.size() == 0) {
394
10.8k
      return 0;
395
10.8k
    }
396
397
444k
    const T wanted_start = ComputeBucketStart(value);
398
444k
    assert(buckets_.size() > 0 &&
399
444k
           "Size must not be 0 here. Has the code above changed?");
400
444k
    size_t index = std::min(buckets_.size() - 1,
401
444k
                            ComputeLargestPossibleBucketIndexFor(value));
402
403
    // This loops behaves like std::upper_bound with a reverse iterator.
404
    // Buckets are sorted. 3 main cases:
405
    //  - The bucket matches
406
    //    => returns the bucket index.
407
    //  - The found bucket is larger
408
    //    => scans left until it finds the correct bucket, or insertion point.
409
    //  - The found bucket is smaller
410
    //    => We are at the end, so we return past-end index for insertion.
411
671k
    for (; buckets_[index].start >= wanted_start; index--) {
412
424k
      if (index == 0) {
413
197k
        return 0;
414
197k
      }
415
424k
    }
416
417
247k
    return index + 1;
418
444k
  }
spvtools::EnumSet<spv::Capability>::FindBucketForValue(spv::Capability) const
Line
Count
Source
391
450k
  size_t FindBucketForValue(T value) const {
392
    // Set is empty, insert at 0.
393
450k
    if (buckets_.size() == 0) {
394
8.59k
      return 0;
395
8.59k
    }
396
397
441k
    const T wanted_start = ComputeBucketStart(value);
398
441k
    assert(buckets_.size() > 0 &&
399
441k
           "Size must not be 0 here. Has the code above changed?");
400
441k
    size_t index = std::min(buckets_.size() - 1,
401
441k
                            ComputeLargestPossibleBucketIndexFor(value));
402
403
    // This loops behaves like std::upper_bound with a reverse iterator.
404
    // Buckets are sorted. 3 main cases:
405
    //  - The bucket matches
406
    //    => returns the bucket index.
407
    //  - The found bucket is larger
408
    //    => scans left until it finds the correct bucket, or insertion point.
409
    //  - The found bucket is smaller
410
    //    => We are at the end, so we return past-end index for insertion.
411
668k
    for (; buckets_[index].start >= wanted_start; index--) {
412
424k
      if (index == 0) {
413
196k
        return 0;
414
196k
      }
415
424k
    }
416
417
244k
    return index + 1;
418
441k
  }
spvtools::EnumSet<spvtools::Extension>::FindBucketForValue(spvtools::Extension) const
Line
Count
Source
391
5.22k
  size_t FindBucketForValue(T value) const {
392
    // Set is empty, insert at 0.
393
5.22k
    if (buckets_.size() == 0) {
394
2.24k
      return 0;
395
2.24k
    }
396
397
2.98k
    const T wanted_start = ComputeBucketStart(value);
398
2.98k
    assert(buckets_.size() > 0 &&
399
2.98k
           "Size must not be 0 here. Has the code above changed?");
400
2.98k
    size_t index = std::min(buckets_.size() - 1,
401
2.98k
                            ComputeLargestPossibleBucketIndexFor(value));
402
403
    // This loops behaves like std::upper_bound with a reverse iterator.
404
    // Buckets are sorted. 3 main cases:
405
    //  - The bucket matches
406
    //    => returns the bucket index.
407
    //  - The found bucket is larger
408
    //    => scans left until it finds the correct bucket, or insertion point.
409
    //  - The found bucket is smaller
410
    //    => We are at the end, so we return past-end index for insertion.
411
3.00k
    for (; buckets_[index].start >= wanted_start; index--) {
412
184
      if (index == 0) {
413
156
        return 0;
414
156
      }
415
184
    }
416
417
2.82k
    return index + 1;
418
2.98k
  }
419
420
  // Creates a new bucket to store `value` and inserts it at `index`.
421
  // If the `index` is past the end, the bucket is inserted at the end of the
422
  // vector.
423
10.9k
  void InsertBucketFor(size_t index, T value) {
424
10.9k
    const T bucket_start = ComputeBucketStart(value);
425
10.9k
    Bucket bucket = {1ULL << ComputeBucketOffset(value), bucket_start};
426
10.9k
    auto it = buckets_.emplace(buckets_.begin() + index, std::move(bucket));
427
10.9k
#if defined(NDEBUG)
428
10.9k
    (void)it;  // Silencing unused variable warning.
429
#else
430
    assert(std::next(it) == buckets_.end() ||
431
           std::next(it)->start > bucket_start);
432
    assert(it == buckets_.begin() || std::prev(it)->start < bucket_start);
433
#endif
434
10.9k
  }
spvtools::EnumSet<spv::Capability>::InsertBucketFor(unsigned long, spv::Capability)
Line
Count
Source
423
9.88k
  void InsertBucketFor(size_t index, T value) {
424
9.88k
    const T bucket_start = ComputeBucketStart(value);
425
9.88k
    Bucket bucket = {1ULL << ComputeBucketOffset(value), bucket_start};
426
9.88k
    auto it = buckets_.emplace(buckets_.begin() + index, std::move(bucket));
427
9.88k
#if defined(NDEBUG)
428
9.88k
    (void)it;  // Silencing unused variable warning.
429
#else
430
    assert(std::next(it) == buckets_.end() ||
431
           std::next(it)->start > bucket_start);
432
    assert(it == buckets_.begin() || std::prev(it)->start < bucket_start);
433
#endif
434
9.88k
  }
spvtools::EnumSet<spvtools::Extension>::InsertBucketFor(unsigned long, spvtools::Extension)
Line
Count
Source
423
1.07k
  void InsertBucketFor(size_t index, T value) {
424
1.07k
    const T bucket_start = ComputeBucketStart(value);
425
1.07k
    Bucket bucket = {1ULL << ComputeBucketOffset(value), bucket_start};
426
1.07k
    auto it = buckets_.emplace(buckets_.begin() + index, std::move(bucket));
427
1.07k
#if defined(NDEBUG)
428
1.07k
    (void)it;  // Silencing unused variable warning.
429
#else
430
    assert(std::next(it) == buckets_.end() ||
431
           std::next(it)->start > bucket_start);
432
    assert(it == buckets_.begin() || std::prev(it)->start < bucket_start);
433
#endif
434
1.07k
  }
435
436
  // Returns true if the bucket at `bucketIndex/ stores the enum at
437
  // `bucketOffset`, false otherwise.
438
93.1k
  bool HasEnumAt(size_t bucketIndex, BucketType bucketOffset) const {
439
93.1k
    assert(bucketIndex < buckets_.size());
440
93.1k
    assert(bucketOffset < kBucketSize);
441
93.1k
    return buckets_[bucketIndex].data & (1ULL << bucketOffset);
442
93.1k
  }
spvtools::EnumSet<spv::Capability>::HasEnumAt(unsigned long, unsigned long) const
Line
Count
Source
438
93.1k
  bool HasEnumAt(size_t bucketIndex, BucketType bucketOffset) const {
439
93.1k
    assert(bucketIndex < buckets_.size());
440
    assert(bucketOffset < kBucketSize);
441
93.1k
    return buckets_[bucketIndex].data & (1ULL << bucketOffset);
442
93.1k
  }
Unexecuted instantiation: spvtools::EnumSet<spvtools::Extension>::HasEnumAt(unsigned long, unsigned long) const
443
444
  // Returns true if `lhs` and `rhs` hold the exact same values.
445
0
  friend bool operator==(const EnumSet& lhs, const EnumSet& rhs) {
446
0
    if (lhs.size_ != rhs.size_) {
447
0
      return false;
448
0
    }
449
450
0
    if (lhs.buckets_.size() != rhs.buckets_.size()) {
451
0
      return false;
452
0
    }
453
0
    return lhs.buckets_ == rhs.buckets_;
454
0
  }
Unexecuted instantiation: spvtools::operator==(spvtools::EnumSet<spv::Capability> const&, spvtools::EnumSet<spv::Capability> const&)
Unexecuted instantiation: spvtools::operator==(spvtools::EnumSet<spvtools::Extension> const&, spvtools::EnumSet<spvtools::Extension> const&)
455
456
  // Returns true if `lhs` and `rhs` hold at least 1 different value.
457
0
  friend bool operator!=(const EnumSet& lhs, const EnumSet& rhs) {
458
0
    return !(lhs == rhs);
459
0
  }
Unexecuted instantiation: spvtools::operator!=(spvtools::EnumSet<spv::Capability> const&, spvtools::EnumSet<spv::Capability> const&)
Unexecuted instantiation: spvtools::operator!=(spvtools::EnumSet<spvtools::Extension> const&, spvtools::EnumSet<spvtools::Extension> const&)
460
461
  // Storage for the buckets.
462
  std::vector<Bucket> buckets_;
463
  // How many enums is this set storing.
464
  size_t size_ = 0;
465
};
466
467
// A set of spv::Capability.
468
using CapabilitySet = EnumSet<spv::Capability>;
469
470
}  // namespace spvtools
471
472
#endif  // SOURCE_ENUM_SET_H_