2 * Copyright 2015 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
27 // extra mix from long hash
29 #include <folly/SpookyHashV2.h>
31 #include <folly/Portability.h>
38 static constexpr auto kAllowUnalignedReads = bool(FOLLY_HAVE_UNALIGNED_READS);
41 // short hash ... it could be used on any message,
42 // but it's used by Spooky just for short messages.
44 void SpookyHashV2::Short(
50 uint64_t buf[2*sc_numVars];
59 u.p8 = (const uint8_t *)message;
61 if (!kAllowUnalignedReads && (u.i & 0x7))
63 memcpy(buf, message, length);
67 size_t remainder = length%32;
75 const uint64_t *end = u.p64 + (length/32)*4;
77 // handle all complete sets of 32 bytes
78 for (; u.p64 < end; u.p64 += 4)
87 //Handle the case of 16+ remaining bytes.
98 // Handle the last 0..15 bytes, and its length
99 d += ((uint64_t)length) << 56;
103 d += ((uint64_t)u.p8[14]) << 48;
105 d += ((uint64_t)u.p8[13]) << 40;
107 d += ((uint64_t)u.p8[12]) << 32;
113 d += ((uint64_t)u.p8[10]) << 16;
115 d += ((uint64_t)u.p8[9]) << 8;
117 d += (uint64_t)u.p8[8];
122 c += ((uint64_t)u.p8[6]) << 48;
124 c += ((uint64_t)u.p8[5]) << 40;
126 c += ((uint64_t)u.p8[4]) << 32;
131 c += ((uint64_t)u.p8[2]) << 16;
133 c += ((uint64_t)u.p8[1]) << 8;
135 c += (uint64_t)u.p8[0];
149 // do the whole hash in one call
150 void SpookyHashV2::Hash128(
156 if (length < sc_bufSize)
158 Short(message, length, hash1, hash2);
162 uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
163 uint64_t buf[sc_numVars];
173 h0=h3=h6=h9 = *hash1;
174 h1=h4=h7=h10 = *hash2;
175 h2=h5=h8=h11 = sc_const;
177 u.p8 = (const uint8_t *)message;
178 end = u.p64 + (length/sc_blockSize)*sc_numVars;
180 // handle all whole sc_blockSize blocks of bytes
181 if (kAllowUnalignedReads || ((u.i & 0x7) == 0))
185 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
193 memcpy(buf, u.p64, sc_blockSize);
194 Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
199 // handle the last partial block of sc_blockSize bytes
200 remainder = (length - ((const uint8_t *)end-(const uint8_t *)message));
201 memcpy(buf, end, remainder);
202 memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder);
203 ((uint8_t *)buf)[sc_blockSize-1] = remainder;
205 // do some final mixing
206 End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
214 void SpookyHashV2::Init(uint64_t seed1, uint64_t seed2)
223 // add a message fragment to the state
224 void SpookyHashV2::Update(const void *message, size_t length)
226 uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
227 size_t newLength = length + m_remainder;
237 // Is this message fragment too short? If it is, stuff it away.
238 if (newLength < sc_bufSize)
240 memcpy(&((uint8_t *)m_data)[m_remainder], message, length);
241 m_length = length + m_length;
242 m_remainder = (uint8_t)newLength;
246 // init the variables
247 if (m_length < sc_bufSize)
249 h0=h3=h6=h9 = m_state[0];
250 h1=h4=h7=h10 = m_state[1];
251 h2=h5=h8=h11 = sc_const;
268 m_length = length + m_length;
270 // if we've got anything stuffed away, use it now
273 uint8_t prefix = sc_bufSize-m_remainder;
274 memcpy(&(((uint8_t *)m_data)[m_remainder]), message, prefix);
276 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
277 Mix(&u.p64[sc_numVars], h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
278 u.p8 = ((const uint8_t *)message) + prefix;
283 u.p8 = (const uint8_t *)message;
286 // handle all whole blocks of sc_blockSize bytes
287 end = u.p64 + (length/sc_blockSize)*sc_numVars;
288 remainder = (uint8_t)(length-((const uint8_t *)end-u.p8));
289 if (kAllowUnalignedReads || (u.i & 0x7) == 0)
293 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
301 memcpy(m_data, u.p8, sc_blockSize);
302 Mix(m_data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
307 // stuff away the last few bytes
308 m_remainder = remainder;
309 memcpy(m_data, end, remainder);
311 // stuff away the variables
327 // report the hash for the concatenation of all message fragments so far
328 void SpookyHashV2::Final(uint64_t *hash1, uint64_t *hash2) const
330 // init the variables
331 if (m_length < sc_bufSize)
335 Short( m_data, m_length, hash1, hash2);
339 uint64_t buf[2*sc_numVars];
340 memcpy(buf, m_data, sizeof(buf));
341 uint64_t *data = buf;
342 uint8_t remainder = m_remainder;
344 uint64_t h0 = m_state[0];
345 uint64_t h1 = m_state[1];
346 uint64_t h2 = m_state[2];
347 uint64_t h3 = m_state[3];
348 uint64_t h4 = m_state[4];
349 uint64_t h5 = m_state[5];
350 uint64_t h6 = m_state[6];
351 uint64_t h7 = m_state[7];
352 uint64_t h8 = m_state[8];
353 uint64_t h9 = m_state[9];
354 uint64_t h10 = m_state[10];
355 uint64_t h11 = m_state[11];
357 if (remainder >= sc_blockSize)
359 // m_data can contain two blocks; handle any whole first block
360 Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
362 remainder -= sc_blockSize;
365 // mix in the last partial block, and the length mod sc_blockSize
366 memset(&((uint8_t *)data)[remainder], 0, (sc_blockSize-remainder));
368 ((uint8_t *)data)[sc_blockSize-1] = remainder;
370 // do some final mixing
371 End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);