xref: /aosp_15_r20/external/libaom/av1/encoder/x86/hash_sse42.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #include "config/av1_rtcd.h"
16 
17 // Byte-boundary alignment issues
18 #define ALIGN_SIZE 8
19 #define ALIGN_MASK (ALIGN_SIZE - 1)
20 
21 #define CALC_CRC(op, crc, type, buf, len) \
22   while ((len) >= sizeof(type)) {         \
23     (crc) = op((crc), *(type *)(buf));    \
24     (len) -= sizeof(type);                \
25     buf += sizeof(type);                  \
26   }
27 
28 /**
29  * Calculates 32-bit CRC for the input buffer
30  * polynomial is 0x11EDC6F41
31  * @return A 32-bit unsigned integer representing the CRC
32  */
av1_get_crc32c_value_sse4_2(void * crc_calculator,uint8_t * p,size_t len)33 uint32_t av1_get_crc32c_value_sse4_2(void *crc_calculator, uint8_t *p,
34                                      size_t len) {
35   (void)crc_calculator;
36   const uint8_t *buf = p;
37   uint32_t crc = 0xFFFFFFFF;
38 
39   // Align the input to the word boundary
40   for (; (len > 0) && ((intptr_t)buf & ALIGN_MASK); len--, buf++) {
41     crc = _mm_crc32_u8(crc, *buf);
42   }
43 
44 #ifdef __x86_64__
45   uint64_t crc64 = crc;
46   CALC_CRC(_mm_crc32_u64, crc64, uint64_t, buf, len)
47   crc = (uint32_t)crc64;
48 #endif
49   CALC_CRC(_mm_crc32_u32, crc, uint32_t, buf, len)
50   CALC_CRC(_mm_crc32_u16, crc, uint16_t, buf, len)
51   CALC_CRC(_mm_crc32_u8, crc, uint8_t, buf, len)
52   return (crc ^ 0xFFFFFFFF);
53 }
54