Cleanup
[carla.git] / source / modules / lilv / sord-0.16.0 / src / zix / digest.c
blob7d9c0352257af4b087696801f5ea0cea51679a99
1 /*
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"
19 #ifdef __SSE4_2__
20 # include <smmintrin.h>
21 #endif
23 ZIX_API uint32_t
24 zix_digest_start(void)
26 #ifdef __SSE4_2__
27 return 1; // CRC32 initial value
28 #else
29 return 5381; // DJB hash initial value
30 #endif
33 ZIX_API uint32_t
34 zix_digest_add(uint32_t hash, const void* const buf, const size_t len)
36 const uint8_t* str = (const uint8_t*)buf;
37 #ifdef __SSE4_2__
38 // SSE 4.2 CRC32
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);
50 #else
51 // Classic DJB hash
52 for (size_t i = 0; i < len; ++i) {
53 hash = (hash << 5) + hash + str[i];
55 #endif
56 return hash;