Skip to content

Add RISC-V Zvbc vector CRC32C acceleration#3332

Open
Felix-Gong wants to merge 2 commits into
apache:masterfrom
Felix-Gong:riscv-crc32c-zvbc-impl
Open

Add RISC-V Zvbc vector CRC32C acceleration#3332
Felix-Gong wants to merge 2 commits into
apache:masterfrom
Felix-Gong:riscv-crc32c-zvbc-impl

Conversation

@Felix-Gong
Copy link
Copy Markdown
Contributor

@Felix-Gong Felix-Gong commented Jun 5, 2026

Summary

  • Implement CRC32C using RISC-V Zvbc vector carry-less multiplication (vclmul/vclmulh RVV intrinsics)
  • Processes 4 lanes of 128-bit folding per iteration (64 bytes), with 4-to-1 lane reduction and Barrett reduction
  • Add WITH_RISCV_ZVBC cmake option
  • Zvbc preferred over Zbc in Choose_Extend() for higher throughput

Background

This extends the existing Zbc scalar CRC32C optimization (merged in #3312) with vector support. The implementation follows the same 128-bit folding + Barrett reduction approach used in x86 SSE4.2 and ARM PMULL.

Key implementation details

  • With VLEN=128, each vector register holds 2×64-bit elements, so 2 vector register pairs = 128-bit lane
  • Fold constants: k1=0x740eef02 (x^256 mod P), k2=0x9e4addf8 (x^320 mod P), k3=0xf20c0dfe (x^128 mod P), k4=0x493c7d27 (x^192 mod P)
  • Cross-element XOR via scalar extraction (practical for VLEN=128 with only 2 elements)
  • Barrett reduction uses scalar clmul/clmulh (Zbc instructions, available when Zvbc is present)

Testing

Verified on QEMU with -cpu rv64,zvbc=true,v=true,vlen=128:

  • All RFC 3720 B.4 test vectors pass
  • Large data (64/128/256/1024 bytes) results match x86 SSE4.2 reference values
  • Extend (chained CRC) works correctly
  • Zvbc and Zbc paths produce identical results

Build

cmake -DWITH_RISCV_ZVBC=ON ..

Implement CRC32C using Zvbc vector carry-less multiplication
(vclmul/vclmulh RVV intrinsics). Processes 4 lanes of 128-bit
folding per iteration (64 bytes), with 4-to-1 lane reduction
and Barrett reduction for finalization.

- Add rv_crc32c_vclmul() using vclmul/vclmulh intrinsics
- Add isZvbc() runtime detection via /proc/cpuinfo
- Add WITH_RISCV_ZVBC cmake option
- Fix macro guards: support __riscv_zvbc without __riscv_zbc
- Zvbc preferred over Zbc in Choose_Extend()
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new RISC-V CRC32C fast path using the Zvbc vector carry-less multiply instructions (RVV vclmul/vclmulh), along with a CMake option to enable compiling that path and runtime selection logic that prefers Zvbc over the existing Zbc scalar optimization.

Changes:

  • Add rv_crc32c_vclmul (RVV-based CRC32C folding + Barrett reduction) and isZvbc() runtime detection.
  • Update runtime dispatch (Choose_Extend / IsFastCrc32Supported) to prefer Zvbc when available, otherwise fall back to Zbc scalar.
  • Add WITH_RISCV_ZVBC CMake option and set -march accordingly.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/butil/crc32c.cc Adds the Zvbc vector CRC32C implementation and runtime selection logic.
CMakeLists.txt Adds build option/flags to enable compiling the Zvbc path on riscv64.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/butil/crc32c.cc
Comment on lines +641 to +642
// Set up RVV for 64-bit elements: vl = min(VLEN/64, 2) = 2 for VLEN=128
size_t vl = __riscv_vsetvl_e64m1(2);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Added a vl < 2 check after __riscv_vsetvl_e64m1(2) that falls back to the bitwise path if VLEN < 128.

Comment thread CMakeLists.txt
Comment on lines +177 to +179
if(WITH_RISCV_ZVBC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gc_zbc_zvbc")
elseif(WITH_RISCV_ZBC)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Changed -march=rv64gc_zbc_zvbc to -march=rv64gcv_zbc_zvbc to include the base RVV extension.

Comment thread src/butil/crc32c.cc Outdated
Comment on lines +654 to +657
vuint64m1_t lane1 = __riscv_vle64_v_u64m1((const uint64_t*)(p + 0), vl);
vuint64m1_t lane2 = __riscv_vle64_v_u64m1((const uint64_t*)(p + 16), vl);
vuint64m1_t lane3 = __riscv_vle64_v_u64m1((const uint64_t*)(p + 32), vl);
vuint64m1_t lane4 = __riscv_vle64_v_u64m1((const uint64_t*)(p + 48), vl);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. All vector loads now go through memcpy into a uint64_t[2] staging buffer before vle64, avoiding the uint8_t*-to-uint64_t* cast.

Comment thread src/butil/crc32c.cc
Comment on lines +783 to +787
if (strstr(line, "isa") || strstr(line, "hart isa")) {
char* colon = strchr(line, ':');
if (colon) {
if (strstr(colon, "_zvbc") || strstr(colon, "zvbc")) {
supported = true;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Both isZbc() and isZvbc() now only match _zbc and _zvbc (with underscore prefix), removing the bare zbc/zvbc substring checks that could cause false positives.

…flag

- Add vl<2 fallback for VLEN<128 (prevents UB from uninitialized elements)
- Use memcpy instead of uint8_t*-to-uint64_t* casts (strict-aliasing safe)
- Tighten /proc/cpuinfo ISA matching to _zbc/_zvbc only (prevents _zvbc
  falsely matching zbc)
- Add missing 'v' base extension in -march flag (rv64gcv_zbc_zvbc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants