ab_riscv_primitives/registers/
vector.rs1use crate::instructions::v::VRegGroupSize;
4use core::fmt;
5
6#[derive(Clone, Copy)]
8#[derive_const(PartialEq, Eq)]
9#[repr(u8)]
10pub enum VReg {
11 V0 = 0,
13 V1 = 1,
15 V2 = 2,
17 V3 = 3,
19 V4 = 4,
21 V5 = 5,
23 V6 = 6,
25 V7 = 7,
27 V8 = 8,
29 V9 = 9,
31 V10 = 10,
33 V11 = 11,
35 V12 = 12,
37 V13 = 13,
39 V14 = 14,
41 V15 = 15,
43 V16 = 16,
45 V17 = 17,
47 V18 = 18,
49 V19 = 19,
51 V20 = 20,
53 V21 = 21,
55 V22 = 22,
57 V23 = 23,
59 V24 = 24,
61 V25 = 25,
63 V26 = 26,
65 V27 = 27,
67 V28 = 28,
69 V29 = 29,
71 V30 = 30,
73 V31 = 31,
75}
76
77impl VReg {
78 #[inline(always)]
80 pub const fn from_bits(bits: u8) -> Option<Self> {
81 match bits {
82 0 => Some(Self::V0),
83 1 => Some(Self::V1),
84 2 => Some(Self::V2),
85 3 => Some(Self::V3),
86 4 => Some(Self::V4),
87 5 => Some(Self::V5),
88 6 => Some(Self::V6),
89 7 => Some(Self::V7),
90 8 => Some(Self::V8),
91 9 => Some(Self::V9),
92 10 => Some(Self::V10),
93 11 => Some(Self::V11),
94 12 => Some(Self::V12),
95 13 => Some(Self::V13),
96 14 => Some(Self::V14),
97 15 => Some(Self::V15),
98 16 => Some(Self::V16),
99 17 => Some(Self::V17),
100 18 => Some(Self::V18),
101 19 => Some(Self::V19),
102 20 => Some(Self::V20),
103 21 => Some(Self::V21),
104 22 => Some(Self::V22),
105 23 => Some(Self::V23),
106 24 => Some(Self::V24),
107 25 => Some(Self::V25),
108 26 => Some(Self::V26),
109 27 => Some(Self::V27),
110 28 => Some(Self::V28),
111 29 => Some(Self::V29),
112 30 => Some(Self::V30),
113 31 => Some(Self::V31),
114 _ => None,
115 }
116 }
117
118 #[inline(always)]
120 pub const fn to_bits(self) -> u8 {
121 self as u8
122 }
123
124 #[inline(always)]
128 pub const fn is_group_aligned(self, group: VRegGroupSize) -> bool {
129 self.to_bits().is_multiple_of(group.get())
130 }
131}
132
133impl fmt::Display for VReg {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 write!(f, "v{}", *self as u8)
136 }
137}
138
139impl fmt::Debug for VReg {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 fmt::Display::fmt(self, f)
142 }
143}
144
145#[derive(Debug, Clone, Copy)]
148#[derive_const(PartialEq, Eq)]
149pub enum VectorCsr {
150 Vstart,
152 Vxsat,
154 Vxrm,
156 Vcsr,
158 Vl,
160 Vtype,
162 Vlenb,
164}
165
166impl VectorCsr {
167 #[inline(always)]
169 pub const fn from_csr_index(index: u16) -> Option<Self> {
170 match index {
171 0x008 => Some(Self::Vstart),
172 0x009 => Some(Self::Vxsat),
173 0x00A => Some(Self::Vxrm),
174 0x00F => Some(Self::Vcsr),
175 0xC20 => Some(Self::Vl),
176 0xC21 => Some(Self::Vtype),
177 0xC22 => Some(Self::Vlenb),
178 _ => None,
179 }
180 }
181
182 #[inline(always)]
184 pub const fn to_csr_index(self) -> u16 {
185 match self {
186 Self::Vstart => 0x008,
187 Self::Vxsat => 0x009,
188 Self::Vxrm => 0x00A,
189 Self::Vcsr => 0x00F,
190 Self::Vl => 0xC20,
191 Self::Vtype => 0xC21,
192 Self::Vlenb => 0xC22,
193 }
194 }
195}