-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_idx.h
167 lines (146 loc) · 4.2 KB
/
generate_idx.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include"assist.h"
#define NUMBER64_1 11400714785074694791ULL
#define NUMBER64_2 14029467366897019727ULL
#define NUMBER64_3 1609587929392839161ULL
#define NUMBER64_4 9650029242287828579ULL
#define NUMBER64_5 2870177450012600261ULL
#define hash_get64bits(x) hash_read64_align(x, align)
#define hash_get32bits(x) hash_read32_align(x, align)
#define shifting_hash(x, r) ((x << r) | (x >> (64 - r)))
#define TO64(x) (((U64_INT *)(x))->v)
#define TO32(x) (((U32_INT *)(x))->v)
typedef struct U64_INT
{
uint64_t v;
} U64_INT;
typedef struct U32_INT
{
uint32_t v;
} U32_INT;
uint64_t hash_read64_align(const void *ptr, uint32_t align)
{
if (align == 0)
{
return TO64(ptr);
}
return *(uint64_t *)ptr;
}
uint32_t hash_read32_align(const void *ptr, uint32_t align)
{
if (align == 0)
{
return TO32(ptr);
}
return *(uint32_t *)ptr;
}
/*
Function: string_key_hash_computation()
A hash function for string keys
*/
uint64_t string_key_hash_computation(const void *data, uint64_t length, uint64_t seed, uint32_t align)
{
const uint8_t *p = (const uint8_t *)data;
const uint8_t *end = p + length;
uint64_t hash;
if (length >= 32)
{
const uint8_t *const limitation = end - 32;
uint64_t v1 = seed + NUMBER64_1 + NUMBER64_2;
uint64_t v2 = seed + NUMBER64_2;
uint64_t v3 = seed + 0;
uint64_t v4 = seed - NUMBER64_1;
do
{
v1 += hash_get64bits(p) * NUMBER64_2;
p += 8;
v1 = shifting_hash(v1, 31);
v1 *= NUMBER64_1;
v2 += hash_get64bits(p) * NUMBER64_2;
p += 8;
v2 = shifting_hash(v2, 31);
v2 *= NUMBER64_1;
v3 += hash_get64bits(p) * NUMBER64_2;
p += 8;
v3 = shifting_hash(v3, 31);
v3 *= NUMBER64_1;
v4 += hash_get64bits(p) * NUMBER64_2;
p += 8;
v4 = shifting_hash(v4, 31);
v4 *= NUMBER64_1;
} while (p <= limitation);
hash = shifting_hash(v1, 1) + shifting_hash(v2, 7) + shifting_hash(v3, 12) + shifting_hash(v4, 18);
v1 *= NUMBER64_2;
v1 = shifting_hash(v1, 31);
v1 *= NUMBER64_1;
hash ^= v1;
hash = hash * NUMBER64_1 + NUMBER64_4;
v2 *= NUMBER64_2;
v2 = shifting_hash(v2, 31);
v2 *= NUMBER64_1;
hash ^= v2;
hash = hash * NUMBER64_1 + NUMBER64_4;
v3 *= NUMBER64_2;
v3 = shifting_hash(v3, 31);
v3 *= NUMBER64_1;
hash ^= v3;
hash = hash * NUMBER64_1 + NUMBER64_4;
v4 *= NUMBER64_2;
v4 = shifting_hash(v4, 31);
v4 *= NUMBER64_1;
hash ^= v4;
hash = hash * NUMBER64_1 + NUMBER64_4;
}
else
{
hash = seed + NUMBER64_5;
}
hash += (uint64_t)length;
while (p + 8 <= end)
{
uint64_t k1 = hash_get64bits(p);
k1 *= NUMBER64_2;
k1 = shifting_hash(k1, 31);
k1 *= NUMBER64_1;
hash ^= k1;
hash = shifting_hash(hash, 27) * NUMBER64_1 + NUMBER64_4;
p += 8;
}
if (p + 4 <= end)
{
hash ^= (uint64_t)(hash_get32bits(p)) * NUMBER64_1;
hash = shifting_hash(hash, 23) * NUMBER64_2 + NUMBER64_3;
p += 4;
}
while (p < end)
{
hash ^= (*p) * NUMBER64_5;
hash = shifting_hash(hash, 11) * NUMBER64_1;
p++;
}
hash ^= hash >> 33;
hash *= NUMBER64_2;
hash ^= hash >> 29;
hash *= NUMBER64_3;
hash ^= hash >> 32;
return hash;
}
uint64_t hash(const void *data, uint64_t length, uint64_t seed)
{
if ((((uint64_t)data) & 7) == 0)
{
return string_key_hash_computation(data, length, seed, 1);
}
return string_key_hash_computation(data, length, seed, 0);
}
uint64_t S_HASH(level_hash *level, const uint8_t *key) {
return (hash((void *)key, strlen((const char *)key), level->s_seed));
}
uint64_t F_HASH(level_hash *level, const uint8_t *key) {
return (hash((void *)key, strlen((const char *)key), level->f_seed));
}
uint64_t F_IDX(uint64_t hashKey, uint64_t capacity) {
return hashKey % (capacity / 2);
}
uint64_t S_IDX(uint64_t hashKey, uint64_t capacity) {
return hashKey % (capacity / 2) + capacity / 2;
}