-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmurmurhash3_simple_rp.js
116 lines (86 loc) · 3.12 KB
/
murmurhash3_simple_rp.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
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
/**
* JS Implementation of MurmurHash3 (r152) (as of May 10, 2013)
* A classical, non-unrolled version.
* The other, unrolled version obviously requires more code space in the CPU,
* so you have to consider how often and on how long strings
* you are going to call the hashing function.
*
* @author <a href="mailto:roland@simplify.ee">Roland Pihlakas</a>
* @see http://github.com/levitation/murmurhash-js
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
* @see https://code.google.com/p/smhasher/
*
* @param {string} key ASCII only
* @param {number} seed positive integer only
* @return {number} 32-bit positive integer hash
*/
function murmurhash3_32_simple_rp(key, seed) {
var keyLength, tailLength, bodyLength, h1, k1, i, c1_low, c1_high, c2_low, c2_high, c3;
keyLength = key.length;
tailLength = keyLength & 3;
bodyLength = keyLength - tailLength;
h1 = seed;
//c1 = 0xcc9e2d51;
c1_low = 0x2d51;
c1_high = 0xcc9e0000;
//c2 = 0x1b873593;
c2_low = 0x3593;
c2_high = 0x1b870000;
c3 = 0xe6546b64;
//----------
// body
i = 0;
while (i < bodyLength) {
k1 =
((key.charCodeAt(i) & 0xff)) |
((key.charCodeAt(++i) & 0xff) << 8) |
((key.charCodeAt(++i) & 0xff) << 16) |
((key.charCodeAt(++i) & 0xff) << 24);
++i;
//k1 *= c1;
k1 = (c1_high * k1 | 0) + (c1_low * k1);
//k1 = ROTL32(k1,15);
k1 = (k1 << 15) | (k1 >>> 17);
//k1 *= c2;
k1 = (c2_high * k1 | 0) + (c2_low * k1);
//h1 ^= k1;
h1 ^= k1;
//h1 = ROTL32(h1,13);
h1 = (h1 << 13) | (h1 >>> 19);
//h1 = h1*5+0xe6546b64;
h1 = h1 * 5 + c3;
} //while (i < bodyLength) {
//----------
// tail
k1 = 0;
switch (tailLength) {
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
//k1 *= c1;
k1 = (c1_high * k1 | 0) + (c1_low * k1);
//k1 = ROTL32(k1,15);
k1 = (k1 << 15) | (k1 >>> 17);
//k1 *= c2;
k1 = (c2_high * k1 | 0) + (c2_low * k1);
//h1 ^= k1;
h1 ^= k1;
} //switch (tailLength) {
//----------
// finalization
h1 ^= keyLength;
//h1 = fmix32(h1);
{
//h ^= h >> 16;
h1 ^= h1 >>> 16;
//h1 *= 0x85ebca6b;
h1 = (0x85eb0000 * h1 | 0) + (0xca6b * h1);
//h ^= h >> 13;
h1 ^= h1 >>> 13;
//h1 *= 0xc2b2ae35;
h1 = (0xc2b20000 * h1 | 0) + (0xae35 * h1);
//h ^= h >> 16;
h1 ^= h1 >>> 16;
}
return h1 >>> 0; //convert to unsigned int
} //function murmurhash3_32_simple_rp(key, seed) {