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, PartialEq, Eq)]
19pub enum Rv32ZbkbInstruction<Reg> {
20 Pack { rd: Reg, rs1: Reg, rs2: Reg },
22 Packh { rd: Reg, rs1: Reg, rs2: Reg },
24 Brev8 { rd: Reg, rs1: Reg },
26 Zip { rd: Reg, rs1: Reg },
29 Unzip { rd: Reg, rs1: Reg },
32}
33
34#[instruction]
35impl<Reg> const Instruction for Rv32ZbkbInstruction<Reg>
36where
37 Reg: [const] Register<Type = u32>,
38{
39 type Reg = Reg;
40
41 #[inline(always)]
42 fn try_decode(instruction: u32) -> Option<Self> {
43 let opcode = (instruction & 0b111_1111) as u8;
44 let rd_bits = ((instruction >> 7) & 0x1f) as u8;
45 let funct3 = ((instruction >> 12) & 0b111) as u8;
46 let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
47 let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
48 let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
49 let funct12 = ((instruction >> 20) & 0xfff) as u16;
50
51 match opcode {
52 0b0010011 => {
54 let rd = Reg::from_bits(rd_bits)?;
55 let rs1 = Reg::from_bits(rs1_bits)?;
56 match funct3 {
57 0b101 if funct12 == 0b0110_1000_0111 => Some(Self::Brev8 { rd, rs1 }),
59 0b001 if funct12 == 0b0000_1000_1111 => Some(Self::Zip { rd, rs1 }),
61 0b101 if funct12 == 0b0000_1000_1111 => Some(Self::Unzip { rd, rs1 }),
63 _ => None,
64 }
65 }
66 0b0110011 => {
68 let rd = Reg::from_bits(rd_bits)?;
69 let rs1 = Reg::from_bits(rs1_bits)?;
70 let rs2 = Reg::from_bits(rs2_bits)?;
71 match (funct3, funct7, rs2_bits) {
72 (0b100, 0b0000100, rs2_bits) if rs2_bits != 0 => {
75 Some(Self::Pack { rd, rs1, rs2 })
76 }
77 (0b111, 0b0000100, _) => Some(Self::Packh { rd, rs1, rs2 }),
79 _ => None,
80 }
81 }
82 _ => None,
83 }
84 }
85
86 #[inline(always)]
87 fn alignment() -> u8 {
88 align_of::<u32>() as u8
89 }
90
91 #[inline(always)]
92 fn size(&self) -> u8 {
93 size_of::<u32>() as u8
94 }
95}
96
97#[instruction]
98impl<Reg> fmt::Display for Rv32ZbkbInstruction<Reg>
99where
100 Reg: fmt::Display + Copy,
101{
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 Self::Pack { rd, rs1, rs2 } => write!(f, "pack {}, {}, {}", rd, rs1, rs2),
105 Self::Packh { rd, rs1, rs2 } => write!(f, "packh {}, {}, {}", rd, rs1, rs2),
106 Self::Brev8 { rd, rs1 } => write!(f, "brev8 {}, {}", rd, rs1),
107 Self::Zip { rd, rs1 } => write!(f, "zip {}, {}", rd, rs1),
108 Self::Unzip { rd, rs1 } => write!(f, "unzip {}, {}", rd, rs1),
109 }
110 }
111}