Last active
October 8, 2023 03:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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