Skip to content

Instantly share code, notes, and snippets.

@simonster
Created April 21, 2017 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonster/b4cb0700cef88c820a634ae9aef94f43 to your computer and use it in GitHub Desktop.
Save simonster/b4cb0700cef88c820a634ae9aef94f43 to your computer and use it in GitHub Desktop.
using Compat
# Original C version at https://naml.us/post/inverse-of-a-hash-function/
function inverse_hash(key::UInt64)
local tmp::UInt64
# Invert key = key + (key << 31)
tmp = key-(key<<31)
key = key-(tmp<<31)
# Invert key = key ^ (key >> 28)
tmp = xor(key, key>>28)
key = xor(key, tmp>>28)
# Invert key *= 21
key *= 0xcf3cf3cf3cf3cf3d
# Invert key = key ^ (key >> 14)
tmp = xor(key, key>>14)
tmp = xor(key, tmp>>14)
tmp = xor(key, tmp>>14)
key = xor(key, tmp>>14)
# Invert key *= 265
key *= 0xd38ff08b1c03dd39
# Invert key = key ^ (key >> 24)
tmp = xor(key, key>>24)
key = xor(key, tmp>>24)
# Invert key = (~key) + (key << 21)
tmp = ~key
tmp = ~(key-(tmp<<21))
tmp = ~(key-(tmp<<21))
key = ~(key-(tmp<<21))
key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment