32 #include "pedigree/kernel/utilities/spooky/SpookyV2.h" 34 #define ALLOW_UNALIGNED_READS 1 40 void SpookyHash::Short(
46 uint64 buf[2*sc_numVars];
55 u.p8 = (
const uint8 *)message;
57 if (!ALLOW_UNALIGNED_READS && (u.i & 0x7))
59 memcpy(buf, message, length);
63 size_t remainder = length%32;
71 const uint64 *end = u.p64 + (length/32)*4;
74 for (; u.p64 < end; u.p64 += 4)
95 d += ((uint64)length) << 56;
99 d += ((uint64)u.p8[14]) << 48;
101 d += ((uint64)u.p8[13]) << 40;
103 d += ((uint64)u.p8[12]) << 32;
109 d += ((uint64)u.p8[10]) << 16;
111 d += ((uint64)u.p8[9]) << 8;
113 d += (uint64)u.p8[8];
118 c += ((uint64)u.p8[6]) << 48;
120 c += ((uint64)u.p8[5]) << 40;
122 c += ((uint64)u.p8[4]) << 32;
127 c += ((uint64)u.p8[2]) << 16;
129 c += ((uint64)u.p8[1]) << 8;
131 c += (uint64)u.p8[0];
146 void SpookyHash::Hash128(
152 if (length < sc_bufSize)
154 Short(message, length, hash1, hash2);
158 uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
159 uint64 buf[sc_numVars];
169 h0=h3=h6=h9 = *hash1;
170 h1=h4=h7=h10 = *hash2;
171 h2=h5=h8=h11 = sc_const;
173 u.p8 = (
const uint8 *)message;
174 end = u.p64 + (length/sc_blockSize)*sc_numVars;
177 if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0))
181 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
189 memcpy(buf, u.p64, sc_blockSize);
190 Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
196 remainder = (length - ((
const uint8 *)end-(
const uint8 *)message));
197 memcpy(buf, end, remainder);
198 memset(((uint8 *)buf)+remainder, 0, sc_blockSize-remainder);
199 ((uint8 *)buf)[sc_blockSize-1] = remainder;
202 End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
210 void SpookyHash::Init(uint64 seed1, uint64 seed2)
220 void SpookyHash::Update(
const void *message,
size_t length)
222 uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
223 size_t newLength = length + m_remainder;
234 if (newLength < sc_bufSize)
236 memcpy(&((uint8 *)m_data)[m_remainder], message, length);
237 m_length = length + m_length;
238 m_remainder = (uint8)newLength;
243 if (m_length < sc_bufSize)
245 h0=h3=h6=h9 = m_state[0];
246 h1=h4=h7=h10 = m_state[1];
247 h2=h5=h8=h11 = sc_const;
264 m_length = length + m_length;
269 uint8 prefix = sc_bufSize-m_remainder;
270 memcpy(&(((uint8 *)m_data)[m_remainder]), message, prefix);
272 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
273 Mix(&u.p64[sc_numVars], h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
274 u.p8 = ((
const uint8 *)message) + prefix;
279 u.p8 = (
const uint8 *)message;
283 end = u.p64 + (length/sc_blockSize)*sc_numVars;
284 remainder = (uint8)(length-((
const uint8 *)end-u.p8));
285 if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0)
289 Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
297 memcpy(m_data, u.p8, sc_blockSize);
298 Mix(m_data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
304 m_remainder = remainder;
305 memcpy(m_data, end, remainder);
324 void SpookyHash::Final(uint64 *hash1, uint64 *hash2)
327 if (m_length < sc_bufSize)
331 Short( m_data, m_length, hash1, hash2);
335 const uint64 *data = (
const uint64 *)m_data;
336 uint8 remainder = m_remainder;
338 uint64 h0 = m_state[0];
339 uint64 h1 = m_state[1];
340 uint64 h2 = m_state[2];
341 uint64 h3 = m_state[3];
342 uint64 h4 = m_state[4];
343 uint64 h5 = m_state[5];
344 uint64 h6 = m_state[6];
345 uint64 h7 = m_state[7];
346 uint64 h8 = m_state[8];
347 uint64 h9 = m_state[9];
348 uint64 h10 = m_state[10];
349 uint64 h11 = m_state[11];
351 if (remainder >= sc_blockSize)
354 Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
356 remainder -= sc_blockSize;
360 memset(&((uint8 *)data)[remainder], 0, (sc_blockSize-remainder));
362 ((uint8 *)data)[sc_blockSize-1] = remainder;
365 End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);