Skip to content

Instantly share code, notes, and snippets.

@o0101
Last active June 17, 2017 18:20
Show Gist options
  • Save o0101/337cc92e9090a5f47f611111e0608f20 to your computer and use it in GitHub Desktop.
Save o0101/337cc92e9090a5f47f611111e0608f20 to your computer and use it in GitHub Desktop.
tifuhash 32 bit in a mini package
// tifuhash 32-bit (c) dosyago & k stringfellow 2017 MIT LICENSE https://www.npmjs.com/package/tifuhash
let f = ( s = 0, m = '', ...n ) => {
s = parseFloat(s); m = Array.from(m).concat(n).map(x => isNaN(parseFloat(x)) ? x.charCodeAt(0) : parseFloat(x));
let a=new Float64Array(4);
a[0] = 1;
a[2] = s ? Math.pow(s+1/s, 1/2) : 3;
a[3] = s ? Math.pow(s+1/s, 1/5) : 7;
m.forEach((x,i) => {
a[1] = (x+i+1)/a[3];
a[2] += a[0]/a[1]; a[2] = 1/a[2];
a[3] += x; a[3] = a[0]/a[3];
a[0] = a[1]+1;
});
a[2] *= Math.PI+a[3];
a[3] *= Math.E+a[2];
const h = new Uint32Array(a.buffer);
return (h[4]^h[5]^h[6]^h[7])>>>0;
};
// useful for strings
console.log("strings...");
console.log(f(0,"analyze this")); // 2587049505
console.log(f(0,"analyze thit")); // 2523560067
console.log(f(0,"analyze that")); // 1994125995
// or numbers
console.log("numbers...");
console.log(f(0,1,2,3,4,5)); // 3120845206
console.log(f(5,4,3,2,1,0)); // 3487245198
console.log(f(5,4,3,2,0,1)); // 451932417
// or floats
console.log("floats...");
console.log(f(0,1,2,3,4+1e-15,5.000000001)); // 3127534970
console.log(f(0,Math.PI,Math.E,1<<30,1/(1<<31))); // 1471402665
// or tiny floats
console.log("tiiiny floats...");
console.log(f(0,1,2,3+1e-15,4,5)); // 3120845212
console.log(f(0,1,2,3+2e-15,4,5)); // 3120845209
console.log(f(0,1,2,3+3e-15,4,5)); // 3120845410
console.log(f(0,1,2,3+4e-15,4,5)); // 3120845412
console.log(f(0,1,2,3,4+1e-15,5)); // 3120845202
// or just nothing but seed
console.log("seeds...");
console.log(f(0)); // 2702599175
console.log(f(1)); // 3794067143
console.log(f(Math.PI)); // 2450270461
// or maybe an rng?
console.log("rng?");
console.log(f(0), f(f(0)), f(f(f(0)))); // 2702599175 3590320966 2801714996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment