|
| 1 | +//! # Bit Manipulation and Machine Integer Utilities |
| 2 | +//! |
| 3 | +//! This module provides utilities for working with individual bits and machine integer types. |
| 4 | +//! It defines a [`Bit`] enum to represent a single bit (`0` or `1`) along with convenient |
| 5 | +//! conversion implementations between `Bit`, [`bool`], and various primitive integer types. |
| 6 | +//! |
| 7 | +//! In addition, the module introduces the [`MachineInteger`] trait which abstracts over |
| 8 | +//! integer types, providing associated constants: |
| 9 | +//! |
| 10 | +//! - `BITS`: The size of the integer type in bits. |
| 11 | +//! - `SIGNED`: A flag indicating whether the type is signed. |
| 12 | +//! |
| 13 | +//! The [`Bit`] type includes methods for extracting the value of a specific bit from an integer. |
| 14 | +//! For example, [`Bit::of_int`] returns the bit at a given position for a provided integer, |
| 15 | +//! handling both positive and negative values (assuming a two's complement representation). |
| 16 | +//! |
| 17 | +//! # Examples |
| 18 | +//! |
| 19 | +//! ```rust |
| 20 | +//! use testable_simd_models::abstractions::bit::{Bit, MachineInteger}; |
| 21 | +//! |
| 22 | +//! // Extract the 3rd bit (0-indexed) from an integer. |
| 23 | +//! let bit = Bit::of_int(42, 2); |
| 24 | +//! println!("The extracted bit is: {:?}", bit); |
| 25 | +//! |
| 26 | +//! // Convert Bit to a primitive integer type. |
| 27 | +//! let num: u8 = bit.into(); |
| 28 | +//! println!("As an integer: {}", num); |
| 29 | +//! ``` |
| 30 | +//! |
| 31 | +//! [`bool`]: https://doc.rust-lang.org/std/primitive.bool.html |
| 32 | +//! [`Bit::of_int`]: enum.Bit.html#method.of_int |
| 33 | +
|
| 34 | +/// Represent a bit: `0` or `1`. |
| 35 | +#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 36 | +pub enum Bit { |
| 37 | + Zero, |
| 38 | + One, |
| 39 | +} |
| 40 | +impl std::ops::BitAnd for Bit { |
| 41 | + type Output = Self; |
| 42 | + fn bitand(self, rhs: Self) -> Self { |
| 43 | + match self { |
| 44 | + Bit::Zero => Bit::Zero, |
| 45 | + Bit::One => rhs, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl std::ops::BitOr for Bit { |
| 51 | + type Output = Self; |
| 52 | + fn bitor(self, rhs: Self) -> Self { |
| 53 | + match self { |
| 54 | + Bit::Zero => rhs, |
| 55 | + Bit::One => Bit::One, |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl std::ops::BitXor for Bit { |
| 61 | + type Output = Self; |
| 62 | + fn bitxor(self, rhs: Self) -> Self { |
| 63 | + match (self, rhs) { |
| 64 | + (Bit::Zero, Bit::Zero) => Bit::Zero, |
| 65 | + (Bit::One, Bit::One) => Bit::Zero, |
| 66 | + _ => Bit::One, |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl std::ops::Neg for Bit { |
| 72 | + type Output = Self; |
| 73 | + fn neg(self) -> Self { |
| 74 | + match self { |
| 75 | + Bit::One => Bit::Zero, |
| 76 | + Bit::Zero => Bit::One, |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +macro_rules! generate_from_bit_impls { |
| 81 | + ($($ty:ident),*) => { |
| 82 | + $(impl From<Bit> for $ty { |
| 83 | + fn from(bit: Bit) -> Self { |
| 84 | + bool::from(bit) as $ty |
| 85 | + } |
| 86 | + })* |
| 87 | + }; |
| 88 | +} |
| 89 | +generate_from_bit_impls!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); |
| 90 | + |
| 91 | +impl From<Bit> for bool { |
| 92 | + fn from(bit: Bit) -> Self { |
| 93 | + match bit { |
| 94 | + Bit::Zero => false, |
| 95 | + Bit::One => true, |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl From<bool> for Bit { |
| 101 | + fn from(b: bool) -> Bit { |
| 102 | + match b { |
| 103 | + false => Bit::Zero, |
| 104 | + true => Bit::One, |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// A trait for types that represent machine integers. |
| 110 | +pub trait MachineInteger { |
| 111 | + /// The size of this integer type in bits. |
| 112 | + fn bits() -> u32; |
| 113 | + |
| 114 | + /// The signedness of this integer type. |
| 115 | + const SIGNED: bool; |
| 116 | + /// Element of the integer type with every bit as 0. |
| 117 | + const ZEROS: Self; |
| 118 | + /// Element of the integer type with every bit as 1. |
| 119 | + const ONES: Self; |
| 120 | + /// Minimum value of the integer type. |
| 121 | + const MIN: Self; |
| 122 | + /// Maximum value of the integer type. |
| 123 | + const MAX: Self; |
| 124 | + |
| 125 | + /// Implements functionality for `simd_add` in `crate::abstractions::simd`. |
| 126 | + fn wrapping_add(self, rhs: Self) -> Self; |
| 127 | + /// Implements functionality for `simd_sub` in `crate::abstractions::simd`. |
| 128 | + fn wrapping_sub(self, rhs: Self) -> Self; |
| 129 | + /// Implements functionality for `simd_mul` in `crate::abstractions::simd`. |
| 130 | + fn overflowing_mul(self, rhs: Self) -> Self; |
| 131 | + /// Implements functionality for `simd_saturating_add` in `crate::abstractions::simd`. |
| 132 | + fn saturating_add(self, rhs: Self) -> Self; |
| 133 | + /// Implements functionality for `simd_saturating_sub` in `crate::abstractions::simd`. |
| 134 | + fn saturating_sub(self, rhs: Self) -> Self; |
| 135 | + /// Implements functionality for `simd_abs_diff` in `crate::abstractions::simd`. |
| 136 | + fn absolute_diff(self, rhs: Self) -> Self; |
| 137 | + /// Implements functionality for `simd_abs` in `crate::abstractions::simd`. |
| 138 | + fn absolute_val(self) -> Self; |
| 139 | +} |
| 140 | + |
| 141 | +macro_rules! generate_imachine_integer_impls { |
| 142 | + ($($ty:ident),*) => { |
| 143 | + $( |
| 144 | + impl MachineInteger for $ty { |
| 145 | + const SIGNED: bool = true; |
| 146 | + const ZEROS: $ty = 0; |
| 147 | + const ONES: $ty = -1; |
| 148 | + const MIN: $ty = $ty::MIN; |
| 149 | + const MAX: $ty = $ty::MAX; |
| 150 | + fn bits() -> u32 { $ty::BITS } |
| 151 | + fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } |
| 152 | + fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } |
| 153 | + fn overflowing_mul(self, rhs: Self) -> Self { self.overflowing_mul(rhs).0 } |
| 154 | + fn saturating_add(self, rhs: Self) -> Self { self.saturating_add(rhs)} |
| 155 | + fn saturating_sub(self, rhs: Self) -> Self { self.saturating_sub(rhs) } |
| 156 | + fn absolute_diff(self, rhs: Self) -> Self {if self > rhs {$ty::wrapping_sub(self, rhs)} else {$ty::wrapping_sub(rhs, self)}} |
| 157 | + fn absolute_val(self) -> Self {if self == $ty::MIN {self} else {self.abs()}} |
| 158 | + })* |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +macro_rules! generate_umachine_integer_impls { |
| 163 | + ($($ty:ident),*) => { |
| 164 | + $( |
| 165 | + impl MachineInteger for $ty { |
| 166 | + const SIGNED: bool = false; |
| 167 | + const ZEROS: $ty = 0; |
| 168 | + const ONES: $ty = $ty::MAX; |
| 169 | + const MIN: $ty = $ty::MIN; |
| 170 | + const MAX: $ty = $ty::MAX; |
| 171 | + |
| 172 | + |
| 173 | + fn bits() -> u32 { $ty::BITS } |
| 174 | + fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } |
| 175 | + fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } |
| 176 | + fn overflowing_mul(self, rhs: Self) -> Self { self.overflowing_mul(rhs).0 } |
| 177 | + fn saturating_add(self, rhs: Self) -> Self { self.saturating_add(rhs)} |
| 178 | + fn saturating_sub(self, rhs: Self) -> Self { self.saturating_sub(rhs)} |
| 179 | + fn absolute_diff(self, rhs: Self) -> Self {if self > rhs {self - rhs} else {rhs - self}} |
| 180 | + fn absolute_val(self) -> Self {self} |
| 181 | + })* |
| 182 | + }; |
| 183 | +} |
| 184 | +generate_imachine_integer_impls!(i8, i16, i32, i64, i128); |
| 185 | +generate_umachine_integer_impls!(u8, u16, u32, u64, u128); |
| 186 | + |
| 187 | +impl Bit { |
| 188 | + fn of_raw_int(x: u128, nth: u32) -> Self { |
| 189 | + if x / 2u128.pow(nth) % 2 == 1 { |
| 190 | + Self::One |
| 191 | + } else { |
| 192 | + Self::Zero |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + pub fn of_int<T: Into<i128> + MachineInteger>(x: T, nth: u32) -> Bit { |
| 197 | + let x: i128 = x.into(); |
| 198 | + if x >= 0 { |
| 199 | + Self::of_raw_int(x as u128, nth) |
| 200 | + } else { |
| 201 | + Self::of_raw_int((2i128.pow(T::bits()) + x) as u128, nth) |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments