-
Notifications
You must be signed in to change notification settings - Fork 5
/
Hash.xs
380 lines (345 loc) · 9.04 KB
/
Hash.xs
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* -*- tab-width:4 mode:c -*- */
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef HAVE_ZLIB
# include "zlib.h"
# define HASHNAME "crc32_zlib"
#else
# ifdef __SSE4_2__
# define crc32(seed,str,len) crc32_sse42((seed),(str),(len))
# define HASHNAME "crc32_sse42"
# else
# define crc32(seed,str,len) fnv_hash_len((seed),(str),(len))
# define HASHNAME "fnv"
# endif
#endif
#ifdef _MSC_VER
#define INLINE __inline
#else
#define INLINE inline
#endif
#if PERL_VERSION < 10
# define USE_PPPORT_H
#endif
#ifdef USE_PPPORT_H
# include "ppport.h"
#endif
#define CRCSTR(sv) (const unsigned char *)(SvPVX(sv))
/* FNV algorithm from http://isthe.com/chongo/tech/comp/fnv/ */
static INLINE
unsigned fnv_hash_len (unsigned d, const char *s, const int l) {
int c = *s++;
int i = 0;
if (!d) d = 0x01000193;
for (; i < l; i++) {
d = ((d * 0x01000193) ^ *s++) & 0xffffffff;
}
return d;
}
#ifdef __SSE4_2__
#include <stdint.h>
#include <smmintrin.h>
/* Byte-boundary alignment issues */
#define ALIGN_SIZE 0x08UL
#define ALIGN_MASK (ALIGN_SIZE - 1)
#define CALC_CRC(op, crc, type, buf, len) \
do { \
for (; (len) >= sizeof (type); (len) -= sizeof(type), buf += sizeof (type)) { \
(crc) = op((crc), *(type *) (buf)); \
} \
} while(0)
/* iSCSI CRC-32C using the Intel hardware instruction. */
/* for better parallelization with bigger buffers see
http://www.drdobbs.com/parallel/fast-parallelized-crc-computation-using/229401411 */
static INLINE
unsigned int crc32_sse42(unsigned int crc, const char *buf, int len) {
/* XOR the initial CRC with INT_MAX */
crc ^= 0xFFFFFFFF;
/* Align the input to the word boundary */
for (; (len > 0) && ((size_t)buf & ALIGN_MASK); len--, buf++) {
crc = _mm_crc32_u8(crc, *buf);
}
#ifdef __x86_64__ /* or Aarch64... */
CALC_CRC(_mm_crc32_u64, crc, uint64_t, buf, len);
#endif
CALC_CRC(_mm_crc32_u32, crc, uint32_t, buf, len);
CALC_CRC(_mm_crc32_u16, crc, uint16_t, buf, len);
CALC_CRC(_mm_crc32_u8, crc, uint8_t, buf, len);
return (crc ^ 0xFFFFFFFF);
}
#endif
/* #define VEC(G, index, bits) (IV)((*(IV*)(G + ((index) * bits/8))) & ((1<<bits)-1)) */
/* Urban TODO: return SV* and store SV's in values, not just indices.
maybe check for indices optimization as stored now.
use AV * V, not the 2nd half of SvPVX(G). */
static INLINE
IV vec(char *G, IV index, IV bits) {
if (bits == 8)
return *(char*)(G + index) & 255;
else if (bits == 4)
return *(char*)(G + (index / 2)) & 15;
else if (bits == 16) {
short l = *(short*)((short*)G + index) & 65535; /* __UINT16_MAX__ */
return (IV)l;
}
else if (bits == 32) {
#if INTSIZE == 4
int l = *(int*)((int*)G + index); /* __UINT32_MAX__ */
#else
long l = *(long*)((long*)G + index); /* __UINT32_MAX__ */
#endif
return (IV)l;
}
#ifdef HAS_QUAD
else if (bits == 64) {
long long l = *(long long*)((long long*)G + index);
return (IV)l;
}
#endif
die("Unsupported bits %"IVdf"\n", bits);
return 0;
}
static char *
hashname() {
return HASHNAME;
}
MODULE = Perfect::Hash PACKAGE = Perfect::Hash::Hanov
PROTOTYPES: DISABLE
UV
hash(obj, key, seed=0)
SV* obj
SV* key
UV seed;
CODE:
if (items < 3) {
if (SvPOK(key))
RETVAL = crc32(0, CRCSTR(key), SvCUR(key));
else
RETVAL = crc32(0, NULL, 0);
}
else
RETVAL = crc32(seed, CRCSTR(key), SvCUR(key));
OUTPUT:
RETVAL
SV*
perfecthash(ph, key)
SV* ph
SV* key;
CODE:
AV *ref = (AV*)SvRV(ph);
AV *g = (AV*)SvRV(AvARRAY(ref)[0]);
SV **ga = AvARRAY(g);
UV size = AvFILL(g) + 1;
SV **va = AvARRAY((AV*)SvRV(AvARRAY(ref)[1]));
UV h = crc32(0, CRCSTR(key), SvCUR(key)) % size;
IV d = SvIVX(ga[h]);
SV *v = d < 0
? va[-d-1]
: d == 0
? va[h]
: va[crc32(d, CRCSTR(key), SvCUR(key)) % size];
if (AvFILL(ref) > 2) {
SV **keys = AvARRAY((AV*)SvRV(AvARRAY(ref)[3]));
IV iv = SvIVX(v);
RETVAL = ( SvCUR(key) == SvCUR(keys[iv])
&& memEQ(SvPVX(keys[iv]), SvPVX(key), SvCUR(key)))
? SvREFCNT_inc_NN(v) : &PL_sv_undef;
} else {
RETVAL = SvREFCNT_inc_NN(v);
}
OUTPUT:
RETVAL
# TODO: allow setting the hashname via 2nd arg
# and store it in a global symbol
char *
hashname(obj)
SV* obj
CODE:
RETVAL = hashname();
OUTPUT:
RETVAL
MODULE = Perfect::Hash PACKAGE = Perfect::Hash::Urban
PROTOTYPES: DISABLE
IV
iv_perfecthash(ph, key)
SV* ph
SV* key;
CODE:
AV *ref = (AV*)SvRV(ph);
SV *g = AvARRAY(ref)[0];
char *G = SvPVX(g);
IV bits = SvIVX(AvARRAY(ref)[1]);
UV size = 4 * SvCUR(g) / bits; /* 40 = (20 * BITS / 4); 20 = 40 * 4 / BITS */
char *V = G+(size * bits/8);
unsigned long h = crc32(0, CRCSTR(key), SvCUR(key)) % size;
IV d = vec(G, h, bits);
IV v;
if (bits == 32) {
if (d >= 2147483647)
d = (long)(d - 4294967295U);
}
#ifdef HAVE_QUAD
else if (bits == 64) {
if (d >= 9223372036854775807ULL)
d = (IV)((long long)(d - 18446744073709551615ULL));
}
#endif
else {
if (d >= 1<<(bits-1))
d = (d - (1<<bits));
}
v = d < 0
? vec(V, -d-1, bits)
: d == 0 ? vec(V, h, bits)
: vec(V, (UV)crc32(d, CRCSTR(key), SvCUR(key)) % size, bits);
#ifdef DEBUGGING
if (hv_exists((HV*)SvRV(AvARRAY(ref)[2]), "-debug", 6)) {
printf("\nxs: h0=%2lu d=%3ld v=%2ld\t", h,
d>0 ? crc32(d,CRCSTR(key),SvCUR(key))%size : (long)d, (long)v);
}
#endif
if (AvFILL(ref) > 2) {
AV *av = (AV*)SvRV(AvARRAY(ref)[3]);
SV **keys = AvARRAY(av);
if (v >= AvFILL(av)) {
#ifdef DEBUGGING
assert(v < size);
#endif
RETVAL = -1;
}
RETVAL = (SvCUR(key) == SvCUR(keys[v]) && memEQ(SvPVX(keys[v]), SvPVX(key), SvCUR(key)))
? v : -1;
} else {
RETVAL = v;
}
OUTPUT:
RETVAL
IV
nvecget(v, index, bits)
SV* v
IV index
IV bits
CODE:
char *V = SvPVX(v);
RETVAL = vec(V, index, bits);
OUTPUT:
RETVAL
void
nvecset(v, index, bits, value)
SV* v
IV index
IV bits
IV value
CODE:
char *V = SvPVX(v);
if (bits == 8)
*(char*)(V + index) = value & 255;
else if (bits == 4) /* TODO: shift and mask ? */
*(char*)(V + (index / 2)) = value & 15;
else if (bits == 16)
*(short*)((short*)V + index) = value & 65535;
else if (bits == 32)
#if INTSIZE == 4
*(int*)((int*)V + index) = value & 4294967295;
#else
*(long*)((long*)V + index) = value & 4294967295;
#endif
#ifdef HAS_QUAD
else if (bits == 64)
*(long long*)((long long*)V + index) = (long long)value;
#endif
MODULE = Perfect::Hash PACKAGE = Perfect::Hash::Pearson
PROTOTYPES: DISABLE
UV
xs_hash(ph, key)
SV* ph
SV* key
CODE:
AV *ref = (AV*)SvRV(ph);
int size = SvIVX(AvARRAY(ref)[0]);
AV *H = (AV*)SvRV(AvARRAY(ref)[1]);
long d = 0;
int hsize = AvFILL(H);
register int i = 0;
assert(SvTYPE(H) == SVt_PVAV);
if (!SvPOK(key)) {
XSRETURN_UV(0);
}
if (hsize == 256) {
unsigned char *s = (unsigned char*)SvPVX(key);
unsigned char h[256];
for (; i < 256; i++) {
h[i] = SvIVX(AvARRAY(H)[i]);
}
for (i=0; i < SvCUR(key); i++) {
d = h[ d ^ *s++ ];
}
}
else {
unsigned char *s = (unsigned char*)SvPVX(key);
for (; i < SvCUR(key); i++) {
d = SvIVX(AvARRAY(H)[(d ^ *s++) % hsize]);
}
}
RETVAL = d % size;
OUTPUT:
RETVAL
MODULE = Perfect::Hash PACKAGE = Perfect::Hash::Pearson16
PROTOTYPES: DISABLE
UV
hash(ph, key)
SV* ph
SV* key
CODE:
AV *ref = (AV*)SvRV(ph);
int size = SvIVX(AvARRAY(ref)[0]);
AV *H = (AV*)SvRV(AvARRAY(ref)[1]);
unsigned char *s = (unsigned char*)SvPVX(key);
const int len = SvCUR(key);
register int i;
register unsigned short d = 0;
if (!SvPOK(key) || !SvCUR(key)) {
XSRETURN_UV(0);
}
for (i = 0; i < (len % 2 ? len -1 : len); i++) {
d = SvIVX(AvARRAY(H)[ (unsigned short)(d ^ *(unsigned short*)s++) ]);
}
if (len % 2)
d = SvIVX(AvARRAY(H)[ (unsigned short)(d ^ SvPVX(key)[len-1]) ]);
RETVAL = d % size;
OUTPUT:
RETVAL
MODULE = Perfect::Hash PACKAGE = Perfect::Hash::Pearson32
PROTOTYPES: DISABLE
UV
hash(ph, key)
SV* ph
SV* key
CODE:
AV *ref = (AV*)SvRV(ph);
int size = SvIVX(AvARRAY(ref)[0]);
AV *H = (AV*)SvRV(AvARRAY(ref)[1]);
unsigned char *s = (unsigned char*)SvPVX(key);
const int len = SvCUR(key);
unsigned char h[256];
unsigned int *hi = (unsigned int *)h;
int limit;
register int i = 0;
register long d = 0;
if (!SvPOK(key) || !SvCUR(key)) {
XSRETURN_UV(0);
}
for (; i < 256; i++) {
h[i] = SvIVX(AvARRAY(H)[i]);
}
for (i=0; i < len/4; i += 4, s += 4) {
d = hi[ ((unsigned int)d ^ *(unsigned int*)s) % 64 ];
}
for (; i < len; i++, s++) {
d = h[ (unsigned char)((d % 256) ^ *s) ];
}
RETVAL = d % size;
OUTPUT:
RETVAL