From a515dfb3b8f8e999362db7a6b52b3104c03b750a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 1 Nov 2024 23:46:08 -0400 Subject: Import quark sources Signed-off-by: Ian Moffett --- compiler/hash.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 compiler/hash.c (limited to 'compiler/hash.c') diff --git a/compiler/hash.c b/compiler/hash.c new file mode 100644 index 0000000..a63691c --- /dev/null +++ b/compiler/hash.c @@ -0,0 +1,35 @@ +/* + * 32-bit FNV-1a hash function. + * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team. + * Provided under the BSD 3-Clause license. + */ + +#include "hash.h" + +hash_t +hash_data(const void *data, size_t length) +{ + hash_t hash; + + hash = FNV_OFFSET_BASIS; + for (size_t i = 0; i < length; i++) { + hash ^= ((uint8_t*)data)[i]; + hash *= FNV_PRIME; + } + + return hash; +} + +hash_t +hash_string(const char *str) +{ + hash_t hash; + + hash = FNV_OFFSET_BASIS; + while (*str) { + hash ^= (uint8_t)*str++; + hash *= FNV_PRIME; + } + + return hash; +} -- cgit v1.2.3