Skip to content

Commit

Permalink
Merge pull request #2 from coasys/adam-dashu
Browse files Browse the repository at this point in the history
Adam compatible dashu implementation
  • Loading branch information
lucksus committed Jul 27, 2023
2 parents 9fd6e18 + 1943b48 commit 625c2f0
Show file tree
Hide file tree
Showing 20 changed files with 912 additions and 595 deletions.
1,178 changes: 715 additions & 463 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ build = "build/main.rs"
rust-version = "1.61"

[features]
default = ["rug"]

[build-dependencies]
indexmap = "1.0.2"
Expand Down Expand Up @@ -43,7 +42,6 @@ ctrlc = "3.2.2"
ordered-float = "2.6.0"
phf = { version = "0.9", features = ["macros"] }
ref_thread_local = "0.0.0"
rug = { version = "1.15.0", optional = true }
rustyline = "9.0.0"
ring = "0.16.13"
ripemd160 = "0.8.0"
Expand All @@ -63,6 +61,14 @@ hyper = { version = "0.14", features = ["full"] }
hyper-tls = "0.5.0"
tokio = { version = "1", features = ["full"] }
futures = "0.3"
libloading = "0.7"
derive_deref = "1.1.1"
http-body-util = "0.1.0-rc.2"
bytes = "1"
reqwest = { version = "0.11.18", features = ["blocking"] }
dashu = { git = "https://github.com/coasys/dashu.git" }
libffi = { git = "https://github.com/coasys/libffi-rs.git", branch = "windows-space" }
rand = "0.8.5"
regex = "1.9.1"

[dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -788,7 +788,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() {
Expand Down Expand Up @@ -889,7 +889,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());
Expand All @@ -915,7 +915,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!()
)
Expand Down
38 changes: 18 additions & 20 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::targets::QueryInstruction;
use crate::types::*;

use crate::parser::ast::*;
use crate::parser::rug::ops::PowAssign;
use crate::parser::rug::{Assign, Integer, Rational};
use crate::parser::dashu::{Integer, Rational};

use crate::machine::machine_errors::*;

use dashu::base::Abs;
use ordered_float::*;

use std::cell::Cell;
Expand Down Expand Up @@ -383,13 +383,11 @@ 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.0 as i64), arena))
}
}
&Number::Rational(ref r) => {
let r_ref = r.fract_floor_ref();
let (mut fract, mut floor) = (Rational::new(), Integer::new());
(&mut fract, &mut floor).assign(r_ref);
let (_, floor) = (r.fract(), r.floor());

if let Some(floor) = floor.to_i64() {
fixnum!(Number, floor, arena)
Expand All @@ -411,9 +409,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(),
}
}

