aboutsummaryrefslogtreecommitdiff
path: root/src/hash.c
blob: 8940d70bc7e4f4f21bde1d8399a6528a5a207030 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// murmur3 originally by Austin Appleby
// Copyright (c) 2013, ksss
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

#include <stdint.h>

#include <hash.h>

hash_t hash(const void *key, int len, uint64_t seed)
{
	const uint64_t m = 0xc6a4a7935bd1e995ULL;
	const int r = 47;

	uint64_t h = seed ^ (len * m);

	const uint64_t *data = (const uint64_t *)key;
	const uint64_t *end = data + (len / 8);

	while (data != end) {
		uint64_t k = *data++;

		k *= m;
		k ^= k >> r;
		k *= m;

		h ^= k;
		h *= m;
	}

	const unsigned char *data2 = (const unsigned char *)data;

	int b = len & 7;
	if (b >= 7) {
		h ^= ((uint64_t)data2[6]) << 48;
	}
	if (b >= 6) {
		h ^= ((uint64_t)data2[5]) << 40;
	}
	if (b >= 5) {
		h ^= ((uint64_t)data2[4]) << 32;
	}
	if (b >= 4) {
		h ^= ((uint64_t)data2[3]) << 24;
	}
	if (b >= 3) {
		h ^= ((uint64_t)data2[2]) << 16;
	}
	if (b >= 2) {
		h ^= ((uint64_t)data2[1]) << 8;
	}
	if (b >= 1) {
		h ^= ((uint64_t)data2[0]);
		h *= m;
	}

	h ^= h >> r;
	h *= m;
	h ^= h >> r;

	return h;
}