-
-
Notifications
You must be signed in to change notification settings - Fork 46
crc32: add riscv64 implementation #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| //! crc32 implementation using the riscv64 zbkc ISA extension. Derived from | ||
| //! zlib-ng's implementation, see | ||
| //! https://github.com/zlib-ng/zlib-ng/blob/da22434b657578c41af1bdf06b27304e4aceb00f/arch/riscv/crc32_zbc.c | ||
| //! | ||
| //! # Safety | ||
| //! | ||
| //! The functions in this module must only be executed on a riscv64 system with | ||
| //! the zbkc feature. | ||
|
|
||
| use crate::crc32::zbkc::asm::{clmul, clmulh}; | ||
|
|
||
| use super::crc32_braid; | ||
|
|
||
| const CLMUL_MIN_LEN: usize = 16; | ||
| const CLMUL_CHUNK_LEN: usize = 16; | ||
|
|
||
| const CONSTANT_R3: u64 = 0x1751997D0; | ||
| const CONSTANT_R4: u64 = 0x0CCAA009E; | ||
| const CONSTANT_R5: u64 = 0x163CD6124; | ||
| const MASK32: u64 = 0xFFFFFFFF; | ||
| const CRCPOLY_TRUE_LE_FULL: u64 = 0x1DB710641; | ||
| const CONSTANT_RU: u64 = 0x1F7011641; | ||
|
|
||
| pub unsafe fn crc32_zbkc_riscv64(mut crc: u32, buf: &[u8]) -> u32 { | ||
| if buf.len() < CLMUL_MIN_LEN { | ||
| return crc32_braid(crc, buf); | ||
| } | ||
|
|
||
| let unaligned_len = buf.len() % CLMUL_CHUNK_LEN; | ||
| if unaligned_len > 0 { | ||
| crc = crc32_braid(crc, &buf[..unaligned_len]); | ||
| } | ||
|
|
||
| !unsafe { crc32_zbkc_riscv64_impl(!crc, &buf[unaligned_len..]) } | ||
| } | ||
|
|
||
| unsafe fn crc32_zbkc_riscv64_impl(crc: u32, buf: &[u8]) -> u32 { | ||
| // This unwrap is legal because crc32_zbkc_riscv64 guarantees the input is at | ||
| // least 16 bytes. | ||
| let mut low = u64::from_le_bytes(buf[..8].try_into().unwrap()) ^ crc as u64; | ||
| let mut high = u64::from_le_bytes(buf[8..16].try_into().unwrap()); | ||
|
|
||
| buf.chunks_exact(16).skip(1).for_each(|chunk| { | ||
| let t2 = unsafe { clmul(CONSTANT_R4, high) }; | ||
| let t3 = unsafe { clmulh(CONSTANT_R4, high) }; | ||
| let t0_new = unsafe { clmul(CONSTANT_R3, low) }; | ||
| let t1_new = unsafe { clmulh(CONSTANT_R3, low) }; | ||
| low = t0_new ^ t2; | ||
| high = t1_new ^ t3; | ||
| low ^= u64::from_le_bytes(chunk[..8].try_into().unwrap()); | ||
| high ^= u64::from_le_bytes(chunk[8..].try_into().unwrap()); | ||
| }); | ||
|
|
||
| // Fold the 128-bit result into 64 bits | ||
| let fold_t3 = unsafe { clmulh(low, CONSTANT_R4) }; | ||
| let fold_t2 = unsafe { clmul(low, CONSTANT_R4) }; | ||
| low = high ^ fold_t2; | ||
| high = fold_t3; | ||
|
|
||
| // Combine the low and high parts and perform polynomial reduction | ||
| let combined = (low >> 32) | ((high & MASK32) << 32); | ||
| let reduced_low = unsafe { clmul(low & MASK32, CONSTANT_R5) } ^ combined; | ||
|
|
||
| // Barrett reduction step | ||
| let mut barrett = unsafe { clmul(reduced_low & MASK32, CONSTANT_RU) & MASK32 }; | ||
| barrett = unsafe { clmul(barrett, CRCPOLY_TRUE_LE_FULL) }; | ||
| let ret = barrett ^ reduced_low; | ||
|
|
||
| (ret >> 32) as u32 | ||
| } | ||
|
|
||
| /// Inline assembly for required instructions, since the stdarch intrinsics are | ||
| /// currently unstable. | ||
| mod asm { | ||
| // Returns the lower half of carryless multiplication of rs1 and rs2. | ||
| // See https://riscv.github.io/riscv-isa-manual/snapshot/spec/#insns-clmul | ||
| #[target_feature(enable = "zbkc")] | ||
| pub unsafe fn clmul(rs1: u64, rs2: u64) -> u64 { | ||
| let rd; | ||
| unsafe { | ||
| core::arch::asm!( | ||
| "clmul {rd}, {rs1}, {rs2}", | ||
| rs1 = in(reg) rs1, | ||
| rs2 = in(reg) rs2, | ||
| rd = out(reg) rd, | ||
| options(pure, nomem, nostack) | ||
| ); | ||
| } | ||
| rd | ||
| } | ||
|
|
||
| // Returns the upper half of carryless multiplication of rs1 and rs2. | ||
| // See https://riscv.github.io/riscv-isa-manual/snapshot/spec/#insns-clmulh | ||
| #[target_feature(enable = "zbkc")] | ||
| pub unsafe fn clmulh(rs1: u64, rs2: u64) -> u64 { | ||
| let rd; | ||
| unsafe { | ||
| core::arch::asm!( | ||
| "clmulh {rd}, {rs1}, {rs2}", | ||
| rs1 = in(reg) rs1, | ||
| rs2 = in(reg) rs2, | ||
| rd = out(reg) rd, | ||
| options(pure, nomem, nostack) | ||
| ); | ||
| } | ||
| rd | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed for this PR, but I suspect an approach like in #514 could work here too? Though aarch64 has
64 x 64 -> 128bits in one instruction, while here it takes 2 so perhaps it's not actually advantageous.