Skip to content

Instantly share code, notes, and snippets.

@JLarky
Created March 18, 2024 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JLarky/bf578f8f39978a5db4a3c7a3d18a38aa to your computer and use it in GitHub Desktop.
Save JLarky/bf578f8f39978a5db4a3c7a3d18a38aa to your computer and use it in GitHub Desktop.
#!/bin/sh
/*/.this-doesnt-exist 2>/dev/null
## Please do not edit this part of the script, this is a loader created by "npx bun-self"
if ! [ -x "$(command -v bun)" ]; then
## it's possible that bun is installed but not in the PATH, let's check if BUN_INSTALL is set
if [ -z "$BUN_INSTALL" ]; then
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
fi
if ! [ -x "$(command -v bun)" ]; then
echo "Installing bun.sh"
[ -z "$CI" ] && sleep 2
curl -fsSL https://bun.sh/install | bash
echo "Now let's run the script"
echo ""
fi >&2
fi
bun "$0" "$@"
exit 0
#*/
// Script starts here
// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
/*
cyrb53a (c) 2023 bryc (github.com/bryc)
License: Public domain. Attribution appreciated.
The original cyrb53 has a slight mixing bias in the low bits of h1.
This shouldn't be a huge problem, but I want to try to improve it.
This new version should have improved avalanche behavior, but
it is not quite final, I may still find improvements.
So don't expect it to always produce the same output.
*/
const cyrb53a = function(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for(let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 0x85ebca77);
h2 = Math.imul(h2 ^ ch, 0xc2b2ae3d);
}
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return 2097152 * (h2 >>> 0) + (h1 >>> 11);
};
console.log('hash for empty string =', cyrb53a(""));
console.log('hash for a =', cyrb53a("a"));
console.log('hash for space =', cyrb53a(" "));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment