use std::fmt;
const MAX_POW_BITS: u64 = 1 << 24;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct BigInt {
negative: bool,
mag: Vec<u32>,
}
impl BigInt {
pub fn zero() -> BigInt {
BigInt {
negative: false,
mag: Vec::new(),
}
}
pub fn one() -> BigInt {
BigInt::from_i64(1)
}
pub fn from_i64(value: i64) -> BigInt {
let negative = value < 0;
let magnitude = value.unsigned_abs();
let mut big = BigInt::from_u64_magnitude(magnitude);
big.negative = negative && !big.is_zero();
big
}
fn from_u64_magnitude(value: u64) -> BigInt {
let mut mag = vec![value as u32, (value >> 32) as u32];
bigint_trim(&mut mag);
BigInt {
negative: false,
mag,
}
}
pub fn from_f64_truncated(value: f64) -> BigInt {
let negative = value < 0.0;
let magnitude = value.abs();
if !magnitude.is_finite() || magnitude == 0.0 {
return BigInt::zero();
}
let mut mag = Vec::new();
let mut remaining = magnitude;
while remaining >= 1.0 {
let low = remaining % 4294967296.0;
mag.push(low as u32);
remaining = (remaining - low) / 4294967296.0;
}
bigint_trim(&mut mag);
BigInt {
negative: negative && !mag.is_empty(),
mag,
}
}
pub fn is_zero(&self) -> bool {
self.mag.is_empty()
}
pub fn is_one(&self) -> bool {
!self.negative && self.mag.len() == 1 && self.mag[0] == 1
}
pub fn is_negative(&self) -> bool {
self.negative
}
pub fn to_f64(&self) -> f64 {
let mut value = 0.0f64;
for word in self.mag.iter().rev() {
value = value * 4294967296.0 + *word as f64;
}
if self.negative { -value } else { value }
}
pub fn to_i64(&self) -> Option<i64> {
if self.mag.len() > 2 {
return None;
}
let magnitude = self.mag.first().copied().unwrap_or(0) as u64
| ((self.mag.get(1).copied().unwrap_or(0) as u64) << 32);
if self.negative {
if magnitude > i64::MIN.unsigned_abs() {
return None;
}
Some((magnitude as i64).wrapping_neg())
} else {
i64::try_from(magnitude).ok()
}
}
pub fn from_radix(text: &str, radix: u32) -> Option<BigInt> {
if !(2..=36).contains(&radix) || text.is_empty() {
return None;
}
let mut result = BigInt::zero();
let multiplier = BigInt::from_i64(radix as i64);
for character in text.chars() {
let digit = character.to_digit(radix)?;
result = result.mul(&multiplier).add(&BigInt::from_i64(digit as i64));
}
Some(result)
}
pub fn from_string(text: &str) -> Option<BigInt> {
let text = text.trim();
if text.is_empty() {
return Some(BigInt::zero());
}
let (negative, body) = match text.strip_prefix('-') {
Some(rest) => (true, rest.trim_start()),
None => (false, text.strip_prefix('+').unwrap_or(text).trim_start()),
};
let (radix, digits) =
if let Some(rest) = body.strip_prefix("0x").or(body.strip_prefix("0X")) {
(16, rest)
} else if let Some(rest) = body.strip_prefix("0o").or(body.strip_prefix("0O")) {
(8, rest)
} else if let Some(rest) = body.strip_prefix("0b").or(body.strip_prefix("0B")) {
(2, rest)
} else {
(10, body)
};
let value = BigInt::from_radix(digits, radix)?;
Some(if negative { value.negate() } else { value })
}
pub fn negate(&self) -> BigInt {
if self.is_zero() {
return BigInt::zero();
}
BigInt {
negative: !self.negative,
mag: self.mag.clone(),
}
}
pub fn abs(&self) -> BigInt {
BigInt {
negative: false,
mag: self.mag.clone(),
}
}
pub fn add(&self, other: &BigInt) -> BigInt {
if self.negative == other.negative {
let mut mag = add_mag(&self.mag, &other.mag);
bigint_trim(&mut mag);
return BigInt {
negative: self.negative && !mag.is_empty(),
mag,
};
}
match cmp_mag(&self.mag, &other.mag) {
std::cmp::Ordering::Equal => BigInt::zero(),
std::cmp::Ordering::Greater => {
let mut mag = sub_mag(&self.mag, &other.mag);
bigint_trim(&mut mag);
BigInt {
negative: self.negative,
mag,
}
}
std::cmp::Ordering::Less => {
let mut mag = sub_mag(&other.mag, &self.mag);
bigint_trim(&mut mag);
BigInt {
negative: other.negative,
mag,
}
}
}
}
pub fn sub(&self, other: &BigInt) -> BigInt {
self.add(&other.negate())
}
pub fn mul(&self, other: &BigInt) -> BigInt {
if self.is_zero() || other.is_zero() {
return BigInt::zero();
}
let mut mag = vec![0u32; self.mag.len() + other.mag.len()];
for (i, a) in self.mag.iter().enumerate() {
let mut carry = 0u64;
for (j, b) in other.mag.iter().enumerate() {
let slot = &mut mag[i + j];
let total = (*a as u64) * (*b as u64) + (*slot as u64) + carry;
*slot = total as u32;
carry = total >> 32;
}
let mut k = i + other.mag.len();
while carry != 0 {
let total = mag[k] as u64 + carry;
mag[k] = total as u32;
carry = total >> 32;
k += 1;
}
}
bigint_trim(&mut mag);
BigInt {
negative: self.negative != other.negative,
mag,
}
}
pub fn div_rem(&self, other: &BigInt) -> Option<(BigInt, BigInt)> {
if other.is_zero() {
return None;
}
let (quotient, remainder) = div_rem_mag(&self.mag, &other.mag);
let mut quotient = quotient;
let mut remainder = remainder;
bigint_trim(&mut quotient);
bigint_trim(&mut remainder);
Some((
BigInt {
negative: (self.negative != other.negative) && !quotient.is_empty(),
mag: quotient,
},
BigInt {
negative: self.negative && !remainder.is_empty(),
mag: remainder,
},
))
}
pub fn pow(&self, exponent: &BigInt) -> Option<BigInt> {
if exponent.negative {
return None;
}
let mut exponent = exponent.to_i64()? as u64;
let bits = self.mag.len() as u64 * 32;
if bits.saturating_mul(exponent) > MAX_POW_BITS {
return None;
}
let mut base = self.clone();
let mut result = BigInt::one();
while exponent > 0 {
if exponent & 1 == 1 {
result = result.mul(&base);
}
exponent >>= 1;
if exponent > 0 {
base = base.mul(&base);
}
}
Some(result)
}
pub fn shl(&self, bits: u64) -> BigInt {
if self.is_zero() || bits == 0 {
return self.clone();
}
let words = (bits / 32) as usize;
let shift = (bits % 32) as u32;
let mut mag = vec![0u32; words];
if shift == 0 {
mag.extend_from_slice(&self.mag);
} else {
let mut carry = 0u32;
for word in &self.mag {
mag.push((*word << shift) | carry);
carry = *word >> (32 - shift);
}
if carry != 0 {
mag.push(carry);
}
}
bigint_trim(&mut mag);
BigInt {
negative: self.negative,
mag,
}
}
pub fn shr(&self, bits: u64) -> BigInt {
if bits == 0 {
return self.clone();
}
let words = (bits / 32) as usize;
let shift = (bits % 32) as u32;
if words >= self.mag.len() {
return if self.negative {
BigInt::from_i64(-1)
} else {
BigInt::zero()
};
}
let mut mag = self.mag[words..].to_vec();
let mut lost_bit = false;
if shift != 0 {
for index in 0..mag.len() {
let next = mag.get(index + 1).copied().unwrap_or(0);
if index == 0 && mag[0] & ((1 << shift) - 1) != 0 {
lost_bit = true;
}
mag[index] = (mag[index] >> shift) | (next << (32 - shift));
}
}
if self.mag[..words].iter().any(|word| *word != 0) {
lost_bit = true;
}
bigint_trim(&mut mag);
let mut result = BigInt {
negative: self.negative && !mag.is_empty(),
mag,
};
if self.negative && lost_bit {
result = result.sub(&BigInt::one());
}
result
}
pub fn bitand(&self, other: &BigInt) -> BigInt {
self.bitwise(other, |a, b| a & b)
}
pub fn bitor(&self, other: &BigInt) -> BigInt {
self.bitwise(other, |a, b| a | b)
}
pub fn bitxor(&self, other: &BigInt) -> BigInt {
self.bitwise(other, |a, b| a ^ b)
}
pub fn bitnot(&self) -> BigInt {
self.negate().sub(&BigInt::one())
}
fn bitwise(&self, other: &BigInt, op: impl Fn(u32, u32) -> u32) -> BigInt {
let width = self.mag.len().max(other.mag.len()) + 1;
let left = self.twos_complement(width);
let right = other.twos_complement(width);
let words: Vec<u32> = left
.iter()
.zip(right.iter())
.map(|(a, b)| op(*a, *b))
.collect();
BigInt::from_twos_complement(words)
}
fn twos_complement(&self, width: usize) -> Vec<u32> {
let mut words = vec![0u32; width];
for (index, word) in self.mag.iter().enumerate().take(width) {
words[index] = *word;
}
if self.negative {
for word in words.iter_mut() {
*word = !*word;
}
let mut carry = 1u64;
for word in words.iter_mut() {
let total = *word as u64 + carry;
*word = total as u32;
carry = total >> 32;
}
}
words
}
fn from_twos_complement(mut words: Vec<u32>) -> BigInt {
let negative = words.last().is_some_and(|word| word & 0x8000_0000 != 0);
if negative {
for word in words.iter_mut() {
*word = !*word;
}
let mut carry = 1u64;
for word in words.iter_mut() {
let total = *word as u64 + carry;
*word = total as u32;
carry = total >> 32;
}
}
bigint_trim(&mut words);
BigInt {
negative: negative && !words.is_empty(),
mag: words,
}
}
pub fn as_uint_n(&self, bits: u64) -> BigInt {
if bits == 0 {
return BigInt::zero();
}
let mask_words = bits.div_ceil(32) as usize;
let mut words = self.twos_complement(mask_words);
let spare = (mask_words as u64 * 32 - bits) as u32;
if spare > 0
&& let Some(last) = words.last_mut()
{
*last &= u32::MAX >> spare;
}
bigint_trim(&mut words);
BigInt {
negative: false,
mag: words,
}
}
pub fn as_int_n(&self, bits: u64) -> BigInt {
if bits == 0 {
return BigInt::zero();
}
let mask_words = bits.div_ceil(32) as usize;
let mut words = self.twos_complement(mask_words);
let spare = (mask_words as u64 * 32 - bits) as u32;
if spare > 0
&& let Some(last) = words.last_mut()
{
*last &= u32::MAX >> spare;
}
let negative = words
.last()
.is_some_and(|word| word & (1 << ((bits - 1) % 32)) != 0);
if negative && spare > 0 {
if let Some(last) = words.last_mut() {
*last |= u32::MAX << (32 - spare);
}
}
BigInt::from_twos_complement(words)
}
pub fn to_decimal_string(&self) -> String {
self.to_radix_string(10)
}
pub fn to_radix_string(&self, radix: u32) -> String {
if self.is_zero() {
return "0".to_string();
}
let radix = radix.clamp(2, 36);
let mut digits = Vec::new();
let mut magnitude = self.mag.clone();
let chunk = 1_000_000_000u32;
if radix == 10 {
while !magnitude.is_empty() {
let remainder = div_rem_small(&mut magnitude, chunk);
if magnitude.is_empty() {
digits.push(remainder.to_string());
} else {
digits.push(format!("{remainder:09}"));
}
}
} else {
while !magnitude.is_empty() {
let remainder = div_rem_small(&mut magnitude, radix);
digits.push(
char::from_digit(remainder, radix)
.unwrap_or('0')
.to_string(),
);
}
}
digits.reverse();
let body = digits.concat();
if self.negative {
format!("-{body}")
} else {
body
}
}
}
impl js_gc::Trace for BigInt {
fn trace(&self, _tracer: &mut js_gc::Tracer) {}
}
impl fmt::Display for BigInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_decimal_string())
}
}
impl PartialOrd for BigInt {
fn partial_cmp(&self, other: &BigInt) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BigInt {
fn cmp(&self, other: &BigInt) -> std::cmp::Ordering {
use std::cmp::Ordering;
match (self.negative, other.negative) {
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
(false, false) => cmp_mag(&self.mag, &other.mag),
(true, true) => cmp_mag(&other.mag, &self.mag),
}
}
}
fn bigint_trim(words: &mut Vec<u32>) {
while words.last() == Some(&0) {
words.pop();
}
}
fn cmp_mag(a: &[u32], b: &[u32]) -> std::cmp::Ordering {
if a.len() != b.len() {
return a.len().cmp(&b.len());
}
for index in (0..a.len()).rev() {
if a[index] != b[index] {
return a[index].cmp(&b[index]);
}
}
std::cmp::Ordering::Equal
}
fn add_mag(a: &[u32], b: &[u32]) -> Vec<u32> {
let (long, short) = if a.len() >= b.len() { (a, b) } else { (b, a) };
let mut out = Vec::with_capacity(long.len() + 1);
let mut carry = 0u64;
for (index, word) in long.iter().enumerate() {
let total = *word as u64 + short.get(index).copied().unwrap_or(0) as u64 + carry;
out.push(total as u32);
carry = total >> 32;
}
if carry != 0 {
out.push(carry as u32);
}
out
}
fn sub_mag(a: &[u32], b: &[u32]) -> Vec<u32> {
let mut out = Vec::with_capacity(a.len());
let mut borrow = 0i64;
for (index, word) in a.iter().enumerate() {
let total = *word as i64 - b.get(index).copied().unwrap_or(0) as i64 - borrow;
if total < 0 {
out.push((total + (1i64 << 32)) as u32);
borrow = 1;
} else {
out.push(total as u32);
borrow = 0;
}
}
out
}
fn div_rem_small(words: &mut Vec<u32>, divisor: u32) -> u32 {
let mut remainder = 0u64;
for index in (0..words.len()).rev() {
let total = (remainder << 32) | words[index] as u64;
words[index] = (total / divisor as u64) as u32;
remainder = total % divisor as u64;
}
bigint_trim(words);
remainder as u32
}
fn div_rem_mag(a: &[u32], b: &[u32]) -> (Vec<u32>, Vec<u32>) {
let mut remainder = Vec::new();
let mut quotient = vec![0u32; a.len()];
let bits = a.len() as u64 * 32;
for bit in (0..bits).rev() {
let mut carry = (a[(bit / 32) as usize] >> (bit % 32)) & 1;
for word in remainder.iter_mut() {
let next = *word >> 31;
*word = (*word << 1) | carry;
carry = next;
}
if carry != 0 {
remainder.push(carry);
}
bigint_trim(&mut remainder);
if cmp_mag(&remainder, b) != std::cmp::Ordering::Less {
remainder = sub_mag(&remainder, b);
bigint_trim(&mut remainder);
quotient[(bit / 32) as usize] |= 1 << (bit % 32);
}
}
(quotient, remainder)
}
#[cfg(test)]
mod tests {
use super::BigInt;
fn big(value: i64) -> BigInt {
BigInt::from_i64(value)
}
fn parsed(text: &str) -> BigInt {
BigInt::from_string(text).expect("parses")
}
#[test]
fn decimal_round_trips() {
for text in [
"0",
"1",
"-1",
"123456789012345678901234567890",
"-999999999999999999999999999999",
] {
assert_eq!(parsed(text).to_decimal_string(), text);
}
assert_eq!(parsed("-0").to_decimal_string(), "0");
}
#[test]
fn arithmetic_matches_i64_for_small_values() {
let cases = [
(7i64, 3i64),
(-7, 3),
(7, -3),
(-7, -3),
(0, 5),
(123456, -789),
];
for (a, b) in cases {
assert_eq!(big(a).add(&big(b)).to_i64(), Some(a + b), "{a} + {b}");
assert_eq!(big(a).sub(&big(b)).to_i64(), Some(a - b), "{a} - {b}");
assert_eq!(big(a).mul(&big(b)).to_i64(), Some(a * b), "{a} * {b}");
assert_eq!(
big(a).div_rem(&big(b)).unwrap().0.to_i64(),
Some(a / b),
"{a} / {b}"
);
assert_eq!(
big(a).div_rem(&big(b)).unwrap().1.to_i64(),
Some(a % b),
"{a} % {b}"
);
}
}
#[test]
fn division_is_exact_on_large_values() {
let big_power = big(2).pow(&big(1000)).unwrap();
let difference = big_power.sub(&big_power.sub(&big(1)));
assert_eq!(difference.to_decimal_string(), "1");
let (quotient, remainder) = big_power.div_rem(&big_power.sub(&big(1))).unwrap();
assert_eq!(quotient.to_decimal_string(), "1");
assert_eq!(remainder.to_decimal_string(), "1");
}
#[test]
fn shifting_is_arithmetic() {
assert_eq!(
big(1).shl(128).to_decimal_string(),
"340282366920938463463374607431768211456"
);
assert_eq!(big(1).shl(128).shr(128).to_decimal_string(), "1");
assert_eq!(big(-1).shr(1).to_decimal_string(), "-1");
assert_eq!(big(-2).shr(1).to_decimal_string(), "-1");
assert_eq!(big(-3).shr(1).to_decimal_string(), "-2");
assert_eq!(big(-1).shl(4).to_decimal_string(), "-16");
}
#[test]
fn bitwise_uses_twos_complement() {
assert_eq!(big(-1).bitand(&big(3)).to_i64(), Some(3));
assert_eq!(big(-1).bitor(&big(3)).to_i64(), Some(-1));
assert_eq!(big(-1).bitxor(&big(3)).to_i64(), Some(-4));
assert_eq!(big(0).bitnot().to_i64(), Some(-1));
assert_eq!(big(-1).bitnot().to_i64(), Some(0));
assert_eq!(big(12).bitand(&big(10)).to_i64(), Some(8));
assert_eq!(big(12).bitor(&big(10)).to_i64(), Some(14));
assert_eq!(big(12).bitxor(&big(10)).to_i64(), Some(6));
let wide = big(1).shl(64);
assert_eq!(
wide.bitand(&wide).to_decimal_string(),
"18446744073709551616"
);
}
#[test]
fn as_int_n_and_as_uint_n_wrap() {
assert_eq!(big(255).as_uint_n(8).to_i64(), Some(255));
assert_eq!(big(256).as_uint_n(8).to_i64(), Some(0));
assert_eq!(big(-1).as_uint_n(8).to_i64(), Some(255));
assert_eq!(big(128).as_int_n(8).to_i64(), Some(-128));
assert_eq!(big(127).as_int_n(8).to_i64(), Some(127));
assert_eq!(big(-129).as_int_n(8).to_i64(), Some(127));
assert_eq!(big(5).as_int_n(0).to_i64(), Some(0));
}
#[test]
fn ordering_is_signed() {
assert!(big(-2) < big(1));
assert!(big(-2) < big(-1));
assert!(big(2) > big(1));
assert_eq!(big(0), big(-0));
let huge = parsed("100000000000000000000000");
assert!(huge > big(i64::MAX));
assert!(huge.negate() < big(i64::MIN + 1));
}
#[test]
fn pow_guards_absurd_exponents() {
assert!(
big(2).pow(&big(-1)).is_none(),
"a negative exponent has no integer result"
);
assert!(
big(2).pow(&big(1_000_000_000)).is_none(),
"the result would not fit memory"
);
assert_eq!(big(2).pow(&big(10)).unwrap().to_i64(), Some(1024));
assert_eq!(big(0).pow(&big(0)).unwrap().to_i64(), Some(1));
}
}