]> www.infradead.org Git - users/jedix/linux-maple.git/commit
riscv/crc: add "template" for Zbc optimized CRC functions
authorEric Biggers <ebiggers@google.com>
Sun, 16 Feb 2025 22:55:27 +0000 (14:55 -0800)
committerEric Biggers <ebiggers@google.com>
Mon, 10 Mar 2025 16:29:08 +0000 (09:29 -0700)
commitbbe2610bc5ada51418a4191e799cfb4577302a31
tree1038e4c4be5ac959a6677c22737b368aa3769c75
parenta0bd462f3a1369f4440cc4448b879ed2c8611f1a
riscv/crc: add "template" for Zbc optimized CRC functions

Add a "template" crc-clmul-template.h that can generate RISC-V Zbc
optimized CRC functions.  Each generated CRC function is parameterized
by CRC length and bit order, and it accepts a pointer to the constants
struct required for the specific CRC polynomial desired.  Update
gen-crc-consts.py to support generating the needed constants structs.

This makes it possible to easily wire up a Zbc optimized implementation
of almost any CRC.

The design generally follows what I did for x86, but it is simplified by
using RISC-V's scalar carryless multiplication Zbc, which has no
equivalent on x86.  RISC-V's clmulr instruction is also helpful.  A
potential switch to Zvbc (or support for Zvbc alongside Zbc) is left for
future work.  For long messages Zvbc should be fastest, but it would
need to be shown to be worthwhile over just using Zbc which is
significantly more convenient to use, especially in the kernel context.

Compared to the existing Zbc-optimized CRC32 code and the earlier
proposed Zbc-optimized CRC-T10DIF code
(https://lore.kernel.org/r/20250211071101.181652-1-zhihang.shao.iscas@gmail.com),
this submission deduplicates the code among CRC variants and is
significantly more optimized.  It uses "folding" to take better
advantage of instruction-level parallelism (to a more limited extent
than x86 for now, but it could be extended to more), it reworks the
Barrett reduction to eliminate unnecessary instructions, and it
documents all the math used and makes all the constants reproducible.

Tested-by: Björn Töpel <bjorn@rivosinc.com>
Acked-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20250216225530.306980-2-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
arch/riscv/lib/crc-clmul-template.h [new file with mode: 0644]
scripts/gen-crc-consts.py