Skip to content

Commit bc825ff

Browse files
committed
RISC-V: Use .insn on (Zkne or Zknd) intrinsics
Using the inline assembly and `.insn` could avoid current issues regarding intrinsics available when either Zknd or Zkne is available.
1 parent 9d616ff commit bc825ff

File tree

1 file changed

+26
-15
lines changed
  • crates/core_arch/src/riscv64

1 file changed

+26
-15
lines changed

crates/core_arch/src/riscv64/zk.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(test)]
22
use stdarch_test::assert_instr;
33

4+
use crate::arch::asm;
5+
46
unsafe extern "unadjusted" {
57
#[link_name = "llvm.riscv.aes64es"]
68
fn _aes64es(rs1: i64, rs2: i64) -> i64;
@@ -14,15 +16,6 @@ unsafe extern "unadjusted" {
1416
#[link_name = "llvm.riscv.aes64dsm"]
1517
fn _aes64dsm(rs1: i64, rs2: i64) -> i64;
1618

17-
#[link_name = "llvm.riscv.aes64ks1i"]
18-
fn _aes64ks1i(rs1: i64, rnum: i32) -> i64;
19-
20-
#[link_name = "llvm.riscv.aes64ks2"]
21-
fn _aes64ks2(rs1: i64, rs2: i64) -> i64;
22-
23-
#[link_name = "llvm.riscv.aes64im"]
24-
fn _aes64im(rs1: i64) -> i64;
25-
2619
#[link_name = "llvm.riscv.sha512sig0"]
2720
fn _sha512sig0(rs1: i64) -> i64;
2821

@@ -135,13 +128,23 @@ pub fn aes64dsm(rs1: u64, rs2: u64) -> u64 {
135128
/// The `RNUM` parameter is expected to be a constant value inside the range of `0..=10`.
136129
//#[target_feature(enable = "zkne", enable = "zknd")] // TODO: zkne_or_zknd
137130
#[rustc_legacy_const_generics(1)]
138-
#[cfg_attr(test, assert_instr(aes64ks1i, RNUM = 0))]
131+
#[cfg_attr(test, assert_instr(aes64ks1i, RNUM = 0))] // REMOVE if anything goes wrong
139132
#[inline]
140133
#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
141134
pub fn aes64ks1i<const RNUM: u8>(rs1: u64) -> u64 {
142135
static_assert!(RNUM <= 10);
143136

144-
unsafe { _aes64ks1i(rs1 as i64, RNUM as i32) as u64 }
137+
unsafe {
138+
let rd: u64;
139+
asm!(
140+
".insn i 0x13, 0x1, {}, {}, {}",
141+
lateout(reg) rd,
142+
in(reg) rs1,
143+
const 0x310 + RNUM as u16,
144+
options(pure, nomem, nostack)
145+
);
146+
rd
147+
}
145148
}
146149

147150
/// This instruction implements part of the KeySchedule operation for the AES Block cipher.
@@ -156,11 +159,15 @@ pub fn aes64ks1i<const RNUM: u8>(rs1: u64) -> u64 {
156159
///
157160
/// Section: 3.11
158161
//#[target_feature(enable = "zkne", enable = "zknd")] // TODO: zkne_or_zknd
159-
#[cfg_attr(test, assert_instr(aes64ks2))]
162+
#[cfg_attr(test, assert_instr(aes64ks2))] // REMOVE if anything goes wrong
160163
#[inline]
161164
#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
162165
pub fn aes64ks2(rs1: u64, rs2: u64) -> u64 {
163-
unsafe { _aes64ks2(rs1 as i64, rs2 as i64) as u64 }
166+
unsafe {
167+
let rd: u64;
168+
asm!(".insn r 0x33, 0x0, 0x3f, {}, {}, {}", lateout(reg) rd, in(reg) rs1, in(reg) rs2, options(pure, nomem, nostack));
169+
rd
170+
}
164171
}
165172

166173
/// This instruction accelerates the inverse MixColumns step of the AES Block Cipher, and is used to aid creation of
@@ -177,11 +184,15 @@ pub fn aes64ks2(rs1: u64, rs2: u64) -> u64 {
177184
///
178185
/// Section: 3.9
179186
//#[target_feature(enable = "zkne", enable = "zknd")] // TODO: zkne_or_zknd
180-
#[cfg_attr(test, assert_instr(aes64im))]
187+
#[cfg_attr(test, assert_instr(aes64im))] // REMOVE if anything goes wrong
181188
#[inline]
182189
#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
183190
pub fn aes64im(rs1: u64) -> u64 {
184-
unsafe { _aes64im(rs1 as i64) as u64 }
191+
unsafe {
192+
let rd: u64;
193+
asm!(".insn i 0x13, 0x1, {}, {}, 0x300", lateout(reg) rd, in(reg) rs1, options(pure, nomem, nostack));
194+
rd
195+
}
185196
}
186197

187198
/// Implements the Sigma0 transformation function as used in the SHA2-512 hash function \[49\]

0 commit comments

Comments
 (0)