/
Komihash4_3.java
360 lines (308 loc) · 10.1 KB
/
Komihash4_3.java
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2023-2024 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This file includes a Java port of the Komihash algorithm originally published
* at https://github.com/avaneev/komihash under the following license:
*
* MIT License
*
* Copyright (c) 2021-2023 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dynatrace.hash4j.hashing;
import static com.dynatrace.hash4j.hashing.UnsignedMultiplyUtil.unsignedMultiplyHigh;
class Komihash4_3 extends AbstractKomihash {
private static final Hasher64 DEFAULT_HASHER_INSTANCE = create(0L);
static Hasher64 create() {
return DEFAULT_HASHER_INSTANCE;
}
static Hasher64 create(long useSeed) {
return new Komihash4_3(useSeed);
}
private Komihash4_3(long seed) {
super(seed);
}
@Override
public HashStream64 hashStream() {
return new HashStreamImpl();
}
@Override
public long hashBytesToLong(byte[] input, int off, int len) {
long see1 = this.seed1;
long see2 = this.seed2;
long see3 = this.seed3;
long see4 = this.seed4;
long see5 = this.seed5;
long see6 = this.seed6;
long see7 = this.seed7;
long see8 = this.seed8;
boolean nonZeroLength = len > 0;
if (len > 63) {
do {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 8);
long tmp3 = see2 ^ getLong(input, off + 16);
long tmp4 = see6 ^ getLong(input, off + 24);
long tmp5 = see3 ^ getLong(input, off + 32);
long tmp6 = see7 ^ getLong(input, off + 40);
long tmp7 = see4 ^ getLong(input, off + 48);
long tmp8 = see8 ^ getLong(input, off + 56);
see1 = tmp1 * tmp2;
see5 += unsignedMultiplyHigh(tmp1, tmp2);
see2 = tmp3 * tmp4;
see6 += unsignedMultiplyHigh(tmp3, tmp4);
see3 = tmp5 * tmp6;
see7 += unsignedMultiplyHigh(tmp5, tmp6);
see4 = tmp7 * tmp8;
see8 += unsignedMultiplyHigh(tmp7, tmp8);
see2 ^= see5;
see3 ^= see6;
see4 ^= see7;
see1 ^= see8;
off += 64;
len -= 64;
} while (len > 63);
see5 ^= see6 ^ see7 ^ see8;
see1 ^= see2 ^ see3 ^ see4;
}
if (len > 31) {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 8);
see1 = tmp1 * tmp2;
see5 += unsignedMultiplyHigh(tmp1, tmp2);
see1 ^= see5;
long tmp3 = see1 ^ getLong(input, off + 16);
long tmp4 = see5 ^ getLong(input, off + 24);
see1 = tmp3 * tmp4;
see5 += unsignedMultiplyHigh(tmp3, tmp4);
see1 ^= see5;
off += 32;
len -= 32;
}
if (len > 15) {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 8);
see1 = tmp1 * tmp2;
see5 += unsignedMultiplyHigh(tmp1, tmp2);
see1 ^= see5;
off += 16;
len -= 16;
}
long r2h = see5;
long r2l = see1;
int ml8 = len << 3;
if (len > 7) {
r2l ^= getLong(input, off);
long y = getLong(input, off + len - 8);
long fb = y >>> 1 >>> ~ml8;
fb |= 1L << ml8 << (y >>> 63);
r2h ^= fb;
} else if (len > 3) {
long fb = getInt(input, off) & 0xFFFFFFFFL;
long y = getInt(input, off + len - 4);
fb |= (y << 32) >>> -ml8;
fb |= 1L << ml8 << (y >>> 63);
r2l ^= fb;
} else if (len > 0) {
long fb = input[off] & 0xFFL;
if (len > 1) fb |= (input[off + 1] & 0xFFL) << 8;
if (len > 2) fb |= (input[off + 2] & 0xFFL) << 16;
fb |= 1L << ml8 << (fb >>> (ml8 - 1));
r2l ^= fb;
} else if (nonZeroLength) {
r2l ^= (input[off - 1] < 0) ? 2L : 1L;
}
return finish(r2h, r2l, see5);
}
@Override
public long hashCharsToLong(CharSequence input) {
int off = 0;
int len = input.length();
long see1 = this.seed1;
long see2 = this.seed2;
long see3 = this.seed3;
long see4 = this.seed4;
long see5 = this.seed5;
long see6 = this.seed6;
long see7 = this.seed7;
long see8 = this.seed8;
boolean nonZeroLength = len > 0;
if (len > 31) {
do {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 4);
long tmp3 = see2 ^ getLong(input, off + 8);
long tmp4 = see6 ^ getLong(input, off + 12);
long tmp5 = see3 ^ getLong(input, off + 16);
long tmp6 = see7 ^ getLong(input, off + 20);
long tmp7 = see4 ^ getLong(input, off + 24);
long tmp8 = see8 ^ getLong(input, off + 28);
long r1l = tmp1 * tmp2;
long r1h = unsignedMultiplyHigh(tmp1, tmp2);
long r2l = tmp3 * tmp4;
long r2h = unsignedMultiplyHigh(tmp3, tmp4);
long r3l = tmp5 * tmp6;
long r3h = unsignedMultiplyHigh(tmp5, tmp6);
long r4l = tmp7 * tmp8;
long r4h = unsignedMultiplyHigh(tmp7, tmp8);
off += 32;
len -= 32;
see5 += r1h;
see6 += r2h;
see7 += r3h;
see8 += r4h;
see2 = see5 ^ r2l;
see3 = see6 ^ r3l;
see4 = see7 ^ r4l;
see1 = see8 ^ r1l;
} while (len > 31);
see5 ^= see6 ^ see7 ^ see8;
see1 ^= see2 ^ see3 ^ see4;
}
if (len > 15) {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 4);
long r1l = tmp1 * tmp2;
long r1h = unsignedMultiplyHigh(tmp1, tmp2);
see5 += r1h;
see1 = see5 ^ r1l;
long tmp3 = see1 ^ getLong(input, off + 8);
long tmp4 = see5 ^ getLong(input, off + 12);
r1l = tmp3 * tmp4;
r1h = unsignedMultiplyHigh(tmp3, tmp4);
see5 += r1h;
see1 = see5 ^ r1l;
off += 16;
len -= 16;
}
if (len > 7) {
long tmp1 = see1 ^ getLong(input, off);
long tmp2 = see5 ^ getLong(input, off + 4);
long r1l = tmp1 * tmp2;
long r1h = unsignedMultiplyHigh(tmp1, tmp2);
see5 += r1h;
see1 = see5 ^ r1l;
off += 8;
len -= 8;
}
long r2h = see5;
long r2l = see1;
int ml8 = len << 4;
if (len > 3) {
r2l ^= getLong(input, off);
long y = getLong(input, off + len - 4);
long fb = y >>> 1 >>> ~ml8;
fb |= 1L << ml8 << (y >>> 63);
r2h ^= fb;
} else if (len > 1) {
long fb = getInt(input, off) & 0xFFFFFFFFL;
long y = getInt(input, off + len - 2);
fb |= (y << 32) >>> -ml8;
fb |= 1L << ml8 << (y >>> 63);
r2l ^= fb;
} else if (len > 0) {
long fb = input.charAt(off);
fb |= 0x10000L << (fb >>> 15);
r2l ^= fb;
} else if (nonZeroLength) {
r2l ^= 0x1L << ((int) input.charAt(off - 1) >>> 15);
}
return finish(r2h, r2l, see5);
}
private class HashStreamImpl extends AbstractKomihash.HashStreamImpl {
@Override
protected void processBuffer(
long b0, long b1, long b2, long b3, long b4, long b5, long b6, long b7) {
b0 ^= see1;
b1 ^= see5;
b2 ^= see2;
b3 ^= see6;
b4 ^= see3;
b5 ^= see7;
b6 ^= see4;
b7 ^= see8;
long r1l = b0 * b1;
long r1h = unsignedMultiplyHigh(b0, b1);
long r2l = b2 * b3;
long r2h = unsignedMultiplyHigh(b2, b3);
long r3l = b4 * b5;
long r3h = unsignedMultiplyHigh(b4, b5);
long r4l = b6 * b7;
long r4h = unsignedMultiplyHigh(b6, b7);
see5 += r1h;
see6 += r2h;
see7 += r3h;
see8 += r4h;
see2 = see5 ^ r2l;
see3 = see6 ^ r3l;
see4 = see7 ^ r4l;
see1 = see8 ^ r1l;
}
@Override
protected long finalizeGetAsLong(long se1, long se5, int off, int len) {
long r2h = se5;
long r2l = se1;
long y = 1L << (len << 3);
long fb = y << ((buffer[(off + len - 1) & 0x3f] < 0) ? 1 : 0);
if (len > 7) {
fb |= getLong(buffer, off + 8) & (y - 1);
r2h ^= fb;
r2l ^= getLong(buffer, off);
} else if (byteCount > 0) {
fb |= getLong(buffer, off) & (y - 1);
r2l ^= fb;
}
return finish(r2h, r2l, se5);
}
@Override
public HashStream64 copy() {
final HashStreamImpl hashStream = new HashStreamImpl();
copyTo(hashStream);
return hashStream;
}
}
@Override
public long hashLongLongToLong(long v1, long v2) {
long tmp1 = this.seed1 ^ v1;
long tmp2 = this.seed5 ^ v2;
long see5 = unsignedMultiplyHigh(tmp1, tmp2) + this.seed5;
return finish(see5, (tmp1 * tmp2) ^ (see5 ^ (1L << (v2 >>> 63))), see5);
}
@Override
public long hashLongLongLongToLong(long v1, long v2, long v3) {
long tmp1 = this.seed1 ^ v1;
long tmp2 = this.seed5 ^ v2;
long see5 = unsignedMultiplyHigh(tmp1, tmp2) + this.seed5;
return finish(see5 ^ (1L << (v3 >>> 63)), (tmp1 * tmp2) ^ (see5 ^ v3), see5);
}
}