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
// 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::integer::conversion::to_twos_complement_limbs::limbs_twos_complement_in_place;
use crate::integer::Integer;
use crate::natural::arithmetic::shr::limbs_slice_shr_in_place;
use crate::natural::Natural;
use crate::platform::{Limb, SignedLimb};
use alloc::vec::Vec;
use itertools::Itertools;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::conversion::traits::{ExactFrom, WrappingFrom};
use malachite_base::num::logic::traits::{BitConvertible, LowMask, NotAssign};
// Given the bits of a non-negative `Integer`, in ascending order, checks whether the most
// significant bit is `false`; if it isn't, appends an extra `false` bit. This way the `Integer`'s
// non-negativity is preserved in its bits.
//
// # Worst-case complexity
// Constant time and additional memory.
pub_test! {bits_to_twos_complement_bits_non_negative(bits: &mut Vec<bool>) {
if !bits.is_empty() && *bits.last().unwrap() {
// Sign-extend with an extra false bit to indicate a positive Integer
bits.push(false);
}
}}
// Given the bits of the absolute value of a negative `Integer`, in ascending order, converts the
// bits to two's complement. Returns whether there is a carry left over from the two's complement
// conversion process.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `bits.len()`.
pub_test! {bits_slice_to_twos_complement_bits_negative(bits: &mut [bool]) -> bool {
let mut true_seen = false;
for bit in &mut *bits {
if true_seen {
bit.not_assign();
} else if *bit {
true_seen = true;
}
}
!true_seen
}}
// Given the bits of the absolute value of a negative `Integer`, in ascending order, converts the
// bits to two's complement and checks whether the most significant bit is `true`; if it isn't,
// appends an extra `true` bit. This way the `Integer`'s negativity is preserved in its bits. The
// bits cannot be empty or contain only `false`s.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `bits.len()`.
//
// # Panics
// Panics if `bits` contains only `false`s.
pub_test! {bits_vec_to_twos_complement_bits_negative(bits: &mut Vec<bool>) {
assert!(!bits_slice_to_twos_complement_bits_negative(bits));
if bits.last() == Some(&false) {
// Sign-extend with an extra true bit to indicate a negative Integer
bits.push(true);
}
}}
fn from_bits_helper(mut limbs: Vec<Limb>, sign_bit: bool, last_width: u64) -> Integer {
if sign_bit {
if last_width != Limb::WIDTH {
*limbs.last_mut().unwrap() |= !Limb::low_mask(last_width);
}
assert!(!limbs_twos_complement_in_place(&mut limbs));
}
Integer::from_sign_and_abs(!sign_bit, Natural::from_owned_limbs_asc(limbs))
}
impl BitConvertible for Integer {
/// Returns a [`Vec`] containing the twos-complement bits of an [`Integer`] in ascending order:
/// least- to most-significant.
///
/// The most significant bit indicates the sign; if the bit is `false`, the [`Integer`] is
/// positive, and if the bit is `true` it is negative. There are no trailing `false` bits if the
/// [`Integer`] is positive or trailing `true` bits if the [`Integer`] is negative, except as
/// necessary to include the correct sign bit. Zero is a special case: it contains no bits.
///
/// This function is more efficient than [`to_bits_desc`](`Self::to_bits_desc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::logic::traits::BitConvertible;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::integer::Integer;
///
/// assert!(Integer::ZERO.to_bits_asc().is_empty());
/// // 105 = 01101001b, with a leading false bit to indicate sign
/// assert_eq!(
/// Integer::from(105).to_bits_asc(),
/// &[true, false, false, true, false, true, true, false]
/// );
/// // -105 = 10010111 in two's complement, with a leading true bit to indicate sign
/// assert_eq!(
/// Integer::from(-105).to_bits_asc(),
/// &[true, true, true, false, true, false, false, true]
/// );
/// ```
fn to_bits_asc(&self) -> Vec<bool> {
let mut bits = self.abs.to_bits_asc();
if self.sign {
bits_to_twos_complement_bits_non_negative(&mut bits);
} else {
bits_vec_to_twos_complement_bits_negative(&mut bits);
}
bits
}
/// Returns a [`Vec`] containing the twos-complement bits of an [`Integer`] in descending order:
/// most- to least-significant.
///
/// The most significant bit indicates the sign; if the bit is `false`, the [`Integer`] is
/// positive, and if the bit is `true` it is negative. There are no leading `false` bits if the
/// [`Integer`] is positive or leading `true` bits if the [`Integer`] is negative, except as
/// necessary to include the correct sign bit. Zero is a special case: it contains no bits.
///
/// This is similar to how `BigInteger`s in Java are represented.
///
/// This function is less efficient than [`to_bits_asc`](`Self::to_bits_asc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::logic::traits::BitConvertible;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::integer::Integer;
///
/// assert!(Integer::ZERO.to_bits_desc().is_empty());
/// // 105 = 01101001b, with a leading false bit to indicate sign
/// assert_eq!(
/// Integer::from(105).to_bits_desc(),
/// &[false, true, true, false, true, false, false, true]
/// );
/// // -105 = 10010111 in two's complement, with a leading true bit to indicate sign
/// assert_eq!(
/// Integer::from(-105).to_bits_desc(),
/// &[true, false, false, true, false, true, true, true]
/// );
/// ```
fn to_bits_desc(&self) -> Vec<bool> {
let mut bits = self.to_bits_asc();
bits.reverse();
bits
}
/// Converts an iterator of twos-complement bits into an [`Integer`]. The bits should be in
/// ascending order (least- to most-significant).
///
/// Let $k$ be `bits.count()`. If $k = 0$ or $b_{k-1}$ is `false`, then
/// $$
/// f((b_i)_ {i=0}^{k-1}) = \sum_{i=0}^{k-1}2^i \[b_i\],
/// $$
/// where braces denote the Iverson bracket, which converts a bit to 0 or 1.
///
/// If $b_{k-1}$ is `true`, then
/// $$
/// f((b_i)_ {i=0}^{k-1}) = \left ( \sum_{i=0}^{k-1}2^i \[b_i\] \right ) - 2^k.
/// $$
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.count()`.
///
/// # Examples
/// ```
/// use malachite_base::num::logic::traits::BitConvertible;
/// use malachite_nz::integer::Integer;
/// use core::iter::empty;
///
/// assert_eq!(Integer::from_bits_asc(empty()), 0);
/// // 105 = 1101001b
/// assert_eq!(
/// Integer::from_bits_asc(
/// [true, false, false, true, false, true, true, false].iter().cloned()
/// ),
/// 105
/// );
/// // -105 = 10010111 in two's complement, with a leading true bit to indicate sign
/// assert_eq!(
/// Integer::from_bits_asc(
/// [true, true, true, false, true, false, false, true].iter().cloned()
/// ),
/// -105
/// );
/// ```
fn from_bits_asc<I: Iterator<Item = bool>>(xs: I) -> Integer {
let mut limbs = Vec::new();
let mut last_width = 0;
let mut last_bit = false;
for chunk in &xs.chunks(usize::exact_from(Limb::WIDTH)) {
let mut limb = 0;
let mut i = 0;
let mut mask = 1;
for bit in chunk {
if bit {
limb |= mask;
}
mask <<= 1;
i += 1;
last_bit = bit;
}
last_width = i;
limbs.push(limb);
}
from_bits_helper(limbs, last_bit, last_width)
}
/// Converts an iterator of twos-complement bits into an [`Integer`]. The bits should be in
/// descending order (most- to least-significant).
///
/// If `bits` is empty or $b_0$ is `false`, then
/// $$
/// f((b_i)_ {i=0}^{k-1}) = \sum_{i=0}^{k-1}2^{k-i-1} \[b_i\],
/// $$
/// where braces denote the Iverson bracket, which converts a bit to 0 or 1.
///
/// If $b_0$ is `true`, then
/// $$
/// f((b_i)_ {i=0}^{k-1}) = \left ( \sum_{i=0}^{k-1}2^{k-i-1} \[b_i\] \right ) - 2^k.
/// $$
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.count()`.
///
/// # Examples
/// ```
/// use malachite_base::num::logic::traits::BitConvertible;
/// use malachite_nz::integer::Integer;
/// use core::iter::empty;
///
/// assert_eq!(Integer::from_bits_desc(empty()), 0);
/// // 105 = 1101001b
/// assert_eq!(
/// Integer::from_bits_desc(
/// [false, true, true, false, true, false, false, true].iter().cloned()
/// ),
/// 105
/// );
/// // -105 = 10010111 in two's complement, with a leading true bit to indicate sign
/// assert_eq!(
/// Integer::from_bits_desc(
/// [true, false, false, true, false, true, true, true].iter().cloned()
/// ),
/// -105
/// );
/// ```
fn from_bits_desc<I: Iterator<Item = bool>>(xs: I) -> Integer {
let mut limbs = Vec::new();
let mut last_width = 0;
let mut first_bit = false;
let mut first = true;
for chunk in &xs.chunks(usize::exact_from(Limb::WIDTH)) {
let mut limb = 0;
let mut i = 0;
for bit in chunk {
if first {
first_bit = bit;
first = false;
}
limb <<= 1;
if bit {
limb |= 1;
}
i += 1;
}
last_width = i;
limbs.push(limb);
}
match limbs.len() {
0 => Integer::ZERO,
1 => {
if first_bit {
if last_width != Limb::WIDTH {
limbs[0] |= !Limb::low_mask(last_width);
}
Integer::from(SignedLimb::wrapping_from(limbs[0]))
} else {
Integer::from(limbs[0])
}
}
_ => {
limbs.reverse();
if last_width != Limb::WIDTH {
let smallest_limb = limbs[0];
limbs[0] = 0;
limbs_slice_shr_in_place(&mut limbs, Limb::WIDTH - last_width);
limbs[0] |= smallest_limb;
}
from_bits_helper(limbs, first_bit, last_width)
}
}
}
}