@thi.ng/vectors
Version:
Optimized 2d/3d/4d and arbitrary length vector operations, support for memory mapping/layouts
25 lines (24 loc) • 707 B
JavaScript
import { floatToUintBits } from "@thi.ng/binary/float";
import { rotateLeft } from "@thi.ng/binary/rotate";
const imul = Math.imul;
const hash = (v, H = 2654435761) => {
let hash4 = -1;
for (let i = v.length; i-- > 0; ) {
hash4 = imul(H, hash4) + __mix(hash4, floatToUintBits(v[i])) >>> 0;
}
return hash4;
};
const M1 = 3432918353;
const M2 = 461845907;
const M3 = 3864292196;
const __mix = (h, k) => {
k = imul(rotateLeft(imul(k, M1) >>> 0, 15), M2) >>> 0;
return (imul(rotateLeft(h ^ k, 13), 5) >>> 0) + M3 >>> 0;
};
const hash2 = (x, y) => (imul(x, M1) ^ imul(y, M3)) >>> 0;
const hash3 = (x, y, z) => (imul(x, M1) ^ imul(y, M2) ^ imul(z, M3)) >>> 0;
export {
hash,
hash2,
hash3
};