Debian Codegraph @

src/nouveau/rust/bitview/lib.rs

raw
1 /*
2 * Copyright © 2022 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 use std::ops::Range;
7
8 pub trait BitViewable {
9 fn bits(&self) -> usize;
10
11 fn get_bit_range_u64(&self, range: Range<usize>) -> u64;
12 }
13
14 pub trait BitMutViewable: BitViewable {
15 fn set_bit_range_u64(&mut self, range: Range<usize>, val: u64);
16 }
17
18 fn u64_mask_for_bits(bits: usize) -> u64 {
19 assert!(bits > 0 && bits <= 64);
20 !0u64 >> (64 - bits)
21 }
22
23 macro_rules! decl_bit_set_viewable_for_uint {
24 ($ty: ty) => {
25 impl BitViewable for $ty {
26 fn bits(&self) -> usize {
27 <$ty>::BITS as usize
28 }
29
30 fn get_bit_range_u64(&self, range: Range<usize>) -> u64 {
31 assert!(!range.is_empty());
32 assert!(range.end <= self.bits());
33
34 let mask = <$ty>::MAX >> (self.bits() - range.len());
35 ((self >> range.start) & mask).into()
36 }
37 }
38
39 impl BitMutViewable for $ty {
40 fn set_bit_range_u64(&mut self, range: Range<usize>, val: u64) {
41 assert!(!range.is_empty());
42 assert!(range.end <= self.bits());
43
44 let mask = <$ty>::MAX >> (self.bits() - range.len());
45
46 assert!((val & u64::from(mask)) == val);
47 let val = val as $ty;
48
49 *self = (*self & !(mask << range.start)) | (val << range.start);
50 }
51 }
52
53 impl BitViewable for [$ty] {
54 fn bits(&self) -> usize {
55 self.len() * (<$ty>::BITS as usize)
56 }
57
58 fn get_bit_range_u64(&self, range: Range<usize>) -> u64 {
59 assert!(!range.is_empty());
60 assert!(range.end <= self.bits());
61
62 let mask = u64_mask_for_bits(range.len());
63
64 let bits = <$ty>::BITS as usize;
65 let c0_idx = range.start / bits;
66 let c0_start = range.start % bits;
67 let chunks = (c0_start + range.len()).div_ceil(bits);
68
69 let mut val = 0_u64;
70 for i in 0..chunks {
71 let chunk = u64::from(self[c0_idx + i]);
72 if i == 0 {
73 val |= chunk >> c0_start;
74 } else {
75 val |= chunk << (i * bits) - c0_start;
76 };
77 }
78 val & mask
79 }
80 }
81
82 impl BitMutViewable for [$ty] {
83 fn set_bit_range_u64(&mut self, range: Range<usize>, val: u64) {
84 assert!(!range.is_empty());
85 assert!(range.end <= self.bits());
86
87 let mask = u64_mask_for_bits(range.len());
88 assert!((val & u64::from(mask)) == val);
89
90 let bits = <$ty>::BITS as usize;
91 let c0_idx = range.start / bits;
92 let c0_start = range.start % bits;
93 let chunks = (c0_start + range.len()).div_ceil(bits);
94
95 for i in 0..chunks {
96 let chunk = &mut self[c0_idx + i];
97 if i == 0 {
98 *chunk &= !((mask << c0_start) as $ty);
99 *chunk |= (val << c0_start) as $ty;
100 } else {
101 let shift = (i * bits) - c0_start;
102 *chunk &= !((mask >> shift) as $ty);
103 *chunk |= (val >> shift) as $ty;
104 }
105 }
106 }
107 }
108
109 impl<const N: usize> BitViewable for [$ty; N] {
110 fn bits(&self) -> usize {
111 N * (<$ty>::BITS as usize)
112 }
113
114 fn get_bit_range_u64(&self, range: Range<usize>) -> u64 {
115 self[..].get_bit_range_u64(range)
116 }
117 }
118
119 impl<const N: usize> BitMutViewable for [$ty; N] {
120 fn set_bit_range_u64(&mut self, range: Range<usize>, val: u64) {
121 self[..].set_bit_range_u64(range, val);
122 }
123 }
124 };
125 }
126
127 decl_bit_set_viewable_for_uint!(u8);
128 decl_bit_set_viewable_for_uint!(u16);
129 decl_bit_set_viewable_for_uint!(u32);
130 decl_bit_set_viewable_for_uint!(u64);
131
132 pub struct BitView<'a, BS: BitViewable + ?Sized> {
133 parent: &'a BS,
134 range: Range<usize>,
135 }
136
137 impl<'a, BS: BitViewable + ?Sized> BitView<'a, BS> {
138 pub fn new(parent: &'a BS) -> Self {
139 let len = parent.bits();
140 Self {
141 parent: parent,
142 range: 0..len,
143 }
144 }
145
146 pub fn new_subset(parent: &'a BS, range: Range<usize>) -> Self {
147 assert!(range.end <= parent.bits());
148 Self {
149 parent: parent,
150 range: range,
151 }
152 }
153
154 pub fn subset(
155 &'a self,
156 range: Range<usize>,
157 ) -> BitView<'a, BitView<'a, BS>> {
158 BitView::new_subset(self, range)
159 }
160
161 fn range_in_parent(&self, range: Range<usize>) -> Range<usize> {
162 let new_start = self.range.start + range.start;
163 let new_end = self.range.start + range.end;
164 assert!(new_end <= self.range.end);
165 new_start..new_end
166 }
167
168 pub fn get_bit(&self, bit: usize) -> bool {
169 self.get_bit_range_u64(bit..(bit + 1)) != 0
170 }
171 }
172
173 impl<'a, BS: BitViewable + ?Sized> BitViewable for BitView<'a, BS> {
174 fn bits(&self) -> usize {
175 self.range.end - self.range.start
176 }
177
178 fn get_bit_range_u64(&self, range: Range<usize>) -> u64 {
179 self.parent.get_bit_range_u64(self.range_in_parent(range))
180 }
181 }
182
183 pub struct BitMutView<'a, BS: BitMutViewable + ?Sized> {
184 parent: &'a mut BS,
185 range: Range<usize>,
186 }
187
188 impl<'a, BS: BitMutViewable + ?Sized> BitMutView<'a, BS> {
189 pub fn new(parent: &'a mut BS) -> Self {
190 let len = parent.bits();
191 Self {
192 parent: parent,
193 range: 0..len,
194 }
195 }
196
197 pub fn new_subset(parent: &'a mut BS, range: Range<usize>) -> Self {
198 assert!(range.end <= parent.bits());
199 Self {
200 parent: parent,
201 range: range,
202 }
203 }
204
205 pub fn subset_mut<'b>(
206 &'b mut self,
207 range: Range<usize>,
208 ) -> BitMutView<'b, BitMutView<'a, BS>> {
209 BitMutView::new_subset(self, range)
210 }
211
212 fn range_in_parent(&self, range: Range<usize>) -> Range<usize> {
213 let new_start = self.range.start + range.start;
214 let new_end = self.range.start + range.end;
215 assert!(new_end <= self.range.end);
216 new_start..new_end
217 }
218
219 pub fn get_bit(&self, bit: usize) -> bool {
220 self.get_bit_range_u64(bit..(bit + 1)) != 0
221 }
222
223 pub fn set_bit(&mut self, bit: usize, val: bool) {
224 self.set_bit_range_u64(bit..(bit + 1), u64::from(val));
225 }
226 }
227
228 impl<'a, BS: BitMutViewable + ?Sized> BitViewable for BitMutView<'a, BS> {
229 fn bits(&self) -> usize {
230 self.range.end - self.range.start
231 }
232
233 fn get_bit_range_u64(&self, range: Range<usize>) -> u64 {
234 self.parent.get_bit_range_u64(self.range_in_parent(range))
235 }
236 }
237
238 impl<'a, BS: BitMutViewable + ?Sized> BitMutViewable for BitMutView<'a, BS> {
239 fn set_bit_range_u64(&mut self, range: Range<usize>, val: u64) {
240 self.parent
241 .set_bit_range_u64(self.range_in_parent(range), val);
242 }
243 }
244
245 pub trait SetFieldU64 {
246 fn set_field_u64(&mut self, range: Range<usize>, val: u64);
247 }
248
249 impl<BS: BitMutViewable> SetFieldU64 for BS {
250 fn set_field_u64(&mut self, range: Range<usize>, val: u64) {
251 let bits = range.end - range.start;
252
253 /* Check that it fits in the bitfield */
254 assert!((val & u64_mask_for_bits(bits)) == val);
255
256 self.set_bit_range_u64(range, val);
257 }
258 }
259
260 pub trait SetField<F> {
261 fn set_field(&mut self, range: Range<usize>, val: F);
262 }
263
264 pub trait ToFieldBits {
265 fn to_field_bits(self, bits: usize) -> u64;
266 }
267
268 impl<T: SetFieldU64, F: ToFieldBits> SetField<F> for T {
269 fn set_field(&mut self, range: Range<usize>, val: F) {
270 let bits = range.len();
271 self.set_field_u64(range, val.to_field_bits(bits));
272 }
273 }
274
275 pub trait SetField2<F> {
276 fn set_field2(
277 &mut self,
278 range1: Range<usize>,
279 range2: Range<usize>,
280 val: F,
281 );
282 }
283
284 impl<T: SetFieldU64, F: ToFieldBits> SetField2<F> for T {
285 fn set_field2(
286 &mut self,
287 range1: Range<usize>,
288 range2: Range<usize>,
289 val: F,
290 ) {
291 let bits1 = range1.len();
292 let bits2 = range2.len();
293 let val = val.to_field_bits(bits1 + bits2);
294 self.set_field_u64(range1, val & u64_mask_for_bits(bits1));
295 self.set_field_u64(range2, val >> bits1);
296 }
297 }
298
299 macro_rules! impl_to_field_bits_for_uN {
300 ($typ: ident) => {
301 impl ToFieldBits for $typ {
302 fn to_field_bits(self, bits: usize) -> u64 {
303 let val = u64::from(self);
304 assert!((val & u64_mask_for_bits(bits)) == val);
305 val
306 }
307 }
308 };
309 }
310
311 impl_to_field_bits_for_uN!(u8);
312 impl_to_field_bits_for_uN!(u16);
313 impl_to_field_bits_for_uN!(u32);
314 impl_to_field_bits_for_uN!(u64);
315
316 macro_rules! impl_to_field_bits_for_iN {
317 ($typ: ident) => {
318 impl ToFieldBits for $typ {
319 fn to_field_bits(self, bits: usize) -> u64 {
320 let mask = u64_mask_for_bits(bits);
321
322 // It's easier to work with a u64
323 let val = i64::from(self) as u64;
324
325 // Check that it fits in the bitfield, taking sign into account
326 let sign_mask = !(mask >> 1);
327 assert!(
328 (val & sign_mask) == 0 || (val & sign_mask) == sign_mask
329 );
330
331 val & mask
332 }
333 }
334 };
335 }
336
337 impl_to_field_bits_for_iN!(i8);
338 impl_to_field_bits_for_iN!(i16);
339 impl_to_field_bits_for_iN!(i32);
340 impl_to_field_bits_for_iN!(i64);
341
342 impl ToFieldBits for bool {
343 fn to_field_bits(self, bits: usize) -> u64 {
344 assert!(bits == 1);
345 u64::from(self)
346 }
347 }
348
349 impl ToFieldBits for f32 {
350 fn to_field_bits(self, bits: usize) -> u64 {
351 assert!(bits == 32);
352 u64::from(self.to_bits())
353 }
354 }
355
356 pub trait SetBit {
357 fn set_bit(&mut self, bit: usize, val: bool);
358 }
359
360 impl<T: SetFieldU64> SetBit for T {
361 fn set_bit(&mut self, bit: usize, val: bool) {
362 self.set_field(bit..(bit + 1), val);
363 }
364 }