harec

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

commit c20949d002c58a4b98e8f8120f087f2df7675fd5
parent 2a741eda80208625e5b4bd3f348a107c8b2b7bf6
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Wed,  5 May 2021 17:07:48 -0400

Add fnv1a_{u64,size} and use when appropriate

Signed-off-by: Eyal Sawady <ecs@d2evs.net>

Diffstat:
Minclude/types.h | 4++--
Minclude/util.h | 4+++-
Msrc/types.c | 6+++---
Msrc/util.c | 24++++++++++++++++++++++++
4 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/include/types.h b/include/types.h @@ -60,8 +60,8 @@ struct type_enum_value { char *name; struct type_enum_value *next; union { - intmax_t ival; - uintmax_t uval; + int64_t ival; + uint64_t uval; }; }; diff --git a/include/util.h b/include/util.h @@ -6,7 +6,9 @@ #define FNV1A_INIT 2166136261u uint32_t fnv1a(uint32_t hash, unsigned char c); -uint32_t fnv1a_u32(uint32_t hash, uint32_t c); +uint32_t fnv1a_u32(uint32_t hash, uint32_t u32); +uint32_t fnv1a_u64(uint32_t hash, uint64_t u64); +uint32_t fnv1a_size(uint32_t hash, size_t sz); uint32_t fnv1a_s(uint32_t hash, const char *str); void *xcalloc(size_t n, size_t s); void *xrealloc(void *p, size_t s); diff --git a/src/types.c b/src/types.c @@ -385,7 +385,7 @@ type_hash(const struct type *type) break; case STORAGE_ARRAY: hash = fnv1a_u32(hash, type_hash(type->array.members)); - hash = fnv1a_u32(hash, type->array.length); + hash = fnv1a_size(hash, type->array.length); break; case STORAGE_FUNCTION: hash = fnv1a_u32(hash, type_hash(type->func.result)); @@ -401,7 +401,7 @@ type_hash(const struct type *type) for (struct type_enum_value *value = type->_enum.values; value; value = value->next) { hash = fnv1a_s(hash, value->name); - hash = fnv1a(hash, value->uval); + hash = fnv1a_u64(hash, value->uval); } break; case STORAGE_POINTER: @@ -417,7 +417,7 @@ type_hash(const struct type *type) field; field = field->next) { hash = fnv1a_s(hash, field->name); hash = fnv1a_u32(hash, type_hash(field->type)); - hash = fnv1a_u32(hash, field->offset); + hash = fnv1a_size(hash, field->offset); } break; case STORAGE_TAGGED: diff --git a/src/util.c b/src/util.c @@ -24,6 +24,30 @@ fnv1a_u32(uint32_t hash, uint32_t u32) } uint32_t +fnv1a_u64(uint32_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); + hash = fnv1a(hash, (u64 >> 32) & 0xFF); + hash = fnv1a(hash, (u64 >> 40) & 0xFF); + hash = fnv1a(hash, (u64 >> 48) & 0xFF); + hash = fnv1a(hash, (u64 >> 54) & 0xFF); + return hash; +} + +uint32_t +fnv1a_size(uint32_t hash, size_t sz) +{ + for (size_t i = 0; i < sizeof(sz); i++) { + hash = fnv1a(hash, sz & 0xFF); + sz >>= 8; + } + return hash; +} + +uint32_t fnv1a_s(uint32_t hash, const char *str) { unsigned char c;