This repository has been archived by the owner. It is now read-only.
forked from rurban/smhasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_cmetrohash64_1.c
318 lines (268 loc) · 5.35 KB
/
opt_cmetrohash64_1.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Original: metrohash64.cpp
// Port to C: Frank Denis (jedisct1@github)
// Optimized variants: Paul G (paulie-g@github, [email protected])
//
//
// The MIT License (MIT)
//
// Copyright (c) 2015 J. Andrew Rogers
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "cmetrohash.h"
#include "opt_cmetrohash.h"
#define DO_16 {\
register uint64_t v0 = hash + (cread_u64(ptr) * k0); ptr += 8; v0 = crotate_right(v0,33) * k1;\
register uint64_t v1 = hash + (cread_u64(ptr) * k1); ptr += 8; v1 = crotate_right(v1,33) * k2;\
v0 ^= crotate_right(v0 * k0, 35) + v1;\
v1 ^= crotate_right(v1 * k3, 35) + v0;\
hash += v1;\
}
#define DO_8 {\
hash += cread_u64(ptr) * k3; ptr += 8;\
hash ^= crotate_right(hash, 33) * k1;\
}
#define DO_4 {\
hash += cread_u32(ptr) * k3; ptr += 4;\
hash ^= crotate_right(hash, 15) * k1;\
}
#define DO_2 {\
hash += cread_u16(ptr) * k3; ptr += 2;\
hash ^= crotate_right(hash, 13) * k1;\
}
#define DO_1 {\
hash += cread_u8 (ptr) * k3;\
hash ^= crotate_right(hash, 25) * k1;\
}
#define OUT {\
hash ^= crotate_right(hash, 33);\
hash *= k0;\
hash ^= crotate_right(hash, 33);\
memcpy(out, &hash, 8);\
return;\
}
void cmetrohash64_1_optshort(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
{
static const uint64_t k0 = 0xC83A91E1;
static const uint64_t k1 = 0x8648DBDB;
static const uint64_t k2 = 0x7BDEC03B;
static const uint64_t k3 = 0x2F5870A5;
const uint8_t * ptr = key;
const uint8_t * const end = ptr + len;
uint64_t hash = ((((uint64_t) seed) + k2) * k0) + len;
switch (len) {
default:
if (len >= 32)
{
uint64_t v[4];
v[0] = hash;
v[1] = hash;
v[2] = hash;
v[3] = hash;
do
{
v[0] += cread_u64(ptr) * k0; ptr += 8; v[0] = crotate_right(v[0], 29) + v[2];
v[1] += cread_u64(ptr) * k1; ptr += 8; v[1] = crotate_right(v[1], 29) + v[3];
v[2] += cread_u64(ptr) * k2; ptr += 8; v[2] = crotate_right(v[2], 29) + v[0];
v[3] += cread_u64(ptr) * k3; ptr += 8; v[3] = crotate_right(v[3], 29) + v[1];
} while (ptr <= (end - 32));
v[2] ^= crotate_right(((v[0] + v[3]) * k0) + v[1], 33) * k1;
v[3] ^= crotate_right(((v[1] + v[2]) * k1) + v[0], 33) * k0;
v[0] ^= crotate_right(((v[0] + v[2]) * k0) + v[3], 33) * k1;
v[1] ^= crotate_right(((v[1] + v[3]) * k1) + v[2], 33) * k0;
hash += v[0] ^ v[1];
}
if ((end - ptr) >= 16)
{
uint64_t v0 = hash + (cread_u64(ptr) * k0); ptr += 8; v0 = crotate_right(v0, 33) * k1;
uint64_t v1 = hash + (cread_u64(ptr) * k1); ptr += 8; v1 = crotate_right(v1, 33) * k2;
v0 ^= crotate_right(v0 * k0, 35) + v1;
v1 ^= crotate_right(v1 * k3, 35) + v0;
hash += v1;
}
if ((end - ptr) >= 8)
{
hash += cread_u64(ptr) * k3; ptr += 8;
hash ^= crotate_right(hash, 33) * k1;
}
if ((end - ptr) >= 4)
{
hash += cread_u32(ptr) * k3; ptr += 4;
hash ^= crotate_right(hash, 15) * k1;
}
if ((end - ptr) >= 2)
{
hash += cread_u16(ptr) * k3; ptr += 2;
hash ^= crotate_right(hash, 13) * k1;
}
if ((end - ptr) >= 1)
{
hash += cread_u8(ptr) * k3;
hash ^= crotate_right(hash, 25) * k1;
}
hash ^= crotate_right(hash, 33);
hash *= k0;
hash ^= crotate_right(hash, 33);
memcpy(out, &hash, 8);
return;
case 31:
DO_16
DO_8
DO_4
DO_2
DO_1
OUT
case 30:
DO_16
DO_8
DO_4
DO_2
OUT
case 29:
DO_16
DO_8
DO_4
DO_1
OUT
case 28:
DO_16
DO_8
DO_4
OUT
case 27:
DO_16
DO_8
DO_2
DO_1
OUT
case 26:
DO_16
DO_8
DO_2
OUT
case 25:
DO_16
DO_8
DO_1
OUT
case 24:
DO_16
DO_8
OUT
case 23:
DO_16
DO_4
DO_2
DO_1
OUT
case 22:
DO_16
DO_4
DO_2
OUT
case 21:
DO_16
DO_4
DO_1
OUT
case 20:
DO_16
DO_4
OUT
case 19:
DO_16
DO_2
DO_1
OUT
case 18:
DO_16
DO_2
OUT
case 17:
DO_16
DO_1
OUT
case 16:
DO_16
OUT
case 15:
DO_8
DO_4
DO_2
DO_1
OUT
case 14:
DO_8
DO_4
DO_2
OUT
case 13:
DO_8
DO_4
DO_1
OUT
case 12:
DO_8
DO_4
OUT
case 11:
DO_8
DO_2
DO_1
OUT
case 10:
DO_8
DO_2
OUT
case 9:
DO_8
DO_1
OUT
case 8:
DO_8
OUT
case 7:
DO_4
DO_2
DO_1
OUT
case 6:
DO_4
DO_2
OUT
case 5:
DO_4
DO_1
OUT
case 4:
DO_4
OUT
case 3:
DO_2
DO_1
OUT
case 2:
DO_2
OUT
case 1:
DO_1
case 0:
OUT
}
}