blob: 6f9be32ce9d37e4e3daa3d8816e9181550e55bd1 [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
24#include "spirv/1.1/spirv.h"
25
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.
42 EnumSet() = default;
43 // 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 }
49 // Copy constructor.
50 EnumSet(const EnumSet& other) { *this = other; }
51 // Move constructor. The moved-from set is emptied.
52 EnumSet(EnumSet&& other) {
53 mask_ = other.mask_;
54 overflow_ = std::move(other.overflow_);
55 other.mask_ = 0;
56 other.overflow_.reset(nullptr);
57 }
58 // Assignment operator.
59 EnumSet& operator=(const EnumSet& other) {
60 if (&other != this) {
61 mask_ = other.mask_;
62 overflow_.reset(other.overflow_ ? new OverflowSetType(*other.overflow_)
63 : nullptr);
64 }
65 return *this;
66 }
67
68 // Adds the given enum value to the set. This has no effect if the
69 // enum value is already in the set.
70 void Add(EnumType c) { Add(ToWord(c)); }
71 // Adds the given enum value (as a 32-bit word) to the set. This has no
72 // effect if the enum value is already in the set.
73 void Add(uint32_t word) {
74 if (auto new_bits = AsMask(word)) {
75 mask_ |= new_bits;
76 } else {
77 Overflow().insert(word);
78 }
79 }
80
81 // Returns true if this enum value is in the set.
82 bool Contains(EnumType c) const { return Contains(ToWord(c)); }
83 // Returns true if the enum represented as a 32-bit word is in the set.
84 bool Contains(uint32_t word) const {
85 // We shouldn't call Overflow() since this is a const method.
86 if (auto bits = AsMask(word)) {
Jamie Madillb3714392016-10-27 16:09:06 -040087 return (mask_ & bits) != 0;
David Neto909d7f92016-08-31 14:35:58 -040088 } else if (auto overflow = overflow_.get()) {
89 return overflow->find(word) != overflow->end();
90 }
91 // The word is large, but the set doesn't have large members, so
92 // it doesn't have an overflow set.
93 return false;
94 }
95
96 // Applies f to each enum in the set, in order from smallest enum
97 // value to largest.
98 void ForEach(std::function<void(EnumType)> f) const {
99 for (uint32_t i = 0; i < 64; ++i) {
100 if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
101 }
102 if (overflow_) {
103 for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
104 }
105 }
106
107 private:
108 // Returns the enum value as a uint32_t.
109 uint32_t ToWord(EnumType value) const {
110 static_assert(sizeof(EnumType) <= sizeof(uint32_t),
111 "EnumType must statically castable to uint32_t");
112 return static_cast<uint32_t>(value);
113 }
114
115 // Determines whether the given enum value can be represented
116 // as a bit in a uint64_t mask. If so, then returns that mask bit.
117 // Otherwise, returns 0.
118 uint64_t AsMask(uint32_t word) const {
119 if (word > 63) return 0;
120 return uint64_t(1) << word;
121 }
122
123 // Ensures that overflow_set_ references a set. A new empty set is
124 // allocated if one doesn't exist yet. Returns overflow_set_.
125 OverflowSetType& Overflow() {
126 if (overflow_.get() == nullptr) {
127 overflow_.reset(new OverflowSetType);
128 }
129 return *overflow_;
130 }
131
132 // Enums with values up to 63 are stored as bits in this mask.
133 uint64_t mask_ = 0;
134 // Enums with values larger than 63 are stored in this set.
135 // This set should normally be empty or very small.
136 std::unique_ptr<OverflowSetType> overflow_ = {};
137};
138
139// A set of SpvCapability, optimized for small capability values.
140using CapabilitySet = EnumSet<SpvCapability>;
141
142} // namespace libspirv
143
144#endif // LIBSPIRV_ENUM_SET_H