|
|
|
@ -10,14 +10,35 @@ use crsn::sexp; |
|
|
|
|
use crsn::sexp::Sexp; |
|
|
|
|
use crsn::utils::A; |
|
|
|
|
|
|
|
|
|
use crate::defs::ArithOp; |
|
|
|
|
use crate::defs::{ArithOp, FloatToIntMode}; |
|
|
|
|
use crsn::asm::instr::cond::Flag; |
|
|
|
|
use rand::Rng; |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn f2u(f: f64) -> u64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn u2f(f: u64) -> f64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn i2u(f: i64) -> u64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn u2i(f: u64) -> i64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl OpTrait for ArithOp { |
|
|
|
|
fn execute(&self, _ti: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { |
|
|
|
|
let eres = EvalRes::default(); |
|
|
|
|
match self { |
|
|
|
|
// Integers
|
|
|
|
|
ArithOp::Test { a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let res = state.read(a)?; |
|
|
|
@ -142,6 +163,122 @@ impl OpTrait for ArithOp { |
|
|
|
|
state.write(dst, remainder)?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Floating point
|
|
|
|
|
|
|
|
|
|
ArithOp::FloatToInt { dst, a, mode } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let val : f64 = u2f(state.read(a)?); |
|
|
|
|
let res = i2u(match mode { |
|
|
|
|
FloatToIntMode::Floor => val.floor(), |
|
|
|
|
FloatToIntMode::Ceil => val.ceil(), |
|
|
|
|
FloatToIntMode::Round => val.round(), |
|
|
|
|
} as i64); |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
ArithOp::IntToFloat { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let val : f64 = u2i(state.read(a)?) as f64; |
|
|
|
|
state.update_status_float(val); |
|
|
|
|
state.write(dst, f2u(val))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatTest { a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let res = u2f(state.read(a)?); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatCompare { a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let y = u2f(state.read(b)?); |
|
|
|
|
state.set_flag(Flag::Equal, x == y); |
|
|
|
|
state.set_flag(Flag::Lower, x < y); |
|
|
|
|
state.set_flag(Flag::Greater, x > y); |
|
|
|
|
// Test flags are set when both arguments have the property
|
|
|
|
|
if x == y { |
|
|
|
|
state.update_status_float(x); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatRangeTest { val, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let val = u2f(state.read(val)?); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let b = u2f(state.read(b)?); |
|
|
|
|
state.set_flag(Flag::Equal, val >= a && val <= b); |
|
|
|
|
state.set_flag(Flag::Lower, val < a); |
|
|
|
|
state.set_flag(Flag::Greater, val > b); |
|
|
|
|
state.update_status_float(val); |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatRng { dst, min, max } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let min = u2f(state.read(min)?); |
|
|
|
|
let max = u2f(state.read(max)?); |
|
|
|
|
|
|
|
|
|
let val = if min == max { |
|
|
|
|
min |
|
|
|
|
} else if min > max { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
min |
|
|
|
|
} else { |
|
|
|
|
rand::thread_rng() |
|
|
|
|
.gen_range(min, max) |
|
|
|
|
}; |
|
|
|
|
state.write(dst, f2u(val))?; |
|
|
|
|
state.update_status_float(val); |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAdd { dst, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let y = u2f(state.read(b)?); |
|
|
|
|
let res = x + y; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatSub { dst, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let y = u2f(state.read(b)?); |
|
|
|
|
let res = x - y; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatMul { dst, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let y = u2f(state.read(b)?); |
|
|
|
|
let res = x * y; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatDiv { dst, rem, a, div } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let d = u2f(state.read(div)?); |
|
|
|
|
if d == 0.0 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let res = x / d; |
|
|
|
|
let remainder = x % d; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
state.write(rem, f2u(remainder))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatMod { dst, a, div } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let d = u2f(state.read(div)?); |
|
|
|
|
if d == 0.0 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let rem = x % d; |
|
|
|
|
state.update_status_float(rem); |
|
|
|
|
state.write(dst, f2u(rem))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Bitwise
|
|
|
|
|
ArithOp::And { dst, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = state.read(a)?; |
|
|
|
@ -282,6 +419,8 @@ impl OpTrait for ArithOp { |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_ => unimplemented!() // TODO implement float trig etc
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(eres) |
|
|
|
@ -289,19 +428,19 @@ impl OpTrait for ArithOp { |
|
|
|
|
|
|
|
|
|
fn to_sexp(&self) -> Sexp { |
|
|
|
|
match self { |
|
|
|
|
ArithOp::Add { dst, a, b } => to_sexp_2_or_3("add", dst, a, b), |
|
|
|
|
ArithOp::Test { a } => sexp::list(&[A("test"), A(a)]), |
|
|
|
|
ArithOp::Test { a } => sexp::list(&[A("tst"), A(a)]), |
|
|
|
|
ArithOp::Compare { a, b } => sexp::list(&[A("cmp"), A(a), A(b)]), |
|
|
|
|
ArithOp::RangeTest { val, a: start, b: end } => sexp::list(&[A("rcmp"), A(val), A(start), A(end)]), |
|
|
|
|
ArithOp::Rng { dst, min, max } => { |
|
|
|
|
if min.is_immediate_equal(0) && max.is_immediate_equal(u64::MAX) { |
|
|
|
|
if min.is_imm_equal(0) && max.is_imm_equal(u64::MAX) { |
|
|
|
|
sexp::list(&[A("rng"), A(dst)]) |
|
|
|
|
} else if min.is_immediate_equal(0) { |
|
|
|
|
} else if min.is_imm_equal(0) { |
|
|
|
|
sexp::list(&[A("rng"), A(dst), A(max)]) |
|
|
|
|
} else { |
|
|
|
|
sexp::list(&[A("rng"), A(dst), A(min), A(max)]) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::Add { dst, a, b } => to_sexp_2_or_3("add", dst, a, b), |
|
|
|
|
ArithOp::Sub { dst, a, b } => to_sexp_2_or_3("sub", dst, a, b), |
|
|
|
|
ArithOp::Mul { dst, a, b } => to_sexp_2_or_3("mul", dst, a, b), |
|
|
|
|
ArithOp::Div { dst, rem, a, div } => { |
|
|
|
@ -316,6 +455,40 @@ impl OpTrait for ArithOp { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::Mod { dst, a, div } => to_sexp_2_or_3("mod", dst, a, div), |
|
|
|
|
|
|
|
|
|
// TODO render as float
|
|
|
|
|
ArithOp::FloatTest { a } => sexp::list(&[A("tstf"), A(a)]), |
|
|
|
|
ArithOp::FloatCompare { a, b } => sexp::list(&[A("cmpf"), A(a), A(b)]), |
|
|
|
|
ArithOp::FloatRangeTest { val, a: start, b: end } => sexp::list(&[A("rcmpf"), A(val), A(start), A(end)]), |
|
|
|
|
ArithOp::FloatRng { dst, min, max } => { |
|
|
|
|
if min.is_imm_equal(0f64.to_bits()) && max.is_imm_equal(f64::MAX.to_bits()) { |
|
|
|
|
sexp::list(&[A("rngf"), A(dst)]) |
|
|
|
|
} else if min.is_imm_equal(0) { |
|
|
|
|
sexp::list(&[A("rngf"), A(dst), A(max)]) |
|
|
|
|
} else { |
|
|
|
|
sexp::list(&[A("rngf"), A(dst), A(min), A(max)]) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAdd { dst, a, b } => to_sexp_2_or_3("addf", dst, a, b), |
|
|
|
|
ArithOp::FloatSub { dst, a, b } => to_sexp_2_or_3("subf", dst, a, b), |
|
|
|
|
ArithOp::FloatMul { dst, a, b } => to_sexp_2_or_3("mulf", dst, a, b), |
|
|
|
|
ArithOp::FloatDiv { dst, rem, a, div } => { |
|
|
|
|
if rem.is_discard() { |
|
|
|
|
to_sexp_2_or_3("divf", dst, a, div) |
|
|
|
|
} else { |
|
|
|
|
if &dst.as_rd() == a { |
|
|
|
|
sexp::list(&[A("divrf"), A(dst), A(rem), A(div)]) |
|
|
|
|
} else { |
|
|
|
|
sexp::list(&[A("divrf"), A(dst), A(rem), A(a), A(div)]) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatMod { dst, a, div } => to_sexp_2_or_3("modf", dst, a, div), |
|
|
|
|
ArithOp::IntToFloat { dst, a } => to_sexp_1_or_2("itf", dst, a), |
|
|
|
|
ArithOp::FloatToInt { dst, a, mode: FloatToIntMode::Floor } => to_sexp_1_or_2("ftif", dst, a), |
|
|
|
|
ArithOp::FloatToInt { dst, a, mode: FloatToIntMode::Round } => to_sexp_1_or_2("ftir", dst, a), |
|
|
|
|
ArithOp::FloatToInt { dst, a, mode: FloatToIntMode::Ceil } => to_sexp_1_or_2("ftic", dst, a), |
|
|
|
|
|
|
|
|
|
ArithOp::And { dst, a, b } => to_sexp_2_or_3("and", dst, a, b), |
|
|
|
|
ArithOp::Or { dst, a, b } => to_sexp_2_or_3("or", dst, a, b), |
|
|
|
|
ArithOp::Xor { dst, a, b } => to_sexp_2_or_3("xor", dst, a, b), |
|
|
|
@ -333,6 +506,8 @@ impl OpTrait for ArithOp { |
|
|
|
|
ArithOp::Clz { dst, src, mask: slice } => to_sexp_1_or_2(&format!("clz{}", slice), dst, src), |
|
|
|
|
ArithOp::Clo { dst, src, mask: slice } => to_sexp_1_or_2(&format!("clo{}", slice), dst, src), |
|
|
|
|
ArithOp::SignExtend { dst, src, mask: slice } => to_sexp_1_or_2(&format!("se{}", slice), dst, src), |
|
|
|
|
|
|
|
|
|
_ => unimplemented!() // FIXME
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|