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

View File

@@ -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<T: ?Sized + ArenaAllocated> TypedArenaPtr<T> {
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]
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<Rational> = 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!()
)

View File

@@ -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<Fixnum> 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(),
}
}

View File

@@ -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),
}
}

View File

@@ -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<String>
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<String>
}
}
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)

View File

@@ -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<Numbe
(Number::Fixnum(n1), Number::Integer(n2)) => {
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<Numbe
(Number::Integer(n1), Number::Fixnum(n2)) => {
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<Numbe
}
}
(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);
Err(numerical_type_error(ValidType::Float, n, stub_gen))
} else {

View File

@@ -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<Iter: Iterator<Item = BranchInfo>>(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

View File

@@ -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;

View File

@@ -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())
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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::*;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;