-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.hash.Murmur.pas
More file actions
237 lines (196 loc) · 5.22 KB
/
Copy pathMaxLogic.hash.Murmur.pas
File metadata and controls
237 lines (196 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
unit MaxLogic.hash.Murmur;
{
MurmurHash family - Fast, non-cryptographic hash algorithms
Version: 1.2
History:
- 2015-07-21: added Q-/R- to disable overflow/range checking
- 2024-12-14: added string overload for MurmurHash3_32
Variants:
- Murmur2A: For AnsiString (legacy, 8-bit chars)
- Murmur2: Generic byte buffer
- MurmurHash3_32: Latest version, best distribution
Original: https://github.com/aappleby/smhasher
MurmurHash is public domain by Austin Appleby
}
interface
/// <summary>
/// MurmurHash2A for AnsiString (legacy 8-bit).
/// </summary>
function Murmur2A(const s: AnsiString; const Seed: LongWord = $9747B28C): LongWord;
/// <summary>
/// MurmurHash2 for raw byte buffer.
/// </summary>
function Murmur2(buffer: pByte; BufferSize: LongWord; const Seed: LongWord = $9747B28C): LongWord;
/// <summary>
/// MurmurHash3_32 for raw byte buffer (recommended variant).
/// </summary>
function MurmurHash3_32(AKey: pByte; ALength: uInt32; ASeed: uInt32 = $9747B28C): uInt32; overload;
/// <summary>
/// MurmurHash3_32 for string (UTF-16 byte representation).
/// </summary>
function MurmurHash3_32(const aValue: string; ASeed: uInt32 = $9747B28C): uInt32; overload;
implementation
uses
System.SysUtils;
{$Q-}
{$R-}
function Murmur2A(const s: AnsiString; const Seed: LongWord = $9747B28C): LongWord;
var
h: LongWord;
len: LongWord;
k: LongWord;
data: integer;
const
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
m = $5BD1E995;
r = 24;
begin
{$Q-}
{$R-}
len := Length(s);
// The default seed, $9747b28c, is from the original C library
// Initialize the hash to a 'random' value
h := Seed xor len;
// Mix 4 bytes at a time into the hash
data := 1;
while (len >= 4) do
begin
k := PLongWord(@s[data])^;
k := k * m;
k := k xor (k shr r);
k := k * m;
h := h * m;
h := h xor k;
data := data + 4;
len := len - 4;
end;
{ Handle the last few bytes of the input array
S: ... $69 $18 $2f
}
Assert(len <= 3);
if len = 3 then
h := h xor (LongWord(s[data + 2]) shl 16);
if len >= 2 then
h := h xor (LongWord(s[data + 1]) shl 8);
if len >= 1 then
begin
h := h xor (LongWord(s[data]));
h := h * m;
end;
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h := h xor (h shr 13);
h := h * m;
h := h xor (h shr 15);
result := h;
end;
function Murmur2(buffer: pByte; BufferSize: LongWord; const Seed: LongWord = $9747B28C): LongWord;
var
h: LongWord;
len: LongWord;
k: LongWord;
const
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
m = $5BD1E995;
r = 24;
begin
{$Q-}
{$R-}
len := BufferSize;
// The default seed, $9747b28c, is from the original C library
// Initialize the hash to a 'random' value
h := Seed xor len;
// Mix 4 bytes at a time into the hash
while (len >= 4) do
begin
try
k := PLongWord(buffer)^;
except
on E: Exception do
begin
raise Exception.Create(E.Message +
'; len=' + IntToStr(len) +
'; BufferSize=' + IntToStr(BufferSize));
end;
end;
k := k * m;
k := k xor (k shr r);
k := k * m;
h := h * m;
h := h xor k;
Inc(buffer, 4);
dec(len, 4);
end;
{ Handle the last few bytes of the input array
S: ... $69 $18 $2f
}
Assert(len <= 3);
if len = 3 then
h := h xor (pByte(buffer + 2)^ shl 16);
if len >= 2 then
h := h xor (pByte(buffer + 1)^ shl 8);
if len >= 1 then
begin
h := h xor buffer^;
h := h * m;
end;
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h := h xor (h shr 13);
h := h * m;
h := h xor (h shr 15);
result := h;
end;
// Originally converted by Lionell Delafosse
function MurmurHash3_32(AKey: pByte; ALength: uInt32; ASeed: uInt32 = $9747B28C): uInt32;
const
c1 = $CC9E2D51;
c2 = $1B873593;
var
i: integer;
h1,
k1: uInt32;
begin
{$Q-}
{$R-}
h1 := ASeed;
i := ALength div SizeOf(uInt32);
while i <> 0 do
begin
k1 := PCardinal(AKey)^ * c1;
k1 := ((k1 shl 15) or (k1 shr 17)) * c2;
h1 := h1 xor k1;
h1 := (h1 shl 13) or (h1 shr 19);
h1 := ((h1 shl 2) + h1) + $E6546B64;
Inc(AKey, SizeOf(uInt32));
dec(i);
end;
// tail
if (ALength and 1) <> 0 then
begin
if (ALength and 2) <> 0 then
k1 := (((uInt32(pByte(AKey + 2)^) shl 16) xor PWord(AKey)^) * c1) // 3 bytes
else
k1 := uInt32(pByte(AKey)^) * c1; // 1 bytes
h1 := h1 xor (((k1 shl 16) or (k1 shr 16)) * c2);
end
else if (ALength and 2) <> 0 then
begin
k1 := uInt32(PWord(AKey)^) * c1; // 2 bytes
h1 := h1 xor (((k1 shl 16) or (k1 shr 16)) * c2);
end;
// finalization mix - force all bits of hash block to avalanche within 0.25% bias
h1 := h1 xor ALength;
h1 := (h1 xor (h1 shr 16)) * $85EBCA6B;
h1 := (h1 xor (h1 shr 13)) * $C2B2AE35;
result := h1 xor (h1 shr 16);
end;
function MurmurHash3_32(const aValue: string; ASeed: uInt32 = $9747B28C): uInt32;
begin
if Length(aValue) = 0 then
Exit(ASeed);
Result := MurmurHash3_32(PByte(PChar(aValue)), Length(aValue) * SizeOf(Char), ASeed);
end;
end.