Upgrade dashu and some changes

This commit is contained in:
Jacob
2023-09-13 12:29:12 -04:00
parent 4077040d03
commit adb5fcf708
8 changed files with 61 additions and 70 deletions

View File

@@ -386,8 +386,7 @@ pub(crate) fn rnd_i<'a>(n: &'a Number, arena: &mut Arena) -> Number {
&Number::Rational(ref r) => {
let (_, floor) = (r.fract(), r.floor());
let result = floor.clone().try_into();
if let Ok(value) = result{
if let Ok(value) = (&floor).try_into() {
fixnum!(Number, value, arena)
} else {
Number::Integer(arena_alloc!(floor, arena))
@@ -713,13 +712,13 @@ impl TryFrom<HeapCellValue> for Number {
// Computes n ^ power. Ignores the sign of power.
pub(crate) fn binary_pow(mut n: Integer, power: &Integer) -> Integer {
let mut power = Integer::from(power.abs());
let mut power = power.abs();
if power.num_eq(&0) {
return Integer::from(1);
if power.is_zero() {
return Integer::ONE;
}
let mut oddand = Integer::from(1);
let mut oddand = Integer::ONE;
while power.num_gt(&1) {
if power.bit(0) {

View File

@@ -1,7 +1,9 @@
use crate::arena::*;
use crate::atom_table::*;
use crate::parser::ast::*;
use crate::parser::dashu::{Integer, Rational};
use crate::parser::dashu::{ibig, Integer, Rational};
use crate::parser::dashu::base::RemEuclid;
use crate::parser::dashu::integer::Sign;
use crate::{
alpha_numeric_char, capital_letter_char, cut_char, decimal_digit_char, graphic_token_char,
is_fx, is_infix, is_postfix, is_prefix, is_xf, is_xfx, is_xfy, is_yfx, semicolon_char,
@@ -18,8 +20,6 @@ use crate::machine::stack::*;
use crate::machine::streams::*;
use crate::types::*;
use dashu::base::DivRem;
use dashu::base::DivRemEuclid;
use ordered_float::OrderedFloat;
use indexmap::IndexMap;
@@ -515,13 +515,10 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
let n_clone: Integer = n.clone();
let i: usize = (&n).rem_euclid(ibig!(26)).try_into().unwrap();
let j = n / ibig!(26);
let i = n.div_rem_euclid(Integer::from(26)).1.to_f32().value() as usize;
let j = n_clone.div_rem(Integer::from(26));
let j = <(Integer, Integer)>::from(j).0;
if j == Integer::from(0) {
if j.is_zero() {
CHAR_CODES[i].to_string()
} else {
format!("{}{}", CHAR_CODES[i], j)
@@ -1024,7 +1021,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
match self.op_dir.get(&(atom!("rdiv"), Fixity::In)) {
Some(op_desc) => {
if r.denominator().is_one() {
if r.is_int() {
let output_str = format!("{}", r);
push_space_if_amb!(self, &output_str, {
@@ -1367,10 +1364,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
if self.numbervars && arity == 1 && name == atom!("$VAR") {
!self.iter.immediate_leaf_has_property(|addr| {
match Number::try_from(addr) {
Ok(Number::Integer(n)) => &*n >= &Integer::from(0),
Ok(Number::Integer(n)) => (*n).sign() == Sign::Positive,
Ok(Number::Fixnum(n)) => n.get_num() >= 0,
Ok(Number::Float(f)) => f >= OrderedFloat(0f64),
Ok(Number::Rational(r)) => &*r >= &Rational::from(0),
Ok(Number::Rational(r)) => (*r).sign() == Sign::Positive,
_ => false,
}
}) && needs_bracketing(op_desc, op)

View File

@@ -1,7 +1,5 @@
use dashu::base::Abs;
use dashu::base::DivRem;
use dashu::base::Gcd;
use dashu::integer::IBig;
use divrem::*;
use num_order::NumOrd;
@@ -562,7 +560,7 @@ pub(crate) fn rdiv(
r1: TypedArenaPtr<Rational>,
r2: TypedArenaPtr<Rational>,
) -> Result<Rational, MachineStubGen> {
if &*r2 == &Rational::from(0) {
if r2.is_zero() {
let stub_gen = || {
let rdiv_atom = atom!("rdiv");
functor_stub(rdiv_atom, 2)
@@ -596,7 +594,7 @@ pub(crate) fn idiv(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number,
}
}
(Number::Fixnum(n1), Number::Integer(n2)) => {
if (&*n2).num_eq(&0) {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
Ok(Number::arena_from(Integer::from(n1) / &*n2, arena))
@@ -610,13 +608,10 @@ pub(crate) fn idiv(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number,
}
}
(Number::Integer(n1), Number::Integer(n2)) => {
if (&*n2).num_eq(&0) {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
Ok(Number::arena_from(
<(Integer, Integer)>::from((&*n1).div_rem(&*n2)).0,
arena,
))
Ok(Number::arena_from(&*n1 / &*n2, arena))
}
}
(Number::Fixnum(_), n2) | (Number::Integer(_), n2) => {
@@ -853,6 +848,16 @@ pub(crate) fn modulus(x: Number, y: Number, arena: &mut Arena) -> Result<Number,
functor_stub(mod_atom, 2)
};
fn ibig_rem_floor(n1: &Integer, n2: &Integer) -> Integer {
if n1 > &Integer::ZERO && n2 < &Integer::ZERO {
((n1 - Integer::ONE) / n2) - Integer::ONE
} else if n1 < &Integer::ZERO && n2 > &Integer::ZERO {
((n1 + Integer::ONE) / n2) - Integer::ONE
} else {
n1 / n2
}
}
match (x, y) {
(Number::Fixnum(n1), Number::Fixnum(n2)) => {
let n2_i = n2.get_num();
@@ -865,14 +870,11 @@ pub(crate) fn modulus(x: Number, y: Number, arena: &mut Arena) -> Result<Number,
}
}
(Number::Fixnum(n1), Number::Integer(n2)) => {
if (&*n2).num_eq(&0) {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
let n1 = Integer::from(n1.get_num());
Ok(Number::arena_from(
<(Integer, Integer)>::from(n1.div_rem(&*n2)).1,
arena,
))
Ok(Number::arena_from(ibig_rem_floor(&n1, &*n2), arena))
}
}
(Number::Integer(n1), Number::Fixnum(n2)) => {
@@ -882,20 +884,14 @@ pub(crate) fn modulus(x: Number, y: Number, arena: &mut Arena) -> Result<Number,
Err(zero_divisor_eval_error(stub_gen))
} else {
let n2 = Integer::from(n2_i);
Ok(Number::arena_from(
<(Integer, Integer)>::from((&*n1).div_rem(&n2)).1,
arena,
))
Ok(Number::arena_from(ibig_rem_floor(&*n1, &n2), arena))
}
}
(Number::Integer(x), Number::Integer(y)) => {
if (&*y).num_eq(&0) {
(Number::Integer(n1), Number::Integer(n2)) => {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
Ok(Number::arena_from(
<(Integer, Integer)>::from((&*x).div_rem(&*y)).1,
arena,
))
Ok(Number::arena_from(ibig_rem_floor(&*n1, &*n2), arena))
}
}
(Number::Integer(_), n2) | (Number::Fixnum(_), n2) => {
@@ -923,7 +919,7 @@ pub(crate) fn remainder(x: Number, y: Number, arena: &mut Arena) -> Result<Numbe
}
}
(Number::Fixnum(n1), Number::Integer(n2)) => {
if (&*n2).num_eq(&0) {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
let n1 = Integer::from(n1.get_num());
@@ -941,7 +937,7 @@ pub(crate) fn remainder(x: Number, y: Number, arena: &mut Arena) -> Result<Numbe
}
}
(Number::Integer(n1), Number::Integer(n2)) => {
if (&*n2).num_eq(&0) {
if n2.is_zero() {
Err(zero_divisor_eval_error(stub_gen))
} else {
Ok(Number::arena_from(Integer::from(&*n1 % &*n2), arena))
@@ -968,7 +964,7 @@ pub(crate) fn gcd(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
if let Some(result) = isize_gcd(n1_i, n2_i) {
Ok(Number::arena_from(result, arena))
} else {
let value: IBig = Integer::from(n1_i).gcd(&Integer::from(n2_i)).into();
let value: Integer = Integer::from(n1_i).gcd(&Integer::from(n2_i)).into();
Ok(Number::arena_from(value, arena))
}
}
@@ -978,9 +974,9 @@ pub(crate) fn gcd(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
Ok(Number::arena_from(Integer::from(n2_clone.gcd(&n1)), arena))
}
(Number::Integer(n1), Number::Integer(n2)) => {
let n1_clone: Integer = (*n1).clone();
let n2: isize = (&*n2).try_into().unwrap();
Ok(Number::arena_from(Integer::from(n1_clone.gcd(&Integer::from(n2))) as IBig, arena))
let value: Integer = (&*n1).gcd(&Integer::from(n2)).into();
Ok(Number::arena_from(value, arena))
}
(Number::Float(f), _) | (_, Number::Float(f)) => {
let n = Number::Float(f);

View File

@@ -1636,7 +1636,7 @@ impl Machine {
let arity = self.deref_register(3);
let arity = match Number::try_from(arity) {
Ok(Number::Integer(n)) if &*n >= &Integer::from(0) && &*n <= &Integer::from(MAX_ARITY) => {
Ok(Number::Integer(n)) if &*n >= &Integer::ZERO && &*n <= &Integer::from(MAX_ARITY) => {
let value: usize = (&*n).try_into().unwrap();
Ok(value)
},

View File

@@ -16,7 +16,6 @@ use crate::parser::dashu::{Integer, Rational};
use crate::types::*;
use indexmap::IndexSet;
use num_order::NumOrd;
use std::cmp::Ordering;
use std::convert::TryFrom;
@@ -1183,10 +1182,7 @@ impl MachineState {
let n = match n {
Number::Fixnum(n) => n.get_num() as usize,
Number::Integer(n) if (*n).num_ge(&0) && (*n).num_le(&std::usize::MAX) => {
let value: usize = (&*n).try_into().unwrap();
value
},
Number::Integer(n) if usize::try_from(&*n).is_ok() => (&*n).try_into().unwrap(),
_ => {
self.fail = true;
return Ok(());

View File

@@ -58,10 +58,7 @@ fn setup_predicate_indicator(term: &mut Term) -> Result<PredicateKey, Compilatio
let name = terms.pop().unwrap();
let arity = match arity {
Term::Literal(_, Literal::Integer(n)) => {
let value: usize = (&*n).try_into().unwrap();
Some(value)
},
Term::Literal(_, Literal::Integer(n)) => (&*n).try_into().ok(),
Term::Literal(_, Literal::Fixnum(n)) => usize::try_from(n.get_num()).ok(),
_ => None,
}