ab_riscv_primitives/instructions/rv32/zk/
zbkb.rs1#[cfg(test)]
4mod tests;
5
6use crate::instructions::Instruction;
7use crate::instructions::rv32::b::zbb::Rv32ZbbInstruction;
8use crate::registers::general_purpose::Register;
9use ab_riscv_macros::instruction;
10use core::fmt;
11
12#[instruction(
14 reorder = [Andn, Orn, Xnor, Rol, Ror, Rori, Rev8, Pack, Packh, Brev8],
15 ignore = [Rv32ZbbInstruction],
16 inherit = [Rv32ZbbInstruction],
17)]
18#[derive(Debug, Clone, Copy)]
19#[derive_const(PartialEq, Eq)]
20pub enum Rv32ZbkbInstruction<Reg> {
21 Pack { rd: Reg, rs1: Reg, rs2: Reg },
23 Packh { rd: Reg, rs1: Reg, rs2: Reg },
25 Brev8 { rd: Reg, rs1: Reg },
27 Zip { rd: Reg, rs1: Reg },
30 Unzip { rd: Reg, rs1: Reg },
33}
34
35#[instruction]
36const impl<Reg> Instruction for Rv32ZbkbInstruction<Reg>
37where
38 Reg: [const] Register<Type = u32>,
39{
40 type Reg = Reg;
41
42 #[inline(always)]
43 #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
44 fn try_decode(instruction: u32) -> Option<Self> {
45 let opcode = (instruction & 0b111_1111) as u8;
46 let rd_bits = ((instruction >> 7) & 0x1f) as u8;
47 let funct3 = ((instruction >> 12) & 0b111) as u8;
48 let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
49 let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
50 let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
51 let funct12 = ((instruction >> 20) & 0xfff) as u16;
52
53 match opcode {
54 0b001_0011 => {
56 let rd = Reg::from_bits(rd_bits)?;
57 let rs1 = Reg::from_bits(rs1_bits)?;
58 match funct3 {
59 0b101 if funct12 == 0b0110_1000_0111 => Some(Self::Brev8 { rd, rs1 }),
61 0b001 if funct12 == 0b0000_1000_1111 => Some(Self::Zip { rd, rs1 }),
63 0b101 if funct12 == 0b0000_1000_1111 => Some(Self::Unzip { rd, rs1 }),
65 _ => None,
66 }
67 }
68 0b011_0011 => {
70 let rd = Reg::from_bits(rd_bits)?;
71 let rs1 = Reg::from_bits(rs1_bits)?;
72 let rs2 = Reg::from_bits(rs2_bits)?;
73 match (funct3, funct7, rs2_bits) {
74 (0b100, 0b000_0100, rs2_bits) if rs2_bits != 0 => {
77 Some(Self::Pack { rd, rs1, rs2 })
78 }
79 (0b111, 0b000_0100, _) => Some(Self::Packh { rd, rs1, rs2 }),
81 _ => None,
82 }
83 }
84 _ => None,
85 }
86 }
87
88 #[inline(always)]
89 fn alignment() -> u8 {
90 align_of::<u32>() as u8
91 }
92
93 #[inline(always)]
94 fn size(&self) -> u8 {
95 size_of::<u32>() as u8
96 }
97}
98
99#[instruction]
100impl<Reg> fmt::Display for Rv32ZbkbInstruction<Reg>
101where
102 Reg: fmt::Display + Copy,
103{
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 match self {
106 Self::Pack { rd, rs1, rs2 } => write!(f, "pack {rd}, {rs1}, {rs2}"),
107 Self::Packh { rd, rs1, rs2 } => write!(f, "packh {rd}, {rs1}, {rs2}"),
108 Self::Brev8 { rd, rs1 } => write!(f, "brev8 {rd}, {rs1}"),
109 Self::Zip { rd, rs1 } => write!(f, "zip {rd}, {rs1}"),
110 Self::Unzip { rd, rs1 } => write!(f, "unzip {rd}, {rs1}"),
111 }
112 }
113}