tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hash.c (2541B)


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