Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created May 13, 2020 01:57
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 kmicinski/c680da1476de572ad93abd0895a7a0ed to your computer and use it in GitHub Desktop.
Save kmicinski/c680da1476de572ad93abd0895a7a0ed to your computer and use it in GitHub Desktop.
;; Hash a vector of numbers (all interpreted to be unsigned 64-bit
;; values) based on the first `len` entries in u64-vector. This uses
;; the same hash function as the backend's tuple_hash.
(define (tuple-hash u64-vector prefix-len)
(define base 14695981039346656037)
(define prime 1099511628211)
(define resulting-hash
(foldl
(lambda (i cur-hash)
(let* ([chunk (vector-ref u64-vector i)]
[h0 (bitwise-xor cur-hash (bitwise-and chunk 255))]
[h1 (* h0 prime)])
;; Fold accumulates (cons chunk hash)
(cdr
(foldl (lambda (j chunk-hash)
(let* ([next-chunk (arithmetic-shift (car chunk-hash) -8)]
[h0 (bitwise-xor (cdr chunk-hash) (bitwise-and next-chunk 255))]
[h1 (* h0 prime)])
(cons next-chunk h1)))
(cons chunk h1)
(range 8))))) base (range prefix-len)))
(modulo resulting-hash (expt 2 64)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment