/src/libavif/ext/aom/av1/encoder/hash.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016, 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 "av1/encoder/hash.h" |
13 | | |
14 | | #include <stddef.h> |
15 | | |
16 | | #include "config/av1_rtcd.h" |
17 | | |
18 | | /* CRC-32C (iSCSI) polynomial in reversed bit order. */ |
19 | 74.5M | #define POLY 0x82f63b78 |
20 | | |
21 | | /* Construct table for software CRC-32C calculation. */ |
22 | 86.1k | void av1_crc32c_calculator_init(CRC32C *p_crc32c) { |
23 | 86.1k | uint32_t crc; |
24 | | |
25 | 18.6M | for (int n = 0; n < 256; n++) { |
26 | 18.6M | crc = n; |
27 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
28 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
29 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
30 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
31 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
32 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
33 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
34 | 18.6M | crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; |
35 | 18.6M | p_crc32c->table[0][n] = crc; |
36 | 18.6M | } |
37 | 19.4M | for (int n = 0; n < 256; n++) { |
38 | 19.4M | crc = p_crc32c->table[0][n]; |
39 | 154M | for (int k = 1; k < 8; k++) { |
40 | 135M | crc = p_crc32c->table[0][crc & 0xff] ^ (crc >> 8); |
41 | 135M | p_crc32c->table[k][n] = crc; |
42 | 135M | } |
43 | 19.4M | } |
44 | 86.1k | } |
45 | | |
46 | | /* Table-driven software version as a fall-back. This is about 15 times slower |
47 | | than using the hardware instructions. This assumes little-endian integers, |
48 | | as is the case on Intel processors that the assembler code here is for. */ |
49 | 2.71M | uint32_t av1_get_crc32c_value_c(void *c, const uint8_t *buf, size_t len) { |
50 | 2.71M | const uint8_t *next = buf; |
51 | 2.71M | uint64_t crc; |
52 | 2.71M | CRC32C *p = (CRC32C *)c; |
53 | 2.71M | crc = 0 ^ 0xffffffff; |
54 | 2.71M | while (len && ((uintptr_t)next & 7) != 0) { |
55 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
56 | 0 | len--; |
57 | 0 | } |
58 | 46.3M | while (len >= 8) { |
59 | 43.5M | crc ^= *(uint64_t *)next; |
60 | 43.5M | crc = p->table[7][crc & 0xff] ^ p->table[6][(crc >> 8) & 0xff] ^ |
61 | 43.5M | p->table[5][(crc >> 16) & 0xff] ^ p->table[4][(crc >> 24) & 0xff] ^ |
62 | 43.5M | p->table[3][(crc >> 32) & 0xff] ^ p->table[2][(crc >> 40) & 0xff] ^ |
63 | 43.5M | p->table[1][(crc >> 48) & 0xff] ^ p->table[0][crc >> 56]; |
64 | 43.5M | next += 8; |
65 | 43.5M | len -= 8; |
66 | 43.5M | } |
67 | 2.71M | while (len) { |
68 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
69 | 0 | len--; |
70 | 0 | } |
71 | 2.71M | return (uint32_t)crc ^ 0xffffffff; |
72 | 2.71M | } |