Skip to content

Commit b2e97b3

Browse files
committed
Fix: Utilize LLVM intrinsics where possible
1 parent db78d79 commit b2e97b3

File tree

3 files changed

+112
-310
lines changed

3 files changed

+112
-310
lines changed

crates/core_arch/src/riscv32/zk.rs

+36-89
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use core::arch::asm;
1+
#[cfg(test)]
2+
use stdarch_test::assert_instr;
23

34
macro_rules! static_assert_imm2 {
45
($imm:ident) => {
@@ -21,6 +22,30 @@ extern "unadjusted" {
2122

2223
#[link_name = "llvm.riscv.aes32dsmi"]
2324
fn _aes32dsmi(rs1: i32, rs2: i32, bs: i32) -> i32;
25+
26+
#[link_name = "llvm.riscv.zip.i32"]
27+
fn _zip(rs1: i32) -> i32;
28+
29+
#[link_name = "llvm.riscv.unzip.i32"]
30+
fn _unzip(rs1: i32) -> i32;
31+
32+
#[link_name = "llvm.riscv.sha512sig0h"]
33+
fn _sha512sig0h(rs1: i32, rs2: i32) -> i32;
34+
35+
#[link_name = "llvm.riscv.sha512sig0l"]
36+
fn _sha512sig0l(rs1: i32, rs2: i32) -> i32;
37+
38+
#[link_name = "llvm.riscv.sha512sig1h"]
39+
fn _sha512sig1h(rs1: i32, rs2: i32) -> i32;
40+
41+
#[link_name = "llvm.riscv.sha512sig1l"]
42+
fn _sha512sig1l(rs1: i32, rs2: i32) -> i32;
43+
44+
#[link_name = "llvm.riscv.sha512sum0r"]
45+
fn _sha512sum0r(rs1: i32, rs2: i32) -> i32;
46+
47+
#[link_name = "llvm.riscv.sha512sum1r"]
48+
fn _sha512sum1r(rs1: i32, rs2: i32) -> i32;
2449
}
2550

2651
/// AES final round encryption instruction for RV32.
@@ -166,17 +191,8 @@ pub unsafe fn aes32dsmi<const BS: u8>(rs1: u32, rs2: u32) -> u32 {
166191
#[target_feature(enable = "zbkb")]
167192
#[cfg_attr(test, assert_instr(zip))]
168193
#[inline]
169-
pub unsafe fn zip(rs: usize) -> usize {
170-
let value: usize;
171-
unsafe {
172-
asm!(
173-
"zip {rd},{rs}",
174-
rd = lateout(reg) value,
175-
rs = in(reg) rs,
176-
options(pure, nomem, nostack),
177-
)
178-
}
179-
value
194+
pub unsafe fn zip(rs: u32) -> u32 {
195+
_zip(rs as i32) as u32
180196
}
181197

182198
/// Place odd and even bits of the source word into upper/lower halves of the destination.
@@ -197,17 +213,8 @@ pub unsafe fn zip(rs: usize) -> usize {
197213
#[target_feature(enable = "zbkb")]
198214
#[cfg_attr(test, assert_instr(unzip))]
199215
#[inline]
200-
pub unsafe fn unzip(rs: usize) -> usize {
201-
let value: usize;
202-
unsafe {
203-
asm!(
204-
"unzip {rd},{rs}",
205-
rd = lateout(reg) value,
206-
rs = in(reg) rs,
207-
options(pure, nomem, nostack),
208-
)
209-
}
210-
value
216+
pub unsafe fn unzip(rs: u32) -> u32 {
217+
_unzip(rs as i32) as u32
211218
}
212219

213220
/// Implements the high half of the Sigma0 transformation, as used in the SHA2-512 hash
@@ -232,17 +239,7 @@ pub unsafe fn unzip(rs: usize) -> usize {
232239
#[cfg_attr(test, assert_instr(sha512sig0h))]
233240
#[inline]
234241
pub unsafe fn sha512sig0h(rs1: u32, rs2: u32) -> u32 {
235-
let value: u32;
236-
unsafe {
237-
asm!(
238-
"sha512sig0h {rd},{rs1},{rs2}",
239-
rd = lateout(reg) value,
240-
rs1 = in(reg) rs1,
241-
rs2 = in(reg) rs2,
242-
options(pure, nomem, nostack),
243-
)
244-
}
245-
value
242+
_sha512sig0h(rs1 as i32, rs2 as i32) as u32
246243
}
247244

248245
/// Implements the low half of the Sigma0 transformation, as used in the SHA2-512 hash function
@@ -267,17 +264,7 @@ pub unsafe fn sha512sig0h(rs1: u32, rs2: u32) -> u32 {
267264
#[cfg_attr(test, assert_instr(sha512sig0l))]
268265
#[inline]
269266
pub unsafe fn sha512sig0l(rs1: u32, rs2: u32) -> u32 {
270-
let value: u32;
271-
unsafe {
272-
asm!(
273-
"sha512sig0l {rd},{rs1},{rs2}",
274-
rd = lateout(reg) value,
275-
rs1 = in(reg) rs1,
276-
rs2 = in(reg) rs2,
277-
options(pure, nomem, nostack),
278-
)
279-
}
280-
value
267+
_sha512sig0l(rs1 as i32, rs2 as i32) as u32
281268
}
282269

283270
/// Implements the high half of the Sigma1 transformation, as used in the SHA2-512 hash
@@ -302,17 +289,7 @@ pub unsafe fn sha512sig0l(rs1: u32, rs2: u32) -> u32 {
302289
#[cfg_attr(test, assert_instr(sha512sig1h))]
303290
#[inline]
304291
pub unsafe fn sha512sig1h(rs1: u32, rs2: u32) -> u32 {
305-
let value: u32;
306-
unsafe {
307-
asm!(
308-
"sha512sig1h {rd},{rs1},{rs2}",
309-
rd = lateout(reg) value,
310-
rs1 = in(reg) rs1,
311-
rs2 = in(reg) rs2,
312-
options(pure, nomem, nostack),
313-
)
314-
}
315-
value
292+
_sha512sig1h(rs1 as i32, rs2 as i32) as u32
316293
}
317294

318295
/// Implements the low half of the Sigma1 transformation, as used in the SHA2-512 hash function
@@ -337,17 +314,7 @@ pub unsafe fn sha512sig1h(rs1: u32, rs2: u32) -> u32 {
337314
#[cfg_attr(test, assert_instr(sha512sig1l))]
338315
#[inline]
339316
pub unsafe fn sha512sig1l(rs1: u32, rs2: u32) -> u32 {
340-
let value: u32;
341-
unsafe {
342-
asm!(
343-
"sha512sig1l {rd},{rs1},{rs2}",
344-
rd = lateout(reg) value,
345-
rs1 = in(reg) rs1,
346-
rs2 = in(reg) rs2,
347-
options(pure, nomem, nostack),
348-
)
349-
}
350-
value
317+
_sha512sig1l(rs1 as i32, rs2 as i32) as u32
351318
}
352319

353320
/// Implements the Sum0 transformation, as used in the SHA2-512 hash function \[49\] (Section
@@ -371,17 +338,7 @@ pub unsafe fn sha512sig1l(rs1: u32, rs2: u32) -> u32 {
371338
#[cfg_attr(test, assert_instr(sha512sum0r))]
372339
#[inline]
373340
pub unsafe fn sha512sum0r(rs1: u32, rs2: u32) -> u32 {
374-
let value: u32;
375-
unsafe {
376-
asm!(
377-
"sha512sum0r {rd},{rs1},{rs2}",
378-
rd = lateout(reg) value,
379-
rs1 = in(reg) rs1,
380-
rs2 = in(reg) rs2,
381-
options(pure, nomem, nostack),
382-
)
383-
}
384-
value
341+
_sha512sum0r(rs1 as i32, rs2 as i32) as u32
385342
}
386343

387344
/// Implements the Sum1 transformation, as used in the SHA2-512 hash function \[49\] (Section
@@ -405,15 +362,5 @@ pub unsafe fn sha512sum0r(rs1: u32, rs2: u32) -> u32 {
405362
#[cfg_attr(test, assert_instr(sha512sum1r))]
406363
#[inline]
407364
pub unsafe fn sha512sum1r(rs1: u32, rs2: u32) -> u32 {
408-
let value: u32;
409-
unsafe {
410-
asm!(
411-
"sha512sum1r {rd},{rs1},{rs2}",
412-
rd = lateout(reg) value,
413-
rs1 = in(reg) rs1,
414-
rs2 = in(reg) rs2,
415-
options(pure, nomem, nostack),
416-
)
417-
}
418-
value
365+
_sha512sum1r(rs1 as i32, rs2 as i32) as u32
419366
}

0 commit comments

Comments
 (0)