use crate::bignum;
use crate::nat::{alloc_big, big_parts};
use crate::object::{box_, dec, inc, is_scalar, unbox, Obj};
use std::cmp::Ordering;
struct Signed {
negative: bool,
magnitude: Vec<u64>,
}
unsafe fn read(o: Obj) -> Signed {
if is_scalar(o) {
let value = unbox(o) as u32 as i32 as i64;
let magnitude = if value == 0 { Vec::new() } else { vec![value.unsigned_abs()] };
Signed { negative: value < 0, magnitude }
} else {
let (negative, limbs) = big_parts(o);
Signed { negative, magnitude: limbs.to_vec() }
}
}
fn write(mut value: Signed) -> Obj {
while value.magnitude.last() == Some(&0) {
value.magnitude.pop();
}
let negative = value.negative && !value.magnitude.is_empty();
if let [] | [_] = value.magnitude.as_slice() {
let magnitude = value.magnitude.first().copied().unwrap_or(0) as i128;
let signed = if negative { -magnitude } else { magnitude };
if let Ok(small) = i32::try_from(signed) {
return box_(small as u32 as usize);
}
}
alloc_big(value.magnitude, negative)
}
fn from_i64(value: i64) -> Obj {
write(Signed { negative: value < 0, magnitude: if value == 0 { Vec::new() } else { vec![value.unsigned_abs()] } })
}
unsafe fn small(o: Obj) -> Option<i64> {
is_scalar(o).then(|| unbox(o) as u32 as i32 as i64)
}
fn add_signed(a: Signed, b: Signed) -> Signed {
if a.negative == b.negative {
return Signed { negative: a.negative, magnitude: bignum::add(&a.magnitude, &b.magnitude) };
}
match bignum::compare(&a.magnitude, &b.magnitude) {
Ordering::Less => Signed { negative: b.negative, magnitude: bignum::sub(&b.magnitude, &a.magnitude) },
_ => Signed { negative: a.negative, magnitude: bignum::sub(&a.magnitude, &b.magnitude) },
}
}
fn negate(a: Signed) -> Signed {
Signed { negative: !a.negative, magnitude: a.magnitude }
}
pub unsafe fn nat_to_int(a: Obj) -> Obj {
if is_scalar(a) && unbox(a) > i32::MAX as usize {
return alloc_big(vec![unbox(a) as u64], false);
}
a
}
pub unsafe fn int_to_nat(a: Obj) -> Obj {
if is_scalar(a) {
return a;
}
let result = crate::nat::of_limbs(big_parts(a).1.to_vec());
dec(a);
result
}
pub unsafe fn nat_abs(a: Obj) -> Obj {
crate::nat::of_limbs(read(a).magnitude)
}
pub unsafe fn int_neg(a: Obj) -> Obj {
write(negate(read(a)))
}
pub unsafe fn int_neg_succ_of_nat(n: Obj) -> Obj {
let successor = crate::nat::nat_succ(n);
dec(n);
let as_int = nat_to_int(successor);
let result = int_neg(as_int);
dec(as_int);
result
}
pub unsafe fn int_add(a: Obj, b: Obj) -> Obj {
match (small(a), small(b)) {
(Some(x), Some(y)) => from_i64(x + y),
_ => write(add_signed(read(a), read(b))),
}
}
pub unsafe fn int_sub(a: Obj, b: Obj) -> Obj {
match (small(a), small(b)) {
(Some(x), Some(y)) => from_i64(x - y),
_ => write(add_signed(read(a), negate(read(b)))),
}
}
pub unsafe fn int_mul(a: Obj, b: Obj) -> Obj {
match (small(a), small(b)) {
(Some(x), Some(y)) => from_i64(x * y),
_ => {
let (a, b) = (read(a), read(b));
write(Signed { negative: a.negative != b.negative, magnitude: bignum::mul(&a.magnitude, &b.magnitude) })
}
}
}
fn divrem(a: &Signed, b: &Signed) -> (Signed, Signed) {
let (quotient, remainder) = bignum::divrem(&a.magnitude, &b.magnitude);
(Signed { negative: a.negative != b.negative, magnitude: quotient }, Signed { negative: a.negative, magnitude: remainder })
}
fn one() -> Signed {
Signed { negative: false, magnitude: vec![1] }
}
pub unsafe fn int_div(a: Obj, b: Obj) -> Obj {
let (a, b) = (read(a), read(b));
if b.magnitude.is_empty() {
return box_(0);
}
write(divrem(&a, &b).0)
}
pub unsafe fn int_div_exact(a: Obj, b: Obj) -> Obj {
int_div(a, b)
}
pub unsafe fn int_mod(a: Obj, b: Obj) -> Obj {
let (x, y) = (read(a), read(b));
if y.magnitude.is_empty() {
inc(a);
return a;
}
write(divrem(&x, &y).1)
}
pub unsafe fn int_ediv(a: Obj, b: Obj) -> Obj {
let (x, y) = (read(a), read(b));
if y.magnitude.is_empty() {
return box_(0);
}
let (quotient, remainder) = divrem(&x, &y);
if remainder.negative && !remainder.magnitude.is_empty() {
let step = if y.negative { one() } else { negate(one()) };
return write(add_signed(quotient, step));
}
write(quotient)
}
pub unsafe fn int_emod(a: Obj, b: Obj) -> Obj {
let (x, y) = (read(a), read(b));
if y.magnitude.is_empty() {
inc(a);
return a;
}
let remainder = divrem(&x, &y).1;
if remainder.negative && !remainder.magnitude.is_empty() {
return write(add_signed(remainder, Signed { negative: false, magnitude: y.magnitude }));
}
write(remainder)
}
unsafe fn compare(a: Obj, b: Obj) -> Ordering {
if let (Some(x), Some(y)) = (small(a), small(b)) {
return x.cmp(&y);
}
let (a, b) = (read(a), read(b));
match (a.negative, b.negative) {
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
(false, false) => bignum::compare(&a.magnitude, &b.magnitude),
(true, true) => bignum::compare(&b.magnitude, &a.magnitude),
}
}
pub unsafe fn int_dec_eq(a: Obj, b: Obj) -> u8 {
(compare(a, b) == Ordering::Equal) as u8
}
pub unsafe fn int_dec_lt(a: Obj, b: Obj) -> u8 {
(compare(a, b) == Ordering::Less) as u8
}
pub unsafe fn int_dec_le(a: Obj, b: Obj) -> u8 {
(compare(a, b) != Ordering::Greater) as u8
}
pub unsafe fn int_dec_nonneg(a: Obj) -> u8 {
!read(a).negative as u8
}
pub unsafe fn float_scaleb(a: f64, b: Obj) -> f64 {
let exponent = match small(b) {
Some(exponent) => exponent as i32,
None if read(b).negative => i32::MIN,
None => i32::MAX,
};
let mut value = a;
let mut left = exponent.clamp(-2200, 2200);
while left != 0 {
let step = left.clamp(-1000, 1000);
value *= 2f64.powi(step);
left -= step;
}
value
}
fn of_i64(value: i64) -> Obj {
from_i64(value)
}
pub fn int8_to_int(a: u8) -> Obj {
of_i64(a as i8 as i64)
}
pub fn int16_to_int(a: u16) -> Obj {
of_i64(a as i16 as i64)
}
pub fn int32_to_int(a: u32) -> Obj {
of_i64(a as i32 as i64)
}
pub fn int64_to_int_sint(a: u64) -> Obj {
of_i64(a as i64)
}
pub fn isize_to_int(a: usize) -> Obj {
of_i64(a as isize as i64)
}
unsafe fn low_bits(a: Obj) -> u64 {
let value = read(a);
let low = value.magnitude.first().copied().unwrap_or(0);
if value.negative {
low.wrapping_neg()
} else {
low
}
}
pub unsafe fn int8_of_int(a: Obj) -> u8 {
low_bits(a) as u8
}
pub unsafe fn int16_of_int(a: Obj) -> u16 {
low_bits(a) as u16
}
pub unsafe fn int32_of_int(a: Obj) -> u32 {
low_bits(a) as u32
}
pub unsafe fn int64_of_int(a: Obj) -> u64 {
low_bits(a)
}
pub unsafe fn isize_of_int(a: Obj) -> usize {
low_bits(a) as usize
}
pub(crate) const OPS: &[&str] = &[
"nat_to_int", "int_to_nat", "nat_abs", "int_neg", "int_neg_succ_of_nat", "int_add", "int_sub", "int_mul",
"int_div", "int_div_exact", "int_mod", "int_ediv", "int_emod", "int_dec_eq", "int_dec_lt", "int_dec_le",
"int_dec_nonneg", "float_scaleb", "int8_to_int", "int16_to_int", "int32_to_int", "int64_to_int_sint",
"isize_to_int", "int8_of_int", "int16_of_int", "int32_of_int", "int64_of_int", "isize_of_int",
];
pub(crate) fn from_decimal(negative: bool, digits: &str) -> Option<Obj> {
Some(write(Signed { negative, magnitude: bignum::from_decimal(digits)? }))
}
pub(crate) unsafe fn to_decimal(a: Obj) -> String {
let value = read(a);
let digits = bignum::to_decimal(&value.magnitude);
if value.negative {
format!("-{digits}")
} else {
digits
}
}
pub(crate) fn of_i128(value: i128) -> Obj {
let magnitude = value.unsigned_abs();
write(Signed { negative: value < 0, magnitude: vec![magnitude as u64, (magnitude >> 64) as u64] })
}
pub(crate) unsafe fn to_i128(a: Obj) -> Option<i128> {
let value = read(a);
let magnitude = match value.magnitude.as_slice() {
[] => 0u128,
[low] => *low as u128,
[low, high] => (*high as u128) << 64 | *low as u128,
_ => return None,
};
if value.negative {
0i128.checked_sub_unsigned(magnitude)
} else {
i128::try_from(magnitude).ok()
}
}
pub(crate) unsafe fn ordering(a: Obj, b: Obj) -> Ordering {
compare(a, b)
}