blob: b956b4957582fb026c3aadf50552768ed38a22e5 [file] [log] [blame]
David Neto909d7f92016-08-31 14:35:58 -04001// Copyright (c) 2016 Google Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
David Neto909d7f92016-08-31 14:35:58 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
David Neto909d7f92016-08-31 14:35:58 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
David Neto909d7f92016-08-31 14:35:58 -040014
15#ifndef LIBSPIRV_ENUM_SET_H
16#define LIBSPIRV_ENUM_SET_H
17
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <set>
22#include <utility>
23
David Netodbc20492017-03-14 12:43:41 -040024#include "spirv/1.2/spirv.h"
David Neto909d7f92016-08-31 14:35:58 -040025
26namespace libspirv {
27
28// A set of values of a 32-bit enum type.
29// It is fast and compact for the common case, where enum values
30// are at most 63. But it can represent enums with larger values,
31// as may appear in extensions.
32template <typename EnumType>
33class EnumSet {
34 private:
35 // The ForEach method will call the functor on enum values in
36 // enum value order (lowest to highest). To make that easier, use
37 // an ordered set for the overflow values.
38 using OverflowSetType = std::set<uint32_t>;
39
40 public:
41 // Construct an empty set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050042 EnumSet() {}
David Neto909d7f92016-08-31 14:35:58 -040043 // Construct an set with just the given enum value.
44 explicit EnumSet(EnumType c) { Add(c); }
45 // Construct an set from an initializer list of enum values.
46 EnumSet(std::initializer_list<EnumType> cs) {
47 for (auto c : cs) Add(c);
48 }
Lei Zhang063dbea2017-10-25 12:15:51 -040049 EnumSet(uint32_t count, const EnumType* ptr) {
50 for (uint32_t i = 0; i < count; ++i) Add(ptr[i]);
51 }
David Neto909d7f92016-08-31 14:35:58 -040052 // Copy constructor.
53 EnumSet(const EnumSet& other) { *this = other; }
54 // Move constructor. The moved-from set is emptied.
55 EnumSet(EnumSet&& other) {
56 mask_ = other.mask_;
57 overflow_ = std::move(other.overflow_);
58 other.mask_ = 0;
59 other.overflow_.reset(nullptr);
60 }
61 // Assignment operator.
62 EnumSet& operator=(const EnumSet& other) {
63 if (&other != this) {
64 mask_ = other.mask_;
65 overflow_.reset(other.overflow_ ? new OverflowSetType(*other.overflow_)
66 : nullptr);
67 }
68 return *this;
69 }
70
71 // Adds the given enum value to the set. This has no effect if the
72 // enum value is already in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050073 void Add(EnumType c) { AddWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040074
75 // Returns true if this enum value is in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050076 bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040077
78 // Applies f to each enum in the set, in order from smallest enum
79 // value to largest.
80 void ForEach(std::function<void(EnumType)> f) const {
81 for (uint32_t i = 0; i < 64; ++i) {
82 if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
83 }
84 if (overflow_) {
85 for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
86 }
87 }
88
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050089 // Returns true if the set is empty.
90 bool IsEmpty() const {
91 if (mask_) return false;
92 if (overflow_ && !overflow_->empty()) return false;
93 return true;
94 }
95
96 // Returns true if the set contains ANY of the elements of |in_set|,
97 // or if |in_set| is empty.
98 bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
99 if (in_set.IsEmpty()) return true;
100
Lei Zhang063dbea2017-10-25 12:15:51 -0400101 if (mask_ & in_set.mask_) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500102
Lei Zhang063dbea2017-10-25 12:15:51 -0400103 if (!overflow_ || !in_set.overflow_) return false;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500104
105 for (uint32_t item : *in_set.overflow_) {
Lei Zhang063dbea2017-10-25 12:15:51 -0400106 if (overflow_->find(item) != overflow_->end()) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500107 }
108
109 return false;
110 }
111
David Neto909d7f92016-08-31 14:35:58 -0400112 private:
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500113 // Adds the given enum value (as a 32-bit word) to the set. This has no
114 // effect if the enum value is already in the set.
115 void AddWord(uint32_t word) {
116 if (auto new_bits = AsMask(word)) {
117 mask_ |= new_bits;
118 } else {
119 Overflow().insert(word);
120 }
121 }
122
123 // Returns true if the enum represented as a 32-bit word is in the set.
124 bool ContainsWord(uint32_t word) const {
125 // We shouldn't call Overflow() since this is a const method.
126 if (auto bits = AsMask(word)) {
127 return (mask_ & bits) != 0;
128 } else if (auto overflow = overflow_.get()) {
129 return overflow->find(word) != overflow->end();
130 }
131 // The word is large, but the set doesn't have large members, so
132 // it doesn't have an overflow set.
133 return false;
134 }
135
David Neto909d7f92016-08-31 14:35:58 -0400136 // Returns the enum value as a uint32_t.
137 uint32_t ToWord(EnumType value) const {
138 static_assert(sizeof(EnumType) <= sizeof(uint32_t),
139 "EnumType must statically castable to uint32_t");
140 return static_cast<uint32_t>(value);
141 }
142
143 // Determines whether the given enum value can be represented
144 // as a bit in a uint64_t mask. If so, then returns that mask bit.
145 // Otherwise, returns 0.
146 uint64_t AsMask(uint32_t word) const {
147 if (word > 63) return 0;
148 return uint64_t(1) << word;
149 }
150
151 // Ensures that overflow_set_ references a set. A new empty set is
152 // allocated if one doesn't exist yet. Returns overflow_set_.
153 OverflowSetType& Overflow() {
154 if (overflow_.get() == nullptr) {
155 overflow_.reset(new OverflowSetType);
156 }
157 return *overflow_;
158 }
159
160 // Enums with values up to 63 are stored as bits in this mask.
161 uint64_t mask_ = 0;
162 // Enums with values larger than 63 are stored in this set.
163 // This set should normally be empty or very small.
164 std::unique_ptr<OverflowSetType> overflow_ = {};
165};
166
167// A set of SpvCapability, optimized for small capability values.
168using CapabilitySet = EnumSet<SpvCapability>;
169
170} // namespace libspirv
171
172#endif // LIBSPIRV_ENUM_SET_H