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 #[inline(always)]
137 pub const fn is_mask(self, vm: bool) -> bool {
138 !vm && self == Self::V0
139 }
140}
141
142impl fmt::Display for VReg {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 write!(f, "v{}", *self as u8)
145 }
146}
147
148impl fmt::Debug for VReg {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 fmt::Display::fmt(self, f)
151 }
152}
153
154#[derive(Debug, Clone, Copy)]
157#[derive_const(PartialEq, Eq)]
158pub enum VectorCsr {
159 Vstart,
161 Vxsat,
163 Vxrm,
165 Vcsr,
167 Vl,
169 Vtype,
171 Vlenb,
173}
174
175impl VectorCsr {
176 #[inline(always)]
178 pub const fn from_csr_index(index: u16) -> Option<Self> {
179 match index {
180 0x008 => Some(Self::Vstart),
181 0x009 => Some(Self::Vxsat),
182 0x00A => Some(Self::Vxrm),
183 0x00F => Some(Self::Vcsr),
184 0xC20 => Some(Self::Vl),
185 0xC21 => Some(Self::Vtype),
186 0xC22 => Some(Self::Vlenb),
187 _ => None,
188 }
189 }
190
191 #[inline(always)]
193 pub const fn to_csr_index(self) -> u16 {
194 match self {
195 Self::Vstart => 0x008,
196 Self::Vxsat => 0x009,
197 Self::Vxrm => 0x00A,
198 Self::Vcsr => 0x00F,
199 Self::Vl => 0xC20,
200 Self::Vtype => 0xC21,
201 Self::Vlenb => 0xC22,
202 }
203 }
204}