Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Last active October 8, 2023 03:50
Show Gist options
  • Save vaiorabbit/5652870 to your computer and use it in GitHub Desktop.
Save vaiorabbit/5652870 to your computer and use it in GitHub Desktop.
FNV-1a Hash (http://isthe.com/chongo/tech/comp/fnv/) in Emacs Lisp.
(defun fnv32a (s)
"Calculates FNV-1a 32-bit hash of given string s."
(let (hval)
(setq hval 2166136261) ; hval = FNV1_32A_INIT
(loop for c across s do
(setq hval (logxor hval c)) ; hval ^= c
(setq hval (* 16777619 hval)) ; hval *= FNV_32_PRIME
(setq hval (mod hval 4294967296))) ; hval %= UINT32_MAX
hval))
; Ref.:
; (string-to-number "811c9dc5" 16)
; 2166136261
;
; (string-to-number "1000193" 16)
; 16777619
;
; (expt 2 32)
; 4294967296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment