Skip to content

Commit

Permalink
Update blaze.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bryc authored Apr 9, 2023
1 parent 901b0de commit 492a8e7
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions jshash/experimental/blaze.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
/*
BlazeHash (64-bit)
BlazeHash (53-bit)
c) 2018 bryc (github.com/bryc)
Intended for blazing fast hashing of byte arrays. Produces two uncorrelated 32-bit hashes in parallel.
Expect these functions to change in the future as they are further tested and developed.
*/

function BlazeHash(key, seed = 0) {
var p1 = 597399067, p2 = 374761393, p3 = 2246822507, p4 = 3266489909;
var h1 = 0xcafebabe ^ seed, h2 = 0xdeadbeef ^ seed;
for(var i = 0, b = key.length & -8; i < b;) {
h1 ^= key[i+3] << 24 | key[i+2] << 16 | key[i+1] << 8 | key[i]; i += 4;
h2 ^= key[i+3] << 24 | key[i+2] << 16 | key[i+1] << 8 | key[i]; i += 4;
h1 = Math.imul(h1 ^ h1 >>> 16, p1) ^ h2;
h2 = Math.imul(h2 ^ h2 >>> 24, p2) ^ h1;
let h1 = 0xcafebabe ^ seed, h2 = 0xdeadbeef ^ seed, i = 0;
for(let b = key.length & -8; i < b; i += 8) {
h1 ^= key[i+3] << 24 | key[i+2] << 16 | key[i+1] << 8 | key[i ];
h2 ^= key[i+7] << 24 | key[i+6] << 16 | key[i+5] << 8 | key[i+4];
h1 ^= Math.imul(h2 ^ (h2 >>> 15), 3399173941);
h2 ^= Math.imul(h1 ^ (h1 >>> 16), 951274213);
}
switch(key.length & 7) {
case 7: h2 ^= key[i+6] << 16;
case 6: h2 ^= key[i+5] << 8;
case 5: h2 ^= key[i+4];
h2 = Math.imul(h2 ^ h2 >>> 24, p2) ^ h1;
case 4: h1 ^= key[i+3] << 24;
case 3: h1 ^= key[i+2] << 16;
case 2: h1 ^= key[i+1] << 8;
case 1: h1 ^= key[i];
h1 = Math.imul(h1 ^ h1 >>> 16, p1) ^ h2;
h1 ^= Math.imul(h2 ^ (h2 >>> 15), 3399173941);
h2 ^= Math.imul(h1 ^ (h1 >>> 16), 951274213);
}
h1 ^= key.length;
h1 = Math.imul(h1 ^ h1 >>> 16, p3) ^ Math.imul(h2 ^ h2 >>> 13, p4);
h2 = Math.imul(h2 ^ h2 >>> 16, p3) ^ Math.imul(h1 ^ h1 >>> 13, p4);
return [h1 >>> 0, h2 >>> 0]; // 53bit: 4294967296 * (2097151 & h2) + (h1>>>0)
h1 ^= key.length ^ h2;
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
}

// BlazeHashB
// An alternate version that reads 4 bytes at a time instead of 8
// An alternate version that reads 4 bytes at a time instead of 8.
// Update 2023: Avalanche behavior seems better now.

function BlazeHashB(key, seed = 0) {
Expand Down

0 comments on commit 492a8e7

Please sign in to comment.