Coverage Report

Created: 2025-12-03 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
85.5M
#define POLY 0x82f63b78
20
21
/* Construct table for software CRC-32C calculation. */
22
85.5k
void av1_crc32c_calculator_init(CRC32C *p_crc32c) {
23
85.5k
  uint32_t crc;
24
25
21.2M
  for (int n = 0; n < 256; n++) {
26
21.1M
    crc = n;
27
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
28
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
29
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
30
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
31
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
32
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
33
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
34
21.1M
    crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
35
21.1M
    p_crc32c->table[0][n] = crc;
36
21.1M
  }
37
21.1M
  for (int n = 0; n < 256; n++) {
38
21.0M
    crc = p_crc32c->table[0][n];
39
165M
    for (int k = 1; k < 8; k++) {
40
144M
      crc = p_crc32c->table[0][crc & 0xff] ^ (crc >> 8);
41
144M
      p_crc32c->table[k][n] = crc;
42
144M
    }
43
21.0M
  }
44
85.5k
}
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.68M
uint32_t av1_get_crc32c_value_c(void *c, const uint8_t *buf, size_t len) {
50
2.68M
  const uint8_t *next = buf;
51
2.68M
  uint64_t crc;
52
2.68M
  CRC32C *p = (CRC32C *)c;
53
2.68M
  crc = 0 ^ 0xffffffff;
54
2.68M
  while (len && ((uintptr_t)next & 7) != 0) {
55
0
    crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
56
0
    len--;
57
0
  }
58
45.8M
  while (len >= 8) {
59
43.1M
    crc ^= *(uint64_t *)next;
60
43.1M
    crc = p->table[7][crc & 0xff] ^ p->table[6][(crc >> 8) & 0xff] ^
61
43.1M
          p->table[5][(crc >> 16) & 0xff] ^ p->table[4][(crc >> 24) & 0xff] ^
62
43.1M
          p->table[3][(crc >> 32) & 0xff] ^ p->table[2][(crc >> 40) & 0xff] ^
63
43.1M
          p->table[1][(crc >> 48) & 0xff] ^ p->table[0][crc >> 56];
64
43.1M
    next += 8;
65
43.1M
    len -= 8;
66
43.1M
  }
67
2.68M
  while (len) {
68
0
    crc = p->table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
69
0
    len--;
70
0
  }
71
2.68M
  return (uint32_t)crc ^ 0xffffffff;
72
2.68M
}