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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::num::arithmetic::traits::{ModPowerOf2, UnsignedAbs};
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::conversion::traits::WrappingFrom;
use crate::num::logic::traits::{BitBlockAccess, LeadingZeros};
use core::cmp::min;
const ERROR_MESSAGE: &str = "Result exceeds width of output type";
fn get_bits_unsigned<T: PrimitiveUnsigned>(x: &T, start: u64, end: u64) -> T {
assert!(start <= end);
if start >= T::WIDTH {
T::ZERO
} else {
(*x >> start).mod_power_of_2(end - start)
}
}
fn assign_bits_unsigned<T: PrimitiveUnsigned>(x: &mut T, start: u64, end: u64, bits: &T) {
assert!(start <= end);
let width = T::WIDTH;
let bits_width = end - start;
let bits = bits.mod_power_of_2(bits_width);
if bits != T::ZERO && LeadingZeros::leading_zeros(bits) < start {
panic!("{}", ERROR_MESSAGE);
} else if start < width {
*x &= !(T::MAX.mod_power_of_2(min(bits_width, width - start)) << start);
*x |= bits << start;
}
}
macro_rules! impl_bit_block_access_unsigned {
($t:ident) => {
impl BitBlockAccess for $t {
type Bits = $t;
/// Extracts a block of adjacent bits from a number.
///
/// The first index is `start` and last index is `end - 1`.
///
/// The block of bits has the same type as the input. If `end` is greater than the
/// type's width, the high bits of the result are all 0.
///
/// Let
/// $$
/// n = \sum_{i=0}^\infty 2^{b_i},
/// $$
/// where for all $i$, $b_i\in \\{0, 1\\}$; so finitely many of the bits are 1, and the
/// rest are 0. Then
/// $$
/// f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Panics if `start < end`.
///
/// # Examples
/// See [here](super::bit_block_access#get_bits).
#[inline]
fn get_bits(&self, start: u64, end: u64) -> Self {
get_bits_unsigned(self, start, end)
}
/// Replaces a block of adjacent bits in a number with other bits.
///
/// The least-significant `end - start` bits of `bits` are assigned to bits `start`
/// through `end - 1`, inclusive, of `self`.
///
/// The block of bits has the same type as the input. If `bits` has fewer bits than `end
/// - start`, the high bits are interpreted as 0. If `end` is greater than the type's
/// width, the high bits of `bits` must be 0.
///
/// Let
/// $$
/// n = \sum_{i=0}^{W-1} 2^{b_i},
/// $$
/// where for all $i$, $b_i\in \\{0, 1\\}$ and $W$ is `$t::WIDTH`. Let
/// $$
/// m = \sum_{i=0}^k 2^{d_i},
/// $$
/// where for all $i$, $d_i\in \\{0, 1\\}$. Also, let $p, q \in \mathbb{N}$, where $d_i
/// = 0$ for all $i \geq W + p$.
///
/// Then
/// $$
/// n \gets \sum_{i=0}^{W-1} 2^{c_i},
/// $$
/// where
/// $$
/// \\{c_0, c_1, c_2, \ldots, c_ {W-1}\\} =
/// \\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
/// b_ {W-1}\\}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Let `W` be the type's width. Panics if `start < end`, or if `end > W` and bits `W -
/// start` through `end - start` of `bits` are nonzero.
///
/// # Examples
/// See [here](super::bit_block_access#assign_bits).
#[inline]
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits) {
assign_bits_unsigned(self, start, end, bits)
}
}
};
}
apply_to_unsigneds!(impl_bit_block_access_unsigned);
fn get_bits_signed<T: ModPowerOf2<Output = U> + PrimitiveSigned, U>(
x: &T,
start: u64,
end: u64,
) -> U {
assert!(start <= end);
(if start >= T::WIDTH {
-T::from(*x < T::ZERO)
} else {
*x >> start
})
.mod_power_of_2(end - start)
}
fn assign_bits_signed<
T: PrimitiveSigned + UnsignedAbs<Output = U> + WrappingFrom<U>,
U: PrimitiveUnsigned,
>(
x: &mut T,
start: u64,
end: u64,
bits: &U,
) {
assert!(start <= end);
if *x >= T::ZERO {
let mut abs_x = x.unsigned_abs();
abs_x.assign_bits(start, end, bits);
assert!(!abs_x.get_highest_bit(), "{ERROR_MESSAGE}");
*x = T::wrapping_from(abs_x);
} else {
let width = T::WIDTH - 1;
let bits_width = end - start;
let bits = bits.mod_power_of_2(bits_width);
let max = U::MAX;
if bits_width > width + 1 {
panic!("{}", ERROR_MESSAGE);
} else if start >= width {
assert!(bits == max.mod_power_of_2(bits_width), "{ERROR_MESSAGE}");
} else {
let lower_width = width - start;
if end > width && bits >> lower_width != max.mod_power_of_2(end - width) {
panic!("{}", ERROR_MESSAGE);
} else {
*x &=
T::wrapping_from(!(max.mod_power_of_2(min(bits_width, lower_width)) << start));
*x |= T::wrapping_from(bits << start);
}
}
}
}
macro_rules! impl_bit_block_access_signed {
($u:ident, $s:ident) => {
impl BitBlockAccess for $s {
type Bits = $u;
/// Extracts a block of adjacent bits from a number.
///
/// The first index is `start` and last index is `end - 1`.
///
/// The type of the block of bits is the unsigned version of the input type. If `end` is
/// greater than the type's width, the high bits of the result are all 0, or all 1,
/// depending on the input value's sign; and if the input is negative and `end - start`
/// is greater than the type's width, the function panics.
///
/// If $n \geq 0$, let
/// $$
/// n = \sum_{i=0}^\infty 2^{b_i},
/// $$
/// where for all $i$, $b_i\in \\{0, 1\\}$; so finitely many of the bits are 1, and the
/// rest are 0. Then
/// $$
/// f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}.
/// $$
///
/// If $n < 0$, let
/// $$
/// 2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
/// $$
/// where
/// - $W$ is the type's width
/// - for all $i$, $b_i\in \\{0, 1\\}$, and $b_i = 1$ for $i \geq W$.
///
/// Then
/// $$
/// f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Let `W` be the type's width. Panics if `start < end` or (`self < 0` and `end - start
/// > W`).
///
/// # Examples
/// See [here](super::bit_block_access#get_bits).
#[inline]
fn get_bits(&self, start: u64, end: u64) -> Self::Bits {
get_bits_signed(self, start, end)
}
/// Replaces a block of adjacent bits in a number with other bits.
///
/// The least-significant `end - start` bits of `bits` are assigned to bits `start`
/// through `end - 1`, inclusive, of `self`.
///
/// The type of the block of bits is the unsigned version of the input type. If `bits`
/// has fewer bits than `end - start`, the high bits are interpreted as 0 or 1,
/// depending on the sign of `self`. If `end` is greater than the type's width, the high
/// bits of `bits` must be 0 or 1, depending on the sign of `self`.
///
/// The sign of `self` remains unchanged, since only a finite number of bits are changed
/// and the sign is determined by the implied infinite prefix of bits.
///
/// If $n \geq 0$ and $j \neq W - 1$, let
/// $$
/// n = \sum_{i=0}^{W-1} 2^{b_i},
/// $$
/// where for all $i$, $b_i\in \\{0, 1\\}$ and $W$ is `$t::WIDTH`. Let
/// $$
/// m = \sum_{i=0}^k 2^{d_i},
/// $$
/// where for all $i$, $d_i\in \\{0, 1\\}$. Also, let $p, q \in \mathbb{N}$, where $d_i
/// = 0$ for all $i \geq W + p - 1$. Then
/// $$
/// n \gets \sum_{i=0}^{W-1} 2^{c_i},
/// $$
/// where
/// $$
/// \\{c_0, c_1, c_2, \ldots, c_ {W-1}\\} =
/// \\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
/// b_ {W-1}\\}.
/// $$
///
/// If $n < 0$ or $j = W - 1$, let
/// $$
/// 2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
/// $$
/// where for all $i$, $b_i\in \\{0, 1\\}$ and $W$ is `$t::WIDTH`. Let
/// $$
/// m = \sum_{i=0}^k 2^{d_i},
/// $$
/// where for all $i$, $d_i\in \\{0, 1\\}$. Also, let $p, q \in \mathbb{N}$, where $d_i
/// = 1$ for all $i \geq W + p - 1$. Then
/// $$
/// f(n, p, q, m) = \left ( \sum_{i=0}^{W-1} 2^{c_i} \right ) - 2^W,
/// $$
/// where
/// $$
/// \\{c_0, c_1, c_2, \ldots, c_ {W-1}\\} =
/// \\{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, \ldots,
/// b_ {W-1}\\}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Let `W` be the type's width Panics if `start < end`, or if `end >= W` and bits `W -
/// start` through `end - start` of `bits` are not equal to the original sign bit of
/// `self`.
///
/// # Examples
/// See [here](super::bit_block_access#assign_bits).
#[inline]
fn assign_bits(&mut self, start: u64, end: u64, bits: &Self::Bits) {
assign_bits_signed(self, start, end, bits)
}
}
};
}
apply_to_unsigned_signed_pairs!(impl_bit_block_access_signed);