|
| 1 | +#ifndef EXPERIMENTAL_USERS_CLUCIE_BALLOTS_H_ |
| 2 | +#define EXPERIMENTAL_USERS_CLUCIE_BALLOTS_H_ |
| 3 | + |
| 4 | +#include <bitset> |
| 5 | +#include <cassert> |
| 6 | +#include <cstdint> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "Ballot.h" |
| 10 | +#include "VectorUtils.h" |
| 11 | + |
| 12 | +namespace reconvergence { |
| 13 | +struct Ballots : protected std::vector<std::bitset<128>> { |
| 14 | + typedef std::vector<value_type> super; |
| 15 | + |
| 16 | + static const constexpr uint32_t subgroupInvocationSize = |
| 17 | + static_cast<uint32_t>(value_type().size()); |
| 18 | + |
| 19 | + Ballots() : super() {} |
| 20 | + |
| 21 | + explicit Ballots(uint32_t subgroupCount, const value_type &ballot = {}) |
| 22 | + : super(subgroupCount) { |
| 23 | + if (ballot.any()) |
| 24 | + *this = ballot; |
| 25 | + } |
| 26 | + |
| 27 | + Ballots(const Ballots &other) : super(upcast(other)) {} |
| 28 | + |
| 29 | + using super::operator[]; |
| 30 | + |
| 31 | + using super::at; |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief size method |
| 35 | + * @return Returns the number of bits that the Ballots holds. |
| 36 | + */ |
| 37 | + uint32_t size() const { |
| 38 | + return static_cast<uint32_t>(super::size() * subgroupInvocationSize); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief count method |
| 43 | + * @return Returns the number of bits that are set to true. |
| 44 | + */ |
| 45 | + uint32_t count() const { |
| 46 | + uint32_t n = 0u; |
| 47 | + for (const value_type &b : *this) |
| 48 | + n += static_cast<uint32_t>(b.count()); |
| 49 | + return n; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief count method |
| 54 | + * @return Returns the number of bits that are set to true in given subgroup. |
| 55 | + */ |
| 56 | + uint32_t count(uint32_t subgroup) const { |
| 57 | + assert(subgroup < subgroupCount()); |
| 58 | + return static_cast<uint32_t>(at(subgroup).count()); |
| 59 | + } |
| 60 | + |
| 61 | + uint32_t subgroupCount() const { |
| 62 | + return static_cast<uint32_t>(super::size()); |
| 63 | + } |
| 64 | + |
| 65 | + bool test(uint32_t bit) const { |
| 66 | + assert(bit < size()); |
| 67 | + return at(bit / subgroupInvocationSize).test(bit % subgroupInvocationSize); |
| 68 | + } |
| 69 | + |
| 70 | + bool set(uint32_t bit, bool value = true) { |
| 71 | + assert(bit <= size()); |
| 72 | + const bool before = test(bit); |
| 73 | + at(bit / subgroupInvocationSize).set((bit % subgroupInvocationSize), value); |
| 74 | + return before; |
| 75 | + } |
| 76 | + |
| 77 | + void full() { |
| 78 | + const uint32_t bb = size(); |
| 79 | + for (uint32_t b = 0u; b < bb; ++b) |
| 80 | + set(b); |
| 81 | + } |
| 82 | + |
| 83 | + Ballots &setn(uint32_t bits) { |
| 84 | + for (uint32_t i = 0u; i < bits; ++i) |
| 85 | + set(i); |
| 86 | + return *this; |
| 87 | + } |
| 88 | + |
| 89 | + bool all() const { |
| 90 | + const uint32_t gg = subgroupCount(); |
| 91 | + for (uint32_t g = 0u; g < gg; ++g) { |
| 92 | + if (false == at(g).all()) |
| 93 | + return false; |
| 94 | + } |
| 95 | + return (gg != 0u); |
| 96 | + } |
| 97 | + |
| 98 | + bool none() const { |
| 99 | + const uint32_t gg = subgroupCount(); |
| 100 | + for (uint32_t g = 0u; g < gg; ++g) { |
| 101 | + if (false == at(g).none()) |
| 102 | + return false; |
| 103 | + } |
| 104 | + return (gg != 0u); |
| 105 | + } |
| 106 | + |
| 107 | + bool any() const { |
| 108 | + bool res = false; |
| 109 | + const uint32_t gg = subgroupCount(); |
| 110 | + for (uint32_t g = 0u; g < gg; ++g) |
| 111 | + res |= super::at(g).any(); |
| 112 | + return res; |
| 113 | + } |
| 114 | + |
| 115 | + static uint32_t findBit(uint32_t otherFullyQualifiedInvocationID, |
| 116 | + uint32_t otherSubgroupSize) { |
| 117 | + return (((otherFullyQualifiedInvocationID / otherSubgroupSize) * |
| 118 | + subgroupInvocationSize) + |
| 119 | + (otherFullyQualifiedInvocationID % otherSubgroupSize)); |
| 120 | + } |
| 121 | + |
| 122 | + inline const super &upcast(const Ballots &other) const { |
| 123 | + return static_cast<const super &>(other); |
| 124 | + } |
| 125 | + |
| 126 | + Ballots &operator&=(const Ballots &other) { |
| 127 | + assert(subgroupCount() == other.subgroupCount()); |
| 128 | + const uint32_t gg = subgroupCount(); |
| 129 | + for (uint32_t g = 0u; g < gg; ++g) |
| 130 | + super::at(g) = super::at(g) & upcast(other).at(g); |
| 131 | + return *this; |
| 132 | + } |
| 133 | + |
| 134 | + Ballots operator&(const Ballots &other) const { |
| 135 | + Ballots res(*this); |
| 136 | + res &= other; |
| 137 | + return res; |
| 138 | + } |
| 139 | + |
| 140 | + Ballots &operator|=(const Ballots &other) { |
| 141 | + assert(subgroupCount() == other.subgroupCount()); |
| 142 | + const uint32_t gg = subgroupCount(); |
| 143 | + for (uint32_t g = 0u; g < gg; ++g) |
| 144 | + super::at(g) = super::at(g) | upcast(other).at(g); |
| 145 | + return *this; |
| 146 | + } |
| 147 | + |
| 148 | + Ballots operator|(const Ballots &other) const { |
| 149 | + Ballots res(*this); |
| 150 | + res |= other; |
| 151 | + return res; |
| 152 | + } |
| 153 | + |
| 154 | + Ballots &operator<<=(uint32_t bits) { return ((*this) = ((*this) << bits)); } |
| 155 | + |
| 156 | + Ballots operator<<(uint32_t bits) const { |
| 157 | + Ballots res(subgroupCount()); |
| 158 | + if (bits < size() && bits != 0u) { |
| 159 | + for (uint32_t b = 0; b < size() - bits; ++b) |
| 160 | + res.set((b + bits), test(b)); |
| 161 | + } |
| 162 | + return res; |
| 163 | + } |
| 164 | + |
| 165 | + Ballots operator~() const { |
| 166 | + Ballots res(*this); |
| 167 | + const uint32_t gg = subgroupCount(); |
| 168 | + for (uint32_t g = 0u; g < gg; ++g) |
| 169 | + res.at(g) = super::at(g).operator~(); |
| 170 | + return res; |
| 171 | + } |
| 172 | + |
| 173 | + bool operator==(const Ballots &other) const { |
| 174 | + if (super::size() == upcast(other).size()) { |
| 175 | + const uint32_t gg = subgroupCount(); |
| 176 | + for (uint32_t g = 0u; g < gg; ++g) { |
| 177 | + if (at(g) != other[g]) |
| 178 | + return false; |
| 179 | + } |
| 180 | + return true; |
| 181 | + } |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + Ballots &operator=(const Ballots &other) { |
| 186 | + assert((subgroupCount() == other.subgroupCount())); |
| 187 | + const uint32_t gg = subgroupCount(); |
| 188 | + for (uint32_t g = 0u; g < gg; ++g) |
| 189 | + at(g) = other.at(g); |
| 190 | + return *this; |
| 191 | + } |
| 192 | + |
| 193 | + Ballots &operator=(const value_type &forAllGroups) { |
| 194 | + assert(super::size() >= 1u); |
| 195 | + const uint32_t gg = subgroupCount(); |
| 196 | + for (uint32_t g = 0u; g < gg; ++g) |
| 197 | + at(g) = forAllGroups; |
| 198 | + return *this; |
| 199 | + } |
| 200 | +}; |
| 201 | +} // namespace reconvergence |
| 202 | + |
| 203 | +#endif // EXPERIMENTAL_USERS_CLUCIE_BALLOTS_H_ |
0 commit comments