Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ComponentMask.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21#include "../core/Log.hpp"
22#include "../core/Types.hpp"
23
24#ifdef _MSC_VER
25#include <intrin.h>
26#endif
27#include <vector>
28
29namespace esengine::ecs {
30
31// =============================================================================
32// ComponentMask Class
33// =============================================================================
34
42public:
43 static constexpr u32 INLINE_WORDS = 2;
44 static constexpr u32 BITS_PER_WORD = 64;
46
47 ComponentMask() = default;
48
50 void set(u32 bit) {
51 if (bit < INLINE_BITS) {
52 inline_[bit / BITS_PER_WORD] |= (u64{1} << (bit % BITS_PER_WORD));
53 } else {
54 u32 idx = (bit - INLINE_BITS) / BITS_PER_WORD;
55 if (idx >= overflow_.size()) {
56 overflow_.resize(idx + 1, 0);
57 }
58 overflow_[idx] |= (u64{1} << ((bit - INLINE_BITS) % BITS_PER_WORD));
59 }
60 }
61
63 void clear(u32 bit) {
64 if (bit < INLINE_BITS) {
65 inline_[bit / BITS_PER_WORD] &= ~(u64{1} << (bit % BITS_PER_WORD));
66 } else {
67 u32 idx = (bit - INLINE_BITS) / BITS_PER_WORD;
68 if (idx < overflow_.size()) {
69 overflow_[idx] &= ~(u64{1} << ((bit - INLINE_BITS) % BITS_PER_WORD));
70 }
71 }
72 }
73
75 [[nodiscard]] bool test(u32 bit) const {
76 if (bit < INLINE_BITS) {
77 return (inline_[bit / BITS_PER_WORD] & (u64{1} << (bit % BITS_PER_WORD))) != 0;
78 }
79 u32 idx = (bit - INLINE_BITS) / BITS_PER_WORD;
80 if (idx >= overflow_.size()) return false;
81 return (overflow_[idx] & (u64{1} << ((bit - INLINE_BITS) % BITS_PER_WORD))) != 0;
82 }
83
85 void reset() {
86 inline_[0] = 0;
87 inline_[1] = 0;
88 overflow_.clear();
89 }
90
92 [[nodiscard]] bool none() const {
93 if (inline_[0] != 0 || inline_[1] != 0) return false;
94 for (auto w : overflow_) {
95 if (w != 0) return false;
96 }
97 return true;
98 }
99
104 template<typename Func>
105 void forEachSet(Func&& fn) const {
106 for (u32 w = 0; w < INLINE_WORDS; ++w) {
107 u64 word = inline_[w];
108 while (word != 0) {
109 u32 bit = ctz64(word);
110 fn(w * BITS_PER_WORD + bit);
111 word &= word - 1;
112 }
113 }
114 for (u32 i = 0; i < overflow_.size(); ++i) {
115 u64 word = overflow_[i];
116 while (word != 0) {
117 u32 bit = ctz64(word);
118 fn(INLINE_BITS + i * BITS_PER_WORD + bit);
119 word &= word - 1;
120 }
121 }
122 }
123
124private:
126 static u32 ctz64(u64 val) {
127 // Defense-in-depth: __builtin_ctzll(0) is UB. Callers guard with
128 // `while (word != 0)`, so this never fires in correct code, but the
129 // guard is always-on (ES_ASSERT is stripped in release). 64 = "no set bit".
130 ES_VERIFY(val != 0, return 64);
131#if defined(__GNUC__) || defined(__clang__)
132 return static_cast<u32>(__builtin_ctzll(val));
133#elif defined(_MSC_VER)
134 unsigned long idx;
135 _BitScanForward64(&idx, val);
136 return static_cast<u32>(idx);
137#else
138 u32 count = 0;
139 while ((val & 1) == 0) { val >>= 1; ++count; }
140 return count;
141#endif
142 }
143
144 u64 inline_[INLINE_WORDS] = {0, 0};
145 std::vector<u64> overflow_;
146};
147
148} // namespace esengine::ecs
Logging utilities for ESEngine.
#define ES_VERIFY(cond,...)
Always-on boundary guard — the release-safe counterpart to ES_ASSERT.
Definition Log.hpp:389
Core type definitions and utilities for ESEngine.
static constexpr u32 INLINE_WORDS
void clear(u32 bit)
Clears the given bit.
static constexpr u32 BITS_PER_WORD
bool test(u32 bit) const
Tests the given bit.
void reset()
Clears all bits.
void set(u32 bit)
Sets the given bit.
static constexpr u32 INLINE_BITS
bool none() const
Returns true if no bits are set.
void forEachSet(Func &&fn) const
Iterates all set bits, calling fn(bitIndex) for each.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45