31 lines
784 B
JavaScript
31 lines
784 B
JavaScript
export function fnv1a32(str) {
|
|
let h = 0x811c9dc5;
|
|
for (let i = 0; i < str.length; i++) {
|
|
h ^= str.charCodeAt(i);
|
|
h = Math.imul(h, 0x01000193);
|
|
}
|
|
return h >>> 0;
|
|
}
|
|
|
|
export function hash2u32(a, b, seed = 0) {
|
|
// 2D integer hash -> uint32 (cheap, stable)
|
|
let x = (a | 0) + 0x9e3779b9 + (seed | 0);
|
|
let y = (b | 0) + 0x85ebca6b;
|
|
x ^= y + 0x27d4eb2d + (x << 6) + (x >>> 2);
|
|
x = Math.imul(x ^ (x >>> 16), 0x7feb352d);
|
|
x = Math.imul(x ^ (x >>> 15), 0x846ca68b);
|
|
x ^= x >>> 16;
|
|
return x >>> 0;
|
|
}
|
|
|
|
export function mulberry32(seedU32) {
|
|
let t = seedU32 >>> 0;
|
|
return function rand() {
|
|
t += 0x6d2b79f5;
|
|
let r = Math.imul(t ^ (t >>> 15), 1 | t);
|
|
r ^= r + Math.imul(r ^ (r >>> 7), 61 | r);
|
|
return ((r ^ (r >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|