From: Fayeed Pawaskar Date: Mon, 17 Jul 2023 15:10:41 +0000 (+0530) Subject: wip dashu move X-Git-Tag: v0.9.2~23^2^2~7 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=cf345d817499caf989f41f4c1374b546199e09dc;p=scryer-prolog.git wip dashu move --- diff --git a/Cargo.lock b/Cargo.lock index 51a8fa49..46731842 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -313,6 +313,79 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "dashu" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51a1b5a00793e3ac2239993ef582603764bcb333a4d04c2a0944639a7e916c85" +dependencies = [ + "dashu-base", + "dashu-float", + "dashu-int", + "dashu-macros", + "dashu-ratio", +] + +[[package]] +name = "dashu-base" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f2585452b8ecf7c874045dba02a7914b7e5b2e3cdd5e152573413aa290197aa" + +[[package]] +name = "dashu-float" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a168f338914fab603c31a371207c8b3245ab5ff5e9a0f4fd64a9b5f8a972d1f" +dependencies = [ + "dashu-base", + "dashu-int", + "num-traits", + "rand", + "static_assertions", +] + +[[package]] +name = "dashu-int" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a1009a3ce0c4c64e977c5e7dd8c475278750e145cbb8956d1e28832f557975" +dependencies = [ + "cfg-if", + "dashu-base", + "num-order", + "num-traits", + "rand", + "static_assertions", +] + +[[package]] +name = "dashu-macros" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6fc2425b6724a7d5bfc8e57044e231803b5fc3a7283d6efbff34bfab6ebe014" +dependencies = [ + "dashu-base", + "dashu-float", + "dashu-int", + "dashu-ratio", + "proc-macro2", + "quote", +] + +[[package]] +name = "dashu-ratio" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4b57c839e72af6be14e5c55630ffc9429c9970aed48f7db449b2399467336b4" +dependencies = [ + "dashu-base", + "dashu-float", + "dashu-int", + "num-traits", + "rand", +] + [[package]] name = "derive_deref" version = "1.1.1" @@ -1153,6 +1226,36 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-modular" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a5fe11d4135c3bcdf3a95b18b194afa9608a5f6ff034f5d857bc9a27fb0119" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-order" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e81e321057a0370997b13e6638bba6bd7f6f426e1f8e9a2562490a28eb23e1bc" +dependencies = [ + "num-modular", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1669,6 +1772,7 @@ dependencies = [ "crossterm", "crrl", "ctrlc", + "dashu", "derive_deref", "dirs-next", "divrem", diff --git a/Cargo.toml b/Cargo.toml index 99218c71..ac9c98b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ futures = "0.3" libffi = "3.1.0" libloading = "0.7" derive_deref = "1.1.1" +dashu = "0.3.0" [dev-dependencies] assert_cmd = "1.0.3" diff --git a/src/arena.rs b/src/arena.rs index b055c8db..fbbcd72a 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -6,7 +6,7 @@ use crate::raw_block::*; use crate::read::*; use ordered_float::OrderedFloat; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use std::alloc; use std::fmt; @@ -252,6 +252,26 @@ impl TypedArenaPtr { self.0.as_ptr() } + #[inline] + pub fn to_i64(&self) -> Option { + self.to_i64() + } + + #[inline] + pub fn to_u32(&self) -> Option { + self.to_u32() + } + + #[inline] + pub fn to_usize(&self) -> Option { + self.to_usize() + } + + #[inline] + pub fn to_isize(&self) -> Option { + self.to_isize() + } + #[inline] pub fn header_ptr(&self) -> *const ArenaHeader { let mut ptr = self.as_ptr() as *const u8 as usize; @@ -788,7 +808,7 @@ mod tests { use crate::machine::partial_string::*; use ordered_float::OrderedFloat; - use crate::parser::rug::{Integer, Rational}; + use crate::parser::dashu::{Integer, Rational}; #[test] fn float_ptr_cast() { @@ -889,7 +909,7 @@ mod tests { // rational - let big_rat = 2 * Rational::from(1u64 << 63); + let big_rat = Rational::from(2) * Rational::from(1u64 << 63); let big_rat_ptr: TypedArenaPtr = arena_alloc!(big_rat, &mut wam.machine_st.arena); assert!(!big_rat_ptr.as_ptr().is_null()); @@ -915,7 +935,7 @@ mod tests { (HeapCellValueTag::Cons, cons_ptr) => { match_untyped_arena_ptr!(cons_ptr, (ArenaHeaderTag::Rational, n) => { - assert_eq!(&*n, &(2 * Rational::from(1u64 << 63))); + assert_eq!(&*n, &(Rational::from(2) * Rational::from(1u64 << 63))); } _ => unreachable!() ) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 7f15a7f1..1a3bdf48 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -10,7 +10,8 @@ use crate::types::*; use crate::parser::ast::*; use crate::parser::rug::ops::PowAssign; -use crate::parser::rug::{Assign, Integer, Rational}; +use crate::parser::rug::{Assign}; +use crate::parser::dashu::{Integer, Rational}; use crate::machine::machine_errors::*; @@ -377,12 +378,12 @@ pub(crate) fn rnd_i<'a>(n: &'a Number, arena: &mut Arena) -> Number { if I64_MIN_TO_F <= f && f <= I64_MAX_TO_F { fixnum!(Number, f.into_inner() as i64, arena) } else { - Number::Integer(arena_alloc!(Integer::from_f64(f.into_inner()).unwrap(), arena)) + Number::Integer(arena_alloc!(Integer::from(f.into_inner()).unwrap(), arena)) } } &Number::Rational(ref r) => { let r_ref = r.fract_floor_ref(); - let (mut fract, mut floor) = (Rational::new(), Integer::new()); + let (mut fract, mut floor) = (Rational::from(0), Integer::from(0)); (&mut fract, &mut floor).assign(r_ref); if let Some(floor) = floor.to_i64() { @@ -405,9 +406,9 @@ impl From for Integer { pub(crate) fn rnd_f(n: &Number) -> f64 { match n { &Number::Fixnum(n) => n.get_num() as f64, - &Number::Integer(ref n) => n.to_f64(), + &Number::Integer(ref n) => n.to_f64().value(), &Number::Float(OrderedFloat(f)) => f, - &Number::Rational(ref r) => r.to_f64(), + &Number::Rational(ref r) => r.to_f64().value(), } } diff --git a/src/forms.rs b/src/forms.rs index 627fb4b2..6cc5315f 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -8,7 +8,7 @@ use crate::machine::machine_errors::*; use crate::machine::machine_indices::*; use crate::parser::ast::*; use crate::parser::parser::CompositeOpDesc; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use crate::types::*; use fxhash::FxBuildHasher; @@ -765,9 +765,9 @@ impl Number { pub(crate) fn is_positive(&self) -> bool { match self { &Number::Fixnum(n) => n.get_num() > 0, - &Number::Integer(ref n) => &**n > &0, + &Number::Integer(ref n) => &**n > &Integer::from(0), &Number::Float(f) => f.is_sign_positive(), - &Number::Rational(ref r) => &**r > &0, + &Number::Rational(ref r) => &**r > &Rational::from(0), } } @@ -775,9 +775,9 @@ impl Number { pub(crate) fn is_negative(&self) -> bool { match self { &Number::Fixnum(n) => n.get_num() < 0, - &Number::Integer(ref n) => &**n < &0, + &Number::Integer(ref n) => &**n < &Integer::from(0), &Number::Float(OrderedFloat(f)) => f.is_sign_negative() && OrderedFloat(f) != -0f64, - &Number::Rational(ref r) => &**r < &0, + &Number::Rational(ref r) => &**r < &Rational::from(0), } } @@ -785,9 +785,9 @@ impl Number { pub(crate) fn is_zero(&self) -> bool { match self { &Number::Fixnum(n) => n.get_num() == 0, - &Number::Integer(ref n) => &**n == &0, + &Number::Integer(ref n) => &**n == &Integer::from(0), &Number::Float(f) => f == OrderedFloat(0f64) || f == OrderedFloat(-0f64), - &Number::Rational(ref r) => &**r == &0, + &Number::Rational(ref r) => &**r == &Rational::from(0), } } diff --git a/src/heap_print.rs b/src/heap_print.rs index 4e7781b7..cf2c293c 100644 --- a/src/heap_print.rs +++ b/src/heap_print.rs @@ -1,7 +1,7 @@ use crate::arena::*; use crate::atom_table::*; use crate::parser::ast::*; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; 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, @@ -196,7 +196,7 @@ impl NumberFocus { fn is_negative(&self) -> bool { match self { NumberFocus::Unfocused(n) => n.is_negative(), - NumberFocus::Denominator(r) | NumberFocus::Numerator(r) => **r < 0, + NumberFocus::Denominator(r) | NumberFocus::Numerator(r) => **r < Rational::from(0), } } } @@ -400,8 +400,8 @@ fn negated_op_needs_bracketing( && iter.leftmost_leaf_has_property(op_dir, |addr| match Number::try_from(addr) { Ok(Number::Fixnum(n)) => n.get_num() > 0, Ok(Number::Float(f)) => f > OrderedFloat(0f64), - Ok(Number::Integer(n)) => &*n > &0, - Ok(Number::Rational(n)) => &*n > &0, + Ok(Number::Integer(n)) => &*n > &Integer::from(0), + Ok(Number::Rational(n)) => &*n > &Rational::from(0), _ => false, }) } else { @@ -514,7 +514,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option let j = n.div_rem_floor(Integer::from(26)); let j = <(Integer, Integer)>::from(j).0; - if j == 0 { + if j == Integer::from(0) { CHAR_CODES[i].to_string() } else { format!("{}{}", CHAR_CODES[i], j) @@ -530,7 +530,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option } } Ok(Number::Integer(n)) => { - if &*n >= &0 { + if &*n >= &Integer::from(0) { Some(numbervar(Integer::from(offset + &*n))) } else { None @@ -1307,10 +1307,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 >= &0, + Ok(Number::Integer(n)) => &*n >= &Integer::from(0), Ok(Number::Fixnum(n)) => n.get_num() >= 0, Ok(Number::Float(f)) => f >= OrderedFloat(0f64), - Ok(Number::Rational(r)) => &*r >= &0, + Ok(Number::Rational(r)) => &*r >= &Integer::from(0), _ => false, } }) && needs_bracketing(op_desc, op) diff --git a/src/machine/arithmetic_ops.rs b/src/machine/arithmetic_ops.rs index f5aa982f..0c948bf6 100644 --- a/src/machine/arithmetic_ops.rs +++ b/src/machine/arithmetic_ops.rs @@ -8,7 +8,7 @@ use crate::heap_iter::*; use crate::machine::machine_errors::*; use crate::machine::machine_state::*; use crate::parser::ast::*; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use crate::types::*; use crate::fixnum; @@ -338,7 +338,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result { let n1_i = n1.get_num(); - if !(n1_i == 1 || n1_i == 0 || n1_i == -1) && &*n2 < &0 { + if !(n1_i == 1 || n1_i == 0 || n1_i == -1) && &*n2 < &Integer::from(0) { let n = Number::Fixnum(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { @@ -349,7 +349,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result { let n2_i = n2.get_num(); - if !(&*n1 == &1 || &*n1 == &0 || &*n1 == &-1) && n2_i < 0 { + if !(&*n1 == &Integer::from(1) || &*n1 == &Integer::from(0) || &*n1 == &Integer::from(-1)) && n2_i < 0 { let n = Number::Integer(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { @@ -358,7 +358,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result { - if !(&*n1 == &1 || &*n1 == &0 || &*n1 == &-1) && &*n2 < &0 { + if !(&*n1 == &Integer::from(1) || &*n1 == &Integer::from(0) || &*n1 == &Integer::from(-1)) && &*n2 < &Integer::from(0) { let n = Number::Integer(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { diff --git a/src/machine/disjuncts.rs b/src/machine/disjuncts.rs index f2a66851..33d24a06 100644 --- a/src/machine/disjuncts.rs +++ b/src/machine/disjuncts.rs @@ -6,7 +6,7 @@ use crate::machine::loader::*; use crate::machine::machine_errors::CompilationError; use crate::machine::preprocessor::*; use crate::parser::ast::*; -use crate::parser::rug::Rational; +use crate::parser::dashu::{Rational, Integer}; use crate::variable_records::*; use indexmap::{IndexMap, IndexSet}; @@ -236,7 +236,7 @@ fn merge_branch_seq>(branches: Iter) -> Branch branch_info.chunks.extend(branch.chunks.drain(..)); } - branch_info.branch_num.delta *= 2; + branch_info.branch_num.delta = branch_info.branch_num.delta * Integer::from(2); branch_info.branch_num.branch_num -= &branch_info.branch_num.delta; branch_info diff --git a/src/machine/heap.rs b/src/machine/heap.rs index 59e63009..1cdfeb74 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -6,7 +6,7 @@ use crate::machine::partial_string::*; use crate::parser::ast::*; use crate::types::*; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use std::convert::TryFrom; diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 5a309197..d5b3a15f 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -1644,7 +1644,7 @@ impl Machine { .store(self.machine_st.deref(self.machine_st.registers[3])); let arity = match Number::try_from(arity) { - Ok(Number::Integer(n)) if &*n >= &0 && &*n <= &MAX_ARITY => Ok(n.to_usize().unwrap()), + Ok(Number::Integer(n)) if &*n >= &Integer::from(0) && &*n <= &Integer::from(MAX_ARITY) => Ok(n.to_usize().unwrap()), Ok(Number::Fixnum(n)) if n.get_num() >= 0 && n.get_num() <= MAX_ARITY as i64 => { Ok(usize::try_from(n.get_num()).unwrap()) } diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index 78b1a7f5..768e4492 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -14,7 +14,7 @@ use crate::machine::streams::*; use crate::parser::ast::*; use crate::types::*; -use crate::parser::rug::Integer; +use crate::parser::dashu::Integer; use indexmap::IndexMap; diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index b4dc3fea..f0f27fcd 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -13,7 +13,7 @@ use crate::machine::partial_string::*; use crate::machine::stack::*; use crate::machine::unify::*; use crate::parser::ast::*; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use indexmap::IndexSet; diff --git a/src/machine/mod.rs b/src/machine/mod.rs index ddf64d34..a212b9ea 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -41,7 +41,7 @@ use crate::machine::machine_state::*; use crate::machine::stack::*; use crate::machine::streams::*; use crate::parser::ast::*; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use crate::types::*; use indexmap::IndexMap; diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 4c9c0f2f..84f86cec 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -24,7 +24,7 @@ use crate::machine::preprocessor::to_op_decl; use crate::machine::stack::*; use crate::machine::streams::*; use crate::parser::char_reader::*; -use crate::parser::rug::Integer; +use crate::parser::dashu::Integer; use crate::parser::rug::rand::RandState; use crate::read::*; use crate::types::*; diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 272d5b7e..73f5b4e5 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -12,7 +12,7 @@ use std::ops::{Deref, Neg}; use std::rc::Rc; use std::vec::Vec; -use crate::parser::rug::{Integer, Rational}; +use crate::parser::dashu::{Integer, Rational}; use fxhash::FxBuildHasher; use indexmap::IndexMap; diff --git a/src/parser/lexer.rs b/src/parser/lexer.rs index 3fbddc1d..92b78050 100644 --- a/src/parser/lexer.rs +++ b/src/parser/lexer.rs @@ -5,7 +5,7 @@ use crate::atom_table::*; pub use crate::machine::machine_state::*; use crate::parser::ast::*; use crate::parser::char_reader::*; -use crate::parser::rug::Integer; +use crate::parser::dashu::Integer; use std::convert::TryFrom; use std::fmt; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index fa7b8859..dccaa49b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3,6 +3,7 @@ pub use num_rug_adapter as rug; #[cfg(feature = "rug")] pub use rug; +pub use dashu; // #[macro_use] // extern crate lazy_static;