diff options
author | quou <quou@disroot.org> | 2025-03-26 20:37:47 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2025-03-26 20:40:05 +1100 |
commit | 303b28ca6138375fc6e095323eee35b9f6e3d5c0 (patch) | |
tree | e3f376312fbb35eb3331f453c71c352023dfb980 /world.hpp | |
parent | 34f484b8bfeb4572a224def19d1484ae058645e0 (diff) |
replace divide and multiply with shifts
Diffstat (limited to 'world.hpp')
-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]; } }; |