forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_hash_primitive.js
49 lines (43 loc) · 1.34 KB
/
caml_hash_primitive.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
'use strict';
function rotl32(x, n) {
return (x << n) | (x >>> (32 - n | 0)) | 0;
}
function hash_mix_int(h, d) {
var d$1 = d;
d$1 = Math.imul(d$1, -862048943);
d$1 = rotl32(d$1, 15);
d$1 = Math.imul(d$1, 461845907);
var h$1 = h ^ d$1;
h$1 = rotl32(h$1, 13);
return (h$1 + (h$1 << 2) | 0) - 430675100 | 0;
}
function hash_final_mix(h) {
var h$1 = h ^ (h >>> 16);
h$1 = Math.imul(h$1, -2048144789);
h$1 = h$1 ^ (h$1 >>> 13);
h$1 = Math.imul(h$1, -1028477387);
return h$1 ^ (h$1 >>> 16);
}
function hash_mix_string(h, s) {
var len = s.length;
var block = (len / 4 | 0) - 1 | 0;
var hash = h;
for(var i = 0; i <= block; ++i){
var j = (i << 2);
var w = s.charCodeAt(j) | (s.charCodeAt(j + 1 | 0) << 8) | (s.charCodeAt(j + 2 | 0) << 16) | (s.charCodeAt(j + 3 | 0) << 24);
hash = hash_mix_int(hash, w);
}
var modulo = len & 3;
if (modulo !== 0) {
var w$1 = modulo === 3 ? (s.charCodeAt(len - 1 | 0) << 16) | (s.charCodeAt(len - 2 | 0) << 8) | s.charCodeAt(len - 3 | 0) : (
modulo === 2 ? (s.charCodeAt(len - 1 | 0) << 8) | s.charCodeAt(len - 2 | 0) : s.charCodeAt(len - 1 | 0)
);
hash = hash_mix_int(hash, w$1);
}
hash = hash ^ len;
return hash;
}
exports.hash_mix_int = hash_mix_int;
exports.hash_mix_string = hash_mix_string;
exports.hash_final_mix = hash_final_mix;
/* No side effect */