Expand Down Expand Up @@ -444,12 +442,12 @@ pub(crate) fn float_fn_to_f(n: i64) -> Result<f64, EvalError> {

#[inline]
pub(crate) fn float_i_to_f(n: &Integer) -> Result<f64, EvalError> {
classify_float(n.to_f64())
classify_float(n.to_f64().value())
}

#[inline]
pub(crate) fn float_r_to_f(r: &Rational) -> Result<f64, EvalError> {
classify_float(r.to_f64())
classify_float(r.to_f64().value())
}

#[inline]
Expand Down Expand Up @@ -548,8 +546,8 @@ impl PartialEq for Number {
(&Number::Fixnum(n1), &Number::Float(n2)) => OrderedFloat(n1.get_num() as f64).eq(&n2),
(&Number::Float(n1), &Number::Fixnum(n2)) => n1.eq(&OrderedFloat(n2.get_num() as f64)),
(&Number::Integer(ref n1), &Number::Integer(ref n2)) => n1.eq(n2),
(&Number::Integer(ref n1), Number::Float(n2)) => OrderedFloat(n1.to_f64()).eq(n2),
(&Number::Float(n1), &Number::Integer(ref n2)) => n1.eq(&OrderedFloat(n2.to_f64())),
(&Number::Integer(ref n1), Number::Float(n2)) => OrderedFloat(n1.to_f64().value()).eq(n2),
(&Number::Float(n1), &Number::Integer(ref n2)) => n1.eq(&OrderedFloat(n2.to_f64().value())),
(&Number::Integer(ref n1), &Number::Rational(ref n2)) => {
#[cfg(feature = "num")]
{
Expand All @@ -570,8 +568,8 @@ impl PartialEq for Number {
&**n1 == &**n2
}
}
(&Number::Rational(ref n1), &Number::Float(n2)) => OrderedFloat(n1.to_f64()).eq(&n2),
(&Number::Float(n1), &Number::Rational(ref n2)) => n1.eq(&OrderedFloat(n2.to_f64())),
(&Number::Rational(ref n1), &Number::Float(n2)) => OrderedFloat(n1.to_f64().value()).eq(&n2),
(&Number::Float(n1), &Number::Rational(ref n2)) => n1.eq(&OrderedFloat(n2.to_f64().value())),
(&Number::Float(f1), &Number::Float(f2)) => f1.eq(&f2),
(&Number::Rational(ref r1), &Number::Rational(ref r2)) => r1.eq(&r2),
}
Expand Down Expand Up @@ -639,8 +637,8 @@ impl Ord for Number {
(&Number::Fixnum(n1), &Number::Float(n2)) => OrderedFloat(n1.get_num() as f64).cmp(&n2),
(&Number::Float(n1), &Number::Fixnum(n2)) => n1.cmp(&OrderedFloat(n2.get_num() as f64)),
(&Number::Integer(n1), &Number::Integer(n2)) => (*n1).cmp(&*n2),
(&Number::Integer(n1), Number::Float(n2)) => OrderedFloat(n1.to_f64()).cmp(n2),
(&Number::Float(n1), &Number::Integer(ref n2)) => n1.cmp(&OrderedFloat(n2.to_f64())),
(&Number::Integer(n1), Number::Float(n2)) => OrderedFloat(n1.to_f64().value()).cmp(n2),
(&Number::Float(n1), &Number::Integer(ref n2)) => n1.cmp(&OrderedFloat(n2.to_f64().value())),
(&Number::Integer(n1), &Number::Rational(n2)) => {
#[cfg(feature = "num")]
{
Expand All @@ -661,8 +659,8 @@ impl Ord for Number {
(&*n1).partial_cmp(&*n2).unwrap_or(Ordering::Less)
}
}
(&Number::Rational(n1), &Number::Float(n2)) => OrderedFloat(n1.to_f64()).cmp(&n2),
(&Number::Float(n1), &Number::Rational(n2)) => n1.cmp(&OrderedFloat(n2.to_f64())),
(&Number::Rational(n1), &Number::Float(n2)) => OrderedFloat(n1.to_f64().value()).cmp(&n2),
(&Number::Float(n1), &Number::Rational(n2)) => n1.cmp(&OrderedFloat(n2.to_f64().value())),
(&Number::Float(f1), &Number::Float(f2)) => f1.cmp(&f2),
(&Number::Rational(r1), &Number::Rational(r2)) => (*r1).cmp(&*r2),
}
Expand Down Expand Up @@ -703,7 +701,7 @@ 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_ref());
let mut power = Integer::from(power.abs());

if power == 0 {
return Integer::from(1);
Expand All @@ -716,7 +714,7 @@ pub(crate) fn binary_pow(mut n: Integer, power: &Integer) -> Integer {
oddand *= &n;
}

n.pow_assign(2);
n = n.pow(2);
power >>= 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/bin/scryer-prolog.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
fn main() {
use std::sync::atomic::Ordering;
use scryer_prolog::*;

use scryer_prolog::machine::mock_wam::Atom;

ctrlc::set_handler(move || {
scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed);
}).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions src/forms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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;
Expand Down Expand Up @@ -661,29 +661,29 @@ 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),
}
}

#[inline]
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),
}
}

#[inline]
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),
}
}

Expand Down
28 changes: 16 additions & 12 deletions src/heap_print.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,6 +17,8 @@ use crate::machine::partial_string::*;
use crate::machine::streams::*;
use crate::types::*;

use dashu::base::DivRem;
use dashu::base::DivRemEuclid;
use ordered_float::OrderedFloat;

use indexmap::IndexMap;
Expand Down Expand Up @@ -188,7 +190,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),
}
}
}
Expand Down Expand Up @@ -392,8 +394,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 {
Expand Down Expand Up @@ -499,11 +501,13 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

let i = n.mod_u(26) as usize;
let j = n.div_rem_floor(Integer::from(26));
let n_clone: Integer = n.clone();

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 == 0 {
if j == Integer::from(0) {
CHAR_CODES[i].to_string()
} else {
format!("{}{}", CHAR_CODES[i], j)
Expand All @@ -519,7 +523,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
Expand Down Expand Up @@ -974,14 +978,14 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
}
},
NumberFocus::Denominator(r) => {
let output_str = format!("{}", r.denom());
let output_str = format!("{}", r.denominator());

push_space_if_amb!(self, &output_str, {
append_str!(self, &output_str);
});
}
NumberFocus::Numerator(r) => {
let output_str = format!("{}", r.numer());
let output_str = format!("{}", r.numerator());

push_space_if_amb!(self, &output_str, {
append_str!(self, &output_str);
Expand Down Expand Up @@ -1310,10 +1314,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)
Expand Down

0 comments on commit 625c2f0

Please sign in to comment.