From ab3e80f020673b5e47ed9c85eed06cc2c93d4c34 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Tue, 22 Oct 2024 17:39:23 +0200 Subject: [PATCH] simd_map_size --- main.cpp | 5 ++++- simd_map.h | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 9a52f95..a8930a4 100644 --- a/main.cpp +++ b/main.cpp @@ -157,11 +157,13 @@ void test_simd_map_basics() { /* Empty search */ assert(simd_map_find(&smap, 42) == NULL); + assert(simd_map_size(&smap) == 0); /* Insertions */ assert(simd_map_set(&smap, 40, 0) != 0); assert(simd_map_set(&smap, 41, 1) != 0); assert(simd_map_set(&smap, 42, 2) != 0); + assert(simd_map_size(&smap) == 3); /* Searches */ assert(*simd_map_find(&smap, 40) == 0); @@ -179,8 +181,9 @@ void test_simd_map_basics() { for(int i = 0; i < cnt; ++i) { 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) { - assert(*simd_map_find(&smap, i) == (cnt - i)); + assert(*simd_map_find(&smap, i) == (uint32_t)(cnt - i)); } /* Filled free */ diff --git a/simd_map.h b/simd_map.h index 61e2aae..900486f 100644 --- a/simd_map.h +++ b/simd_map.h @@ -89,7 +89,7 @@ static inline SM_ALWAYS_INLINE uint32_t *simd_map_lane_find(simd_map_lane *map_l #endif /* Regular integer code - should have good ILP and cache locality patterns anyways */ 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) { if(SM_UNLIKELY(keys[i] == key)) { return &values[i]; @@ -177,8 +177,15 @@ static inline char simd_map_set(simd_map *map, uint32_t key, uint32_t value) { /** Empties the map - this does not free resources, just makes it reusable! */ static inline void simd_map_erase(simd_map *map) { - map->usage_end = 0; - map->lane_modulo = 0; + map->usage_end = 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. */