← Back to C-Kernel-Engine Docs Doxygen Source Documentation
murmurhash3.c
Go to the documentation of this file.
1 /*
2  * MurmurHash3 Implementation - Direct copy from HPC_Embeddings
3  *
4  * High-performance 32-bit hash function.
5  */
6 
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 uint32_t ck_murmurhash3(const char* key, uint32_t len, uint32_t seed) {
12  const uint32_t c1 = 0xcc9e2d51;
13  const uint32_t c2 = 0x1b873593;
14  const uint32_t r1 = 15;
15  const uint32_t r2 = 13;
16  const uint32_t m = 5;
17  const uint32_t n = 0xe6546b64;
18 
19  uint32_t hash = seed;
20 
21  const int nblocks = len / 4;
22  const uint32_t *blocks = (const uint32_t *)(key);
23  int i;
24  for (i = 0; i < nblocks; i++) {
25  uint32_t k = blocks[i];
26 
27  k *= c1;
28  k = (k << r1) | (k >> (32 - r1));
29  k *= c2;
30 
31  hash ^= k;
32  hash = (hash << r2) | (hash >> (32 - r2));
33  hash = hash * m + n;
34  }
35 
36  const uint8_t *tail = (const uint8_t *)(key + nblocks * 4);
37  uint32_t k1 = 0;
38 
39  switch (len & 3) {
40  case 3:
41  k1 ^= tail[2] << 16;
42  case 2:
43  k1 ^= tail[1] << 8;
44  case 1:
45  k1 ^= tail[0];
46  k1 *= c1;
47  k1 = (k1 << r1) | (k1 >> (32 - r1));
48  k1 *= c2;
49  hash ^= k1;
50  }
51 
52  hash ^= len;
53  hash ^= hash >> 16;
54  hash *= 0x85ebca6b;
55  hash ^= hash >> 13;
56  hash *= 0xc2b2ae35;
57  hash ^= hash >> 16;
58 
59  return hash;
60 }
61 
62 /* Convenience wrapper for null-terminated strings */
63 static inline uint32_t ck_murmurhash3_str(const char *key, uint32_t seed) {
64  return ck_murmurhash3(key, (uint32_t)strlen(key), seed);
65 }
66 
67 /* Alias for ck_murmurhash3_32 compatibility */
68 uint32_t ck_murmurhash3_32(const void *key, size_t len, uint32_t seed) {
69  return ck_murmurhash3((const char *)key, (uint32_t)len, seed);
70 }
static uint32_t ck_murmurhash3_str(const char *key, uint32_t seed)
Definition: murmurhash3.c:63
uint32_t ck_murmurhash3_32(const void *key, size_t len, uint32_t seed)
Definition: murmurhash3.c:68
uint32_t ck_murmurhash3(const char *key, uint32_t len, uint32_t seed)
Definition: murmurhash3.c:11