Skip to main content

ab_riscv_primitives/instructions/rv32/zk/
zbkb.rs

1//! RV32 Zbkb extension
2
3#[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/// RISC-V RV32 Zbkb instruction (Bit-manipulation for Cryptography)
13#[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 low 16 bits of `rs1` into `rd[15:0]`, low 16 bits of `rs2` into `rd[31:16]`
21    Pack { rd: Reg, rs1: Reg, rs2: Reg },
22    /// Pack low 8 bits of `rs1` into `rd[7:0]`, low 8 bits of `rs2` into `rd[15:8]`
23    Packh { rd: Reg, rs1: Reg, rs2: Reg },
24    /// Reverse bits in each byte of `rs1`
25    Brev8 { rd: Reg, rs1: Reg },
26    /// Bit-interleave: scatter lower-half bits of `rs1` to even positions, upper-half bits to odd
27    /// positions
28    Zip { rd: Reg, rs1: Reg },
29    /// Inverse of zip: gather even-position bits of `rs1` to lower half, odd-position bits to upper
30    /// half
31    Unzip { rd: Reg, rs1: Reg },
32}
33
34#[instruction]
35const impl<Reg> Instruction for Rv32ZbkbInstruction<Reg>
36where
37    Reg: [const] Register<Type = u32>,
38{
39    type Reg = Reg;
40
41    #[inline(always)]
42    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
43    fn try_decode(instruction: u32) -> Option<Self> {
44        let opcode = (instruction & 0b111_1111) as u8;
45        let rd_bits = ((instruction >> 7) & 0x1f) as u8;
46        let funct3 = ((instruction >> 12) & 0b111) as u8;
47        let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
48        let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
49        let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
50        let funct12 = ((instruction >> 20) & 0xfff) as u16;
51
52        match opcode {
53            // OP-IMM (I-type): brev8, zip, unzip
54            0b001_0011 => {
55                let rd = Reg::from_bits(rd_bits)?;
56                let rs1 = Reg::from_bits(rs1_bits)?;
57                match funct3 {
58                    // brev8: funct12 = 0b011010000111 = 0x687
59                    0b101 if funct12 == 0b0110_1000_0111 => Some(Self::Brev8 { rd, rs1 }),
60                    // zip:   funct7=0000100, rs2=01111, funct3=001 -> funct12=0x08F
61                    0b001 if funct12 == 0b0000_1000_1111 => Some(Self::Zip { rd, rs1 }),
62                    // unzip: funct7=0000100, rs2=01111, funct3=101 -> funct12=0x08F
63                    0b101 if funct12 == 0b0000_1000_1111 => Some(Self::Unzip { rd, rs1 }),
64                    _ => None,
65                }
66            }
67            // OP (R-type): pack, packh
68            0b011_0011 => {
69                let rd = Reg::from_bits(rd_bits)?;
70                let rs1 = Reg::from_bits(rs1_bits)?;
71                let rs2 = Reg::from_bits(rs2_bits)?;
72                match (funct3, funct7, rs2_bits) {
73                    // pack: funct3=100, funct7=0000100, rs2!=0
74                    // rs2=0 collides with inherited RV32 Zbb zext.h and must fall through.
75                    (0b100, 0b000_0100, rs2_bits) if rs2_bits != 0 => {
76                        Some(Self::Pack { rd, rs1, rs2 })
77                    }
78                    // packh: funct3=111, funct7=0000100
79                    (0b111, 0b000_0100, _) => Some(Self::Packh { rd, rs1, rs2 }),
80                    _ => None,
81                }
82            }
83            _ => None,
84        }
85    }
86
87    #[inline(always)]
88    fn alignment() -> u8 {
89        align_of::<u32>() as u8
90    }
91
92    #[inline(always)]
93    fn size(&self) -> u8 {
94        size_of::<u32>() as u8
95    }
96}
97
98#[instruction]
99impl<Reg> fmt::Display for Rv32ZbkbInstruction<Reg>
100where
101    Reg: fmt::Display + Copy,
102{
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        match self {
105            Self::Pack { rd, rs1, rs2 } => write!(f, "pack {rd}, {rs1}, {rs2}"),
106            Self::Packh { rd, rs1, rs2 } => write!(f, "packh {rd}, {rs1}, {rs2}"),
107            Self::Brev8 { rd, rs1 } => write!(f, "brev8 {rd}, {rs1}"),
108            Self::Zip { rd, rs1 } => write!(f, "zip {rd}, {rs1}"),
109            Self::Unzip { rd, rs1 } => write!(f, "unzip {rd}, {rs1}"),
110        }
111    }
112}