wip dashu move

This commit is contained in:
Fayeed Pawaskar
2023-07-17 20:40:41 +05:30
parent 5d09449c95
commit cf345d8174
17 changed files with 165 additions and 38 deletions

104
Cargo.lock generated
View File

@@ -313,6 +313,79 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "derive_deref" name = "derive_deref"
version = "1.1.1" version = "1.1.1"
@@ -1153,6 +1226,36 @@ dependencies = [
"winapi", "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]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.15" version = "0.2.15"
@@ -1669,6 +1772,7 @@ dependencies = [
"crossterm", "crossterm",
"crrl", "crrl",
"ctrlc", "ctrlc",
"dashu",
"derive_deref", "derive_deref",
"dirs-next", "dirs-next",
"divrem", "divrem",

View File

@@ -68,6 +68,7 @@ futures = "0.3"
libffi = "3.1.0" libffi = "3.1.0"
libloading = "0.7" libloading = "0.7"
derive_deref = "1.1.1" derive_deref = "1.1.1"
dashu = "0.3.0"
[dev-dependencies] [dev-dependencies]
assert_cmd = "1.0.3" assert_cmd = "1.0.3"

View File

@@ -6,7 +6,7 @@ use crate::raw_block::*;
use crate::read::*; use crate::read::*;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use std::alloc; use std::alloc;
use std::fmt; use std::fmt;
@@ -252,6 +252,26 @@ impl<T: ?Sized + ArenaAllocated> TypedArenaPtr<T> {
self.0.as_ptr() self.0.as_ptr()
} }
#[inline]
pub fn to_i64(&self) -> Option<i64> {
self.to_i64()
}
#[inline]
pub fn to_u32(&self) -> Option<u32> {
self.to_u32()
}
#[inline]
pub fn to_usize(&self) -> Option<usize> {
self.to_usize()
}
#[inline]
pub fn to_isize(&self) -> Option<isize> {
self.to_isize()
}
#[inline] #[inline]
pub fn header_ptr(&self) -> *const ArenaHeader { pub fn header_ptr(&self) -> *const ArenaHeader {
let mut ptr = self.as_ptr() as *const u8 as usize; let mut ptr = self.as_ptr() as *const u8 as usize;
@@ -788,7 +808,7 @@ mod tests {
use crate::machine::partial_string::*; use crate::machine::partial_string::*;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
#[test] #[test]
fn float_ptr_cast() { fn float_ptr_cast() {
@@ -889,7 +909,7 @@ mod tests {
// rational // rational
let big_rat = 2 * Rational::from(1u64 << 63); let big_rat = Rational::from(2) * Rational::from(1u64 << 63);
let big_rat_ptr: TypedArenaPtr<Rational> = arena_alloc!(big_rat, &mut wam.machine_st.arena); let big_rat_ptr: TypedArenaPtr<Rational> = arena_alloc!(big_rat, &mut wam.machine_st.arena);
assert!(!big_rat_ptr.as_ptr().is_null()); assert!(!big_rat_ptr.as_ptr().is_null());
@@ -915,7 +935,7 @@ mod tests {
(HeapCellValueTag::Cons, cons_ptr) => { (HeapCellValueTag::Cons, cons_ptr) => {
match_untyped_arena_ptr!(cons_ptr, match_untyped_arena_ptr!(cons_ptr,
(ArenaHeaderTag::Rational, n) => { (ArenaHeaderTag::Rational, n) => {
assert_eq!(&*n, &(2 * Rational::from(1u64 << 63))); assert_eq!(&*n, &(Rational::from(2) * Rational::from(1u64 << 63)));
} }
_ => unreachable!() _ => unreachable!()
) )

View File

@@ -10,7 +10,8 @@ use crate::types::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::ops::PowAssign; 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::*; 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 { if I64_MIN_TO_F <= f && f <= I64_MAX_TO_F {
fixnum!(Number, f.into_inner() as i64, arena) fixnum!(Number, f.into_inner() as i64, arena)
} else { } 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) => { &Number::Rational(ref r) => {
let r_ref = r.fract_floor_ref(); 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); (&mut fract, &mut floor).assign(r_ref);
if let Some(floor) = floor.to_i64() { if let Some(floor) = floor.to_i64() {
@@ -405,9 +406,9 @@ impl From<Fixnum> for Integer {
pub(crate) fn rnd_f(n: &Number) -> f64 { pub(crate) fn rnd_f(n: &Number) -> f64 {
match n { match n {
&Number::Fixnum(n) => n.get_num() as f64, &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::Float(OrderedFloat(f)) => f,
&Number::Rational(ref r) => r.to_f64(), &Number::Rational(ref r) => r.to_f64().value(),
} }
} }

View File

@@ -8,7 +8,7 @@ use crate::machine::machine_errors::*;
use crate::machine::machine_indices::*; use crate::machine::machine_indices::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::parser::CompositeOpDesc; use crate::parser::parser::CompositeOpDesc;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use crate::types::*; use crate::types::*;
use fxhash::FxBuildHasher; use fxhash::FxBuildHasher;
@@ -765,9 +765,9 @@ impl Number {
pub(crate) fn is_positive(&self) -> bool { pub(crate) fn is_positive(&self) -> bool {
match self { match self {
&Number::Fixnum(n) => n.get_num() > 0, &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::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 { pub(crate) fn is_negative(&self) -> bool {
match self { match self {
&Number::Fixnum(n) => n.get_num() < 0, &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::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 { pub(crate) fn is_zero(&self) -> bool {
match self { match self {
&Number::Fixnum(n) => n.get_num() == 0, &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::Float(f) => f == OrderedFloat(0f64) || f == OrderedFloat(-0f64),
&Number::Rational(ref r) => &**r == &0, &Number::Rational(ref r) => &**r == &Rational::from(0),
} }
} }

View File

@@ -1,7 +1,7 @@
use crate::arena::*; use crate::arena::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use crate::{ use crate::{
alpha_numeric_char, capital_letter_char, cut_char, decimal_digit_char, graphic_token_char, 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, 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 { fn is_negative(&self) -> bool {
match self { match self {
NumberFocus::Unfocused(n) => n.is_negative(), 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) { && iter.leftmost_leaf_has_property(op_dir, |addr| match Number::try_from(addr) {
Ok(Number::Fixnum(n)) => n.get_num() > 0, Ok(Number::Fixnum(n)) => n.get_num() > 0,
Ok(Number::Float(f)) => f > OrderedFloat(0f64), Ok(Number::Float(f)) => f > OrderedFloat(0f64),
Ok(Number::Integer(n)) => &*n > &0, Ok(Number::Integer(n)) => &*n > &Integer::from(0),
Ok(Number::Rational(n)) => &*n > &0, Ok(Number::Rational(n)) => &*n > &Rational::from(0),
_ => false, _ => false,
}) })
} else { } else {
@@ -514,7 +514,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
let j = n.div_rem_floor(Integer::from(26)); let j = n.div_rem_floor(Integer::from(26));
let j = <(Integer, Integer)>::from(j).0; let j = <(Integer, Integer)>::from(j).0;
if j == 0 { if j == Integer::from(0) {
CHAR_CODES[i].to_string() CHAR_CODES[i].to_string()
} else { } else {
format!("{}{}", CHAR_CODES[i], j) format!("{}{}", CHAR_CODES[i], j)
@@ -530,7 +530,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
} }
} }
Ok(Number::Integer(n)) => { Ok(Number::Integer(n)) => {
if &*n >= &0 { if &*n >= &Integer::from(0) {
Some(numbervar(Integer::from(offset + &*n))) Some(numbervar(Integer::from(offset + &*n)))
} else { } else {
None None
@@ -1307,10 +1307,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
if self.numbervars && arity == 1 && name == atom!("$VAR") { if self.numbervars && arity == 1 && name == atom!("$VAR") {
!self.iter.immediate_leaf_has_property(|addr| { !self.iter.immediate_leaf_has_property(|addr| {
match Number::try_from(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::Fixnum(n)) => n.get_num() >= 0,
Ok(Number::Float(f)) => f >= OrderedFloat(0f64), Ok(Number::Float(f)) => f >= OrderedFloat(0f64),
Ok(Number::Rational(r)) => &*r >= &0, Ok(Number::Rational(r)) => &*r >= &Integer::from(0),
_ => false, _ => false,
} }
}) && needs_bracketing(op_desc, op) }) && needs_bracketing(op_desc, op)

View File

@@ -8,7 +8,7 @@ use crate::heap_iter::*;
use crate::machine::machine_errors::*; use crate::machine::machine_errors::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use crate::types::*; use crate::types::*;
use crate::fixnum; use crate::fixnum;
@@ -338,7 +338,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result<Numbe
(Number::Fixnum(n1), Number::Integer(n2)) => { (Number::Fixnum(n1), Number::Integer(n2)) => {
let n1_i = n1.get_num(); 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); let n = Number::Fixnum(n1);
Err(numerical_type_error(ValidType::Float, n, stub_gen)) Err(numerical_type_error(ValidType::Float, n, stub_gen))
} else { } else {
@@ -349,7 +349,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result<Numbe
(Number::Integer(n1), Number::Fixnum(n2)) => { (Number::Integer(n1), Number::Fixnum(n2)) => {
let n2_i = n2.get_num(); 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); let n = Number::Integer(n1);
Err(numerical_type_error(ValidType::Float, n, stub_gen)) Err(numerical_type_error(ValidType::Float, n, stub_gen))
} else { } else {
@@ -358,7 +358,7 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result<Numbe
} }
} }
(Number::Integer(n1), Number::Integer(n2)) => { (Number::Integer(n1), Number::Integer(n2)) => {
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); let n = Number::Integer(n1);
Err(numerical_type_error(ValidType::Float, n, stub_gen)) Err(numerical_type_error(ValidType::Float, n, stub_gen))
} else { } else {

View File

@@ -6,7 +6,7 @@ use crate::machine::loader::*;
use crate::machine::machine_errors::CompilationError; use crate::machine::machine_errors::CompilationError;
use crate::machine::preprocessor::*; use crate::machine::preprocessor::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::Rational; use crate::parser::dashu::{Rational, Integer};
use crate::variable_records::*; use crate::variable_records::*;
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
@@ -236,7 +236,7 @@ fn merge_branch_seq<Iter: Iterator<Item = BranchInfo>>(branches: Iter) -> Branch
branch_info.chunks.extend(branch.chunks.drain(..)); 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.branch_num.branch_num -= &branch_info.branch_num.delta;
branch_info branch_info

View File

@@ -6,7 +6,7 @@ use crate::machine::partial_string::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::types::*; use crate::types::*;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use std::convert::TryFrom; use std::convert::TryFrom;

View File

@@ -1644,7 +1644,7 @@ impl Machine {
.store(self.machine_st.deref(self.machine_st.registers[3])); .store(self.machine_st.deref(self.machine_st.registers[3]));
let arity = match Number::try_from(arity) { 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(Number::Fixnum(n)) if n.get_num() >= 0 && n.get_num() <= MAX_ARITY as i64 => {
Ok(usize::try_from(n.get_num()).unwrap()) Ok(usize::try_from(n.get_num()).unwrap())
} }

View File

@@ -14,7 +14,7 @@ use crate::machine::streams::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::types::*; use crate::types::*;
use crate::parser::rug::Integer; use crate::parser::dashu::Integer;
use indexmap::IndexMap; use indexmap::IndexMap;

View File

@@ -13,7 +13,7 @@ use crate::machine::partial_string::*;
use crate::machine::stack::*; use crate::machine::stack::*;
use crate::machine::unify::*; use crate::machine::unify::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use indexmap::IndexSet; use indexmap::IndexSet;

View File

@@ -41,7 +41,7 @@ use crate::machine::machine_state::*;
use crate::machine::stack::*; use crate::machine::stack::*;
use crate::machine::streams::*; use crate::machine::streams::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use crate::types::*; use crate::types::*;
use indexmap::IndexMap; use indexmap::IndexMap;

View File

@@ -24,7 +24,7 @@ use crate::machine::preprocessor::to_op_decl;
use crate::machine::stack::*; use crate::machine::stack::*;
use crate::machine::streams::*; use crate::machine::streams::*;
use crate::parser::char_reader::*; use crate::parser::char_reader::*;
use crate::parser::rug::Integer; use crate::parser::dashu::Integer;
use crate::parser::rug::rand::RandState; use crate::parser::rug::rand::RandState;
use crate::read::*; use crate::read::*;
use crate::types::*; use crate::types::*;

View File

@@ -12,7 +12,7 @@ use std::ops::{Deref, Neg};
use std::rc::Rc; use std::rc::Rc;
use std::vec::Vec; use std::vec::Vec;
use crate::parser::rug::{Integer, Rational}; use crate::parser::dashu::{Integer, Rational};
use fxhash::FxBuildHasher; use fxhash::FxBuildHasher;
use indexmap::IndexMap; use indexmap::IndexMap;

View File

@@ -5,7 +5,7 @@ use crate::atom_table::*;
pub use crate::machine::machine_state::*; pub use crate::machine::machine_state::*;
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::char_reader::*; use crate::parser::char_reader::*;
use crate::parser::rug::Integer; use crate::parser::dashu::Integer;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt; use std::fmt;

View File

@@ -3,6 +3,7 @@ pub use num_rug_adapter as rug;
#[cfg(feature = "rug")] #[cfg(feature = "rug")]
pub use rug; pub use rug;
pub use dashu;
// #[macro_use] // #[macro_use]
// extern crate lazy_static; // extern crate lazy_static;