harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit baa263225dfba47e1fa987e3f9c2ba88fd79e2e3
parent d06c0e1b569aaf4dc2c23af1ab360d0c61ba69bc
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu, 14 Jan 2021 09:42:39 -0500

type store: correctly hash uint64_t values

Diffstat:
Minclude/util.h | 3++-
Msrc/type_store.c | 18+++++++++---------
Msrc/util.c | 10++++++++++
3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/include/util.h b/include/util.h @@ -5,7 +5,8 @@ #define FNV1A_INIT 14695981039346656037UL -uint64_t fnv1a(uint64_t hash, char c); +uint64_t fnv1a(uint64_t hash, unsigned char c); +uint64_t fnv1a_u64(uint64_t hash, uint64_t c); uint64_t fnv1a_s(uint64_t hash, const char *str); void *xcalloc(size_t n, size_t s); void *xrealloc(void *p, size_t s); diff --git a/src/type_store.c b/src/type_store.c @@ -321,37 +321,37 @@ type_hash(struct type_store *store, const struct type *type) ident = ident->ns) { hash = fnv1a_s(hash, ident->name); } - hash = fnv1a(hash, type_hash(store, type->alias.type)); + hash = fnv1a_u64(hash, type_hash(store, type->alias.type)); break; case TYPE_STORAGE_ARRAY: - hash = fnv1a(hash, type_hash(store, type->array.members)); - hash = fnv1a(hash, type->array.length); + hash = fnv1a_u64(hash, type_hash(store, type->array.members)); + hash = fnv1a_u64(hash, type->array.length); break; case TYPE_STORAGE_FUNCTION: - hash = fnv1a(hash, type_hash(store, type->func.result)); + hash = fnv1a_u64(hash, type_hash(store, type->func.result)); hash = fnv1a(hash, type->func.variadism); hash = fnv1a(hash, type->func.flags); for (struct type_func_param *param = type->func.params; param; param = param->next) { - hash = fnv1a(hash, type_hash(store, param->type)); + hash = fnv1a_u64(hash, type_hash(store, param->type)); } break; case TYPE_STORAGE_ENUM: assert(0); // TODO case TYPE_STORAGE_POINTER: hash = fnv1a(hash, type->pointer.flags); - hash = fnv1a(hash, type_hash(store, type->pointer.referent)); + hash = fnv1a_u64(hash, type_hash(store, type->pointer.referent)); break; case TYPE_STORAGE_SLICE: - hash = fnv1a(hash, type_hash(store, type->array.members)); + hash = fnv1a_u64(hash, type_hash(store, type->array.members)); break; case TYPE_STORAGE_STRUCT: case TYPE_STORAGE_UNION: for (const struct struct_field *field = type->struct_union.fields; field; field = field->next) { hash = fnv1a_s(hash, field->name); - hash = fnv1a(hash, type_hash(store, field->type)); - hash = fnv1a(hash, field->offset); + hash = fnv1a_u64(hash, type_hash(store, field->type)); + hash = fnv1a_u64(hash, field->offset); } break; case TYPE_STORAGE_TAGGED_UNION: diff --git a/src/util.c b/src/util.c @@ -10,6 +10,16 @@ fnv1a(uint64_t hash, unsigned char c) } uint64_t +fnv1a_u64(uint64_t hash, uint64_t u64) +{ + hash = fnv1a(hash, (u64) & 0xFF); + hash = fnv1a(hash, (u64 >> 8) & 0xFF); + hash = fnv1a(hash, (u64 >> 16) & 0xFF); + hash = fnv1a(hash, (u64 >> 24) & 0xFF); + return hash; +} + +uint64_t fnv1a_s(uint64_t hash, const char *str) { unsigned char c;