|
9 | 9 | #include <stddef.h>
|
10 | 10 | #include <string.h>
|
11 | 11 |
|
| 12 | +#include "iree/base/alignment.h" |
12 | 13 | #include "iree/vm/instance.h"
|
13 | 14 |
|
14 | 15 | IREE_VM_DEFINE_TYPE_ADAPTERS(iree_vm_buffer, iree_vm_buffer_t);
|
@@ -314,6 +315,90 @@ IREE_API_EXPORT iree_status_t iree_vm_buffer_write_elements(
|
314 | 315 | return iree_ok_status();
|
315 | 316 | }
|
316 | 317 |
|
| 318 | +// Based on reference implementation from https://github.com/veorq/SipHash |
| 319 | +// By Jean-Philippe Aumasson and Daniel J. Bernstein. |
| 320 | +#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) |
| 321 | +#define SIPROUND(v0, v1, v2, v3) \ |
| 322 | + v0 += v1; \ |
| 323 | + v1 = ROTL(v1, 13); \ |
| 324 | + v1 ^= v0; \ |
| 325 | + v0 = ROTL(v0, 32); \ |
| 326 | + v2 += v3; \ |
| 327 | + v3 = ROTL(v3, 16); \ |
| 328 | + v3 ^= v2; \ |
| 329 | + v0 += v3; \ |
| 330 | + v3 = ROTL(v3, 21); \ |
| 331 | + v3 ^= v0; \ |
| 332 | + v2 += v1; \ |
| 333 | + v1 = ROTL(v1, 17); \ |
| 334 | + v1 ^= v2; \ |
| 335 | + v2 = ROTL(v2, 32) |
| 336 | + |
| 337 | +// Using SipHash-2-4. |
| 338 | +#ifndef cROUNDS |
| 339 | +#define cROUNDS 2 |
| 340 | +#endif |
| 341 | +#ifndef dROUNDS |
| 342 | +#define dROUNDS 4 |
| 343 | +#endif |
| 344 | + |
| 345 | +IREE_API_EXPORT iree_status_t iree_vm_buffer_hash( |
| 346 | + const iree_vm_buffer_t* source_buffer, iree_host_size_t source_offset, |
| 347 | + iree_host_size_t length, int64_t* result) { |
| 348 | + IREE_TRACE_ZONE_BEGIN(z0); |
| 349 | + IREE_ASSERT_ARGUMENT(source_buffer); |
| 350 | + // Get the byte span for the source data. |
| 351 | + iree_const_byte_span_t source_span = iree_const_byte_span_empty(); |
| 352 | + IREE_RETURN_AND_END_ZONE_IF_ERROR( |
| 353 | + z0, iree_vm_buffer_map_ro(source_buffer, source_offset, length, 1, |
| 354 | + &source_span)); |
| 355 | + const uint8_t* source = source_span.data; |
| 356 | + const uint8_t* end = source + source_span.data_length - |
| 357 | + (source_span.data_length % sizeof(uint64_t)); |
| 358 | + const int left = source_span.data_length & 7; |
| 359 | + uint64_t hash = ((uint64_t)source_span.data_length) << 56; |
| 360 | + |
| 361 | + // Using key = 0x000102030405060708090a0b0c0d0e0f |
| 362 | + uint64_t v0 = UINT64_C(0x736f6d6570736575 ^ 0x0706050403020100); |
| 363 | + uint64_t v1 = UINT64_C(0x646f72616e646f6d ^ 0x0f0e0d0c0b0a0908); |
| 364 | + uint64_t v2 = UINT64_C(0x6c7967656e657261 ^ 0x0706050403020100); |
| 365 | + uint64_t v3 = UINT64_C(0x7465646279746573 ^ 0x0f0e0d0c0b0a0908); |
| 366 | + uint64_t m; |
| 367 | + |
| 368 | + for (; source != end; source += 8) { |
| 369 | + m = iree_unaligned_load_le_u64((const uint64_t*)source); |
| 370 | + v3 ^= m; |
| 371 | + for (int i = 0; i < cROUNDS; ++i) { |
| 372 | + SIPROUND(v0, v1, v2, v3); |
| 373 | + } |
| 374 | + v0 ^= m; |
| 375 | + } |
| 376 | + |
| 377 | + uint64_t tmp = 0; |
| 378 | + for (int l = left; l > 0; --l) { |
| 379 | + tmp = tmp << 8; |
| 380 | + tmp |= (uint64_t)source[l - 1]; |
| 381 | + } |
| 382 | + hash |= tmp; |
| 383 | + v3 ^= hash; |
| 384 | + |
| 385 | + for (int i = 0; i < cROUNDS; ++i) { |
| 386 | + SIPROUND(v0, v1, v2, v3); |
| 387 | + } |
| 388 | + |
| 389 | + v0 ^= hash; |
| 390 | + v2 ^= 0xff; |
| 391 | + |
| 392 | + for (int i = 0; i < dROUNDS; ++i) { |
| 393 | + SIPROUND(v0, v1, v2, v3); |
| 394 | + } |
| 395 | + |
| 396 | + hash = v0 ^ v1 ^ v2 ^ v3; |
| 397 | + iree_unaligned_store_le_u64(result, hash); |
| 398 | + IREE_TRACE_ZONE_END(z0); |
| 399 | + return iree_ok_status(); |
| 400 | +} |
| 401 | + |
317 | 402 | iree_status_t iree_vm_buffer_register_types(iree_vm_instance_t* instance) {
|
318 | 403 | static const iree_vm_ref_type_descriptor_t descriptor = {
|
319 | 404 | .destroy = iree_vm_buffer_destroy,
|
|
0 commit comments