-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathhash.h
76 lines (64 loc) · 1.71 KB
/
hash.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
/* A Universally Unique IDentifier (UUID) compliant with RFC 4122.
*
* Each of the parts is stored in host byte order, but the parts themselves are
* ordered from left to right. That is, (parts[0] >> 24) is the first 8 bits
* of the UUID when output in the standard form, and (parts[3] & 0xff) is the
* final 8 bits.
*/
struct uuid {
uint32_t parts[4];
};
static inline uint32_t hash_rot(uint32_t x, int k)
{
return (x << k) | (x >> (32 - k));
}
/* Murmurhash by Austin Appleby,
* from https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
*/
static inline uint32_t mhash_add__(uint32_t hash, uint32_t data)
{
/* zero-valued 'data' will not change the 'hash' value */
if (!data)
return hash;
data *= 0xcc9e2d51;
data = hash_rot(data, 15);
data *= 0x1b873593;
return hash ^ data;
}
static inline uint32_t mhash_add(uint32_t hash, uint32_t data)
{
hash = mhash_add__(hash, data);
hash = hash_rot(hash, 13);
return hash * 5 + 0xe6546b64;
}
static inline uint32_t mhash_finish(uint32_t hash)
{
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
static inline uint32_t hash_add(uint32_t hash, uint32_t data)
{
return mhash_add(hash, data);
}
static inline uint32_t hash_finish(uint32_t hash, uint32_t final)
{
return mhash_finish(hash ^ final);
}
static inline uint32_t hash_2words(uint32_t x, uint32_t y)
{
return hash_finish(hash_add(hash_add(x, 0), y), 8);
}
static inline uint32_t hash_int(uint32_t x, uint32_t basis)
{
return hash_2words(x, basis);
}