|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
use std::ops::Rem; |
|
|
|
|
|
|
|
|
|
use num_traits::PrimInt; |
|
|
|
|
use num_traits::{PrimInt}; |
|
|
|
|
|
|
|
|
|
use crsn::asm::data::{Rd, Wr}; |
|
|
|
|
use crsn::module::{EvalRes, OpTrait}; |
|
|
|
@ -13,24 +13,25 @@ use crsn::utils::A; |
|
|
|
|
use crate::defs::{ArithOp, FloatToIntMode}; |
|
|
|
|
use crsn::asm::instr::cond::Flag; |
|
|
|
|
use rand::Rng; |
|
|
|
|
use std::f64::consts::{PI, FRAC_PI_2}; |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn f2u(f: f64) -> u64 { |
|
|
|
|
pub(crate) fn f2u(f: f64) -> u64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn u2f(f: u64) -> f64 { |
|
|
|
|
pub(crate) fn u2f(f: u64) -> f64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn i2u(f: i64) -> u64 { |
|
|
|
|
pub(crate) fn i2u(f: i64) -> u64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
fn u2i(f: u64) -> i64 { |
|
|
|
|
pub(crate) fn u2i(f: u64) -> i64 { |
|
|
|
|
unsafe { std::mem::transmute(f) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -174,14 +175,17 @@ impl OpTrait for ArithOp { |
|
|
|
|
FloatToIntMode::Ceil => val.ceil(), |
|
|
|
|
FloatToIntMode::Round => val.round(), |
|
|
|
|
} as i64); |
|
|
|
|
trace!("fti {} -> {}", val, res); |
|
|
|
|
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))?; |
|
|
|
|
let i = u2i(state.read(a)?); |
|
|
|
|
let res: f64 = i as f64; |
|
|
|
|
trace!("itf {} -> {}", i, res); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatTest { a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
@ -419,8 +423,227 @@ impl OpTrait for ArithOp { |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
ArithOp::Abs { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2i(state.read(a)?); |
|
|
|
|
let res = i2u(x.abs()); |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
ArithOp::Sgn { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2i(state.read(a)?); |
|
|
|
|
let res = i2u(if x >= 0 { 0 } else { -1 }); |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
ArithOp::Pow { dst, a, pow } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = state.read(a)?; |
|
|
|
|
let p = state.read(pow)?; |
|
|
|
|
if p > u32::MAX as u64 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let res = x.pow(p as u32); |
|
|
|
|
state.update_status(res); |
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatPow { dst, a, pow } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let p = u2f(state.read(pow)?); |
|
|
|
|
let res = x.powf(p); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatRoot { dst, a, root } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
let p = u2f(state.read(root)?); |
|
|
|
|
if p == 0f64 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let res = x.powf(1f64 / p); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHyp { dst, a, b } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let b = u2f(state.read(b)?); |
|
|
|
|
let res = a.hypot(b); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAbs { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.abs(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatSgn { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.signum(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
/* Simple trig */ |
|
|
|
|
ArithOp::FloatSin { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.sin(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAsin { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.asin(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatCos { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.cos(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAcos { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.acos(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatTan { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.tan(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAtan { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.atan(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAtan2 { dst, y, x } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(y)?); |
|
|
|
|
let b = u2f(state.read(x)?); |
|
|
|
|
let res = a.atan2(b); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatCot { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let tan = a.tan(); |
|
|
|
|
if tan == 0.0 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let res = 1f64 / tan; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatAcot { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
|
|
|
|
|
// TODO verify
|
|
|
|
|
let res = if x > 1.0 { |
|
|
|
|
(1.0/x).atan() |
|
|
|
|
} else if x < -1.0 { |
|
|
|
|
PI + (1.0/x).atan() |
|
|
|
|
} else { |
|
|
|
|
FRAC_PI_2 - x.atan() |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
_ => unimplemented!() // TODO implement float trig etc
|
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
/* Hyperbolic trig */ |
|
|
|
|
ArithOp::FloatHypSin { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.sinh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypAsin { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.asinh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypCos { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.cosh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypAcos { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
if a > 1.0 { |
|
|
|
|
let res = a.acos(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypTan { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let res = a.tanh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypAtan { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
if a > -1.0 && a < 1.0 { |
|
|
|
|
let res = a.atanh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} else { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypCot { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let a = u2f(state.read(a)?); |
|
|
|
|
let tan = a.tanh(); |
|
|
|
|
if tan == 0.0 { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
} else { |
|
|
|
|
let res = 1f64 / tan; |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatHypAcot { dst, a } => { |
|
|
|
|
state.clear_status(); |
|
|
|
|
let x = u2f(state.read(a)?); |
|
|
|
|
|
|
|
|
|
// TODO verify
|
|
|
|
|
if x > 1.0 || x < -1.0 { |
|
|
|
|
let res = (1.0/x).atanh(); |
|
|
|
|
state.update_status_float(res); |
|
|
|
|
state.write(dst, f2u(res))?; |
|
|
|
|
} else { |
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(eres) |
|
|
|
@ -457,33 +680,33 @@ 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::FloatTest { a } => sexp::list(&[A("ftst"), A(a)]), |
|
|
|
|
ArithOp::FloatCompare { a, b } => sexp::list(&[A("fcmp"), A(a), A(b)]), |
|
|
|
|
ArithOp::FloatRangeTest { val, a: start, b: end } => sexp::list(&[A("frcmp"), 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)]) |
|
|
|
|
if min.is_imm_equal((0f64).to_bits()) && max.is_imm_equal(f64::MAX.to_bits()) { |
|
|
|
|
sexp::list(&[A("frng"), A(dst)]) |
|
|
|
|
} else if min.is_imm_equal(0) { |
|
|
|
|
sexp::list(&[A("rngf"), A(dst), A(max)]) |
|
|
|
|
sexp::list(&[A("frng"), A(dst), A(max)]) |
|
|
|
|
} else { |
|
|
|
|
sexp::list(&[A("rngf"), A(dst), A(min), A(max)]) |
|
|
|
|
sexp::list(&[A("frng"), 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::FloatAdd { dst, a, b } => to_sexp_2_or_3("fadd", dst, a, b), |
|
|
|
|
ArithOp::FloatSub { dst, a, b } => to_sexp_2_or_3("fsub", dst, a, b), |
|
|
|
|
ArithOp::FloatMul { dst, a, b } => to_sexp_2_or_3("fmul", dst, a, b), |
|
|
|
|
ArithOp::FloatDiv { dst, rem, a, div } => { |
|
|
|
|
if rem.is_discard() { |
|
|
|
|
to_sexp_2_or_3("divf", dst, a, div) |
|
|
|
|
to_sexp_2_or_3("fdiv", dst, a, div) |
|
|
|
|
} else { |
|
|
|
|
if &dst.as_rd() == a { |
|
|
|
|
sexp::list(&[A("divrf"), A(dst), A(rem), A(div)]) |
|
|
|
|
sexp::list(&[A("fdivr"), A(dst), A(rem), A(div)]) |
|
|
|
|
} else { |
|
|
|
|
sexp::list(&[A("divrf"), A(dst), A(rem), A(a), A(div)]) |
|
|
|
|
sexp::list(&[A("fdivr"), A(dst), A(rem), A(a), A(div)]) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ArithOp::FloatMod { dst, a, div } => to_sexp_2_or_3("modf", dst, a, div), |
|
|
|
|
ArithOp::FloatMod { dst, a, div } => to_sexp_2_or_3("fmod", 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), |
|
|
|
@ -507,7 +730,31 @@ impl OpTrait for ArithOp { |
|
|
|
|
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
|
|
|
|
|
ArithOp::Abs { dst, a } => to_sexp_1_or_2("abs", dst, a), |
|
|
|
|
ArithOp::Sgn { dst, a } => to_sexp_1_or_2("sgn", dst, a), |
|
|
|
|
ArithOp::Pow { dst, a, pow } => to_sexp_2_or_3("pow", dst, a, pow), |
|
|
|
|
ArithOp::FloatPow { dst, a, pow } => to_sexp_2_or_3("fpow", dst, a, pow), |
|
|
|
|
ArithOp::FloatRoot { dst, a, root } => to_sexp_2_or_3("froot", dst, a, root), |
|
|
|
|
ArithOp::FloatHyp { dst, a, b } => to_sexp_2_or_3("fhyp", dst, a, b), |
|
|
|
|
ArithOp::FloatAbs { dst, a } => to_sexp_1_or_2("fabs", dst, a), |
|
|
|
|
ArithOp::FloatSgn { dst, a } => to_sexp_1_or_2("fsgn", dst, a), |
|
|
|
|
ArithOp::FloatSin { dst, a } => to_sexp_1_or_2("fsin", dst, a), |
|
|
|
|
ArithOp::FloatAsin { dst, a } => to_sexp_1_or_2("fasin", dst, a), |
|
|
|
|
ArithOp::FloatCos { dst, a } => to_sexp_1_or_2("fcos", dst, a), |
|
|
|
|
ArithOp::FloatAcos { dst, a } => to_sexp_1_or_2("facos", dst, a), |
|
|
|
|
ArithOp::FloatTan { dst, a } => to_sexp_1_or_2("ftan", dst, a), |
|
|
|
|
ArithOp::FloatAtan { dst, a } => to_sexp_1_or_2("fatan", dst, a), |
|
|
|
|
ArithOp::FloatAtan2 { dst, x, y } => to_sexp_2_or_3("fatan2", dst, x, y), |
|
|
|
|
ArithOp::FloatCot { dst, a } => to_sexp_1_or_2("fcot", dst, a), |
|
|
|
|
ArithOp::FloatAcot { dst, a } => to_sexp_1_or_2("facot", dst, a), |
|
|
|
|
ArithOp::FloatHypSin { dst, a } => to_sexp_1_or_2("fsinh", dst, a), |
|
|
|
|
ArithOp::FloatHypAsin { dst, a } => to_sexp_1_or_2("fasinh", dst, a), |
|
|
|
|
ArithOp::FloatHypCos { dst, a } => to_sexp_1_or_2("fcosh", dst, a), |
|
|
|
|
ArithOp::FloatHypAcos { dst, a } => to_sexp_1_or_2("facosh", dst, a), |
|
|
|
|
ArithOp::FloatHypTan { dst, a } => to_sexp_1_or_2("ftanh", dst, a), |
|
|
|
|
ArithOp::FloatHypAtan { dst, a } => to_sexp_1_or_2("fatanh", dst, a), |
|
|
|
|
ArithOp::FloatHypCot { dst, a } => to_sexp_1_or_2("fcoth", dst, a), |
|
|
|
|
ArithOp::FloatHypAcot { dst, a } => to_sexp_1_or_2("facoth", dst, a), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|