diff options
-rw-r--r-- | world.hpp | 20 |
1 files changed, 16 insertions, 4 deletions
@@ -16,6 +16,18 @@ using Entity_Id = uint32_t; #define max_components 64 #define max_slinkies (max_entities / slinky_size) +static_assert((!(slinky_size & (slinky_size - 1)))); +/* slinky_size needs to be a power of 2 */ +static consteval int get_slinky_size_bit() { + int i; + int m = slinky_size; + for (i = 0; m; m >>= 1, i++); + if (i == 0) + throw "wtf"; + return i - 1; +} +static constexpr int slinky_size_bit = get_slinky_size_bit(); + struct Slinky { Entity_Id* entities; void* data; @@ -142,9 +154,9 @@ struct World { template <typename C> C& get() { Pool* p = v->pools[ind]; - Slinky& s = p->slinkies[ptr / slinky_size]; + Slinky& s = p->slinkies[ptr >> slinky_size_bit]; int si = &s - p->slinkies; - int off = ptr - (si * slinky_size); + int off = ptr - (si << slinky_size_bit); return *(C*)( (char*)s.data + off * p->size + p->offsets[v->w->get_component_id<C>()] @@ -157,9 +169,9 @@ struct World { Entity_Id entity() { Pool* p = v->pools[ind]; - Slinky& s = p->slinkies[ptr / slinky_size]; + Slinky& s = p->slinkies[ptr >> slinky_size_bit]; int si = &s - p->slinkies; - int off = ptr - (si * slinky_size); + int off = ptr - (si << slinky_size_bit); return s.entities[off]; } }; |