From 34b814349b58480b235674f892b70b41cd728652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 14 Feb 2021 13:28:18 +0100 Subject: [PATCH] util: add a sdbm hash implementation --- util.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util.h b/util.h index d2059379..aa9fc8ba 100644 --- a/util.h +++ b/util.h @@ -1,5 +1,6 @@ #pragma once +#include #include #define ALEN(v) (sizeof(v) / sizeof((v)[0])) @@ -21,3 +22,16 @@ thrd_err_as_string(int thrd_err) return "unknown error"; } + +static inline uint64_t +sdbm_hash(const char *s) +{ + uint64_t hash = 0; + + for (; *s != '\0'; s++) { + int c = *s; + hash = c + (hash << 6) + (hash << 16) - hash; + } + + return hash; +}