2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 // A 128-bit noncryptographic hash, for checksums and table lookup
19 // By Bob Jenkins. Public domain.
20 // Oct 31 2010: published framework, disclaimer ShortHash isn't right
21 // Nov 7 2010: disabled ShortHash
22 // Oct 31 2011: replace End, ShortMix, ShortEnd, enable ShortHash again
23 // April 10 2012: buffer overflow on platforms without unaligned reads
24 // July 12 2012: was passing out variables in final to in/out in short
25 // July 30 2012: I reintroduced the buffer overflow
26 // August 5 2012: SpookyV2: d = should be d += in short hash, and remove extra mix from long hash
28 #include <folly/SpookyHashV2.h>
32 #define ALLOW_UNALIGNED_READS 1
38 // short hash ... it could be used on any message,
39 // but it's used by Spooky just for short messages.
41 void SpookyHashV2::Short(
47 uint64_t buf[2*sc_numVars];
56 u.p8 = (const uint8_t *)message;
58 if (!ALLOW_UNALIGNED_READS && (u.i & 0x7))
60 memcpy(buf, message, length);
64 size_t remainder = length%32;
72 const uint64_t *end = u.p64 + (length/32)*4;
74 // handle all complete sets of 32 bytes
75 for (; u.p64 < end; u.p64 += 4)
84 //Handle the case of 16+ remaining bytes.
95 // Handle the last 0..15 bytes, and its length
96 d += ((uint64_t)length) << 56;
100 d += ((uint64_t)u.p8[14]) << 48;
102 d += ((uint64_t)u.p8[13]) << 40;
104 d += ((uint64_t)u.p8[12]) << 32;
110 d += ((uint64_t)u.p8[10]) << 16;
112 d += ((uint64_t)u.p8[9]) << 8;
114 d += (uint64_t)u.p8[8];
119 c += ((uint64_t)u.p8[6]) << 48;
121 c += ((uint64_t)u.p8[5]) << 40;
123 c += ((uint64_t)u.p8[4]) << 32;
128 c += ((uint64_t)u.p8[2]) << 16;
130 c += ((uint64_t)u.p8[1]) << 8;
132 c += (uint64_t)u.p8[0];
146 // do the whole hash in one call
147 void SpookyHashV2::Hash128(
153 if (length < sc_bufSize)
155 Short(message, length, hash1, hash2);
159 uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
160 uint64_t buf[sc_numVars];
170 h0=h3=h6=h9 = *hash1;
171 h1=h4=h7=h10 = *hash2;
172 h2=h5=h8=h11 = sc_const;
174 u.p8 = (const uint8_t *)message;
175 end = u.p64 + (length/sc_blockSize)*sc_numVars;
177 // handle all whole sc_blockSize blocks of bytes
178 if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0))
182 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
190 memcpy(buf, u.p64, sc_blockSize);
191 Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
196 // handle the last partial block of sc_blockSize bytes
197 remainder = (length - ((const uint8_t *)end-(const uint8_t *)message));
198 memcpy(buf, end, remainder);
199 memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder);
200 ((uint8_t *)buf)[sc_blockSize-1] = remainder;
202 // do some final mixing
203 End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
211 void SpookyHashV2::Init(uint64_t seed1, uint64_t seed2)
220 // add a message fragment to the state
221 void SpookyHashV2::Update(const void *message, size_t length)
223 uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
224 size_t newLength = length + m_remainder;
234 // Is this message fragment too short? If it is, stuff it away.
235 if (newLength < sc_bufSize)
237 memcpy(&((uint8_t *)m_data)[m_remainder], message, length);
238 m_length = length + m_length;
239 m_remainder = (uint8_t)newLength;
243 // init the variables
244 if (m_length < sc_bufSize)
246 h0=h3=h6=h9 = m_state[0];
247 h1=h4=h7=h10 = m_state[1];
248 h2=h5=h8=h11 = sc_const;
265 m_length = length + m_length;
267 // if we've got anything stuffed away, use it now
270 uint8_t prefix = sc_bufSize-m_remainder;
271 memcpy(&(((uint8_t *)m_data)[m_remainder]), message, prefix);
273 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
274 Mix(&u.p64[sc_numVars], h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
275 u.p8 = ((const uint8_t *)message) + prefix;
280 u.p8 = (const uint8_t *)message;
283 // handle all whole blocks of sc_blockSize bytes
284 end = u.p64 + (length/sc_blockSize)*sc_numVars;
285 remainder = (uint8_t)(length-((const uint8_t *)end-u.p8));
286 if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0)
290 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
298 memcpy(m_data, u.p8, sc_blockSize);
299 Mix(m_data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
304 // stuff away the last few bytes
305 m_remainder = remainder;
306 memcpy(m_data, end, remainder);
308 // stuff away the variables
324 // report the hash for the concatenation of all message fragments so far
325 void SpookyHashV2::Final(uint64_t *hash1, uint64_t *hash2)
327 // init the variables
328 if (m_length < sc_bufSize)
332 Short( m_data, m_length, hash1, hash2);
336 const uint64_t *data = (const uint64_t *)m_data;
337 uint8_t remainder = m_remainder;
339 uint64_t h0 = m_state[0];
340 uint64_t h1 = m_state[1];
341 uint64_t h2 = m_state[2];
342 uint64_t h3 = m_state[3];
343 uint64_t h4 = m_state[4];
344 uint64_t h5 = m_state[5];
345 uint64_t h6 = m_state[6];
346 uint64_t h7 = m_state[7];
347 uint64_t h8 = m_state[8];
348 uint64_t h9 = m_state[9];
349 uint64_t h10 = m_state[10];
350 uint64_t h11 = m_state[11];
352 if (remainder >= sc_blockSize)
354 // m_data can contain two blocks; handle any whole first block
355 Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
357 remainder -= sc_blockSize;
360 // mix in the last partial block, and the length mod sc_blockSize
361 memset(&((uint8_t *)data)[remainder], 0, (sc_blockSize-remainder));
363 ((uint8_t *)data)[sc_blockSize-1] = remainder;
365 // do some final mixing
366 End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);