forked from apache/commons-codec
/
MurmurHash3.java
1216 lines (1133 loc) · 43.4 KB
/
MurmurHash3.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.commons.codec.digest;
/**
* Implementation of the MurmurHash3 32-bit and 128-bit hash functions.
*
* <p>MurmurHash is a non-cryptographic hash function suitable for general
* hash-based lookup. The name comes from two basic operations, multiply (MU)
* and rotate (R), used in its inner loop. Unlike cryptographic hash functions,
* it is not specifically designed to be difficult to reverse by an adversary,
* making it unsuitable for cryptographic purposes.</p>
*
* <p>This contains a Java port of the 32-bit hash function {@code MurmurHash3_x86_32}
* and the 128-bit hash function {@code MurmurHash3_x64_128} from Austin Applyby's
* original {@code c++} code in SMHasher.</p>
*
* <p>This is public domain code with no copyrights. From home page of
* <a href="https://github.com/aappleby/smhasher">SMHasher</a>:</p>
*
* <blockquote>
* "All MurmurHash versions are public domain software, and the author
* disclaims all copyright to their code."
* </blockquote>
*
* <p>Original adaption from Apache Hive. That adaption contains a {@code hash64} method
* that is not part of the original MurmurHash3 code. It is not recommended to use these methods.
* They will be removed in a future release. To obtain a 64-bit hash use half of the bits
* from the {@code hash128x64} methods using the input data converted to bytes.<p>
*
* @see <a href="https://en.wikipedia.org/wiki/MurmurHash">MurmurHash</a>
* @see <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">
* Original MurmurHash3 c++ code</a>
* @see <a href="https://github.com/apache/hive/blob/master/storage-api/src/java/org/apache/hive/common/util/Murmur3.java">
* Apache Hive Murmer3</a>
* @since 1.13
*/
public final class MurmurHash3 {
/**
* A random number to use for a hash code.
*
* @deprecated This is not used internally and will be removed in a future release.
*/
@Deprecated
public static final long NULL_HASHCODE = 2862933555777941757L;
/**
* A default seed to use for the murmur hash algorithm.
* Has the value {@code 104729}.
*/
public static final int DEFAULT_SEED = 104729;
/** TODO Replace on Java 8 with Long.BYTES. */
static final int LONG_BYTES = Long.SIZE / Byte.SIZE;
/** TODO Replace on Java 8 with Integer.BYTES. */
static final int INTEGER_BYTES = Integer.SIZE / Byte.SIZE;
/** TODO Replace on Java 8 with Short.BYTES. */
static final int SHORT_BYTES = Short.SIZE / Byte.SIZE;
// Constants for 32-bit variant
private static final int C1_32 = 0xcc9e2d51;
private static final int C2_32 = 0x1b873593;
private static final int R1_32 = 15;
private static final int R2_32 = 13;
private static final int M_32 = 5;
private static final int N_32 = 0xe6546b64;
// Constants for 128-bit variant
private static final long C1 = 0x87c37b91114253d5L;
private static final long C2 = 0x4cf5ad432745937fL;
private static final int R1 = 31;
private static final int R2 = 27;
private static final int R3 = 33;
private static final int M = 5;
private static final int N1 = 0x52dce729;
private static final int N2 = 0x38495ab5;
/** No instance methods. */
private MurmurHash3() {
}
/**
* Generates 32-bit hash from two longs with a default seed value.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(16)
* .putLong(data1)
* .putLong(data2)
* .array(), offset, 16, seed);
* </pre>
*
* @param data1 The first long to hash
* @param data2 The second long to hash
* @return The 32-bit hash
* @see #hash32x86(byte[], int, int, int)
*/
public static int hash32(final long data1, final long data2) {
return hash32(data1, data2, DEFAULT_SEED);
}
/**
* Generates 32-bit hash from two longs with the given seed.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(16)
* .putLong(data1)
* .putLong(data2)
* .array(), offset, 16, seed);
* </pre>
*
* @param data1 The first long to hash
* @param data2 The second long to hash
* @param seed The initial seed value
* @return The 32-bit hash
* @see #hash32x86(byte[], int, int, int)
*/
public static int hash32(final long data1, final long data2, final int seed) {
int hash = seed;
final long r0 = Long.reverseBytes(data1);
final long r1 = Long.reverseBytes(data2);
hash = mix32((int) r0, hash);
hash = mix32((int) (r0 >>> 32), hash);
hash = mix32((int) (r1), hash);
hash = mix32((int) (r1 >>> 32), hash);
hash ^= LONG_BYTES * 2;
return fmix32(hash);
}
/**
* Generates 32-bit hash from a long with a default seed value.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(8)
* .putLong(data)
* .array(), offset, 8, seed);
* </pre>
*
* @param data The long to hash
* @return The 32-bit hash
* @see #hash32x86(byte[], int, int, int)
*/
public static int hash32(final long data) {
return hash32(data, DEFAULT_SEED);
}
/**
* Generates 32-bit hash from a long with the given seed.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(8)
* .putLong(data)
* .array(), offset, 8, seed);
* </pre>
*
* @param data The long to hash
* @param seed The initial seed value
* @return The 32-bit hash
* @see #hash32x86(byte[], int, int, int)
*/
public static int hash32(final long data, final int seed) {
int hash = seed;
final long r0 = Long.reverseBytes(data);
hash = mix32((int) r0, hash);
hash = mix32((int) (r0 >>> 32), hash);
hash ^= LONG_BYTES;
return fmix32(hash);
}
/**
* Generates 32-bit hash from the byte array with a default seed.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* int hash = MurmurHash3.hash32(data, offset, data.length, seed);
* </pre>
*
* <p>This implementation contains a sign-extension bug in the finalisation step of
* any bytes left over from dividing the length by 4. This manifests if any of these
* bytes are negative.<p>
*
* @param data The input byte array
* @return The 32-bit hash
* @see #hash32(byte[], int, int, int)
* @deprecated Use {@link #hash32x86(byte[], int, int, int)}. This corrects the processing of trailing bytes.
*/
@Deprecated
public static int hash32(final byte[] data) {
return hash32(data, 0, data.length, DEFAULT_SEED);
}
/**
* Generates 32-bit hash from a string with a default seed.
* The string is converted to bytes using the default encoding.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* byte[] bytes = data.getBytes();
* int hash = MurmurHash3.hash32(bytes, offset, bytes.length, seed);
* </pre>
*
* <p>This implementation contains a sign-extension bug in the finalisation step of
* any bytes left over from dividing the length by 4. This manifests if any of these
* bytes are negative.<p>
*
* @param data The input string
* @return The 32-bit hash
* @see #hash32(byte[], int, int, int)
* @deprecated Use {@link #hash32x86(byte[], int, int, int)} with the bytes returned from
* {@link String#getBytes(java.nio.charset.Charset)}. This corrects the processing of trailing bytes.
*/
@Deprecated
public static int hash32(final String data) {
final byte[] bytes = data.getBytes();
return hash32(bytes, 0, bytes.length, DEFAULT_SEED);
}
/**
* Generates 32-bit hash from the byte array with the given length and a default seed.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* int hash = MurmurHash3.hash32(data, offset, length, seed);
* </pre>
*
* <p>This implementation contains a sign-extension bug in the finalisation step of
* any bytes left over from dividing the length by 4. This manifests if any of these
* bytes are negative.<p>
*
* @param data The input byte array
* @param length The length of array
* @return The 32-bit hash
* @see #hash32(byte[], int, int, int)
* @deprecated Use {@link #hash32x86(byte[], int, int, int)}. This corrects the processing of trailing bytes.
*/
@Deprecated
public static int hash32(final byte[] data, final int length) {
return hash32(data, length, DEFAULT_SEED);
}
/**
* Generates 32-bit hash from the byte array with the given length and seed. This is a
* helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int hash = MurmurHash3.hash32(data, offset, length, seed);
* </pre>
*
* <p>This implementation contains a sign-extension bug in the finalisation step of
* any bytes left over from dividing the length by 4. This manifests if any of these
* bytes are negative.<p>
*
* @param data The input byte array
* @param length The length of array
* @param seed The initial seed value
* @return The 32-bit hash
* @see #hash32(byte[], int, int, int)
* @deprecated Use {@link #hash32x86(byte[], int, int, int)}. This corrects the processing of trailing bytes.
*/
@Deprecated
public static int hash32(final byte[] data, final int length, final int seed) {
return hash32(data, 0, length, seed);
}
/**
* Generates 32-bit hash from the byte array with the given offset, length and seed.
*
* <p>This is an implementation of the 32-bit hash function {@code MurmurHash3_x86_32}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* <p>This implementation contains a sign-extension bug in the finalisation step of
* any bytes left over from dividing the length by 4. This manifests if any of these
* bytes are negative.<p>
*
* @param data The input byte array
* @param offset The offset of data
* @param length The length of array
* @param seed The initial seed value
* @return The 32-bit hash
* @deprecated Use {@link #hash32x86(byte[], int, int, int)}. This corrects the processing of trailing bytes.
*/
@Deprecated
public static int hash32(final byte[] data, final int offset, final int length, final int seed) {
int hash = seed;
final int nblocks = length >> 2;
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 2);
final int k = getLittleEndianInt(data, index);
hash = mix32(k, hash);
}
// tail
// ************
// Note: This fails to apply masking using 0xff to the 3 remaining bytes.
// ************
final int index = offset + (nblocks << 2);
int k1 = 0;
switch (offset + length - index) {
case 3:
k1 ^= data[index + 2] << 16;
case 2:
k1 ^= data[index + 1] << 8;
case 1:
k1 ^= data[index];
// mix functions
k1 *= C1_32;
k1 = Integer.rotateLeft(k1, R1_32);
k1 *= C2_32;
hash ^= k1;
}
hash ^= length;
return fmix32(hash);
}
/**
* Generates 32-bit hash from the byte array with a seed of zero.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 0;
* int hash = MurmurHash3.hash32x86(data, offset, data.length, seed);
* </pre>
*
* @param data The input byte array
* @return The 32-bit hash
* @see #hash32x86(byte[], int, int, int)
* @since 1.14
*/
public static int hash32x86(final byte[] data) {
return hash32x86(data, 0, data.length, 0);
}
/**
* Generates 32-bit hash from the byte array with the given offset, length and seed.
*
* <p>This is an implementation of the 32-bit hash function {@code MurmurHash3_x86_32}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* @param data The input byte array
* @param offset The offset of data
* @param length The length of array
* @param seed The initial seed value
* @return The 32-bit hash
* @since 1.14
*/
public static int hash32x86(final byte[] data, final int offset, final int length, final int seed) {
int hash = seed;
final int nblocks = length >> 2;
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 2);
final int k = getLittleEndianInt(data, index);
hash = mix32(k, hash);
}
// tail
final int index = offset + (nblocks << 2);
int k1 = 0;
switch (offset + length - index) {
case 3:
k1 ^= (data[index + 2] & 0xff) << 16;
case 2:
k1 ^= (data[index + 1] & 0xff) << 8;
case 1:
k1 ^= (data[index] & 0xff);
// mix functions
k1 *= C1_32;
k1 = Integer.rotateLeft(k1, R1_32);
k1 *= C2_32;
hash ^= k1;
}
hash ^= length;
return fmix32(hash);
}
/**
* Generates 64-bit hash from a long with a default seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* The method does not produce the same result as either half of the hash bytes from
* {@linkplain #hash128x64(byte[])} with the same byte data from the {@code long}.
* This method will be removed in a future release.</p>
*
* <p>Note: The sign extension bug in {@link #hash64(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* <p>This is a helper method that will produce the same result as:</p>
*
* <pre>
* int offset = 0;
* int seed = 104729;
* long hash = MurmurHash3.hash64(ByteBuffer.allocate(8)
* .putLong(data)
* .array(), offset, 8, seed);
* </pre>
*
* @param data The long to hash
* @return The 64-bit hash
* @see #hash64(byte[], int, int, int)
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[])} with the bytes from the {@code long}.
*/
@Deprecated
public static long hash64(final long data) {
long hash = DEFAULT_SEED;
long k = Long.reverseBytes(data);
final int length = LONG_BYTES;
// mix functions
k *= C1;
k = Long.rotateLeft(k, R1);
k *= C2;
hash ^= k;
hash = Long.rotateLeft(hash, R2) * M + N1;
// finalization
hash ^= length;
hash = fmix64(hash);
return hash;
}
/**
* Generates 64-bit hash from an int with a default seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* The method does not produce the same result as either half of the hash bytes from
* {@linkplain #hash128x64(byte[])} with the same byte data from the {@code int}.
* This method will be removed in a future release.</p>
*
* <p>Note: The sign extension bug in {@link #hash64(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* <p>This is a helper method that will produce the same result as:</p>
*
* <pre>
* int offset = 0;
* int seed = 104729;
* long hash = MurmurHash3.hash64(ByteBuffer.allocate(4)
* .putInt(data)
* .array(), offset, 4, seed);
* </pre>
*
* @param data The int to hash
* @return The 64-bit hash
* @see #hash64(byte[], int, int, int)
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[])} with the bytes from the {@code int}.
*/
@Deprecated
public static long hash64(final int data) {
long k1 = Integer.reverseBytes(data) & (-1L >>> 32);
final int length = INTEGER_BYTES;
long hash = DEFAULT_SEED;
k1 *= C1;
k1 = Long.rotateLeft(k1, R1);
k1 *= C2;
hash ^= k1;
// finalization
hash ^= length;
hash = fmix64(hash);
return hash;
}
/**
* Generates 64-bit hash from a short with a default seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* The method does not produce the same result as either half of the hash bytes from
* {@linkplain #hash128x64(byte[])} with the same byte data from the {@code short}.
* This method will be removed in a future release.</p>
*
* <p>Note: The sign extension bug in {@link #hash64(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* <p>This is a helper method that will produce the same result as:</p>
*
* <pre>
* int offset = 0;
* int seed = 104729;
* long hash = MurmurHash3.hash64(ByteBuffer.allocate(2)
* .putShort(data)
* .array(), offset, 2, seed);
* </pre>
*
* @param data The short to hash
* @return The 64-bit hash
* @see #hash64(byte[], int, int, int)
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[])} with the bytes from the {@code short}.
*/
@Deprecated
public static long hash64(final short data) {
long hash = DEFAULT_SEED;
long k1 = 0;
k1 ^= ((long) data & 0xff) << 8;
k1 ^= ((long) ((data & 0xFF00) >> 8) & 0xff);
k1 *= C1;
k1 = Long.rotateLeft(k1, R1);
k1 *= C2;
hash ^= k1;
// finalization
hash ^= SHORT_BYTES;
hash = fmix64(hash);
return hash;
}
/**
* Generates 64-bit hash from a byte array with a default seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* The method does not produce the same result as either half of the hash bytes from
* {@linkplain #hash128x64(byte[])} with the same byte data.
* This method will be removed in a future release.</p>
*
* <p>Note: The sign extension bug in {@link #hash64(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* <p>This is a helper method that will produce the same result as:</p>
*
* <pre>
* int offset = 0;
* int seed = 104729;
* long hash = MurmurHash3.hash64(data, offset, data.length, seed);
* </pre>
*
* @param data The input byte array
* @return The 64-bit hash
* @see #hash64(byte[], int, int, int)
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[])}.
*/
@Deprecated
public static long hash64(final byte[] data) {
return hash64(data, 0, data.length, DEFAULT_SEED);
}
/**
* Generates 64-bit hash from a byte array with the given offset and length and a default seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* The method does not produce the same result as either half of the hash bytes from
* {@linkplain #hash128x64(byte[])} with the same byte data.
* This method will be removed in a future release.</p>
*
* <p>Note: The sign extension bug in {@link #hash64(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* <p>This is a helper method that will produce the same result as:</p>
*
* <pre>
* int seed = 104729;
* long hash = MurmurHash3.hash64(data, offset, length, seed);
* </pre>
*
* @param data The input byte array
* @param offset The offset of data
* @param length The length of array
* @return The 64-bit hash
* @see #hash64(byte[], int, int, int)
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[], int, int, int)}.
*/
@Deprecated
public static long hash64(final byte[] data, final int offset, final int length) {
return hash64(data, offset, length, DEFAULT_SEED);
}
/**
* Generates 64-bit hash from a byte array with the given offset, length and seed.
*
* <p><strong>This is not part of the original MurmurHash3 {@code c++} implementation.</strong></p>
*
* <p>This is a Murmur3-like 64-bit variant.
* This method will be removed in a future release.</p>
*
* <p>This implementation contains a sign-extension bug in the seed initialization.
* This manifests if the seed is negative.<p>
*
* <p>This algorithm processes 8 bytes chunks of data in a manner similar to the 16 byte chunks
* of data processed in the MurmurHash3 {@code MurmurHash3_x64_128} method. However the hash
* is not mixed with a hash chunk from the next 8 bytes of data. The method will not return
* the same value as the first or second 64-bits of the function
* {@link #hash128(byte[], int, int, int)}.</p>
*
* <p>Use of this method is not advised. Use the first long returned from
* {@link #hash128x64(byte[], int, int, int)}.<p>
*
* @param data The input byte array
* @param offset The offset of data
* @param length The length of array
* @param seed The initial seed value
* @return The 64-bit hash
* @deprecated Not part of the MurmurHash3 implementation.
* Use half of the hash bytes from {@link #hash128x64(byte[], int, int, int)}.
*/
@Deprecated
public static long hash64(final byte[] data, final int offset, final int length, final int seed) {
// ************
// Note: This fails to apply masking using 0xffffffffL to the seed.
// ************
long hash = seed;
final int nblocks = length >> 3;
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 3);
long k = getLittleEndianLong(data, index);
// mix functions
k *= C1;
k = Long.rotateLeft(k, R1);
k *= C2;
hash ^= k;
hash = Long.rotateLeft(hash, R2) * M + N1;
}
// tail
long k1 = 0;
final int index = offset + (nblocks << 3);
switch (offset + length - index) {
case 7:
k1 ^= ((long) data[index + 6] & 0xff) << 48;
case 6:
k1 ^= ((long) data[index + 5] & 0xff) << 40;
case 5:
k1 ^= ((long) data[index + 4] & 0xff) << 32;
case 4:
k1 ^= ((long) data[index + 3] & 0xff) << 24;
case 3:
k1 ^= ((long) data[index + 2] & 0xff) << 16;
case 2:
k1 ^= ((long) data[index + 1] & 0xff) << 8;
case 1:
k1 ^= ((long) data[index] & 0xff);
k1 *= C1;
k1 = Long.rotateLeft(k1, R1);
k1 *= C2;
hash ^= k1;
}
// finalization
hash ^= length;
hash = fmix64(hash);
return hash;
}
/**
* Generates 128-bit hash from the byte array with a default seed.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* int hash = MurmurHash3.hash128(data, offset, data.length, seed);
* </pre>
*
* <p>Note: The sign extension bug in {@link #hash128(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* @param data The input byte array
* @return The 128-bit hash (2 longs)
* @see #hash128(byte[], int, int, int)
*/
public static long[] hash128(final byte[] data) {
return hash128(data, 0, data.length, DEFAULT_SEED);
}
/**
* Generates 128-bit hash from the byte array with a seed of zero.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 0;
* int hash = MurmurHash3.hash128x64(data, offset, data.length, seed);
* </pre>
*
* @param data The input byte array
* @return The 128-bit hash (2 longs)
* @see #hash128x64(byte[], int, int, int)
* @since 1.14
*/
public static long[] hash128x64(final byte[] data) {
return hash128x64(data, 0, data.length, 0);
}
/**
* Generates 128-bit hash from a string with a default seed.
* The string is converted to bytes using the default encoding.
* This is a helper method that will produce the same result as:
*
* <pre>
* int offset = 0;
* int seed = 104729;
* byte[] bytes = data.getBytes();
* int hash = MurmurHash3.hash128(bytes, offset, bytes.length, seed);
* </pre>
*
* <p>Note: The sign extension bug in {@link #hash128(byte[], int, int, int)} does not effect
* this result as the default seed is positive.<p>
*
* @param data The input String
* @return The 128-bit hash (2 longs)
* @see #hash128(byte[], int, int, int)
* @deprecated Use {@link #hash128x64(byte[])} using the bytes returned from
* {@link String#getBytes(java.nio.charset.Charset)}.
*/
@Deprecated
public static long[] hash128(final String data) {
final byte[] bytes = data.getBytes();
return hash128(bytes, 0, bytes.length, DEFAULT_SEED);
}
/**
* Generates 128-bit hash from the byte array with the given offset, length and seed.
*
* <p>This is an implementation of the 128-bit hash function {@code MurmurHash3_x64_128}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* <p>This implementation contains a sign-extension bug in the seed initialization.
* This manifests if the seed is negative.<p>
*
* @param data The input byte array
* @param offset The first element of array
* @param length The length of array
* @param seed The initial seed value
* @return The 128-bit hash (2 longs)
* @deprecated Use {@link #hash128x64(byte[], int, int, int)}. This corrects the seed initialization.
*/
@Deprecated
public static long[] hash128(final byte[] data, final int offset, final int length, final int seed) {
// ************
// Note: This fails to apply masking using 0xffffffffL to the seed.
// ************
return hash128x64(data, offset, length, seed);
}
/**
* Generates 128-bit hash from the byte array with the given offset, length and seed.
*
* <p>This is an implementation of the 128-bit hash function {@code MurmurHash3_x64_128}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* @param data The input byte array
* @param offset The first element of array
* @param length The length of array
* @param seed The initial seed value
* @return The 128-bit hash (2 longs)
* @since 1.14
*/
public static long[] hash128x64(final byte[] data, final int offset, final int length, final int seed) {
// Use an unsigned 32-bit integer as the seed
return hash128x64(data, offset, length, seed & 0xffffffffL);
}
/**
* Generates 128-bit hash from the byte array with the given offset, length and seed.
*
* <p>This is an implementation of the 128-bit hash function {@code MurmurHash3_x64_128}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* @param data The input byte array
* @param offset The first element of array
* @param length The length of array
* @param seed The initial seed value
* @return The 128-bit hash (2 longs)
*/
private static long[] hash128x64(final byte[] data, final int offset, final int length, final long seed) {
long h1 = seed;
long h2 = seed;
final int nblocks = length >> 4;
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 4);
long k1 = getLittleEndianLong(data, index);
long k2 = getLittleEndianLong(data, index + 8);
// mix functions for k1
k1 *= C1;
k1 = Long.rotateLeft(k1, R1);
k1 *= C2;
h1 ^= k1;
h1 = Long.rotateLeft(h1, R2);
h1 += h2;
h1 = h1 * M + N1;
// mix functions for k2
k2 *= C2;
k2 = Long.rotateLeft(k2, R3);
k2 *= C1;
h2 ^= k2;
h2 = Long.rotateLeft(h2, R1);
h2 += h1;
h2 = h2 * M + N2;
}
// tail
long k1 = 0;
long k2 = 0;
final int index = offset + (nblocks << 4);
switch (offset + length - index) {
case 15:
k2 ^= ((long) data[index + 14] & 0xff) << 48;
case 14:
k2 ^= ((long) data[index + 13] & 0xff) << 40;
case 13:
k2 ^= ((long) data[index + 12] & 0xff) << 32;
case 12:
k2 ^= ((long) data[index + 11] & 0xff) << 24;
case 11:
k2 ^= ((long) data[index + 10] & 0xff) << 16;
case 10:
k2 ^= ((long) data[index + 9] & 0xff) << 8;
case 9:
k2 ^= data[index + 8] & 0xff;
k2 *= C2;
k2 = Long.rotateLeft(k2, R3);
k2 *= C1;
h2 ^= k2;
case 8:
k1 ^= ((long) data[index + 7] & 0xff) << 56;
case 7:
k1 ^= ((long) data[index + 6] & 0xff) << 48;
case 6:
k1 ^= ((long) data[index + 5] & 0xff) << 40;
case 5:
k1 ^= ((long) data[index + 4] & 0xff) << 32;
case 4:
k1 ^= ((long) data[index + 3] & 0xff) << 24;
case 3:
k1 ^= ((long) data[index + 2] & 0xff) << 16;
case 2:
k1 ^= ((long) data[index + 1] & 0xff) << 8;
case 1:
k1 ^= data[index] & 0xff;
k1 *= C1;
k1 = Long.rotateLeft(k1, R1);
k1 *= C2;
h1 ^= k1;
}
// finalization
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
return new long[] { h1, h2 };
}
/**
* Gets the little-endian long from 8 bytes starting at the specified index.
*
* @param data The data
* @param index The index
* @return The little-endian long
*/
private static long getLittleEndianLong(final byte[] data, final int index) {
return (((long) data[index ] & 0xff) ) |
(((long) data[index + 1] & 0xff) << 8) |
(((long) data[index + 2] & 0xff) << 16) |
(((long) data[index + 3] & 0xff) << 24) |
(((long) data[index + 4] & 0xff) << 32) |
(((long) data[index + 5] & 0xff) << 40) |
(((long) data[index + 6] & 0xff) << 48) |
(((long) data[index + 7] & 0xff) << 56);
}
/**
* Gets the little-endian int from 4 bytes starting at the specified index.
*
* @param data The data
* @param index The index
* @return The little-endian int
*/
private static int getLittleEndianInt(final byte[] data, final int index) {
return ((data[index ] & 0xff) ) |
((data[index + 1] & 0xff) << 8) |
((data[index + 2] & 0xff) << 16) |
((data[index + 3] & 0xff) << 24);
}
/**
* Perform the intermediate mix step of the 32-bit hash function {@code MurmurHash3_x86_32}.
*
* @param k The data to add to the hash
* @param hash The current hash
* @return The new hash
*/
private static int mix32(int k, int hash) {
k *= C1_32;
k = Integer.rotateLeft(k, R1_32);
k *= C2_32;
hash ^= k;
return Integer.rotateLeft(hash, R2_32) * M_32 + N_32;
}
/**
* Perform the final avalanche mix step of the 32-bit hash function {@code MurmurHash3_x86_32}.
*
* @param hash The current hash
* @return The final hash
*/
private static int fmix32(int hash) {
hash ^= (hash >>> 16);
hash *= 0x85ebca6b;
hash ^= (hash >>> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >>> 16);
return hash;
}
/**
* Perform the final avalanche mix step of the 64-bit hash function {@code MurmurHash3_x64_128}.
*
* @param hash The current hash
* @return The final hash
*/
private static long fmix64(long hash) {
hash ^= (hash >>> 33);
hash *= 0xff51afd7ed558ccdL;
hash ^= (hash >>> 33);
hash *= 0xc4ceb9fe1a85ec53L;
hash ^= (hash >>> 33);
return hash;
}
/**
* Generates 32-bit hash from input bytes. Bytes can be added incrementally and the new
* hash computed.
*
* <p>This is an implementation of the 32-bit hash function {@code MurmurHash3_x86_32}
* from from Austin Applyby's original MurmurHash3 {@code c++} code in SMHasher.</p>
*
* @since 1.14
*/