File size: 2,187 Bytes
21dd449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// xxh32.js - implementation of xxhash32 in plain JavaScript
import * as util from "./util.js";

// xxhash32 primes
const prime1 = 0x9e3779b1;
const prime2 = 0x85ebca77;
const prime3 = 0xc2b2ae3d;
const prime4 = 0x27d4eb2f;
const prime5 = 0x165667b1;

// Utility functions/primitives
// --
function rotl32(x: number, r: number): number {
	x = x | 0;
	r = r | 0;

	return (x >>> ((32 - r) | 0)) | (x << r) | 0;
}

function rotmul32(h: number, r: number, m: number): number {
	h = h | 0;
	r = r | 0;
	m = m | 0;

	return util.imul((h >>> ((32 - r) | 0)) | (h << r), m) | 0;
}

function shiftxor32(h: number, s: number): number {
	h = h | 0;
	s = s | 0;

	return ((h >>> s) ^ h) | 0;
}

// Implementation
// --

function xxhapply(h: number, src: number, m0: number, s: number, m1: number): number {
	return rotmul32(util.imul(src, m0) + h, s, m1);
}

function xxh1(h: number, src: Uint8Array, index: number): number {
	return rotmul32(h + util.imul(src[index], prime5), 11, prime1);
}

function xxh4(h: number, src: Uint8Array, index: number): number {
	return xxhapply(h, util.readU32(src, index), prime3, 17, prime4);
}

function xxh16(h: number[], src: Uint8Array, index: number): number[] {
	return [
		xxhapply(h[0], util.readU32(src, index + 0), prime2, 13, prime1),
		xxhapply(h[1], util.readU32(src, index + 4), prime2, 13, prime1),
		xxhapply(h[2], util.readU32(src, index + 8), prime2, 13, prime1),
		xxhapply(h[3], util.readU32(src, index + 12), prime2, 13, prime1),
	];
}

function xxh32(seed: number, src: Uint8Array, index: number, len: number): number {
	let h;
	const l = len;
	if (len >= 16) {
		h = [seed + prime1 + prime2, seed + prime2, seed, seed - prime1];

		while (len >= 16) {
			h = xxh16(h, src, index);

			index += 16;
			len -= 16;
		}

		h = rotl32(h[0], 1) + rotl32(h[1], 7) + rotl32(h[2], 12) + rotl32(h[3], 18) + l;
	} else {
		h = (seed + prime5 + len) >>> 0;
	}

	while (len >= 4) {
		h = xxh4(h, src, index);

		index += 4;
		len -= 4;
	}

	while (len > 0) {
		h = xxh1(h, src, index);

		index++;
		len--;
	}

	h = shiftxor32(util.imul(shiftxor32(util.imul(shiftxor32(h, 15), prime2), 13), prime3), 16);

	return h >>> 0;
}

export const hash = xxh32;