api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_murmurhash.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdint.h>
6
7 #include <ecoli_murmurhash.h>
8
9 uint32_t ec_murmurhash3(const void *key, int len, uint32_t seed)
10 {
11         const uint8_t *data = (const uint8_t *)key;
12         const uint8_t *tail;
13         const int nblocks = len / 4;
14         uint32_t h1 = seed;
15         uint32_t k1;
16         const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
17         int i;
18
19         for (i = -nblocks; i; i++) {
20                 k1 = blocks[i];
21
22                 h1 = ec_murmurhash3_add32(h1, k1);
23                 h1 = ec_murmurhash3_mix32(h1);
24         }
25
26         tail = (const uint8_t *)(data + nblocks * 4);
27         k1 = 0;
28
29         switch(len & 3) {
30         case 3: k1 ^= tail[2] << 16; /* fallthrough */
31         case 2: k1 ^= tail[1] << 8; /* fallthrough */
32         case 1: k1 ^= tail[0];
33                 h1 = ec_murmurhash3_add32(h1, k1);
34         };
35
36         /* finalization */
37         h1 ^= len;
38         h1 = ec_murmurhash3_fmix32(h1);
39         return h1;
40 }