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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
use core::ptr;
use awint_internals::*;
use const_fn::const_fn;
use crate::Bits;
/// # Miscellanious
impl Bits {
/// Returns the least significant bit
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn lsb(&self) -> bool {
(self.first() & 1) != 0
}
/// Returns the most significant bit
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn msb(&self) -> bool {
if self.extra() == 0 {
(self.last() as IDigit) < 0
} else {
(self.last() & (1 << (self.extra() - 1))) != 0
}
}
/// Gets the bit at `inx` bits from the least significant bit, returning
/// `None` if `inx >= self.bw()`
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn get(&self, inx: usize) -> Option<bool> {
if inx >= self.bw() {
None
} else {
unsafe { Some((self.get_unchecked(digits_u(inx)) & (1 << extra_u(inx))) != 0) }
}
}
/// Sets the bit at `inx` bits from the least significant bit, returning
/// `None` if `inx >= self.bw()`
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn set(&mut self, inx: usize, bit: bool) -> Option<()> {
if inx >= self.bw() {
None
} else {
unsafe {
let x = self.get_unchecked(digits_u(inx));
*self.get_unchecked_mut(digits_u(inx)) = if bit {
x | (1 << extra_u(inx))
} else {
x & (!(1 << extra_u(inx)))
};
}
Some(())
}
}
/// Returns the number of leading zero bits
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn lz(&self) -> usize {
// If unused bits are set, then the caller is going to get unexpected behavior
// somewhere, also prevent overflow
self.assert_cleared_unused_bits();
const_for!(i in {0..self.total_digits()}.rev() {
let x = unsafe{self.get_unchecked(i)};
if x != 0 {
return ((self.total_digits() - 1 - i) * BITS)
+ (x.leading_zeros() as usize) - self.unused();
}
});
(self.total_digits() * BITS) - self.unused()
}
/// Returns the number of trailing zero bits
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn tz(&self) -> usize {
// If unused bits are set, then the caller is going to get unexpected behavior
// somewhere, also prevent overflow
self.assert_cleared_unused_bits();
const_for!(i in {0..self.total_digits()} {
let x = unsafe{self.get_unchecked(i)};
if x != 0 {
return (i * BITS) + (x.trailing_zeros() as usize);
}
});
(self.total_digits() * BITS) - self.unused()
}
/// Returns the number of significant bits, `self.bw() - self.lz()`
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn sig(&self) -> usize {
self.bw() - self.lz()
}
/// Returns the number of set ones
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn count_ones(&self) -> usize {
// If unused bits are set, then the caller is going to get unexpected behavior
// somewhere, also prevent overflow
self.assert_cleared_unused_bits();
let mut ones = 0;
const_for!(i in {0..self.total_digits()} {
let x = unsafe{self.get_unchecked(i)};
ones += x.count_ones() as usize;
});
ones
}
/// "Fielding" bitfields with targeted copy assigns. The bitwidths of `self`
/// and `rhs` do not have to be equal, but the inputs must collectively obey
/// `width <= self.bw() && width <= rhs.bw() && to <= (self.bw() - width)
/// && from <= (rhs.bw() - width)` or else `None` is
/// returned. `width` can be zero, in which case this function just checks
/// the input correctness and does not mutate `self`.
///
/// This function works by copying a `width` sized bitfield from `rhs` at
/// bitposition `from` and overwriting `width` bits at bitposition `to` in
/// `self`. Only the `width` bits in `self` are mutated, any bits before and
/// after the bitfield are left unchanged.
///
/// ```
/// use awint::{inlawi, Bits, InlAwi};
/// // As an example, two hexadecimal digits will be overwritten
/// // starting with the 12th digit in `y` using a bitfield with
/// // value 0x42u8 extracted from `x`.
/// let x = inlawi!(0x11142111u50);
/// // the underscores are just for emphasis
/// let mut y = inlawi!(0xfd_ec_ba9876543210u100);
/// // from `x` digit place 3, we copy 2 digits to `y` digit place 12.
/// y.field(12 * 4, &x, 3 * 4, 2 * 4);
/// assert_eq!(y, inlawi!(0xfd_42_ba9876543210u100));
/// ```
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn field(&mut self, to: usize, rhs: &Self, from: usize, width: usize) -> Option<()> {
let bw_digits = digits_u(width);
let bw_bits = extra_u(width);
let from_digits = digits_u(from);
let from_bits = extra_u(from);
let to_digits = digits_u(to);
let to_bits = extra_u(to);
// we do the comparisons in this order to make sure that the subtractions do not
// overflow
if (width > self.bw())
|| (width > rhs.bw())
|| (to > (self.bw() - width))
|| (from > (rhs.bw() - width))
{
return None
}
if width == 0 {
return Some(())
}
// since we are dealing with three different sets of digit and subdigit shifts,
// the only sane way to do this is to make a digit aligned copy
// (`tmp`) of the bitfield from `rhs` and then copy again to its final alignment
// Safety: we test this vigorously in `testcrate` through `multi_bw.rs`,
// `misc.rs`, and `macro_fuzzing.rs`. There are debug asserts for out of bounds.
unsafe {
if (bw_digits != 0) && (from_bits == 0) && (to_bits == 0) {
const_for!(i in {0..bw_digits} {
*self.get_unchecked_mut(i + to_digits) =
rhs.get_unchecked(i + from_digits);
});
// handle last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits + to_digits) =
(self.get_unchecked(bw_digits + to_digits) & to_mask)
| (rhs.get_unchecked(bw_digits + from_digits) & from_mask);
}
} else if (bw_digits != 0) && (to_bits == 0) {
const_for!(i in {0..bw_digits} {
*self.get_unchecked_mut(i + to_digits) =
rhs.get_digit(from + (i * BITS));
});
// handle last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits + to_digits) =
(self.get_unchecked(bw_digits + to_digits) & to_mask)
| (rhs.get_digit(from + (bw_digits * BITS)) & from_mask);
}
} else {
let mut from = from;
let mut i = to_digits;
loop {
if i >= (bw_digits + to_digits) {
// handle the extra bits from the field
if bw_bits != 0 {
let tmp = rhs.get_digit(from);
// add extra masking for the extra temporary bits
let tmp = tmp & (MAX >> (BITS - bw_bits));
let mut total = to_bits + bw_bits;
if to_bits == 0 {
let mask = MAX << bw_bits;
*self.get_unchecked_mut(to_digits) =
tmp | (self.get_unchecked(to_digits) & mask);
} else if total >= BITS {
total -= BITS;
let tmp = (tmp << to_bits, tmp >> (BITS - to_bits));
let mask = MAX >> (BITS - to_bits);
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask) | tmp.0;
if total != 0 {
// the extra bits cross a digit boundary
i += 1;
let mask = MAX << total;
*self.get_unchecked_mut(i) =
(self.get_unchecked(i) & mask) | tmp.1;
}
} else {
// total < BITS
let tmp = tmp << to_bits;
// Because the extra bits are fewer than BITS and they are
// positioned in the middle. The mask has to cover before and after
// the extra bits.
let mask = (MAX << total) | (MAX >> (BITS - to_bits));
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask) | tmp;
}
}
break
}
let tmp = rhs.get_digit(from);
// shift up into new field placements
let tmp = (tmp << to_bits, tmp >> (BITS - to_bits));
// mask
let mask1 = MAX << to_bits;
// because the partial field is one `Digit` long
let mask0 = !mask1;
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask0) | tmp.0;
i += 1;
from += BITS;
// this incurs more stores to `self` than necessary,
// but the alternative is even more complex
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask1) | tmp.1;
}
}
}
Some(())
}
/// A specialization of [Bits::field] with `from` set to 0.
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn field_to(&mut self, to: usize, rhs: &Self, width: usize) -> Option<()> {
let bw_digits = digits_u(width);
let bw_bits = extra_u(width);
let to_digits = digits_u(to);
let to_bits = extra_u(to);
// we do the comparisons in this order to make sure that the subtractions do not
// overflow
if (width > self.bw()) || (width > rhs.bw()) || (to > (self.bw() - width)) {
return None
}
if width == 0 {
return Some(())
}
unsafe {
if (bw_digits != 0) && (to_bits == 0) {
const_for!(i in {0..bw_digits} {
*self.get_unchecked_mut(i + to_digits) =
rhs.get_unchecked(i);
});
// handle last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits + to_digits) =
(self.get_unchecked(bw_digits + to_digits) & to_mask)
| (rhs.get_unchecked(bw_digits) & from_mask);
}
} else {
let mut i = to_digits;
loop {
if i >= (bw_digits + to_digits) {
// handle the extra bits from the field
if bw_bits != 0 {
let tmp = rhs.get_unchecked(i - to_digits);
// add extra masking for the extra temporary bits
let tmp = tmp & (MAX >> (BITS - bw_bits));
let mut total = to_bits + bw_bits;
if to_bits == 0 {
let mask = MAX << bw_bits;
*self.get_unchecked_mut(to_digits) =
tmp | (self.get_unchecked(to_digits) & mask);
} else if total >= BITS {
total -= BITS;
let tmp = (tmp << to_bits, tmp >> (BITS - to_bits));
let mask = MAX >> (BITS - to_bits);
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask) | tmp.0;
if total != 0 {
// the extra bits cross a digit boundary
i += 1;
let mask = MAX << total;
*self.get_unchecked_mut(i) =
(self.get_unchecked(i) & mask) | tmp.1;
}
} else {
// total < BITS
let tmp = tmp << to_bits;
// Because the extra bits are fewer than BITS and they are
// positioned in the middle. The mask has to cover before and after
// the extra bits.
let mask = (MAX << total) | (MAX >> (BITS - to_bits));
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask) | tmp;
}
}
break
}
let tmp = rhs.get_unchecked(i - to_digits);
// shift up into new field placements
let tmp = (tmp << to_bits, tmp >> (BITS - to_bits));
// mask
let mask1 = MAX << to_bits;
// because the partial field is one `Digit` long
let mask0 = !mask1;
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask0) | tmp.0;
i += 1;
// this incurs more stores to `self` than necessary,
// but the alternative is even more complex
*self.get_unchecked_mut(i) = (self.get_unchecked(i) & mask1) | tmp.1;
}
}
}
Some(())
}
/// A specialization of [Bits::field] with `to` set to 0.
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn field_from(&mut self, rhs: &Self, from: usize, width: usize) -> Option<()> {
let bw_digits = digits_u(width);
let bw_bits = extra_u(width);
let from_digits = digits_u(from);
let from_bits = extra_u(from);
// we do the comparisons in this order to make sure that the subtractions do not
// overflow
if (width > self.bw()) || (width > rhs.bw()) || (from > (rhs.bw() - width)) {
return None
}
if width == 0 {
return Some(())
}
unsafe {
if (bw_digits != 0) && (from_bits == 0) {
const_for!(i in {0..bw_digits} {
*self.get_unchecked_mut(i) =
rhs.get_unchecked(i + from_digits);
});
// handle last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits) = (self.get_unchecked(bw_digits) & to_mask)
| (rhs.get_unchecked(bw_digits + from_digits) & from_mask);
}
} else if bw_digits != 0 {
const_for!(i in {0..bw_digits} {
*self.get_unchecked_mut(i) =
rhs.get_digit(from + (i * BITS));
});
// handle last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits) = (self.get_unchecked(bw_digits) & to_mask)
| (rhs.get_digit(from + (bw_digits * BITS)) & from_mask);
}
} else {
let mut from = from;
let mut i = 0;
loop {
if i >= bw_digits {
// handle the extra bits from the field
if bw_bits != 0 {
let tmp = rhs.get_digit(from);
// add extra masking for the extra temporary bits
let tmp = tmp & (MAX >> (BITS - bw_bits));
let mask = MAX << bw_bits;
*self.first_mut() = tmp | (self.first() & mask);
}
break
}
*self.get_unchecked_mut(i) = rhs.get_digit(from);
i += 1;
from += BITS;
}
}
}
Some(())
}
/// A specialization of [Bits::field] with `to` and `from` set to 0.
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn field_width(&mut self, rhs: &Self, width: usize) -> Option<()> {
if (width > self.bw()) || (width > rhs.bw()) {
return None
}
let bw_digits = digits_u(width);
let bw_bits = extra_u(width);
unsafe {
ptr::copy_nonoverlapping(rhs.as_ptr(), self.as_mut_ptr(), bw_digits);
// last digit
if bw_bits != 0 {
let to_mask = MAX << bw_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(bw_digits) = (self.get_unchecked(bw_digits) & to_mask)
| (rhs.get_unchecked(bw_digits) & from_mask);
}
}
Some(())
}
/// A specialization of [Bits::field] with `width` set to 1.
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn field_bit(&mut self, to: usize, rhs: &Bits, from: usize) -> Option<()> {
if let Some(b) = rhs.get(from) {
self.set(to, b)
} else {
None
}
}
/// Copy entry from lookup table. Copies a `self.bw()` sized bitfield from
/// `lut` at bit position `inx.to_usize() * self.bw()`. If `lut.bw() !=
/// (self.bw() * (2^inx.bw()))`, `None` will be returned.
///
/// ```
/// use awint::{inlawi, Bits, InlAwi};
/// let mut out = inlawi!(0u10);
/// // lookup table consisting of 4 10-bit entries
/// let lut = inlawi!(4u10, 3u10, 2u10, 1u10);
/// // the indexer has to have a bitwidth of 2 to index 2^2 = 4 entries
/// let mut inx = inlawi!(0u2);
///
/// // get the third entry (this is using zero indexing)
/// inx.usize_(2);
/// out.lut_(&lut, &inx).unwrap();
/// assert_eq!(out, inlawi!(3u10));
/// ```
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn lut_(&mut self, lut: &Self, inx: &Self) -> Option<()> {
// because we later call `inx.to_usize()` and assume that it fits within
// `inx.bw()`
inx.assert_cleared_unused_bits();
// make sure the left shift does not overflow
if inx.bw() < BITS {
if let Some(lut_len) = (1usize << inx.bw()).checked_mul(self.bw()) {
if lut_len == lut.bw() {
let index = inx.to_usize().wrapping_mul(self.bw());
let digits = digits_u(index);
let bits = extra_u(index);
let self_bits = extra_u(self.bw());
// Safety: Because of the strict bitwidths of `self`, `lut`, and `inx`, the
// value of `inx` cannot index beyond the width of `lut`.
unsafe {
if bits == 0 {
const_for!(i in {0..self.total_digits()} {
*self.get_unchecked_mut(i) = lut.get_unchecked(digits + i);
});
} else {
const_for!(i in {0..(self.total_digits() - 1)} {
*self.get_unchecked_mut(i) = (lut.get_unchecked(digits + i) >> bits)
| (lut.get_unchecked(digits + i + 1) << (BITS - bits));
});
if (bits + self_bits) > BITS {
// this is tricky, because the extra bits from `self` and `index`
// can combine to push the end of
// the bitfield over a digit boundary
*self.last_mut() =
(lut.get_unchecked(digits + self.total_digits() - 1) >> bits)
| (lut.get_unchecked(digits + self.total_digits())
<< (BITS - bits));
} else {
*self.last_mut() =
lut.get_unchecked(digits + self.total_digits() - 1) >> bits;
}
}
}
self.clear_unused_bits();
return Some(())
}
}
}
None
}
/// Set entry in lookup table. The inverse of [Bits::lut_], this uses
/// `entry` as a bitfield to overwrite part of `self` at bit position
/// `inx.to_usize() * entry.bw()`. If
/// `self.bw() != (entry.bw() * (2^inx.bw()))`, `None` will be returned.
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn lut_set(&mut self, entry: &Self, inx: &Self) -> Option<()> {
// because we later call `inx.to_usize()` and assume that it fits within
// `inx.bw()`
inx.assert_cleared_unused_bits();
// make sure the left shift does not overflow
if inx.bw() < BITS {
if let Some(lut_len) = (1usize << inx.bw()).checked_mul(entry.bw()) {
if lut_len == self.bw() {
let index = inx.to_usize().wrapping_mul(entry.bw());
let inx_digits = digits_u(index);
let inx_bits = extra_u(index);
let entry_digits = digits_u(entry.bw());
let entry_bits = extra_u(entry.bw());
// Safety: Because of the strict bitwidths of `self`, `lut`, and `inx`, the
// value of `inx` cannot index beyond the width of `lut`. We also apply
// extensive testing in `testcrate`.
unsafe {
if (entry_digits != 0) && (inx_bits == 0) {
const_for!(i in {0..entry_digits} {
*self.get_unchecked_mut(i + inx_digits) =
entry.get_unchecked(i);
});
// handle last digit
if entry_bits != 0 {
// `entry.get_unchecked(entry_digits)` is the same as
// `entry.last()`, but it can produce faster assembly in this case.
let to_mask = MAX << entry_bits;
let from_mask = !to_mask;
*self.get_unchecked_mut(entry_digits + inx_digits) =
(self.get_unchecked(entry_digits + inx_digits) & to_mask)
| (entry.get_unchecked(entry_digits) & from_mask);
}
} else {
let mut i = 0;
loop {
if i >= entry_digits {
// handle the extra bits from the field
if entry_bits != 0 {
let tmp = entry.get_unchecked(entry_digits);
let mut total = inx_bits + entry_bits;
if inx_bits == 0 {
let mask = MAX << entry_bits;
*self.get_unchecked_mut(inx_digits) =
tmp | (self.get_unchecked(inx_digits) & mask);
} else if total >= BITS {
total -= BITS;
let tmp = (tmp << inx_bits, tmp >> (BITS - inx_bits));
let mask = MAX >> (BITS - inx_bits);
*self.get_unchecked_mut(i + inx_digits) =
(self.get_unchecked(i + inx_digits) & mask) | tmp.0;
if total != 0 {
// the extra bits cross a digit boundary
i += 1;
let mask = MAX << total;
*self.get_unchecked_mut(i + inx_digits) =
(self.get_unchecked(i + inx_digits) & mask)
| tmp.1;
}
} else {
// total < BITS
let tmp = tmp << inx_bits;
// Because the extra bits are fewer than BITS and they
// are
// positioned in the middle. The mask has to cover
// before and after
// the extra bits.
let mask = (MAX << total) | (MAX >> (BITS - inx_bits));
*self.get_unchecked_mut(i + inx_digits) =
(self.get_unchecked(i + inx_digits) & mask) | tmp;
}
}
break
}
let tmp = entry.get_unchecked(i);
// shift up into new field placements
let tmp = (tmp << inx_bits, tmp >> (BITS - inx_bits));
// mask
let mask1 = MAX << inx_bits;
// because the partial field is one `Digit` long
let mask0 = !mask1;
*self.get_unchecked_mut(i + inx_digits) =
(self.get_unchecked(i + inx_digits) & mask0) | tmp.0;
i += 1;
// this incurs more stores to `self` than necessary,
// but the alternative is even more complex
*self.get_unchecked_mut(i + inx_digits) =
(self.get_unchecked(i + inx_digits) & mask1) | tmp.1;
}
}
}
self.clear_unused_bits();
return Some(())
}
}
}
None
}
/// Multiplex by conditionally copy-assigning `rhs` to `self` if `b`
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn mux_(&mut self, rhs: &Bits, b: bool) -> Option<()> {
if self.bw() == rhs.bw() {
if b {
self.copy_(rhs).unwrap();
}
Some(())
} else {
None
}
}
/// Repeat-assigns `self` by `rhs`. This is logically equivalent to
/// concatenating an infinite number of `rhs` bit strings together, then
/// resize-assigning to `self`.
#[const_fn(cfg(feature = "const_support"))]
pub const fn repeat_(&mut self, rhs: &Bits) {
let w = rhs.bw();
if w == 1 {
// repeat a single bit
if rhs.lsb() {
self.umax_();
} else {
self.zero_();
}
} else if self.bw() <= w {
// just resize
self.resize_(rhs, false);
} else if w > BITS {
// general case
let mut to = 0;
loop {
if to > self.bw() {
break
}
let min = if w < (self.bw() - to) {
w
} else {
self.bw() - to
};
self.field_to(to, rhs, min).unwrap();
to += w;
}
} else {
// exponentially expand to more than half a digit
let mut x = rhs.to_digit();
let mut s = w;
loop {
if s > (BITS / 2) {
break
}
x |= x << s;
s <<= 1;
}
if s == BITS {
// simple digit repeating
// Safety: the indexes are in bounds
unsafe {
const_for!(i in {0..self.total_digits()} {
*self.get_unchecked_mut(i) = x;
});
}
} else {
// `digit_or` in a loop onto a zeroed `self`
self.zero_();
let mut to = 0;
loop {
if to > self.bw() {
break
}
self.digit_or_(x, to);
to += s;
}
}
self.clear_unused_bits();
}
}
}