View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  /*
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   *
26   *     http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   */
34  package org.waarp.compress.zstdsafe;
35  
36  import static java.lang.Long.*;
37  import static org.waarp.compress.zstdsafe.UnsafeUtil.getLong;
38  import static org.waarp.compress.zstdsafe.UnsafeUtil.*;
39  
40  // forked from https://github.com/airlift/slice
41  final class XxHash64 {
42    private static final long PRIME64_1 = 0x9E3779B185EBCA87L;
43    private static final long PRIME64_2 = 0xC2B2AE3D27D4EB4FL;
44    private static final long PRIME64_3 = 0x165667B19E3779F9L;
45    private static final long PRIME64_4 = 0x85EBCA77C2b2AE63L;
46    private static final long PRIME64_5 = 0x27D4EB2F165667C5L;
47  
48    private XxHash64() {
49    }
50  
51    public static long hash(final long seed, final byte[] base, final int address,
52                            final int length) {
53      long hash;
54      if (length >= 32) {
55        hash = updateBody(seed, base, address, length);
56      } else {
57        hash = seed + PRIME64_5;
58      }
59  
60      hash += length;
61  
62      // round to the closest 32 byte boundary
63      // this is the point up to which updateBody() processed
64      final int index = length & 0xFFFFFFE0;
65  
66      return updateTail(hash, base, address, index, length);
67    }
68  
69    private static long updateTail(long hash, final byte[] base,
70                                   final int address, int index,
71                                   final int length) {
72      while (index <= length - 8) {
73        hash = updateTail(hash, getLong(base, address + index));
74        index += 8;
75      }
76  
77      if (index <= length - 4) {
78        hash = updateTail(hash, getInt(base, address + index));
79        index += 4;
80      }
81  
82      while (index < length) {
83        hash = updateTail(hash, base[address + index]);
84        index++;
85      }
86  
87      hash = finalShuffle(hash);
88  
89      return hash;
90    }
91  
92    private static long updateBody(final long seed, final byte[] base,
93                                   int address, final int length) {
94      long v1 = seed + PRIME64_1 + PRIME64_2;
95      long v2 = seed + PRIME64_2;
96      long v3 = seed;
97      long v4 = seed - PRIME64_1;
98  
99      int remaining = length;
100     while (remaining >= 32) {
101       v1 = mix(v1, getLong(base, address));
102       v2 = mix(v2, getLong(base, address + 8));
103       v3 = mix(v3, getLong(base, address + 16));
104       v4 = mix(v4, getLong(base, address + 24));
105 
106       address += 32;
107       remaining -= 32;
108     }
109 
110     long hash = rotateLeft(v1, 1) + rotateLeft(v2, 7) + rotateLeft(v3, 12) +
111                 rotateLeft(v4, 18);
112 
113     hash = update(hash, v1);
114     hash = update(hash, v2);
115     hash = update(hash, v3);
116     hash = update(hash, v4);
117 
118     return hash;
119   }
120 
121   private static long mix(final long current, final long value) {
122     return rotateLeft(current + value * PRIME64_2, 31) * PRIME64_1;
123   }
124 
125   private static long update(final long hash, final long value) {
126     final long temp = hash ^ mix(0, value);
127     return temp * PRIME64_1 + PRIME64_4;
128   }
129 
130   private static long updateTail(final long hash, final long value) {
131     final long temp = hash ^ mix(0, value);
132     return rotateLeft(temp, 27) * PRIME64_1 + PRIME64_4;
133   }
134 
135   private static long updateTail(final long hash, final int value) {
136     final long unsigned = value & 0xFFFFFFFFL;
137     final long temp = hash ^ (unsigned * PRIME64_1);
138     return rotateLeft(temp, 23) * PRIME64_2 + PRIME64_3;
139   }
140 
141   private static long updateTail(final long hash, final byte value) {
142     final int unsigned = value & 0xFF;
143     final long temp = hash ^ (unsigned * PRIME64_5);
144     return rotateLeft(temp, 11) * PRIME64_1;
145   }
146 
147   private static long finalShuffle(long hash) {
148     hash ^= hash >>> 33;
149     hash *= PRIME64_2;
150     hash ^= hash >>> 29;
151     hash *= PRIME64_3;
152     hash ^= hash >>> 32;
153     return hash;
154   }
155 }