use crate::{
repr::{Repr, TypedRepr, TypedReprRef},
Sign, UBig,
};
#[derive(Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct IBig(pub(crate) Repr);
impl IBig {
#[rustversion::attr(since(1.64), const)]
#[inline]
pub(crate) fn as_sign_repr(&self) -> (Sign, TypedReprRef<'_>) {
self.0.as_sign_typed()
}
#[inline]
pub(crate) fn into_sign_repr(self) -> (Sign, TypedRepr) {
self.0.into_sign_typed()
}
#[inline]
pub fn as_sign_words(&self) -> (Sign, &[crate::Word]) {
self.0.as_sign_slice()
}
#[inline]
pub const fn sign(&self) -> Sign {
self.0.sign()
}
#[inline]
pub fn into_parts(self) -> (Sign, UBig) {
let sign = self.0.sign();
let mag = self.0.with_sign(Sign::Positive);
(sign, UBig(mag))
}
#[inline]
pub fn from_parts(sign: Sign, magnitude: UBig) -> Self {
IBig(magnitude.0.with_sign(sign))
}
#[inline]
pub const fn from_parts_const(sign: Sign, dword: crate::DoubleWord) -> Self {
Self(Repr::from_dword(dword).with_sign(sign))
}
#[cfg(not(any(target_pointer_width = "16", force_bits = "16")))]
#[inline]
pub const fn from_i64(n: i64) -> Self {
let sign = if n >= 0 {
Sign::Positive
} else {
Sign::Negative
};
let mag = n.unsigned_abs() as crate::DoubleWord;
Self(Repr::from_dword(mag).with_sign(sign))
}
#[cfg(any(target_pointer_width = "16", force_bits = "16"))]
#[inline]
pub fn from_i64(n: i64) -> Self {
Self::from(n)
}
#[doc(hidden)]
#[inline]
pub const unsafe fn from_static_words(sign: Sign, words: &'static [crate::Word]) -> Self {
Self(Repr::from_static_words(words).with_sign(sign))
}
pub const ZERO: Self = Self(Repr::zero());
pub const ONE: Self = Self(Repr::one());
pub const NEG_ONE: Self = Self(Repr::neg_one());
#[inline]
pub const fn is_zero(&self) -> bool {
self.0.is_zero()
}
#[inline]
pub const fn is_one(&self) -> bool {
self.0.is_one()
}
}
impl Clone for IBig {
#[inline]
fn clone(&self) -> IBig {
IBig(self.0.clone())
}
#[inline]
fn clone_from(&mut self, source: &IBig) {
self.0.clone_from(&source.0)
}
}