data-structures-c
Loading...
Searching...
No Matches
fnvhash.h
Go to the documentation of this file.
1/* fnvhash.h
2 *
3 * Copyright (C) 2023 abxh
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 * See the file LICENSE included with this distribution for more
10 * information. */
11
22#pragma once
23
24#include <stdint.h> // uint8_t, uint32_t
25#include <stdlib.h> // size_t, NULL
26
34static inline uint32_t fnvhash_32_str(const char *char_p)
35{
36 uint32_t hash = 0x811c9dc5;
37 while (*char_p != '\0') {
38 const uint32_t c = (uint32_t) * (char_p++);
39 hash ^= c;
40 hash *= 0x01000193;
41 }
42 return hash;
43}
44
53static inline uint32_t fnvhash_32(const uint8_t *char_p, const size_t length)
54{
55 uint32_t hash = 0x811c9dc5;
56 for (size_t i = 0; i < length; i++) {
57 hash ^= *(char_p++);
58 hash *= 0x01000193;
59 }
60 return hash;
61}
62
63// vim: ft=c
static uint32_t fnvhash_32_str(const char *char_p)
Get the FNV-1a 32-bit hash of a char array (ending with a \0).
Definition fnvhash.h:34
static uint32_t fnvhash_32(const uint8_t *char_p, const size_t length)
Get the FNV-1a 32-bit hash of a string of bytes.
Definition fnvhash.h:53