avutil/crc: refactor helper functions to separate header file

Move the reverse and xnmodp functions to a separate header
so that it can be reused for aarch64 implementation of av_crc.
This commit is contained in:
Shreesh Adiga
2026-02-05 18:23:18 +05:30
committed by Martin Storsjö
parent b19bd0de6c
commit 952e588600
2 changed files with 68 additions and 42 deletions

66
libavutil/crc_internal.h Normal file
View File

@@ -0,0 +1,66 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVUTIL_CRC_INTERNAL_H
#define AVUTIL_CRC_INTERNAL_H
#include <stdint.h>
#include "libavutil/reverse.h"
static uint64_t reverse(uint64_t p, unsigned int deg)
{
uint64_t ret = 0;
int i;
for (i = 0; i < (deg / 8); i += 1) {
ret = (ret << 8) | (ff_reverse[p & 0xff]);
p >>= 8;
}
int rem = (deg + 1) - 8 * i;
ret = (ret << rem) | (ff_reverse[p & 0xff] >> (8 - rem));
return ret;
}
static uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse)
{
uint64_t mod, mask, high;
if (n < deg) {
*div = 0;
return poly;
}
mask = ((uint64_t)1 << deg) - 1;
poly &= mask;
mod = poly;
*div = 1;
deg--;
while (--n > deg) {
high = (mod >> deg) & 1;
*div = (*div << 1) | high;
mod <<= 1;
if (high)
mod ^= poly;
}
uint64_t ret = mod & mask;
if (bitreverse) {
*div = reverse(*div, deg) << 1;
return reverse(ret, deg) << 1;
}
return ret;
}
#endif /* AVUTIL_CRC_INTERNAL_H */

View File

@@ -28,10 +28,11 @@
#include "libavutil/cpu.h"
#include "libavutil/crc.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/reverse.h"
#include "libavutil/x86/cpu.h"
#if HAVE_CLMUL_EXTERNAL
#include "libavutil/crc_internal.h"
FF_VISIBILITY_PUSH_HIDDEN
uint32_t ff_crc_clmul(const AVCRC *ctx, uint32_t crc,
const uint8_t *buffer, size_t length);
@@ -104,47 +105,6 @@ static const AVCRC crc_table_clmul[AV_CRC_MAX][17] = {
},
};
static uint64_t reverse(uint64_t p, unsigned int deg)
{
uint64_t ret = 0;
int i;
for (i = 0; i < (deg / 8); i += 1) {
ret = (ret << 8) | (ff_reverse[p & 0xff]);
p >>= 8;
}
int rem = (deg + 1) - 8 * i;
ret = (ret << rem) | (ff_reverse[p & 0xff] >> (8 - rem));
return ret;
}
static uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse)
{
uint64_t mod, mask, high;
if (n < deg) {
*div = 0;
return poly;
}
mask = ((uint64_t)1 << deg) - 1;
poly &= mask;
mod = poly;
*div = 1;
deg--;
while (--n > deg) {
high = (mod >> deg) & 1;
*div = (*div << 1) | high;
mod <<= 1;
if (high)
mod ^= poly;
}
uint64_t ret = mod & mask;
if (bitreverse) {
*div = reverse(*div, deg) << 1;
return reverse(ret, deg) << 1;
}
return ret;
}
static inline void crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
{
uint64_t poly_;