/work/svt-av1/Source/Lib/Codec/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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license. |
10 | | */ |
11 | | |
12 | | #include "hash.h" |
13 | | |
14 | | /* CRC-32C (iSCSI) polynomial in reversed bit order. */ |
15 | 0 | #define POLY 0x82f63b78 |
16 | | |
17 | | /* Construct table for software CRC-32C calculation. */ |
18 | 0 | void svt_av1_crc32c_calculator_init(CRC32C* p_crc32c) { |
19 | 0 | uint32_t crc; |
20 | |
|
21 | 0 | for (int n = 0; n < 256; n++) { |
22 | 0 | crc = n; |
23 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
24 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
25 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
26 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
27 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
28 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
29 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
30 | 0 | crc = (crc & 1) ? (crc >> 1) ^ POLY : crc >> 1; |
31 | 0 | p_crc32c->table[0][n] = crc; |
32 | 0 | } |
33 | 0 | for (int n = 0; n < 256; n++) { |
34 | 0 | crc = p_crc32c->table[0][n]; |
35 | 0 | for (int k = 1; k < 8; k++) { |
36 | 0 | crc = p_crc32c->table[0][crc & 0xff] ^ (crc >> 8); |
37 | 0 | p_crc32c->table[k][n] = crc; |
38 | 0 | } |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | | /* Table-driven software version as a fall-back. This is about 15 times slower |
43 | | than using the hardware instructions. This assumes little-endian integers, |
44 | | as is the case on Intel processors that the assembler code here is for. */ |
45 | 0 | uint32_t svt_av1_get_crc32c_value_c(void* c, const uint8_t* buf, size_t len) { |
46 | 0 | const uint8_t* next = buf; |
47 | 0 | uint64_t crc; |
48 | 0 | CRC32C* p = (CRC32C*)c; |
49 | 0 | crc = 0 ^ 0xffffffff; |
50 | 0 | while (len && ((uintptr_t)next & 7) != 0) { |
51 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
52 | 0 | len--; |
53 | 0 | } |
54 | 0 | while (len >= 8) { |
55 | 0 | crc ^= *(uint64_t*)next; |
56 | 0 | crc = p->table[7][crc & 0xff] ^ p->table[6][(crc >> 8) & 0xff] ^ p->table[5][(crc >> 16) & 0xff] ^ |
57 | 0 | p->table[4][(crc >> 24) & 0xff] ^ p->table[3][(crc >> 32) & 0xff] ^ p->table[2][(crc >> 40) & 0xff] ^ |
58 | 0 | p->table[1][(crc >> 48) & 0xff] ^ p->table[0][crc >> 56]; |
59 | 0 | next += 8; |
60 | 0 | len -= 8; |
61 | 0 | } |
62 | 0 | while (len) { |
63 | 0 | crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); |
64 | 0 | len--; |
65 | 0 | } |
66 | 0 | return (uint32_t)crc ^ 0xffffffff; |
67 | 0 | } |