31#include "pedigree/kernel/utilities/spooky/SpookyV2.h"
32#include "pedigree/kernel/utilities/utility.h"
34#define memcpy MemoryCopy
37#define ALLOW_UNALIGNED_READS 1
43void SpookyHash::Short(
const void*
message,
size_t length, uint64* hash1, uint64* hash2) {
44 uint64 buf[2 * sc_numVars];
54 if (!ALLOW_UNALIGNED_READS && (u.i & 0x7)) {
59 size_t remainder = length % 32;
66 const uint64* end = u.p64 + (length / 32) * 4;
69 for (; u.p64 < end; u.p64 += 4) {
78 if (remainder >= 16) {
88 d += ((uint64)length) << 56;
91 d += ((uint64)u.p8[14]) << 48;
94 d += ((uint64)u.p8[13]) << 40;
97 d += ((uint64)u.p8[12]) << 32;
104 d += ((uint64)u.p8[10]) << 16;
107 d += ((uint64)u.p8[9]) << 8;
110 d += (uint64)u.p8[8];
116 c += ((uint64)u.p8[6]) << 48;
119 c += ((uint64)u.p8[5]) << 40;
122 c += ((uint64)u.p8[4]) << 32;
128 c += ((uint64)u.p8[2]) << 16;
131 c += ((uint64)u.p8[1]) << 8;
134 c += (uint64)u.p8[0];
140 ShortEnd(a, b, c, d);
146void SpookyHash::Hash128(
const void*
message,
size_t length, uint64* hash1, uint64* hash2) {
147 if (length < sc_bufSize) {
148 Short(
message, length, hash1, hash2);
152 uint64 h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11;
153 uint64 buf[sc_numVars];
162 h0 = h3 = h6 = h9 = *hash1;
163 h1 = h4 = h7 = h10 = *hash2;
164 h2 = h5 = h8 = h11 = sc_const;
167 end = u.p64 + (length / sc_blockSize) * sc_numVars;
170 if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0)) {
171 while (u.p64 < end) {
172 Mix(u.p64, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
176 while (u.p64 < end) {
177 memcpy(buf, u.p64, sc_blockSize);
178 Mix(buf, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
184 remainder = (length - ((
const uint8*)end - (
const uint8*)
message));
185 memcpy(buf, end, remainder);
186 memset(((uint8*)buf) + remainder, 0, sc_blockSize - remainder);
187 ((uint8*)buf)[sc_blockSize - 1] = remainder;
190 End(buf, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
196void SpookyHash::Init(uint64 seed1, uint64 seed2) {
204void SpookyHash::Update(
const void*
message,
size_t length) {
205 uint64 h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11;
206 size_t newLength = length + m_remainder;
216 if (newLength < sc_bufSize) {
217 memcpy(&((uint8*)m_data)[m_remainder],
message, length);
218 m_length = length + m_length;
219 m_remainder = (uint8)newLength;
224 if (m_length < sc_bufSize) {
225 h0 = h3 = h6 = h9 = m_state[0];
226 h1 = h4 = h7 = h10 = m_state[1];
227 h2 = h5 = h8 = h11 = sc_const;
242 m_length = length + m_length;
246 uint8 prefix = sc_bufSize - m_remainder;
247 memcpy(&(((uint8*)m_data)[m_remainder]),
message, prefix);
249 Mix(u.p64, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
250 Mix(&u.p64[sc_numVars], h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
251 u.p8 = ((
const uint8*)
message) + prefix;
258 end = u.p64 + (length / sc_blockSize) * sc_numVars;
259 remainder = (uint8)(length - ((
const uint8*)end - u.p8));
260 if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0) {
261 while (u.p64 < end) {
262 Mix(u.p64, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
266 while (u.p64 < end) {
267 memcpy(m_data, u.p8, sc_blockSize);
268 Mix(m_data, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
274 m_remainder = remainder;
275 memcpy(m_data, end, remainder);
293void SpookyHash::Final(uint64* hash1, uint64* hash2) {
295 if (m_length < sc_bufSize) {
298 Short(m_data, m_length, hash1, hash2);
302 uint64* data = (uint64*)m_data;
303 uint8 remainder = m_remainder;
305 uint64 h0 = m_state[0];
306 uint64 h1 = m_state[1];
307 uint64 h2 = m_state[2];
308 uint64 h3 = m_state[3];
309 uint64 h4 = m_state[4];
310 uint64 h5 = m_state[5];
311 uint64 h6 = m_state[6];
312 uint64 h7 = m_state[7];
313 uint64 h8 = m_state[8];
314 uint64 h9 = m_state[9];
315 uint64 h10 = m_state[10];
316 uint64 h11 = m_state[11];
318 if (remainder >= sc_blockSize) {
320 Mix(data, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);
322 remainder -= sc_blockSize;
326 memset(&((uint8*)data)[remainder], 0, (sc_blockSize - remainder));
328 ((uint8*)data)[sc_blockSize - 1] = remainder;
331 End(data, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11);