Skip to main content

ab_riscv_interpreter/rv64/zk/zkn/
zknh.rs

1//! RV64 Zknh extension
2
3pub mod rv64_zknh_helpers;
4#[cfg(test)]
5mod tests;
6
7use crate::{
8    ExecutableInstruction, ExecutableInstructionCsr, ExecutableInstructionOperands, ExecutionError,
9    RegisterFile, Rs1Rs2OperandValues, Rs1Rs2Operands,
10};
11use ab_riscv_macros::instruction_execution;
12use ab_riscv_primitives::prelude::*;
13use core::ops::ControlFlow;
14
15#[instruction_execution]
16impl<Reg> ExecutableInstructionOperands for Rv64ZknhInstruction<Reg> where Reg: Register<Type = u64> {}
17
18#[instruction_execution]
19impl<Reg, ExtState, CustomError> ExecutableInstructionCsr<ExtState, CustomError>
20    for Rv64ZknhInstruction<Reg>
21where
22    Reg: Register<Type = u64>,
23{
24}
25
26#[instruction_execution]
27impl<Reg, Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
28    ExecutableInstruction<Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
29    for Rv64ZknhInstruction<Reg>
30where
31    Reg: Register<Type = u64>,
32    Regs: RegisterFile<Reg>,
33{
34    #[inline(always)]
35    fn execute(
36        self,
37        Rs1Rs2OperandValues {
38            rs1_value,
39            rs2_value: _,
40        }: Rs1Rs2OperandValues<<Self::Reg as Register>::Type>,
41        _regs: &mut Regs,
42        _ext_state: &mut ExtState,
43        _memory: &mut Memory,
44        _program_counter: &mut PC,
45        _system_instruction_handler: &mut InstructionHandler,
46    ) -> Result<
47        ControlFlow<(), (Self::Reg, <Self::Reg as Register>::Type)>,
48        ExecutionError<Reg::Type, CustomError>,
49    > {
50        match self {
51            Self::Sha256Sig0 { rd, rs1: _ } => {
52                let x = rs1_value as u32;
53
54                let res32 = rv64_zknh_helpers::sha256sig0(x);
55
56                Ok(ControlFlow::Continue((
57                    rd,
58                    i64::from(res32.cast_signed()).cast_unsigned(),
59                )))
60            }
61            Self::Sha256Sig1 { rd, rs1: _ } => {
62                let x = rs1_value as u32;
63
64                let res32 = rv64_zknh_helpers::sha256sig1(x);
65
66                Ok(ControlFlow::Continue((
67                    rd,
68                    i64::from(res32.cast_signed()).cast_unsigned(),
69                )))
70            }
71            Self::Sha256Sum0 { rd, rs1: _ } => {
72                let x = rs1_value as u32;
73
74                let res32 = rv64_zknh_helpers::sha256sum0(x);
75
76                Ok(ControlFlow::Continue((
77                    rd,
78                    i64::from(res32.cast_signed()).cast_unsigned(),
79                )))
80            }
81            Self::Sha256Sum1 { rd, rs1: _ } => {
82                let x = rs1_value as u32;
83
84                let res32 = rv64_zknh_helpers::sha256sum1(x);
85
86                Ok(ControlFlow::Continue((
87                    rd,
88                    i64::from(res32.cast_signed()).cast_unsigned(),
89                )))
90            }
91            Self::Sha512Sig0 { rd, rs1: _ } => {
92                let x = rs1_value;
93
94                Ok(ControlFlow::Continue((
95                    rd,
96                    rv64_zknh_helpers::sha512sig0(x),
97                )))
98            }
99            Self::Sha512Sig1 { rd, rs1: _ } => {
100                let x = rs1_value;
101
102                Ok(ControlFlow::Continue((
103                    rd,
104                    rv64_zknh_helpers::sha512sig1(x),
105                )))
106            }
107            Self::Sha512Sum0 { rd, rs1: _ } => {
108                let x = rs1_value;
109
110                Ok(ControlFlow::Continue((
111                    rd,
112                    rv64_zknh_helpers::sha512sum0(x),
113                )))
114            }
115            Self::Sha512Sum1 { rd, rs1: _ } => {
116                let x = rs1_value;
117
118                Ok(ControlFlow::Continue((
119                    rd,
120                    rv64_zknh_helpers::sha512sum1(x),
121                )))
122            }
123        }
124    }
125}