1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
use super::*;
macro_rules! impl_bits {
($($t:ident, $wrapped:ty),*) => {
$(
paste! {
impl $t {
/// Returns the number of ones in the binary representation of `self`.
///
#[inline]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn count_ones(self) -> u32 {
self.0.count_ones()
}
/// Returns the number of zeros in the binary representation of `self`.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn count_zeros(self) -> u32 {
self.0.count_zeros()
}
/// Returns the number of trailing zeros in the binary representation of `self`.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn trailing_zeros(self) -> u32 {
self.0.trailing_zeros()
}
/// Reverses the byte order of the integer.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn swap_bytes(self) -> Self {
Self(self.0.swap_bytes())
}
/// Reverses the bit pattern of the integer.
///
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn reverse_bits(self) -> Self {
Self(self.0.reverse_bits())
}
/// Returns the number of leading zeros in the binary representation of `self`.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn leading_zeros(self) -> u32 {
self.0.leading_zeros()
}
/// Converts an integer from big endian to the target's endianness.
///
/// On big endian this is a no-op. On little endian the bytes are
/// swapped.
///
#[inline]
#[must_use]
pub const fn from_be(x: Self) -> Self {
if cfg!(target_endian = "big") {
x
} else {
Self(<$wrapped>::from_be(x.0))
}
}
/// Converts an integer from little endian to the target's endianness.
///
/// On little endian this is a no-op. On big endian the bytes are
/// swapped.
///
#[inline]
#[must_use]
pub const fn from_le(x: Self) -> Self {
if cfg!(target_endian = "big") {
Self(<$wrapped>::from_be(x.0))
} else {
x
}
}
/// Converts `self` to big endian from the target's endianness.
///
/// On big endian this is a no-op. On little endian the bytes are
/// swapped.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn to_be(self) -> Self {
if cfg!(target_endian = "big") {
self
} else {
Self(self.0.to_be())
}
}
/// Converts `self` to little endian from the target's endianness.
///
/// On little endian this is a no-op. On big endian the bytes are
/// swapped.
///
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn to_le(self) -> Self {
if cfg!(target_endian = "big") {
Self(self.0.to_le())
} else {
self
}
}
}
impl BitXor for $t {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
Self(self.0 ^ other.0)
}
}
impl BitXorAssign for $t {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.0 ^= other.0
}
}
impl BitOr for $t {
type Output = Self;
#[inline]
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitOrAssign for $t {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.0 |= other.0
}
}
impl BitAnd for $t {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
Self(self.0 & other.0)
}
}
impl BitAndAssign for $t {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.0 &= other.0
}
}
impl Shl<u32> for $t {
type Output = Self;
#[inline]
fn shl(self, other: u32) -> Self {
Self(self.0.checked_shl(other).expect("Overflow"))
}
}
impl ShlAssign<u32> for $t {
#[inline]
fn shl_assign(&mut self, other: u32) {
self.0 = self.0.checked_shl(other).expect("Overflow");
}
}
impl Shr<u32> for $t {
type Output = Self;
#[inline]
fn shr(self, other: u32) -> $t {
Self(self.0.checked_shr(other).expect("Overflow"))
}
}
impl ShrAssign<u32> for $t {
#[inline]
fn shr_assign(&mut self, other: u32) {
self.0 = self.0.checked_shr(other).expect("Overflow");
}
}
}
)*
}
}
impl_bits! { I192, BInt::<3> }
impl_bits! { I256, BInt::<4> }
impl_bits! { I320, BInt::<5> }
impl_bits! { I384, BInt::<6> }
impl_bits! { I448, BInt::<7> }
impl_bits! { I512, BInt::<8> }
impl_bits! { I768, BInt::<12> }
impl_bits! { U192, BUint::<3> }
impl_bits! { U256, BUint::<4> }
impl_bits! { U320, BUint::<5> }
impl_bits! { U384, BUint::<6> }
impl_bits! { U448, BUint::<7> }
impl_bits! { U512, BUint::<8> }
impl_bits! { U768, BUint::<12> }