-
Notifications
You must be signed in to change notification settings - Fork 26
/
blaze.js
58 lines (55 loc) · 2.22 KB
/
blaze.js
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
/*
BlazeHash (64-bit) - still experimental and subject to change.
(c) 2018-2024 bryc (github.com/bryc)
License: Public domain. Attribution appreciated.
Intended for blazing fast hashing of byte arrays. Produces two uncorrelated 32-bit hashes in parallel.
*/
function BlazeHash(key, seed = 0) {
let h1 = 0xcafebabe ^ seed, h2 = 0xdeadbeef ^ seed, i = 0;
for(let b = key.length & -8; i < b; i += 8) {
h1 ^= key[i+3] << 24 | key[i+2] << 16 | key[i+1] << 8 | key[i ];
h2 ^= key[i+7] << 24 | key[i+6] << 16 | key[i+5] << 8 | key[i+4];
h1 ^= Math.imul(h2 ^ (h2 >>> 15), 0xca9b4735);
h2 ^= Math.imul(h1 ^ (h1 >>> 16), 0x38b34ae5);
}
switch(key.length & 7) {
case 7: h2 ^= key[i+6] << 16;
case 6: h2 ^= key[i+5] << 8;
case 5: h2 ^= key[i+4];
case 4: h1 ^= key[i+3] << 24;
case 3: h1 ^= key[i+2] << 16;
case 2: h1 ^= key[i+1] << 8;
case 1: h1 ^= key[i];
h1 ^= Math.imul(h2 ^ (h2 >>> 15), 0xca9b4735);
h2 ^= Math.imul(h1 ^ (h1 >>> 16), 0x38b34ae5);
}
h1 ^= key.length ^ h2;
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return [h1 >>> 0, h2 >>> 0];
//return 2097152 * (h2 >>> 0) + (h1 >>> 11);
}
// BlazeHashB
// An alternate version that reads 4 bytes at a time instead of 8. Not as fast.
function BlazeHashB(key, seed = 0) {
var h1 = 0xcafebabe ^ seed, h2 = 0xdeadbeef ^ seed;
for(var k, i = 0, b = key.length & -4; i < b; i += 4) {
k = key[i+3] << 24 | key[i+2] << 16 | key[i+1] << 8 | key[i];
h1 ^= k; h1 = Math.imul(h1 ^ h1 >>> 16, 597399067) ^ h2;
h2 ^= k; h2 = Math.imul(h2 ^ h2 >>> 24, 374761393) ^ h1;
}
k = 0;
switch(key.length & 3) {
case 3: k ^= key[i+2] << 16;
case 2: k ^= key[i+1] << 8;
case 1: k ^= key[i];
h1 ^= k; h1 = Math.imul(h1 ^ h1 >>> 16, 597399067) ^ h2;
h2 ^= k; h2 = Math.imul(h2 ^ h2 >>> 24, 374761393) ^ h1;
}
h1 ^= key.length;
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return 2097152 * (h2 >>> 0) + (h1 >>> 11);
}