From 9ea14019f65ba3b78927177833651cab25b55399 Mon Sep 17 00:00:00 2001 From: TechnoPorg Date: Tue, 20 Oct 2026 09:22:35 -0400 Subject: [PATCH 1/3] refactor: move RISC-V bit manipulation intrinsics into zb.rs --- crates/core_arch/src/riscv32/mod.rs | 4 + crates/core_arch/src/riscv32/zb.rs | 137 +++++++++++++++++++++++ crates/core_arch/src/riscv32/zk.rs | 47 -------- crates/core_arch/src/riscv64/mod.rs | 4 + crates/core_arch/src/riscv64/zb.rs | 132 ++++++++++++++++++++++ crates/core_arch/src/riscv_shared/mod.rs | 2 + crates/core_arch/src/riscv_shared/zb.rs | 91 +-------------- crates/core_arch/src/riscv_shared/zk.rs | 74 ------------ 8 files changed, 281 insertions(+), 210 deletions(-) create mode 100644 crates/core_arch/src/riscv32/zb.rs create mode 100644 crates/core_arch/src/riscv64/zb.rs diff --git a/crates/core_arch/src/riscv32/mod.rs b/crates/core_arch/src/riscv32/mod.rs index 7ff871227b..7017b7edc4 100644 --- a/crates/core_arch/src/riscv32/mod.rs +++ b/crates/core_arch/src/riscv32/mod.rs @@ -1,6 +1,10 @@ //! RISC-V RV32 specific intrinsics +mod zb; mod zk; +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +pub use zb::*; + #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use zk::*; diff --git a/crates/core_arch/src/riscv32/zb.rs b/crates/core_arch/src/riscv32/zb.rs new file mode 100644 index 0000000000..c50268a462 --- /dev/null +++ b/crates/core_arch/src/riscv32/zb.rs @@ -0,0 +1,137 @@ +#[cfg(test)] +use stdarch_test::assert_instr; + +unsafe extern "llvm-intrinsic" { + #[link_name = "llvm.riscv.xperm8.i32"] + fn _xperm8_32(rs1: i32, rs2: i32) -> i32; + + #[link_name = "llvm.riscv.xperm4.i32"] + fn _xperm4_32(rs1: i32, rs2: i32) -> i32; + + #[link_name = "llvm.riscv.clmulh.i32"] + fn _clmulh_32(rs1: i32, rs2: i32) -> i32; + + #[link_name = "llvm.riscv.clmulr.i32"] + fn _clmulr_32(rs1: i32, rs2: i32) -> i32; + + #[link_name = "llvm.riscv.zip.i32"] + fn _zip(rs1: i32) -> i32; + + #[link_name = "llvm.riscv.unzip.i32"] + fn _unzip(rs1: i32) -> i32; +} + +/// Byte-wise lookup of indicies into a vector in registers. +/// +/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of XLEN/8 +/// 8-bit elements. The rs2 register contains a vector of XLEN/8 8-bit indexes. The result is +/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 +/// is out of bounds. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.47 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkx")] +#[cfg_attr(test, assert_instr(xperm8))] +#[inline] +pub fn xperm8(rs1: usize, rs2: usize) -> usize { + unsafe { _xperm8_32(rs1 as i32, rs2 as i32) as usize } +} + +/// Nibble-wise lookup of indicies into a vector. +/// +/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of XLEN/4 +/// 4-bit elements. The rs2 register contains a vector of XLEN/4 4-bit indexes. The result is +/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 +/// is out of bounds. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.48 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkx")] +#[cfg_attr(test, assert_instr(xperm4))] +#[inline] +pub fn xperm4(rs1: usize, rs2: usize) -> usize { + unsafe { _xperm4_32(rs1 as i32, rs2 as i32) as usize } +} + +/// Carry-less multiply (high-part) +/// +/// clmulh produces the upper half of the 2·XLEN carry-less product. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.12 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkc")] +#[cfg_attr(test, assert_instr(clmulh))] +#[inline] +pub fn clmulh(rs1: usize, rs2: usize) -> usize { + unsafe { _clmulh_32(rs1 as i32, rs2 as i32) as usize } +} + +/// Carry-less multiply (reversed) +/// +/// clmulr produces bits 2·XLEN−2:XLEN-1 of the 2·XLEN carry-less product. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.13 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbc")] +#[cfg_attr(test, assert_instr(clmulr))] +#[inline] +pub fn clmulr(rs1: usize, rs2: usize) -> usize { + unsafe { _clmulr_32(rs1 as i32, rs2 as i32) as usize } +} + +/// Place upper/lower halves of the source register into odd/even bits of the destination +/// respectivley. +/// +/// This instruction places bits in the low half of the source register into the even bit +/// positions of the destination, and bits in the high half of the source register into the odd +/// bit positions of the destination. It is the inverse of the unzip instruction. This +/// instruction is available only on RV32. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.49 +#[target_feature(enable = "zbkb")] +// See #1464 +// #[cfg_attr(test, assert_instr(zip))] +#[inline] +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +pub fn zip(rs: u32) -> u32 { + unsafe { _zip(rs as i32) as u32 } +} + +/// Place odd and even bits of the source word into upper/lower halves of the destination. +/// +/// This instruction places the even bits of the source register into the low half of the +/// destination, and the odd bits of the source into the high bits of the destination. It is +/// the inverse of the zip instruction. This instruction is available only on RV32. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.45 +#[target_feature(enable = "zbkb")] +#[cfg_attr(test, assert_instr(unzip))] +#[inline] +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +pub fn unzip(rs: u32) -> u32 { + unsafe { _unzip(rs as i32) as u32 } +} diff --git a/crates/core_arch/src/riscv32/zk.rs b/crates/core_arch/src/riscv32/zk.rs index 3ffe0819e7..1613245182 100644 --- a/crates/core_arch/src/riscv32/zk.rs +++ b/crates/core_arch/src/riscv32/zk.rs @@ -14,12 +14,6 @@ unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.aes32dsmi"] fn _aes32dsmi(rs1: i32, rs2: i32, bs: i32) -> i32; - #[link_name = "llvm.riscv.zip.i32"] - fn _zip(rs1: i32) -> i32; - - #[link_name = "llvm.riscv.unzip.i32"] - fn _unzip(rs1: i32) -> i32; - #[link_name = "llvm.riscv.sha512sig0h"] fn _sha512sig0h(rs1: i32, rs2: i32) -> i32; @@ -154,47 +148,6 @@ pub fn aes32dsmi(rs1: u32, rs2: u32) -> u32 { unsafe { _aes32dsmi(rs1 as i32, rs2 as i32, BS as i32) as u32 } } -/// Place upper/lower halves of the source register into odd/even bits of the destination -/// respectivley. -/// -/// This instruction places bits in the low half of the source register into the even bit -/// positions of the destination, and bits in the high half of the source register into the odd -/// bit positions of the destination. It is the inverse of the unzip instruction. This -/// instruction is available only on RV32. -/// -/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions -/// -/// Version: v1.0.1 -/// -/// Section: 3.49 -#[target_feature(enable = "zbkb")] -// See #1464 -// #[cfg_attr(test, assert_instr(zip))] -#[inline] -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -pub fn zip(rs: u32) -> u32 { - unsafe { _zip(rs as i32) as u32 } -} - -/// Place odd and even bits of the source word into upper/lower halves of the destination. -/// -/// This instruction places the even bits of the source register into the low half of the -/// destination, and the odd bits of the source into the high bits of the destination. It is -/// the inverse of the zip instruction. This instruction is available only on RV32. -/// -/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions -/// -/// Version: v1.0.1 -/// -/// Section: 3.45 -#[target_feature(enable = "zbkb")] -#[cfg_attr(test, assert_instr(unzip))] -#[inline] -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -pub fn unzip(rs: u32) -> u32 { - unsafe { _unzip(rs as i32) as u32 } -} - /// Implements the high half of the Sigma0 transformation, as used in the SHA2-512 hash /// function \[49\] (Section 4.1.3). /// diff --git a/crates/core_arch/src/riscv64/mod.rs b/crates/core_arch/src/riscv64/mod.rs index a7efc0c7f5..8301ea2b23 100644 --- a/crates/core_arch/src/riscv64/mod.rs +++ b/crates/core_arch/src/riscv64/mod.rs @@ -1,8 +1,12 @@ //! RISC-V RV64 specific intrinsics use crate::arch::asm; +mod zb; mod zk; +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +pub use zb::*; + #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use zk::*; diff --git a/crates/core_arch/src/riscv64/zb.rs b/crates/core_arch/src/riscv64/zb.rs new file mode 100644 index 0000000000..a26ddaa5be --- /dev/null +++ b/crates/core_arch/src/riscv64/zb.rs @@ -0,0 +1,132 @@ +#[cfg(test)] +use stdarch_test::assert_instr; + +unsafe extern "llvm-intrinsic" { + #[link_name = "llvm.riscv.xperm8.i64"] + fn _xperm8_64(rs1: i64, rs2: i64) -> i64; + + #[link_name = "llvm.riscv.xperm4.i64"] + fn _xperm4_64(rs1: i64, rs2: i64) -> i64; + + #[link_name = "llvm.riscv.orc.b.i64"] + fn _orc_b_64(rs1: i64) -> i64; + + #[link_name = "llvm.riscv.clmul.i64"] + fn _clmul_64(rs1: i64, rs2: i64) -> i64; + + #[link_name = "llvm.riscv.clmulh.i64"] + fn _clmulh_64(rs1: i64, rs2: i64) -> i64; + + #[link_name = "llvm.riscv.clmulr.i64"] + fn _clmulr_64(rs1: i64, rs2: i64) -> i64; +} + +/// Byte-wise lookup of indicies into a vector in registers. +/// +/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of XLEN/8 +/// 8-bit elements. The rs2 register contains a vector of XLEN/8 8-bit indexes. The result is +/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 +/// is out of bounds. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.47 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkx")] +#[cfg_attr(test, assert_instr(xperm8))] +#[inline] +pub fn xperm8(rs1: usize, rs2: usize) -> usize { + unsafe { _xperm8_64(rs1 as i64, rs2 as i64) as usize } +} + +/// Nibble-wise lookup of indicies into a vector. +/// +/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of XLEN/4 +/// 4-bit elements. The rs2 register contains a vector of XLEN/4 4-bit indexes. The result is +/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 +/// is out of bounds. +/// +/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions +/// +/// Version: v1.0.1 +/// +/// Section: 3.48 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkx")] +#[cfg_attr(test, assert_instr(xperm4))] +#[inline] +pub fn xperm4(rs1: usize, rs2: usize) -> usize { + unsafe { _xperm4_64(rs1 as i64, rs2 as i64) as usize } +} + +/// Bitwise OR-Combine, byte granule +/// +/// Combines the bits within every byte through a reciprocal bitwise logical OR. This sets the bits of each byte in +/// the result rd to all zeros if no bit within the respective byte of rs is set, or to all ones if any bit within the +/// respective byte of rs is set. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.24 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbb")] +#[cfg_attr(test, assert_instr(orc.b))] +#[inline] +pub fn orc_b(rs: usize) -> usize { + unsafe { _orc_b_64(rs as i64) as usize } +} + +/// Carry-less multiply (low-part) +/// +/// clmul produces the lower half of the 2·XLEN carry-less product. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.11 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkc")] +#[cfg_attr(test, assert_instr(clmul))] +#[inline] +pub fn clmul(rs1: usize, rs2: usize) -> usize { + unsafe { _clmul_64(rs1 as i64, rs2 as i64) as usize } +} + +/// Carry-less multiply (high-part) +/// +/// clmulh produces the upper half of the 2·XLEN carry-less product. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.12 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbkc")] +#[cfg_attr(test, assert_instr(clmulh))] +#[inline] +pub fn clmulh(rs1: usize, rs2: usize) -> usize { + unsafe { _clmulh_64(rs1 as i64, rs2 as i64) as usize } +} + +/// Carry-less multiply (reversed) +/// +/// clmulr produces bits 2·XLEN−2:XLEN-1 of the 2·XLEN carry-less product. +/// +/// Source: RISC-V Bit-Manipulation ISA-extensions +/// +/// Version: v1.0.0 +/// +/// Section: 2.13 +#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] +#[target_feature(enable = "zbc")] +#[cfg_attr(test, assert_instr(clmulr))] +#[inline] +pub fn clmulr(rs1: usize, rs2: usize) -> usize { + unsafe { _clmulr_64(rs1 as i64, rs2 as i64) as usize } +} diff --git a/crates/core_arch/src/riscv_shared/mod.rs b/crates/core_arch/src/riscv_shared/mod.rs index f3933f58b1..b99b963edd 100644 --- a/crates/core_arch/src/riscv_shared/mod.rs +++ b/crates/core_arch/src/riscv_shared/mod.rs @@ -25,11 +25,13 @@ //! single inline assembly block and make sure to restore the original rounding mode before the end //! of the block. mod p; +#[cfg(target_arch = "riscv32")] mod zb; mod zk; #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use p::*; +#[cfg(target_arch = "riscv32")] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use zb::*; #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] diff --git a/crates/core_arch/src/riscv_shared/zb.rs b/crates/core_arch/src/riscv_shared/zb.rs index 79ffe2af33..2f53005898 100644 --- a/crates/core_arch/src/riscv_shared/zb.rs +++ b/crates/core_arch/src/riscv_shared/zb.rs @@ -8,27 +8,6 @@ unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.clmul.i32"] fn _clmul_32(rs1: i32, rs2: i32) -> i32; - - #[link_name = "llvm.riscv.clmulh.i32"] - fn _clmulh_32(rs1: i32, rs2: i32) -> i32; - - #[link_name = "llvm.riscv.clmulr.i32"] - fn _clmulr_32(rs1: i32, rs2: i32) -> i32; -} - -#[cfg(target_arch = "riscv64")] -unsafe extern "llvm-intrinsic" { - #[link_name = "llvm.riscv.orc.b.i64"] - fn _orc_b_64(rs1: i64) -> i64; - - #[link_name = "llvm.riscv.clmul.i64"] - fn _clmul_64(rs1: i64, rs2: i64) -> i64; - - #[link_name = "llvm.riscv.clmulh.i64"] - fn _clmulh_64(rs1: i64, rs2: i64) -> i64; - - #[link_name = "llvm.riscv.clmulr.i64"] - fn _clmulr_64(rs1: i64, rs2: i64) -> i64; } /// Bitwise OR-Combine, byte granule @@ -47,15 +26,7 @@ unsafe extern "llvm-intrinsic" { #[cfg_attr(test, assert_instr(orc.b))] #[inline] pub fn orc_b(rs: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _orc_b_32(rs as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _orc_b_64(rs as i64) as usize - } + unsafe { _orc_b_32(rs as i32) as usize } } /// Carry-less multiply (low-part) @@ -72,63 +43,5 @@ pub fn orc_b(rs: usize) -> usize { #[cfg_attr(test, assert_instr(clmul))] #[inline] pub fn clmul(rs1: usize, rs2: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _clmul_32(rs1 as i32, rs2 as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _clmul_64(rs1 as i64, rs2 as i64) as usize - } -} - -/// Carry-less multiply (high-part) -/// -/// clmulh produces the upper half of the 2·XLEN carry-less product. -/// -/// Source: RISC-V Bit-Manipulation ISA-extensions -/// -/// Version: v1.0.0 -/// -/// Section: 2.12 -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -#[target_feature(enable = "zbkc")] -#[cfg_attr(test, assert_instr(clmulh))] -#[inline] -pub fn clmulh(rs1: usize, rs2: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _clmulh_32(rs1 as i32, rs2 as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _clmulh_64(rs1 as i64, rs2 as i64) as usize - } -} - -/// Carry-less multiply (reversed) -/// -/// clmulr produces bits 2·XLEN−2:XLEN-1 of the 2·XLEN carry-less product. -/// -/// Source: RISC-V Bit-Manipulation ISA-extensions -/// -/// Version: v1.0.0 -/// -/// Section: 2.13 -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -#[target_feature(enable = "zbc")] -#[cfg_attr(test, assert_instr(clmulr))] -#[inline] -pub fn clmulr(rs1: usize, rs2: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _clmulr_32(rs1 as i32, rs2 as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _clmulr_64(rs1 as i64, rs2 as i64) as usize - } + unsafe { _clmul_32(rs1 as i32, rs2 as i32) as usize } } diff --git a/crates/core_arch/src/riscv_shared/zk.rs b/crates/core_arch/src/riscv_shared/zk.rs index 589ea97798..0fcf13c7ac 100644 --- a/crates/core_arch/src/riscv_shared/zk.rs +++ b/crates/core_arch/src/riscv_shared/zk.rs @@ -27,80 +27,6 @@ unsafe extern "llvm-intrinsic" { fn _sha256sum1(rs1: i32) -> i32; } -#[cfg(target_arch = "riscv32")] -unsafe extern "llvm-intrinsic" { - #[link_name = "llvm.riscv.xperm8.i32"] - fn _xperm8_32(rs1: i32, rs2: i32) -> i32; - - #[link_name = "llvm.riscv.xperm4.i32"] - fn _xperm4_32(rs1: i32, rs2: i32) -> i32; -} - -#[cfg(target_arch = "riscv64")] -unsafe extern "llvm-intrinsic" { - #[link_name = "llvm.riscv.xperm8.i64"] - fn _xperm8_64(rs1: i64, rs2: i64) -> i64; - - #[link_name = "llvm.riscv.xperm4.i64"] - fn _xperm4_64(rs1: i64, rs2: i64) -> i64; -} - -/// Byte-wise lookup of indicies into a vector in registers. -/// -/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of XLEN/8 -/// 8-bit elements. The rs2 register contains a vector of XLEN/8 8-bit indexes. The result is -/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 -/// is out of bounds. -/// -/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions -/// -/// Version: v1.0.1 -/// -/// Section: 3.47 -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -#[target_feature(enable = "zbkx")] -#[cfg_attr(test, assert_instr(xperm8))] -#[inline] -pub fn xperm8(rs1: usize, rs2: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _xperm8_32(rs1 as i32, rs2 as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _xperm8_64(rs1 as i64, rs2 as i64) as usize - } -} - -/// Nibble-wise lookup of indicies into a vector. -/// -/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of XLEN/4 -/// 4-bit elements. The rs2 register contains a vector of XLEN/4 4-bit indexes. The result is -/// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 -/// is out of bounds. -/// -/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions -/// -/// Version: v1.0.1 -/// -/// Section: 3.48 -#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -#[target_feature(enable = "zbkx")] -#[cfg_attr(test, assert_instr(xperm4))] -#[inline] -pub fn xperm4(rs1: usize, rs2: usize) -> usize { - #[cfg(target_arch = "riscv32")] - unsafe { - _xperm4_32(rs1 as i32, rs2 as i32) as usize - } - - #[cfg(target_arch = "riscv64")] - unsafe { - _xperm4_64(rs1 as i64, rs2 as i64) as usize - } -} - /// Implements the Sigma0 transformation function as used in the SHA2-256 hash function \[49\] /// (Section 4.1.2). /// From c474d3dc872b867c735c12b6cceb277678aa9839 Mon Sep 17 00:00:00 2001 From: TechnoPorg Date: Tue, 20 Oct 2026 09:23:51 -0400 Subject: [PATCH 2/3] refactor: make RISC-V bit manip intrinsics match C API Also exposes the 32-bit intrinsics that are supported on RV64 in riscv_shared --- crates/core_arch/src/riscv32/zb.rs | 44 ++++++++++++------------ crates/core_arch/src/riscv64/zb.rs | 44 ++++++++++++------------ crates/core_arch/src/riscv_shared/mod.rs | 2 -- crates/core_arch/src/riscv_shared/zb.rs | 11 +++--- 4 files changed, 49 insertions(+), 52 deletions(-) diff --git a/crates/core_arch/src/riscv32/zb.rs b/crates/core_arch/src/riscv32/zb.rs index c50268a462..7c5aef260b 100644 --- a/crates/core_arch/src/riscv32/zb.rs +++ b/crates/core_arch/src/riscv32/zb.rs @@ -15,16 +15,16 @@ unsafe extern "llvm-intrinsic" { fn _clmulr_32(rs1: i32, rs2: i32) -> i32; #[link_name = "llvm.riscv.zip.i32"] - fn _zip(rs1: i32) -> i32; + fn _zip_32(rs1: i32) -> i32; #[link_name = "llvm.riscv.unzip.i32"] - fn _unzip(rs1: i32) -> i32; + fn _unzip_32(rs1: i32) -> i32; } -/// Byte-wise lookup of indicies into a vector in registers. +/// Byte-wise lookup of indices into a vector in registers. /// -/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of XLEN/8 -/// 8-bit elements. The rs2 register contains a vector of XLEN/8 8-bit indexes. The result is +/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of 4 +/// 8-bit elements. The rs2 register contains a vector of 4 8-bit indexes. The result is /// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 /// is out of bounds. /// @@ -37,14 +37,14 @@ unsafe extern "llvm-intrinsic" { #[target_feature(enable = "zbkx")] #[cfg_attr(test, assert_instr(xperm8))] #[inline] -pub fn xperm8(rs1: usize, rs2: usize) -> usize { - unsafe { _xperm8_32(rs1 as i32, rs2 as i32) as usize } +pub fn xperm8_32(rs1: u32, rs2: u32) -> u32 { + unsafe { _xperm8_32(rs1 as i32, rs2 as i32) as u32 } } -/// Nibble-wise lookup of indicies into a vector. +/// Nibble-wise lookup of indices into a vector. /// -/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of XLEN/4 -/// 4-bit elements. The rs2 register contains a vector of XLEN/4 4-bit indexes. The result is +/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of 8 +/// 4-bit elements. The rs2 register contains a vector of 8 4-bit indexes. The result is /// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 /// is out of bounds. /// @@ -57,13 +57,13 @@ pub fn xperm8(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbkx")] #[cfg_attr(test, assert_instr(xperm4))] #[inline] -pub fn xperm4(rs1: usize, rs2: usize) -> usize { - unsafe { _xperm4_32(rs1 as i32, rs2 as i32) as usize } +pub fn xperm4_32(rs1: u32, rs2: u32) -> u32 { + unsafe { _xperm4_32(rs1 as i32, rs2 as i32) as u32 } } /// Carry-less multiply (high-part) /// -/// clmulh produces the upper half of the 2·XLEN carry-less product. +/// clmulh produces the upper half of the 64-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -74,13 +74,13 @@ pub fn xperm4(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbkc")] #[cfg_attr(test, assert_instr(clmulh))] #[inline] -pub fn clmulh(rs1: usize, rs2: usize) -> usize { - unsafe { _clmulh_32(rs1 as i32, rs2 as i32) as usize } +pub fn clmulh_32(rs1: u32, rs2: u32) -> u32 { + unsafe { _clmulh_32(rs1 as i32, rs2 as i32) as u32 } } /// Carry-less multiply (reversed) /// -/// clmulr produces bits 2·XLEN−2:XLEN-1 of the 2·XLEN carry-less product. +/// clmulr produces bits 62:31 of the 64-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -91,8 +91,8 @@ pub fn clmulh(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbc")] #[cfg_attr(test, assert_instr(clmulr))] #[inline] -pub fn clmulr(rs1: usize, rs2: usize) -> usize { - unsafe { _clmulr_32(rs1 as i32, rs2 as i32) as usize } +pub fn clmulr_32(rs1: u32, rs2: u32) -> u32 { + unsafe { _clmulr_32(rs1 as i32, rs2 as i32) as u32 } } /// Place upper/lower halves of the source register into odd/even bits of the destination @@ -113,8 +113,8 @@ pub fn clmulr(rs1: usize, rs2: usize) -> usize { // #[cfg_attr(test, assert_instr(zip))] #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -pub fn zip(rs: u32) -> u32 { - unsafe { _zip(rs as i32) as u32 } +pub fn zip_32(rs: u32) -> u32 { + unsafe { _zip_32(rs as i32) as u32 } } /// Place odd and even bits of the source word into upper/lower halves of the destination. @@ -132,6 +132,6 @@ pub fn zip(rs: u32) -> u32 { #[cfg_attr(test, assert_instr(unzip))] #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] -pub fn unzip(rs: u32) -> u32 { - unsafe { _unzip(rs as i32) as u32 } +pub fn unzip_32(rs: u32) -> u32 { + unsafe { _unzip_32(rs as i32) as u32 } } diff --git a/crates/core_arch/src/riscv64/zb.rs b/crates/core_arch/src/riscv64/zb.rs index a26ddaa5be..014c5b8c2b 100644 --- a/crates/core_arch/src/riscv64/zb.rs +++ b/crates/core_arch/src/riscv64/zb.rs @@ -9,7 +9,7 @@ unsafe extern "llvm-intrinsic" { fn _xperm4_64(rs1: i64, rs2: i64) -> i64; #[link_name = "llvm.riscv.orc.b.i64"] - fn _orc_b_64(rs1: i64) -> i64; + fn _orc_b_64(rs: i64) -> i64; #[link_name = "llvm.riscv.clmul.i64"] fn _clmul_64(rs1: i64, rs2: i64) -> i64; @@ -21,10 +21,10 @@ unsafe extern "llvm-intrinsic" { fn _clmulr_64(rs1: i64, rs2: i64) -> i64; } -/// Byte-wise lookup of indicies into a vector in registers. +/// Byte-wise lookup of indices into a vector in registers. /// -/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of XLEN/8 -/// 8-bit elements. The rs2 register contains a vector of XLEN/8 8-bit indexes. The result is +/// The xperm8 instruction operates on bytes. The rs1 register contains a vector of 8 +/// 8-bit elements. The rs2 register contains a vector of 8 8-bit indexes. The result is /// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 /// is out of bounds. /// @@ -37,14 +37,14 @@ unsafe extern "llvm-intrinsic" { #[target_feature(enable = "zbkx")] #[cfg_attr(test, assert_instr(xperm8))] #[inline] -pub fn xperm8(rs1: usize, rs2: usize) -> usize { - unsafe { _xperm8_64(rs1 as i64, rs2 as i64) as usize } +pub fn xperm8_64(rs1: u64, rs2: u64) -> u64 { + unsafe { _xperm8_64(rs1 as i64, rs2 as i64) as u64 } } -/// Nibble-wise lookup of indicies into a vector. +/// Nibble-wise lookup of indices into a vector. /// -/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of XLEN/4 -/// 4-bit elements. The rs2 register contains a vector of XLEN/4 4-bit indexes. The result is +/// The xperm4 instruction operates on nibbles. The rs1 register contains a vector of 16 +/// 4-bit elements. The rs2 register contains a vector of 16 4-bit indexes. The result is /// each element in rs2 replaced by the indexed element in rs1, or zero if the index into rs2 /// is out of bounds. /// @@ -57,8 +57,8 @@ pub fn xperm8(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbkx")] #[cfg_attr(test, assert_instr(xperm4))] #[inline] -pub fn xperm4(rs1: usize, rs2: usize) -> usize { - unsafe { _xperm4_64(rs1 as i64, rs2 as i64) as usize } +pub fn xperm4_64(rs1: u64, rs2: u64) -> u64 { + unsafe { _xperm4_64(rs1 as i64, rs2 as i64) as u64 } } /// Bitwise OR-Combine, byte granule @@ -76,13 +76,13 @@ pub fn xperm4(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbb")] #[cfg_attr(test, assert_instr(orc.b))] #[inline] -pub fn orc_b(rs: usize) -> usize { - unsafe { _orc_b_64(rs as i64) as usize } +pub fn orc_b_64(rs: u64) -> u64 { + unsafe { _orc_b_64(rs as i64) as u64 } } /// Carry-less multiply (low-part) /// -/// clmul produces the lower half of the 2·XLEN carry-less product. +/// clmul produces the lower half of the 128-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -93,13 +93,13 @@ pub fn orc_b(rs: usize) -> usize { #[target_feature(enable = "zbkc")] #[cfg_attr(test, assert_instr(clmul))] #[inline] -pub fn clmul(rs1: usize, rs2: usize) -> usize { - unsafe { _clmul_64(rs1 as i64, rs2 as i64) as usize } +pub fn clmul_64(rs1: u64, rs2: u64) -> u64 { + unsafe { _clmul_64(rs1 as i64, rs2 as i64) as u64 } } /// Carry-less multiply (high-part) /// -/// clmulh produces the upper half of the 2·XLEN carry-less product. +/// clmulh produces the upper half of the 128-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -110,13 +110,13 @@ pub fn clmul(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbkc")] #[cfg_attr(test, assert_instr(clmulh))] #[inline] -pub fn clmulh(rs1: usize, rs2: usize) -> usize { - unsafe { _clmulh_64(rs1 as i64, rs2 as i64) as usize } +pub fn clmulh_64(rs1: u64, rs2: u64) -> u64 { + unsafe { _clmulh_64(rs1 as i64, rs2 as i64) as u64 } } /// Carry-less multiply (reversed) /// -/// clmulr produces bits 2·XLEN−2:XLEN-1 of the 2·XLEN carry-less product. +/// clmulr produces bits 126:63 of the 128-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -127,6 +127,6 @@ pub fn clmulh(rs1: usize, rs2: usize) -> usize { #[target_feature(enable = "zbc")] #[cfg_attr(test, assert_instr(clmulr))] #[inline] -pub fn clmulr(rs1: usize, rs2: usize) -> usize { - unsafe { _clmulr_64(rs1 as i64, rs2 as i64) as usize } +pub fn clmulr_64(rs1: u64, rs2: u64) -> u64 { + unsafe { _clmulr_64(rs1 as i64, rs2 as i64) as u64 } } diff --git a/crates/core_arch/src/riscv_shared/mod.rs b/crates/core_arch/src/riscv_shared/mod.rs index b99b963edd..f3933f58b1 100644 --- a/crates/core_arch/src/riscv_shared/mod.rs +++ b/crates/core_arch/src/riscv_shared/mod.rs @@ -25,13 +25,11 @@ //! single inline assembly block and make sure to restore the original rounding mode before the end //! of the block. mod p; -#[cfg(target_arch = "riscv32")] mod zb; mod zk; #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use p::*; -#[cfg(target_arch = "riscv32")] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub use zb::*; #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] diff --git a/crates/core_arch/src/riscv_shared/zb.rs b/crates/core_arch/src/riscv_shared/zb.rs index 2f53005898..83d8bc9367 100644 --- a/crates/core_arch/src/riscv_shared/zb.rs +++ b/crates/core_arch/src/riscv_shared/zb.rs @@ -1,7 +1,6 @@ #[cfg(test)] use stdarch_test::assert_instr; -#[cfg(target_arch = "riscv32")] unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.orc.b.i32"] fn _orc_b_32(rs: i32) -> i32; @@ -25,13 +24,13 @@ unsafe extern "llvm-intrinsic" { #[target_feature(enable = "zbb")] #[cfg_attr(test, assert_instr(orc.b))] #[inline] -pub fn orc_b(rs: usize) -> usize { - unsafe { _orc_b_32(rs as i32) as usize } +pub fn orc_b_32(rs: u32) -> u32 { + unsafe { _orc_b_32(rs as i32) as u32 } } /// Carry-less multiply (low-part) /// -/// clmul produces the lower half of the 2·XLEN carry-less product. +/// clmul produces the lower half of the 32-bit carry-less product. /// /// Source: RISC-V Bit-Manipulation ISA-extensions /// @@ -42,6 +41,6 @@ pub fn orc_b(rs: usize) -> usize { #[target_feature(enable = "zbkc")] #[cfg_attr(test, assert_instr(clmul))] #[inline] -pub fn clmul(rs1: usize, rs2: usize) -> usize { - unsafe { _clmul_32(rs1 as i32, rs2 as i32) as usize } +pub fn clmul_32(rs1: u32, rs2: u32) -> u32 { + unsafe { _clmul_32(rs1 as i32, rs2 as i32) as u32 } } From 76b4f9454ca628b7793d14ae9e51ac7a2b4cfcfd Mon Sep 17 00:00:00 2001 From: TechnoPorg Date: Mon, 26 Oct 2026 09:40:37 -0400 Subject: [PATCH 3/3] refactor: make LLVM RISC-V intrinsics use unsigned types --- crates/core_arch/src/riscv32/zb.rs | 24 +++++++------- crates/core_arch/src/riscv32/zk.rs | 42 ++++++++++++------------- crates/core_arch/src/riscv64/zb.rs | 24 +++++++------- crates/core_arch/src/riscv64/zk.rs | 36 ++++++++++----------- crates/core_arch/src/riscv_shared/zb.rs | 8 ++--- crates/core_arch/src/riscv_shared/zk.rs | 32 +++++++++---------- 6 files changed, 83 insertions(+), 83 deletions(-) diff --git a/crates/core_arch/src/riscv32/zb.rs b/crates/core_arch/src/riscv32/zb.rs index 7c5aef260b..1639890baf 100644 --- a/crates/core_arch/src/riscv32/zb.rs +++ b/crates/core_arch/src/riscv32/zb.rs @@ -3,22 +3,22 @@ use stdarch_test::assert_instr; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.xperm8.i32"] - fn _xperm8_32(rs1: i32, rs2: i32) -> i32; + fn _xperm8_32(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.xperm4.i32"] - fn _xperm4_32(rs1: i32, rs2: i32) -> i32; + fn _xperm4_32(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.clmulh.i32"] - fn _clmulh_32(rs1: i32, rs2: i32) -> i32; + fn _clmulh_32(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.clmulr.i32"] - fn _clmulr_32(rs1: i32, rs2: i32) -> i32; + fn _clmulr_32(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.zip.i32"] - fn _zip_32(rs1: i32) -> i32; + fn _zip_32(rs1: u32) -> u32; #[link_name = "llvm.riscv.unzip.i32"] - fn _unzip_32(rs1: i32) -> i32; + fn _unzip_32(rs1: u32) -> u32; } /// Byte-wise lookup of indices into a vector in registers. @@ -38,7 +38,7 @@ unsafe extern "llvm-intrinsic" { #[cfg_attr(test, assert_instr(xperm8))] #[inline] pub fn xperm8_32(rs1: u32, rs2: u32) -> u32 { - unsafe { _xperm8_32(rs1 as i32, rs2 as i32) as u32 } + unsafe { _xperm8_32(rs1, rs2) } } /// Nibble-wise lookup of indices into a vector. @@ -58,7 +58,7 @@ pub fn xperm8_32(rs1: u32, rs2: u32) -> u32 { #[cfg_attr(test, assert_instr(xperm4))] #[inline] pub fn xperm4_32(rs1: u32, rs2: u32) -> u32 { - unsafe { _xperm4_32(rs1 as i32, rs2 as i32) as u32 } + unsafe { _xperm4_32(rs1, rs2) } } /// Carry-less multiply (high-part) @@ -75,7 +75,7 @@ pub fn xperm4_32(rs1: u32, rs2: u32) -> u32 { #[cfg_attr(test, assert_instr(clmulh))] #[inline] pub fn clmulh_32(rs1: u32, rs2: u32) -> u32 { - unsafe { _clmulh_32(rs1 as i32, rs2 as i32) as u32 } + unsafe { _clmulh_32(rs1, rs2) } } /// Carry-less multiply (reversed) @@ -92,7 +92,7 @@ pub fn clmulh_32(rs1: u32, rs2: u32) -> u32 { #[cfg_attr(test, assert_instr(clmulr))] #[inline] pub fn clmulr_32(rs1: u32, rs2: u32) -> u32 { - unsafe { _clmulr_32(rs1 as i32, rs2 as i32) as u32 } + unsafe { _clmulr_32(rs1, rs2) } } /// Place upper/lower halves of the source register into odd/even bits of the destination @@ -114,7 +114,7 @@ pub fn clmulr_32(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn zip_32(rs: u32) -> u32 { - unsafe { _zip_32(rs as i32) as u32 } + unsafe { _zip_32(rs) } } /// Place odd and even bits of the source word into upper/lower halves of the destination. @@ -133,5 +133,5 @@ pub fn zip_32(rs: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn unzip_32(rs: u32) -> u32 { - unsafe { _unzip_32(rs as i32) as u32 } + unsafe { _unzip_32(rs) } } diff --git a/crates/core_arch/src/riscv32/zk.rs b/crates/core_arch/src/riscv32/zk.rs index 1613245182..34e5fb90a9 100644 --- a/crates/core_arch/src/riscv32/zk.rs +++ b/crates/core_arch/src/riscv32/zk.rs @@ -3,34 +3,34 @@ use stdarch_test::assert_instr; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.aes32esi"] - fn _aes32esi(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _aes32esi(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.aes32esmi"] - fn _aes32esmi(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _aes32esmi(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.aes32dsi"] - fn _aes32dsi(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _aes32dsi(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.aes32dsmi"] - fn _aes32dsmi(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _aes32dsmi(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.sha512sig0h"] - fn _sha512sig0h(rs1: i32, rs2: i32) -> i32; + fn _sha512sig0h(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.sha512sig0l"] - fn _sha512sig0l(rs1: i32, rs2: i32) -> i32; + fn _sha512sig0l(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.sha512sig1h"] - fn _sha512sig1h(rs1: i32, rs2: i32) -> i32; + fn _sha512sig1h(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.sha512sig1l"] - fn _sha512sig1l(rs1: i32, rs2: i32) -> i32; + fn _sha512sig1l(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.sha512sum0r"] - fn _sha512sum0r(rs1: i32, rs2: i32) -> i32; + fn _sha512sum0r(rs1: u32, rs2: u32) -> u32; #[link_name = "llvm.riscv.sha512sum1r"] - fn _sha512sum1r(rs1: i32, rs2: i32) -> i32; + fn _sha512sum1r(rs1: u32, rs2: u32) -> u32; } /// AES final round encryption instruction for RV32. @@ -59,10 +59,10 @@ unsafe extern "llvm-intrinsic" { pub fn aes32esi(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _aes32esi(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _aes32esi(rs1, rs2, BS as u32) } } -/// AES middle round encryption instruction for RV32 with. +/// AES middle round encryption instruction for RV32. /// /// This instruction sources a single byte from rs2 according to bs. To this it applies the /// forward AES SBox operation, and a partial forward MixColumn, before XOR’ing the result with @@ -88,7 +88,7 @@ pub fn aes32esi(rs1: u32, rs2: u32) -> u32 { pub fn aes32esmi(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _aes32esmi(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _aes32esmi(rs1, rs2, BS as u32) } } /// AES final round decryption instruction for RV32. @@ -116,7 +116,7 @@ pub fn aes32esmi(rs1: u32, rs2: u32) -> u32 { pub fn aes32dsi(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _aes32dsi(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _aes32dsi(rs1, rs2, BS as u32) } } /// AES middle round decryption instruction for RV32. @@ -145,7 +145,7 @@ pub fn aes32dsi(rs1: u32, rs2: u32) -> u32 { pub fn aes32dsmi(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _aes32dsmi(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _aes32dsmi(rs1, rs2, BS as u32) } } /// Implements the high half of the Sigma0 transformation, as used in the SHA2-512 hash @@ -168,7 +168,7 @@ pub fn aes32dsmi(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig0h(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sig0h(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sig0h(rs1, rs2) } } /// Implements the low half of the Sigma0 transformation, as used in the SHA2-512 hash function @@ -191,7 +191,7 @@ pub fn sha512sig0h(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig0l(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sig0l(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sig0l(rs1, rs2) } } /// Implements the high half of the Sigma1 transformation, as used in the SHA2-512 hash @@ -214,7 +214,7 @@ pub fn sha512sig0l(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig1h(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sig1h(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sig1h(rs1, rs2) } } /// Implements the low half of the Sigma1 transformation, as used in the SHA2-512 hash function @@ -236,7 +236,7 @@ pub fn sha512sig1h(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig1l(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sig1l(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sig1l(rs1, rs2) } } /// Implements the Sum0 transformation, as used in the SHA2-512 hash function \[49\] (Section @@ -258,7 +258,7 @@ pub fn sha512sig1l(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sum0r(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sum0r(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sum0r(rs1, rs2) } } /// Implements the Sum1 transformation, as used in the SHA2-512 hash function \[49\] (Section @@ -280,5 +280,5 @@ pub fn sha512sum0r(rs1: u32, rs2: u32) -> u32 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sum1r(rs1: u32, rs2: u32) -> u32 { - unsafe { _sha512sum1r(rs1 as i32, rs2 as i32) as u32 } + unsafe { _sha512sum1r(rs1, rs2) } } diff --git a/crates/core_arch/src/riscv64/zb.rs b/crates/core_arch/src/riscv64/zb.rs index 014c5b8c2b..95c6fdf9cd 100644 --- a/crates/core_arch/src/riscv64/zb.rs +++ b/crates/core_arch/src/riscv64/zb.rs @@ -3,22 +3,22 @@ use stdarch_test::assert_instr; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.xperm8.i64"] - fn _xperm8_64(rs1: i64, rs2: i64) -> i64; + fn _xperm8_64(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.xperm4.i64"] - fn _xperm4_64(rs1: i64, rs2: i64) -> i64; + fn _xperm4_64(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.orc.b.i64"] - fn _orc_b_64(rs: i64) -> i64; + fn _orc_b_64(rs: u64) -> u64; #[link_name = "llvm.riscv.clmul.i64"] - fn _clmul_64(rs1: i64, rs2: i64) -> i64; + fn _clmul_64(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.clmulh.i64"] - fn _clmulh_64(rs1: i64, rs2: i64) -> i64; + fn _clmulh_64(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.clmulr.i64"] - fn _clmulr_64(rs1: i64, rs2: i64) -> i64; + fn _clmulr_64(rs1: u64, rs2: u64) -> u64; } /// Byte-wise lookup of indices into a vector in registers. @@ -38,7 +38,7 @@ unsafe extern "llvm-intrinsic" { #[cfg_attr(test, assert_instr(xperm8))] #[inline] pub fn xperm8_64(rs1: u64, rs2: u64) -> u64 { - unsafe { _xperm8_64(rs1 as i64, rs2 as i64) as u64 } + unsafe { _xperm8_64(rs1, rs2) } } /// Nibble-wise lookup of indices into a vector. @@ -58,7 +58,7 @@ pub fn xperm8_64(rs1: u64, rs2: u64) -> u64 { #[cfg_attr(test, assert_instr(xperm4))] #[inline] pub fn xperm4_64(rs1: u64, rs2: u64) -> u64 { - unsafe { _xperm4_64(rs1 as i64, rs2 as i64) as u64 } + unsafe { _xperm4_64(rs1, rs2) } } /// Bitwise OR-Combine, byte granule @@ -77,7 +77,7 @@ pub fn xperm4_64(rs1: u64, rs2: u64) -> u64 { #[cfg_attr(test, assert_instr(orc.b))] #[inline] pub fn orc_b_64(rs: u64) -> u64 { - unsafe { _orc_b_64(rs as i64) as u64 } + unsafe { _orc_b_64(rs) } } /// Carry-less multiply (low-part) @@ -94,7 +94,7 @@ pub fn orc_b_64(rs: u64) -> u64 { #[cfg_attr(test, assert_instr(clmul))] #[inline] pub fn clmul_64(rs1: u64, rs2: u64) -> u64 { - unsafe { _clmul_64(rs1 as i64, rs2 as i64) as u64 } + unsafe { _clmul_64(rs1, rs2) } } /// Carry-less multiply (high-part) @@ -111,7 +111,7 @@ pub fn clmul_64(rs1: u64, rs2: u64) -> u64 { #[cfg_attr(test, assert_instr(clmulh))] #[inline] pub fn clmulh_64(rs1: u64, rs2: u64) -> u64 { - unsafe { _clmulh_64(rs1 as i64, rs2 as i64) as u64 } + unsafe { _clmulh_64(rs1, rs2) } } /// Carry-less multiply (reversed) @@ -128,5 +128,5 @@ pub fn clmulh_64(rs1: u64, rs2: u64) -> u64 { #[cfg_attr(test, assert_instr(clmulr))] #[inline] pub fn clmulr_64(rs1: u64, rs2: u64) -> u64 { - unsafe { _clmulr_64(rs1 as i64, rs2 as i64) as u64 } + unsafe { _clmulr_64(rs1, rs2) } } diff --git a/crates/core_arch/src/riscv64/zk.rs b/crates/core_arch/src/riscv64/zk.rs index 21cfefafdd..ea5486b391 100644 --- a/crates/core_arch/src/riscv64/zk.rs +++ b/crates/core_arch/src/riscv64/zk.rs @@ -5,31 +5,31 @@ use crate::arch::asm; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.aes64es"] - fn _aes64es(rs1: i64, rs2: i64) -> i64; + fn _aes64es(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.aes64esm"] - fn _aes64esm(rs1: i64, rs2: i64) -> i64; + fn _aes64esm(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.aes64ds"] - fn _aes64ds(rs1: i64, rs2: i64) -> i64; + fn _aes64ds(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.aes64dsm"] - fn _aes64dsm(rs1: i64, rs2: i64) -> i64; + fn _aes64dsm(rs1: u64, rs2: u64) -> u64; #[link_name = "llvm.riscv.aes64im"] - fn _aes64im(rs1: i64) -> i64; + fn _aes64im(rs1: u64) -> u64; #[link_name = "llvm.riscv.sha512sig0"] - fn _sha512sig0(rs1: i64) -> i64; + fn _sha512sig0(rs1: u64) -> u64; #[link_name = "llvm.riscv.sha512sig1"] - fn _sha512sig1(rs1: i64) -> i64; + fn _sha512sig1(rs1: u64) -> u64; #[link_name = "llvm.riscv.sha512sum0"] - fn _sha512sum0(rs1: i64) -> i64; + fn _sha512sum0(rs1: u64) -> u64; #[link_name = "llvm.riscv.sha512sum1"] - fn _sha512sum1(rs1: i64) -> i64; + fn _sha512sum1(rs1: u64) -> u64; } /// AES final round encryption instruction for RV64. @@ -49,7 +49,7 @@ unsafe extern "llvm-intrinsic" { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn aes64es(rs1: u64, rs2: u64) -> u64 { - unsafe { _aes64es(rs1 as i64, rs2 as i64) as u64 } + unsafe { _aes64es(rs1, rs2) } } /// AES middle round encryption instruction for RV64. @@ -69,7 +69,7 @@ pub fn aes64es(rs1: u64, rs2: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn aes64esm(rs1: u64, rs2: u64) -> u64 { - unsafe { _aes64esm(rs1 as i64, rs2 as i64) as u64 } + unsafe { _aes64esm(rs1, rs2) } } /// AES final round decryption instruction for RV64. @@ -89,7 +89,7 @@ pub fn aes64esm(rs1: u64, rs2: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn aes64ds(rs1: u64, rs2: u64) -> u64 { - unsafe { _aes64ds(rs1 as i64, rs2 as i64) as u64 } + unsafe { _aes64ds(rs1, rs2) } } /// AES middle round decryption instruction for RV64. @@ -109,7 +109,7 @@ pub fn aes64ds(rs1: u64, rs2: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn aes64dsm(rs1: u64, rs2: u64) -> u64 { - unsafe { _aes64dsm(rs1 as i64, rs2 as i64) as u64 } + unsafe { _aes64dsm(rs1, rs2) } } /// This instruction implements part of the KeySchedule operation for the AES Block cipher @@ -200,7 +200,7 @@ pub fn aes64ks2(rs1: u64, rs2: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn aes64im(rs1: u64) -> u64 { - unsafe { _aes64im(rs1 as i64) as u64 } + unsafe { _aes64im(rs1) } } /// Implements the Sigma0 transformation function as used in the SHA2-512 hash function \[49\] @@ -220,7 +220,7 @@ pub fn aes64im(rs1: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig0(rs1: u64) -> u64 { - unsafe { _sha512sig0(rs1 as i64) as u64 } + unsafe { _sha512sig0(rs1) } } /// Implements the Sigma1 transformation function as used in the SHA2-512 hash function \[49\] @@ -240,7 +240,7 @@ pub fn sha512sig0(rs1: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sig1(rs1: u64) -> u64 { - unsafe { _sha512sig1(rs1 as i64) as u64 } + unsafe { _sha512sig1(rs1) } } /// Implements the Sum0 transformation function as used in the SHA2-512 hash function \[49\] @@ -260,7 +260,7 @@ pub fn sha512sig1(rs1: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sum0(rs1: u64) -> u64 { - unsafe { _sha512sum0(rs1 as i64) as u64 } + unsafe { _sha512sum0(rs1) } } /// Implements the Sum1 transformation function as used in the SHA2-512 hash function \[49\] @@ -280,5 +280,5 @@ pub fn sha512sum0(rs1: u64) -> u64 { #[inline] #[unstable(feature = "riscv_ext_intrinsics", issue = "114544")] pub fn sha512sum1(rs1: u64) -> u64 { - unsafe { _sha512sum1(rs1 as i64) as u64 } + unsafe { _sha512sum1(rs1) } } diff --git a/crates/core_arch/src/riscv_shared/zb.rs b/crates/core_arch/src/riscv_shared/zb.rs index 83d8bc9367..04a42f0235 100644 --- a/crates/core_arch/src/riscv_shared/zb.rs +++ b/crates/core_arch/src/riscv_shared/zb.rs @@ -3,10 +3,10 @@ use stdarch_test::assert_instr; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.orc.b.i32"] - fn _orc_b_32(rs: i32) -> i32; + fn _orc_b_32(rs: u32) -> u32; #[link_name = "llvm.riscv.clmul.i32"] - fn _clmul_32(rs1: i32, rs2: i32) -> i32; + fn _clmul_32(rs1: u32, rs2: u32) -> u32; } /// Bitwise OR-Combine, byte granule @@ -25,7 +25,7 @@ unsafe extern "llvm-intrinsic" { #[cfg_attr(test, assert_instr(orc.b))] #[inline] pub fn orc_b_32(rs: u32) -> u32 { - unsafe { _orc_b_32(rs as i32) as u32 } + unsafe { _orc_b_32(rs) } } /// Carry-less multiply (low-part) @@ -42,5 +42,5 @@ pub fn orc_b_32(rs: u32) -> u32 { #[cfg_attr(test, assert_instr(clmul))] #[inline] pub fn clmul_32(rs1: u32, rs2: u32) -> u32 { - unsafe { _clmul_32(rs1 as i32, rs2 as i32) as u32 } + unsafe { _clmul_32(rs1, rs2) } } diff --git a/crates/core_arch/src/riscv_shared/zk.rs b/crates/core_arch/src/riscv_shared/zk.rs index 0fcf13c7ac..6966686731 100644 --- a/crates/core_arch/src/riscv_shared/zk.rs +++ b/crates/core_arch/src/riscv_shared/zk.rs @@ -3,28 +3,28 @@ use stdarch_test::assert_instr; unsafe extern "llvm-intrinsic" { #[link_name = "llvm.riscv.sm4ed"] - fn _sm4ed(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _sm4ed(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.sm4ks"] - fn _sm4ks(rs1: i32, rs2: i32, bs: i32) -> i32; + fn _sm4ks(rs1: u32, rs2: u32, bs: u32) -> u32; #[link_name = "llvm.riscv.sm3p0"] - fn _sm3p0(rs1: i32) -> i32; + fn _sm3p0(rs1: u32) -> u32; #[link_name = "llvm.riscv.sm3p1"] - fn _sm3p1(rs1: i32) -> i32; + fn _sm3p1(rs1: u32) -> u32; #[link_name = "llvm.riscv.sha256sig0"] - fn _sha256sig0(rs1: i32) -> i32; + fn _sha256sig0(rs1: u32) -> u32; #[link_name = "llvm.riscv.sha256sig1"] - fn _sha256sig1(rs1: i32) -> i32; + fn _sha256sig1(rs1: u32) -> u32; #[link_name = "llvm.riscv.sha256sum0"] - fn _sha256sum0(rs1: i32) -> i32; + fn _sha256sum0(rs1: u32) -> u32; #[link_name = "llvm.riscv.sha256sum1"] - fn _sha256sum1(rs1: i32) -> i32; + fn _sha256sum1(rs1: u32) -> u32; } /// Implements the Sigma0 transformation function as used in the SHA2-256 hash function \[49\] @@ -47,7 +47,7 @@ unsafe extern "llvm-intrinsic" { #[cfg_attr(test, assert_instr(sha256sig0))] #[inline] pub fn sha256sig0(rs1: u32) -> u32 { - unsafe { _sha256sig0(rs1 as i32) as u32 } + unsafe { _sha256sig0(rs1) } } /// Implements the Sigma1 transformation function as used in the SHA2-256 hash function \[49\] @@ -70,7 +70,7 @@ pub fn sha256sig0(rs1: u32) -> u32 { #[cfg_attr(test, assert_instr(sha256sig1))] #[inline] pub fn sha256sig1(rs1: u32) -> u32 { - unsafe { _sha256sig1(rs1 as i32) as u32 } + unsafe { _sha256sig1(rs1) } } /// Implements the Sum0 transformation function as used in the SHA2-256 hash function \[49\] @@ -93,7 +93,7 @@ pub fn sha256sig1(rs1: u32) -> u32 { #[cfg_attr(test, assert_instr(sha256sum0))] #[inline] pub fn sha256sum0(rs1: u32) -> u32 { - unsafe { _sha256sum0(rs1 as i32) as u32 } + unsafe { _sha256sum0(rs1) } } /// Implements the Sum1 transformation function as used in the SHA2-256 hash function \[49\] @@ -116,7 +116,7 @@ pub fn sha256sum0(rs1: u32) -> u32 { #[cfg_attr(test, assert_instr(sha256sum1))] #[inline] pub fn sha256sum1(rs1: u32) -> u32 { - unsafe { _sha256sum1(rs1 as i32) as u32 } + unsafe { _sha256sum1(rs1) } } /// Accelerates the block encrypt/decrypt operation of the SM4 block cipher \[5, 31\]. @@ -192,7 +192,7 @@ pub fn sha256sum1(rs1: u32) -> u32 { pub fn sm4ed(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _sm4ed(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _sm4ed(rs1, rs2, BS as u32) } } /// Accelerates the Key Schedule operation of the SM4 block cipher \[5, 31\] with `bs=0`. @@ -268,7 +268,7 @@ pub fn sm4ed(rs1: u32, rs2: u32) -> u32 { pub fn sm4ks(rs1: u32, rs2: u32) -> u32 { static_assert!(BS < 4); - unsafe { _sm4ks(rs1 as i32, rs2 as i32, BS as i32) as u32 } + unsafe { _sm4ks(rs1, rs2, BS as u32) } } /// Implements the P0 transformation function as used in the SM3 hash function [4, 30]. @@ -303,7 +303,7 @@ pub fn sm4ks(rs1: u32, rs2: u32) -> u32 { #[cfg_attr(test, assert_instr(sm3p0))] #[inline] pub fn sm3p0(rs1: u32) -> u32 { - unsafe { _sm3p0(rs1 as i32) as u32 } + unsafe { _sm3p0(rs1) } } /// Implements the P1 transformation function as used in the SM3 hash function [4, 30]. @@ -344,5 +344,5 @@ pub fn sm3p0(rs1: u32) -> u32 { #[cfg_attr(test, assert_instr(sm3p1))] #[inline] pub fn sm3p1(rs1: u32) -> u32 { - unsafe { _sm3p1(rs1 as i32) as u32 } + unsafe { _sm3p1(rs1) } }