Last active
February 14, 2020 22:45
-
-
Save sevaa/6d2d1d2f0af6c085bd02cf48a0b9b55f to your computer and use it in GitHub Desktop.
MurMurHash64B in Pascal
This file contains hidden or 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
unit MurMur; | |
interface | |
function MurmurHash64B(s: PAnsiChar; Len: Integer; Seed: UInt32) : UInt64; | |
implementation | |
function MurmurHash64B(s: PAnsiChar; Len: Integer; Seed: UInt32) : UInt64; | |
const | |
m = $5bd1e995; | |
r = 24; | |
var | |
h1, h2, k1, k2, c: UInt32; | |
data: PUInt32; | |
begin | |
h1 := Seed Xor Len; | |
h2 := 0; | |
data := PUInt32(s); | |
while Len >= 8 do | |
begin | |
k1 := data^; | |
Inc(data); | |
k1 := k1 * m; | |
k1 := (k1 Xor (k1 Shr r)) * m; | |
h1 := (h1 * m) Xor k1; | |
Len := Len - 4; | |
k2 := data^; | |
Inc(data); | |
k2 := k2 * m; | |
k2 := (k2 Xor (k2 Shr r)) * m; | |
h2 := (h2 * m) Xor k2; | |
Len := Len - 4; | |
end; | |
if Len >= 4 then | |
begin | |
k1 := data^; | |
Inc(data); | |
k1 := k1 * m; | |
k1 := (k1 Xor (k1 Shr r)) * m; | |
h1 := (h1 * m) Xor k1; | |
Len := Len - 4; | |
end; | |
if Len > 0 then | |
begin | |
if Len > 1 then | |
begin | |
if Len > 2 then | |
begin | |
c := UInt32((PAnsiChar(data))[2]); | |
h2 := h2 Xor (c Shl 16) | |
end; | |
c := UInt32((PAnsiChar(data))[1]); | |
h2 := h2 Xor (c Shl 8) | |
end; | |
h2 := h2 Xor UInt32((PAnsiChar(data))^); | |
h2 := h2 * m; | |
end; | |
h1 := (h1 Xor(h2 Shr 18)) * m; | |
h2 := (h2 Xor (h1 Shr 22)) * m; | |
h1 := (h1 Xor (h2 Shr 17)) * m; | |
h2 := (h2 Xor (h1 Shr 19)) * m; | |
Result := h1; | |
Result := (Result Shl 32) Or h2; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Free Pascal and Delphi.