Merge pull request #42 from birth-software/better-hash-conversion

Better hash conversion
This commit is contained in:
David 2024-08-10 21:07:51 +02:00 committed by GitHub
commit c2ce7403ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View File

@ -384,6 +384,14 @@ may_be_unused fn Hash64 hash_bytes(String bytes)
return result; return result;
} }
may_be_unused fn Hash32 hash64_to_hash32(Hash64 hash64)
{
Hash32 low = hash64 & 0xffff;
Hash32 high = (hash64 >> 32) & 0xffff;
Hash32 result = (high << 16) | low;
return result;
}
#if LINK_LIBC == 0 #if LINK_LIBC == 0
#ifdef __linux__ #ifdef __linux__
may_be_unused fn forceinline long syscall0(long n) may_be_unused fn forceinline long syscall0(long n)

View File

@ -153,7 +153,9 @@ fn StringMapPut string_map_get(StringMap* map, String key)
{ {
u32 value = 0; u32 value = 0;
auto long_hash = hash_bytes(key); auto long_hash = hash_bytes(key);
auto hash = (Hash32)long_hash; static_assert(sizeof(long_hash) == sizeof(u64));
auto hash = hash64_to_hash32(long_hash);
static_assert(sizeof(hash) == sizeof(u32));
assert(hash); assert(hash);
auto index = hash & (map->capacity - 1); auto index = hash & (map->capacity - 1);
auto slot = string_map_find_slot(map, index, key); auto slot = string_map_find_slot(map, index, key);
@ -174,7 +176,9 @@ fn StringMapPut string_map_get(StringMap* map, String key)
fn StringMapPut string_map_put(StringMap* map, Arena* arena, String key, u32 value) fn StringMapPut string_map_put(StringMap* map, Arena* arena, String key, u32 value)
{ {
auto long_hash = hash_bytes(key); auto long_hash = hash_bytes(key);
auto hash = (Hash32)long_hash; static_assert(sizeof(long_hash) == sizeof(u64));
auto hash = hash64_to_hash32(long_hash);
static_assert(sizeof(hash) == sizeof(u32));
assert(hash); assert(hash);
auto index = hash & (map->capacity - 1); auto index = hash & (map->capacity - 1);
auto slot = string_map_find_slot(map, index, key); auto slot = string_map_find_slot(map, index, key);