| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#![no_std] |
| 5 |
|
| 6 |
#[cfg(test)] |
| 7 |
mod tests; |
| 8 |
|
| 9 |
use core::ops::{Bound, Range, RangeBounds}; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
pub trait BitField { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const BIT_LENGTH: usize; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
fn get_bit(&self, bit: usize) -> bool; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
fn set_bits<T: RangeBounds<usize>>(&mut self, range: T, value: Self) -> &mut Self; |
| 109 |
} |
| 110 |
|
| 111 |
pub trait BitArray<T: BitField> { |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
fn bit_length(&self) -> usize; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
fn get_bit(&self, bit: usize) -> bool; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
fn get_bits<U: RangeBounds<usize>>(&self, range: U) -> T; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
fn set_bit(&mut self, bit: usize, value: bool); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
fn set_bits<U: RangeBounds<usize>>(&mut self, range: U, value: T); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
macro_rules! bitfield_numeric_impl { |
| 210 |
($($t:ty)*) => ($( |
| 211 |
impl BitField for $t { |
| 212 |
const BIT_LENGTH: usize = ::core::mem::size_of::<Self>() as usize * 8; |
| 213 |
|
| 214 |
#[track_caller] |
| 215 |
#[inline] |
| 216 |
fn get_bit(&self, bit: usize) -> bool { |
| 217 |
assert!(bit < Self::BIT_LENGTH); |
| 218 |
|
| 219 |
(*self & (1 << bit)) != 0 |
| 220 |
} |
| 221 |
|
| 222 |
#[track_caller] |
| 223 |
#[inline] |
| 224 |
fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self { |
| 225 |
let range = to_regular_range(&range, Self::BIT_LENGTH); |
| 226 |
|
| 227 |
assert!(range.start < Self::BIT_LENGTH); |
| 228 |
assert!(range.end <= Self::BIT_LENGTH); |
| 229 |
assert!(range.start <= range.end); |
| 230 |
|
| 231 |
if range.start == range.end { |
| 232 |
0 |
| 233 |
} else { |
| 234 |
|
| 235 |
let bits = *self << (Self::BIT_LENGTH - range.end) >> (Self::BIT_LENGTH - range.end); |
| 236 |
|
| 237 |
|
| 238 |
bits >> range.start |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
#[track_caller] |
| 243 |
#[inline] |
| 244 |
fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self { |
| 245 |
assert!(bit < Self::BIT_LENGTH); |
| 246 |
|
| 247 |
if value { |
| 248 |
*self |= 1 << bit; |
| 249 |
} else { |
| 250 |
*self &= !(1 << bit); |
| 251 |
} |
| 252 |
|
| 253 |
self |
| 254 |
} |
| 255 |
|
| 256 |
#[track_caller] |
| 257 |
#[inline] |
| 258 |
fn set_bits<T: RangeBounds<usize>>(&mut self, range: T, value: Self) -> &mut Self { |
| 259 |
let range = to_regular_range(&range, Self::BIT_LENGTH); |
| 260 |
|
| 261 |
assert!(range.start < Self::BIT_LENGTH); |
| 262 |
assert!(range.end <= Self::BIT_LENGTH); |
| 263 |
assert!(range.start <= range.end); |
| 264 |
assert!(range.start == range.end && value == 0 || |
| 265 |
value << (Self::BIT_LENGTH - (range.end - range.start)) >> |
| 266 |
(Self::BIT_LENGTH - (range.end - range.start)) == value, |
| 267 |
"value does not fit into bit range"); |
| 268 |
if range.start != range.end { |
| 269 |
let bitmask: Self = !(!0 << (Self::BIT_LENGTH - range.end) >> |
| 270 |
(Self::BIT_LENGTH - range.end) >> |
| 271 |
range.start << range.start); |
| 272 |
|
| 273 |
|
| 274 |
*self = (*self & bitmask) | (value << range.start); |
| 275 |
} |
| 276 |
self |
| 277 |
} |
| 278 |
} |
| 279 |
)*) |
| 280 |
} |
| 281 |
|
| 282 |
bitfield_numeric_impl! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } |
| 283 |
|
| 284 |
impl<T: BitField> BitArray<T> for [T] { |
| 285 |
#[inline] |
| 286 |
fn bit_length(&self) -> usize { |
| 287 |
self.len() * T::BIT_LENGTH |
| 288 |
} |
| 289 |
|
| 290 |
#[track_caller] |
| 291 |
#[inline] |
| 292 |
fn get_bit(&self, bit: usize) -> bool { |
| 293 |
let slice_index = bit / T::BIT_LENGTH; |
| 294 |
let bit_index = bit % T::BIT_LENGTH; |
| 295 |
self[slice_index].get_bit(bit_index) |
| 296 |
} |
| 297 |
|
| 298 |
#[track_caller] |
| 299 |
#[inline] |
| 300 |
fn get_bits<U: RangeBounds<usize>>(&self, range: U) -> T { |
| 301 |
let range = to_regular_range(&range, self.bit_length()); |
| 302 |
|
| 303 |
assert!(range.len() <= T::BIT_LENGTH); |
| 304 |
|
| 305 |
let slice_start = range.start / T::BIT_LENGTH; |
| 306 |
let slice_end = range.end / T::BIT_LENGTH; |
| 307 |
let bit_start = range.start % T::BIT_LENGTH; |
| 308 |
let bit_end = range.end % T::BIT_LENGTH; |
| 309 |
let len = range.len(); |
| 310 |
|
| 311 |
assert!(slice_end - slice_start <= 1); |
| 312 |
|
| 313 |
if slice_start == slice_end { |
| 314 |
self[slice_start].get_bits(bit_start..bit_end) |
| 315 |
} else if bit_end == 0 { |
| 316 |
self[slice_start].get_bits(bit_start..T::BIT_LENGTH) |
| 317 |
} else { |
| 318 |
let mut ret = self[slice_start].get_bits(bit_start..T::BIT_LENGTH); |
| 319 |
ret.set_bits( |
| 320 |
(T::BIT_LENGTH - bit_start)..len, |
| 321 |
self[slice_end].get_bits(0..bit_end), |
| 322 |
); |
| 323 |
ret |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#[track_caller] |
| 328 |
#[inline] |
| 329 |
fn set_bit(&mut self, bit: usize, value: bool) { |
| 330 |
let slice_index = bit / T::BIT_LENGTH; |
| 331 |
let bit_index = bit % T::BIT_LENGTH; |
| 332 |
self[slice_index].set_bit(bit_index, value); |
| 333 |
} |
| 334 |
|
| 335 |
#[track_caller] |
| 336 |
#[inline] |
| 337 |
fn set_bits<U: RangeBounds<usize>>(&mut self, range: U, value: T) { |
| 338 |
let range = to_regular_range(&range, self.bit_length()); |
| 339 |
|
| 340 |
assert!(range.len() <= T::BIT_LENGTH); |
| 341 |
|
| 342 |
let slice_start = range.start / T::BIT_LENGTH; |
| 343 |
let slice_end = range.end / T::BIT_LENGTH; |
| 344 |
let bit_start = range.start % T::BIT_LENGTH; |
| 345 |
let bit_end = range.end % T::BIT_LENGTH; |
| 346 |
|
| 347 |
assert!(slice_end - slice_start <= 1); |
| 348 |
|
| 349 |
if slice_start == slice_end { |
| 350 |
self[slice_start].set_bits(bit_start..bit_end, value); |
| 351 |
} else if bit_end == 0 { |
| 352 |
self[slice_start].set_bits(bit_start..T::BIT_LENGTH, value); |
| 353 |
} else { |
| 354 |
self[slice_start].set_bits( |
| 355 |
bit_start..T::BIT_LENGTH, |
| 356 |
value.get_bits(0..T::BIT_LENGTH - bit_start), |
| 357 |
); |
| 358 |
self[slice_end].set_bits( |
| 359 |
0..bit_end, |
| 360 |
value.get_bits(T::BIT_LENGTH - bit_start..T::BIT_LENGTH), |
| 361 |
); |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
#[inline] |
| 367 |
fn to_regular_range<T: RangeBounds<usize>>(generic_rage: &T, bit_length: usize) -> Range<usize> { |
| 368 |
let start = match generic_rage.start_bound() { |
| 369 |
Bound::Excluded(&value) => value + 1, |
| 370 |
Bound::Included(&value) => value, |
| 371 |
Bound::Unbounded => 0, |
| 372 |
}; |
| 373 |
let end = match generic_rage.end_bound() { |
| 374 |
Bound::Excluded(&value) => value, |
| 375 |
Bound::Included(&value) => value + 1, |
| 376 |
Bound::Unbounded => bit_length, |
| 377 |
}; |
| 378 |
|
| 379 |
start..end |
| 380 |
} |