1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <stdint.h>
13 #include <smmintrin.h>
14
15 // Byte-boundary alignment issues
16 #define ALIGN_SIZE 8
17 #define ALIGN_MASK (ALIGN_SIZE - 1)
18
19 #define CALC_CRC(op, crc, type, buf, len) \
20 while ((len) >= sizeof(type)) { \
21 (crc) = op((crc), *(type *)(buf)); \
22 (len) -= sizeof(type); \
23 buf += sizeof(type); \
24 }
25
26 /**
27 * Calculates 32-bit CRC for the input buffer
28 * polynomial is 0x11EDC6F41
29 * @return A 32-bit unsigned integer representing the CRC
30 */
av1_get_crc32c_value_sse4_2(void * crc_calculator,uint8_t * p,size_t len)31 uint32_t av1_get_crc32c_value_sse4_2(void *crc_calculator, uint8_t *p,
32 size_t len) {
33 (void)crc_calculator;
34 const uint8_t *buf = p;
35 uint32_t crc = 0xFFFFFFFF;
36
37 // Align the input to the word boundary
38 for (; (len > 0) && ((intptr_t)buf & ALIGN_MASK); len--, buf++) {
39 crc = _mm_crc32_u8(crc, *buf);
40 }
41
42 #ifdef __x86_64__
43 uint64_t crc64 = crc;
44 CALC_CRC(_mm_crc32_u64, crc64, uint64_t, buf, len)
45 crc = (uint32_t)crc64;
46 #endif
47 CALC_CRC(_mm_crc32_u32, crc, uint32_t, buf, len)
48 CALC_CRC(_mm_crc32_u16, crc, uint16_t, buf, len)
49 CALC_CRC(_mm_crc32_u8, crc, uint8_t, buf, len)
50 return (crc ^ 0xFFFFFFFF);
51 }
52