Skip to content

Commit 72acff5

Browse files
committed
riscv/crc32: reimplement the CRC32 functions using new template
Delete the previous Zbc optimized CRC32 code, and re-implement it using the new template. The new implementation is more optimized and shares more code among CRC variants. Tested-by: Björn Töpel <[email protected]> Acked-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent bbe2610 commit 72acff5

File tree

7 files changed

+177
-310
lines changed

7 files changed

+177
-310
lines changed

arch/riscv/lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lib-$(CONFIG_MMU) += uaccess.o
1616
lib-$(CONFIG_64BIT) += tishift.o
1717
lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.o
1818
obj-$(CONFIG_CRC32_ARCH) += crc32-riscv.o
19+
crc32-riscv-y := crc32.o crc32_msb.o crc32_lsb.o
1920
obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
2021
lib-$(CONFIG_RISCV_ISA_V) += xor.o
2122
lib-$(CONFIG_RISCV_ISA_V) += riscv_v_helpers.o

arch/riscv/lib/crc-clmul-consts.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* CRC constants generated by:
4+
*
5+
* ./scripts/gen-crc-consts.py riscv_clmul crc32_msb_0x04c11db7,crc32_lsb_0xedb88320,crc32_lsb_0x82f63b78
6+
*
7+
* Do not edit manually.
8+
*/
9+
10+
struct crc_clmul_consts {
11+
unsigned long fold_across_2_longs_const_hi;
12+
unsigned long fold_across_2_longs_const_lo;
13+
unsigned long barrett_reduction_const_1;
14+
unsigned long barrett_reduction_const_2;
15+
};
16+
17+
/*
18+
* Constants generated for most-significant-bit-first CRC-32 using
19+
* G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 +
20+
* x^5 + x^4 + x^2 + x^1 + x^0
21+
*/
22+
static const struct crc_clmul_consts crc32_msb_0x04c11db7_consts __maybe_unused = {
23+
#ifdef CONFIG_64BIT
24+
.fold_across_2_longs_const_hi = 0x00000000c5b9cd4c, /* x^192 mod G */
25+
.fold_across_2_longs_const_lo = 0x00000000e8a45605, /* x^128 mod G */
26+
.barrett_reduction_const_1 = 0x826880efa40da72d, /* floor(x^95 / G) */
27+
.barrett_reduction_const_2 = 0x0000000004c11db7, /* G - x^32 */
28+
#else
29+
.fold_across_2_longs_const_hi = 0xf200aa66, /* x^96 mod G */
30+
.fold_across_2_longs_const_lo = 0x490d678d, /* x^64 mod G */
31+
.barrett_reduction_const_1 = 0x826880ef, /* floor(x^63 / G) */
32+
.barrett_reduction_const_2 = 0x04c11db7, /* G - x^32 */
33+
#endif
34+
};
35+
36+
/*
37+
* Constants generated for least-significant-bit-first CRC-32 using
38+
* G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 +
39+
* x^5 + x^4 + x^2 + x^1 + x^0
40+
*/
41+
static const struct crc_clmul_consts crc32_lsb_0xedb88320_consts __maybe_unused = {
42+
#ifdef CONFIG_64BIT
43+
.fold_across_2_longs_const_hi = 0x65673b4600000000, /* x^191 mod G */
44+
.fold_across_2_longs_const_lo = 0x9ba54c6f00000000, /* x^127 mod G */
45+
.barrett_reduction_const_1 = 0xb4e5b025f7011641, /* floor(x^95 / G) */
46+
.barrett_reduction_const_2 = 0x00000000edb88320, /* (G - x^32) * x^32 */
47+
#else
48+
.fold_across_2_longs_const_hi = 0xccaa009e, /* x^95 mod G */
49+
.fold_across_2_longs_const_lo = 0xb8bc6765, /* x^63 mod G */
50+
.barrett_reduction_const_1 = 0xf7011641, /* floor(x^63 / G) */
51+
.barrett_reduction_const_2 = 0xedb88320, /* (G - x^32) * x^0 */
52+
#endif
53+
};
54+
55+
/*
56+
* Constants generated for least-significant-bit-first CRC-32 using
57+
* G(x) = x^32 + x^28 + x^27 + x^26 + x^25 + x^23 + x^22 + x^20 + x^19 + x^18 +
58+
* x^14 + x^13 + x^11 + x^10 + x^9 + x^8 + x^6 + x^0
59+
*/
60+
static const struct crc_clmul_consts crc32_lsb_0x82f63b78_consts __maybe_unused = {
61+
#ifdef CONFIG_64BIT
62+
.fold_across_2_longs_const_hi = 0x3743f7bd00000000, /* x^191 mod G */
63+
.fold_across_2_longs_const_lo = 0x3171d43000000000, /* x^127 mod G */
64+
.barrett_reduction_const_1 = 0x4869ec38dea713f1, /* floor(x^95 / G) */
65+
.barrett_reduction_const_2 = 0x0000000082f63b78, /* (G - x^32) * x^32 */
66+
#else
67+
.fold_across_2_longs_const_hi = 0x493c7d27, /* x^95 mod G */
68+
.fold_across_2_longs_const_lo = 0xdd45aab8, /* x^63 mod G */
69+
.barrett_reduction_const_1 = 0xdea713f1, /* floor(x^63 / G) */
70+
.barrett_reduction_const_2 = 0x82f63b78, /* (G - x^32) * x^0 */
71+
#endif
72+
};

arch/riscv/lib/crc-clmul.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/* Copyright 2025 Google LLC */
3+
4+
#ifndef _RISCV_CRC_CLMUL_H
5+
#define _RISCV_CRC_CLMUL_H
6+
7+
#include <linux/types.h>
8+
#include "crc-clmul-consts.h"
9+
10+
u32 crc32_msb_clmul(u32 crc, const void *p, size_t len,
11+
const struct crc_clmul_consts *consts);
12+
u32 crc32_lsb_clmul(u32 crc, const void *p, size_t len,
13+
const struct crc_clmul_consts *consts);
14+
15+
#endif /* _RISCV_CRC_CLMUL_H */

0 commit comments

Comments
 (0)