Skip to content

Instantly share code, notes, and snippets.

@kbrsh
Last active October 27, 2017 16:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbrsh/bafb4b3642b3b1e13210153826fcddf7 to your computer and use it in GitHub Desktop.
Save kbrsh/bafb4b3642b3b1e13210153826fcddf7 to your computer and use it in GitHub Desktop.
Slash - Fast, efficient hash

Slash

Fast, efficient hash

Usage

char *str = "Slash";
unsigned long long hash = slash(str);

Implementation

  1. The result is initialized at 1
  2. For each byte (8 bits):
    • Set the result to (result @ prime) ^ (byte)
  3. Return 64 bit unsigned result
Details
  • @ denotes carry-less multiplication
  • ^ denotes exclusive OR (XOR)
  • byte denotes the current byte being operated on
  • prime is a constant chosen by a simulated annealing algorithm (0xA171020315130201ULL)

License

Licensed under the MIT License by Kabir Shah

/**
* Slash
* Fast, efficient hash
* Copyright 2017 Kabir Shah
* Released under the MIT License
*/
unsigned long long slash(const unsigned char *key) {
unsigned long long result = 1;
unsigned long long current;
while((current = *key++) != '\0') {
result = ((result) ^ (result << 9) ^ (result << 16) ^ (result << 17) ^ (result << 20) ^ (result << 24) ^ (result << 26) ^ (result << 28) ^ (result << 32) ^ (result << 33) ^ (result << 41) ^ (result << 48) ^ (result << 52) ^ (result << 53) ^ (result << 54) ^ (result << 56) ^ (result << 61) ^ (result << 63)) ^ (current);
result = (result >> 7) | (result << 57);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment