comparison ext/sord/src/zix/digest.c @ 247:8a031eb9a25f

Merge branch 'output-type-uri'
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 15 Jun 2017 09:52:01 +0100
parents b32c68f08ec0
children
comparison
equal deleted inserted replaced
219:db929669e7d3 247:8a031eb9a25f
1 /*
2 Copyright 2012-2014 David Robillard <http://drobilla.net>
3
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.
7
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.
15 */
16
17 #include "digest.h"
18
19 #ifdef __SSE4_2__
20 # include <smmintrin.h>
21 #endif
22
23 ZIX_API uint32_t
24 zix_digest_start(void)
25 {
26 #ifdef __SSE4_2__
27 return 1; // CRC32 initial value
28 #else
29 return 5381; // DJB hash initial value
30 #endif
31 }
32
33 ZIX_API uint32_t
34 zix_digest_add(uint32_t hash, const void* const buf, const size_t len)
35 {
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);
42 }
43 if (len & sizeof(uint16_t)) {
44 hash = _mm_crc32_u16(hash, *(const uint16_t*)str);
45 str += sizeof(uint16_t);
46 }
47 if (len & sizeof(uint8_t)) {
48 hash = _mm_crc32_u8(hash, *(const uint8_t*)str);
49 }
50 #else
51 // Classic DJB hash
52 for (size_t i = 0; i < len; ++i) {
53 hash = (hash << 5) + hash + str[i];
54 }
55 #endif
56 return hash;
57 }