summaryrefslogtreecommitdiff
path: root/src/til_jenkins.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-01-10 15:31:01 -0800
committerVito Caputo <vcaputo@pengaru.com>2023-01-10 23:51:44 -0800
commitb384a96e4a0692d8ed01d4ddf34ccba204e3d7f6 (patch)
tree851a488f77fb07d9999e721624c939b14e58cfbc /src/til_jenkins.c
parent19966a5cb3a8d9cf8edbae225953da042dadfa96 (diff)
til_jenkins: add a rudimentary hash function
Presently just for hashing paths and names
Diffstat (limited to 'src/til_jenkins.c')
-rw-r--r--src/til_jenkins.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/til_jenkins.c b/src/til_jenkins.c
new file mode 100644
index 0000000..0bc74ba
--- /dev/null
+++ b/src/til_jenkins.c
@@ -0,0 +1,26 @@
+#include <stddef.h>
+#include <stdint.h>
+
+#include "til_jenkins.h"
+
+/* we just need something to hash paths/names, it's not super perf sensitive since
+ * the hashes will be cached @ path/name intialization (they don't change).
+ */
+
+/* simple "one at a time" variant from https://en.wikipedia.org/wiki/Jenkins_hash_function */
+uint32_t til_jenkins(const uint8_t *key, size_t length)
+{
+ uint32_t hash = 0;
+
+ for (size_t i = 0; i < length; i++) {
+ hash += key[i];
+ hash += hash << 10;
+ hash ^= hash >> 6;
+ }
+
+ hash += hash << 3;
+ hash ^= hash >> 11;
+ hash += hash << 15;
+
+ return hash;
+}
© All Rights Reserved