2 Copyright 2012-2014 David Robillard <http://drobilla.net>
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "zix/digest.h"
20 # include <smmintrin.h>
24 zix_digest_start(void)
27 return 1; // CRC32 initial value
29 return 5381; // DJB hash initial value
34 zix_digest_add(uint32_t hash
, const void* const buf
, const size_t len
)
36 const uint8_t* str
= (const uint8_t*)buf
;
39 for (size_t i
= 0; i
< (len
/ sizeof(uint32_t)); ++i
) {
40 hash
= _mm_crc32_u32(hash
, *(const uint32_t*)str
);
41 str
+= sizeof(uint32_t);
43 if (len
& sizeof(uint16_t)) {
44 hash
= _mm_crc32_u16(hash
, *(const uint16_t*)str
);
45 str
+= sizeof(uint16_t);
47 if (len
& sizeof(uint8_t)) {
48 hash
= _mm_crc32_u8(hash
, *(const uint8_t*)str
);
52 for (size_t i
= 0; i
< len
; ++i
) {
53 hash
= (hash
<< 5) + hash
+ str
[i
];