simd_map_size

This commit is contained in:
Richard Thier 2024-10-22 17:39:23 +02:00
parent c5e5993001
commit ab3e80f020
2 changed files with 14 additions and 4 deletions

View File

@ -157,11 +157,13 @@ void test_simd_map_basics() {
/* Empty search */ /* Empty search */
assert(simd_map_find(&smap, 42) == NULL); assert(simd_map_find(&smap, 42) == NULL);
assert(simd_map_size(&smap) == 0);
/* Insertions */ /* Insertions */
assert(simd_map_set(&smap, 40, 0) != 0); assert(simd_map_set(&smap, 40, 0) != 0);
assert(simd_map_set(&smap, 41, 1) != 0); assert(simd_map_set(&smap, 41, 1) != 0);
assert(simd_map_set(&smap, 42, 2) != 0); assert(simd_map_set(&smap, 42, 2) != 0);
assert(simd_map_size(&smap) == 3);
/* Searches */ /* Searches */
assert(*simd_map_find(&smap, 40) == 0); assert(*simd_map_find(&smap, 40) == 0);
@ -179,8 +181,9 @@ void test_simd_map_basics() {
for(int i = 0; i < cnt; ++i) { for(int i = 0; i < cnt; ++i) {
assert(simd_map_set(&smap, i, (cnt - i)) != 0); assert(simd_map_set(&smap, i, (cnt - i)) != 0);
} }
assert(simd_map_size(&smap) == 100); // 42->2 should get overwritten
for(int i = 0; i < cnt; ++i) { for(int i = 0; i < cnt; ++i) {
assert(*simd_map_find(&smap, i) == (cnt - i)); assert(*simd_map_find(&smap, i) == (uint32_t)(cnt - i));
} }
/* Filled free */ /* Filled free */

View File

@ -89,7 +89,7 @@ static inline SM_ALWAYS_INLINE uint32_t *simd_map_lane_find(simd_map_lane *map_l
#endif #endif
/* Regular integer code - should have good ILP and cache locality patterns anyways */ /* Regular integer code - should have good ILP and cache locality patterns anyways */
if(lane_modulo == 0) { if(lane_modulo == 0) {
/** Pretty hopeful this can get more easily inlined */ /** Pretty hopeful this can get more easily unrolled / autovectorized */
for(int i = 0; i < SM_LANE_SPAN; ++i) { for(int i = 0; i < SM_LANE_SPAN; ++i) {
if(SM_UNLIKELY(keys[i] == key)) { if(SM_UNLIKELY(keys[i] == key)) {
return &values[i]; return &values[i];
@ -181,6 +181,13 @@ static inline void simd_map_erase(simd_map *map) {
map->lane_modulo = 0; map->lane_modulo = 0;
} }
/** Returns count of elements in the given simd_map */
static inline size_t simd_map_size(simd_map *map) {
return (map->usage_end > 0) ?
(((size_t)(map->usage_end) - 1) * 8 + map->lane_modulo) :
0;
}
/** Remove the given key from the map so its not stored anymore. Returns 1 when found and removed, 0 otherwise. */ /** Remove the given key from the map so its not stored anymore. Returns 1 when found and removed, 0 otherwise. */
static inline int simd_map_remove(simd_map *map, uint32_t key) { static inline int simd_map_remove(simd_map *map, uint32_t key) {
assert(0); // TODO: Implement by swapping to end + shrink! assert(0); // TODO: Implement by swapping to end + shrink!