Skip to content

Commit

Permalink
Merge pull request #5 from bksaiki/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
bksaiki committed Mar 8, 2024
2 parents 832d1a2 + f6d8f69 commit 1ccae08
Show file tree
Hide file tree
Showing 32 changed files with 1,242 additions and 1,307 deletions.
6 changes: 3 additions & 3 deletions src/fixed/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Real for Fixed {
2
}

fn sign(&self) -> bool {
fn sign(&self) -> Option<bool> {
self.num.sign()
}

Expand All @@ -85,8 +85,8 @@ impl Real for Fixed {
self.num.m()
}

fn p(&self) -> usize {
self.num.p()
fn prec(&self) -> Option<usize> {
self.num.prec()
}

fn is_nar(&self) -> bool {
Expand Down
133 changes: 53 additions & 80 deletions src/fixed/ops.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use crate::fixed::FixedContext;
use crate::math::*;
use crate::mpfr::*;
use crate::ops::*;
use crate::rfloat::RFloat;
use crate::{Real, RoundingContext};

macro_rules! rounded_1ary_impl {
($tname:ident, $name:ident, $mpmf:ident, $mpfr:ident) => {
($tname:ident, $name:ident, $mpfr:ident) => {
impl $tname for FixedContext {
fn $name(&self, src: &Self::Rounded) -> Self::Rounded {
self.$mpmf(src)
}

fn $mpmf<N: Real>(&self, src: &N) -> Self::Rounded {
fn $name<N: Real>(&self, src: &N) -> Self::Format {
// compute approximately, rounding-to-odd,
// with 2 rounding bits
let p = self.nbits + 2;
Expand All @@ -25,56 +21,47 @@ macro_rules! rounded_1ary_impl {
};
}

rounded_1ary_impl!(RoundedNeg, format_neg, neg, mpfr_neg);
rounded_1ary_impl!(RoundedAbs, format_abs, abs, mpfr_abs);
rounded_1ary_impl!(RoundedSqrt, format_sqrt, sqrt, mpfr_sqrt);
rounded_1ary_impl!(RoundedCbrt, format_cbrt, cbrt, mpfr_cbrt);
rounded_1ary_impl!(RoundedRecip, format_recip, recip, mpfr_recip);
rounded_1ary_impl!(
RoundedRecipSqrt,
format_recip_sqrt,
recip_sqrt,
mpfr_recip_sqrt
);
rounded_1ary_impl!(RoundedExp, format_exp, exp, mpfr_exp);
rounded_1ary_impl!(RoundedExp2, format_exp2, exp2, mpfr_exp2);
rounded_1ary_impl!(RoundedLog, format_log, log, mpfr_log);
rounded_1ary_impl!(RoundedLog2, format_log2, log2, mpfr_log2);
rounded_1ary_impl!(RoundedLog10, format_log10, log10, mpfr_log10);
rounded_1ary_impl!(RoundedExpm1, format_expm1, expm1, mpfr_expm1);
rounded_1ary_impl!(RoundedExp2m1, format_exp2m1, exp2m1, mpfr_exp2m1);
rounded_1ary_impl!(RoundedExp10m1, format_exp10m1, exp10m1, mpfr_exp10m1);
rounded_1ary_impl!(RoundedLog1p, format_log1p, log1p, mpfr_log1p);
rounded_1ary_impl!(RoundedLog2p1, format_log2p1, log2p1, mpfr_log2p1);
rounded_1ary_impl!(RoundedLog10p1, format_log10p1, log10p1, mpfr_log10p1);
rounded_1ary_impl!(RoundedSin, format_sin, sin, mpfr_sin);
rounded_1ary_impl!(RoundedCos, format_cos, cos, mpfr_cos);
rounded_1ary_impl!(RoundedTan, format_tan, tan, mpfr_tan);
rounded_1ary_impl!(RoundedSinPi, format_sin_pi, sin_pi, mpfr_sin_pi);
rounded_1ary_impl!(RoundedCosPi, format_cos_pi, cos_pi, mpfr_cos_pi);
rounded_1ary_impl!(RoundedTanPi, format_tan_pi, tan_pi, mpfr_tan_pi);
rounded_1ary_impl!(RoundedAsin, format_asin, asin, mpfr_asin);
rounded_1ary_impl!(RoundedAcos, format_acos, acos, mpfr_acos);
rounded_1ary_impl!(RoundedAtan, format_atan, atan, mpfr_atan);
rounded_1ary_impl!(RoundedSinh, format_sinh, sinh, mpfr_sinh);
rounded_1ary_impl!(RoundedCosh, format_cosh, cosh, mpfr_cosh);
rounded_1ary_impl!(RoundedTanh, format_tanh, tanh, mpfr_tanh);
rounded_1ary_impl!(RoundedAsinh, format_asinh, asinh, mpfr_asinh);
rounded_1ary_impl!(RoundedAcosh, format_acosh, acosh, mpfr_acosh);
rounded_1ary_impl!(RoundedAtanh, format_atanh, atanh, mpfr_atanh);
rounded_1ary_impl!(RoundedErf, format_erf, erf, mpfr_erf);
rounded_1ary_impl!(RoundedErfc, format_erfc, erfc, mpfr_erfc);
rounded_1ary_impl!(RoundedGamma, format_tgamma, tgamma, mpfr_tgamma);
rounded_1ary_impl!(RoundedLgamma, format_lgamma, lgamma, mpfr_lgamma);
rounded_1ary_impl!(RoundedNeg, neg, mpfr_neg);
rounded_1ary_impl!(RoundedAbs, abs, mpfr_abs);
rounded_1ary_impl!(RoundedSqrt, sqrt, mpfr_sqrt);
rounded_1ary_impl!(RoundedCbrt, cbrt, mpfr_cbrt);
rounded_1ary_impl!(RoundedRecip, recip, mpfr_recip);
rounded_1ary_impl!(RoundedRecipSqrt, recip_sqrt, mpfr_recip_sqrt);
rounded_1ary_impl!(RoundedExp, exp, mpfr_exp);
rounded_1ary_impl!(RoundedExp2, exp2, mpfr_exp2);
rounded_1ary_impl!(RoundedLog, log, mpfr_log);
rounded_1ary_impl!(RoundedLog2, log2, mpfr_log2);
rounded_1ary_impl!(RoundedLog10, log10, mpfr_log10);
rounded_1ary_impl!(RoundedExpm1, expm1, mpfr_expm1);
rounded_1ary_impl!(RoundedExp2m1, exp2m1, mpfr_exp2m1);
rounded_1ary_impl!(RoundedExp10m1, exp10m1, mpfr_exp10m1);
rounded_1ary_impl!(RoundedLog1p, log1p, mpfr_log1p);
rounded_1ary_impl!(RoundedLog2p1, log2p1, mpfr_log2p1);
rounded_1ary_impl!(RoundedLog10p1, log10p1, mpfr_log10p1);
rounded_1ary_impl!(RoundedSin, sin, mpfr_sin);
rounded_1ary_impl!(RoundedCos, cos, mpfr_cos);
rounded_1ary_impl!(RoundedTan, tan, mpfr_tan);
rounded_1ary_impl!(RoundedSinPi, sin_pi, mpfr_sin_pi);
rounded_1ary_impl!(RoundedCosPi, cos_pi, mpfr_cos_pi);
rounded_1ary_impl!(RoundedTanPi, tan_pi, mpfr_tan_pi);
rounded_1ary_impl!(RoundedAsin, asin, mpfr_asin);
rounded_1ary_impl!(RoundedAcos, acos, mpfr_acos);
rounded_1ary_impl!(RoundedAtan, atan, mpfr_atan);
rounded_1ary_impl!(RoundedSinh, sinh, mpfr_sinh);
rounded_1ary_impl!(RoundedCosh, cosh, mpfr_cosh);
rounded_1ary_impl!(RoundedTanh, tanh, mpfr_tanh);
rounded_1ary_impl!(RoundedAsinh, asinh, mpfr_asinh);
rounded_1ary_impl!(RoundedAcosh, acosh, mpfr_acosh);
rounded_1ary_impl!(RoundedAtanh, atanh, mpfr_atanh);
rounded_1ary_impl!(RoundedErf, erf, mpfr_erf);
rounded_1ary_impl!(RoundedErfc, erfc, mpfr_erfc);
rounded_1ary_impl!(RoundedGamma, tgamma, mpfr_tgamma);
rounded_1ary_impl!(RoundedLgamma, lgamma, mpfr_lgamma);

macro_rules! rounded_2ary_impl {
($tname:ident, $name:ident, $mpmf:ident, $mpfr:ident) => {
($tname:ident, $name:ident, $mpfr:ident) => {
impl $tname for FixedContext {
fn $name(&self, src1: &Self::Rounded, src2: &Self::Rounded) -> Self::Rounded {
self.$mpmf(src1, src2)
}

fn $mpmf<N1, N2>(&self, src1: &N1, src2: &N2) -> Self::Rounded
fn $name<N1, N2>(&self, src1: &N1, src2: &N2) -> Self::Format
where
N1: Real,
N2: Real,
Expand All @@ -93,34 +80,20 @@ macro_rules! rounded_2ary_impl {
};
}

rounded_2ary_impl!(RoundedAdd, format_add, add, mpfr_add);
rounded_2ary_impl!(RoundedSub, format_sub, sub, mpfr_sub);
rounded_2ary_impl!(RoundedMul, format_mul, mul, mpfr_mul);
rounded_2ary_impl!(RoundedDiv, format_div, div, mpfr_div);
rounded_2ary_impl!(RoundedPow, format_pow, pow, mpfr_pow);
rounded_2ary_impl!(RoundedHypot, format_hypot, hypot, mpfr_hypot);
rounded_2ary_impl!(RoundedFmod, format_fmod, fmod, mpfr_fmod);
rounded_2ary_impl!(
RoundedRemainder,
format_remainder,
remainder,
mpfr_remainder
);
rounded_2ary_impl!(RoundedAtan2, format_atan2, atan2, mpfr_atan2);
rounded_2ary_impl!(RoundedAdd, add, mpfr_add);
rounded_2ary_impl!(RoundedSub, sub, mpfr_sub);
rounded_2ary_impl!(RoundedMul, mul, mpfr_mul);
rounded_2ary_impl!(RoundedDiv, div, mpfr_div);
rounded_2ary_impl!(RoundedPow, pow, mpfr_pow);
rounded_2ary_impl!(RoundedHypot, hypot, mpfr_hypot);
rounded_2ary_impl!(RoundedFmod, fmod, mpfr_fmod);
rounded_2ary_impl!(RoundedRemainder, remainder, mpfr_remainder);
rounded_2ary_impl!(RoundedAtan2, atan2, mpfr_atan2);

macro_rules! rounded_3ary_impl {
($tname:ident, $name:ident, $mpmf:ident, $mpfr:ident) => {
($tname:ident, $name:ident, $mpfr:ident) => {
impl $tname for FixedContext {
fn $name(
&self,
src1: &Self::Rounded,
src2: &Self::Rounded,
src3: &Self::Rounded,
) -> Self::Rounded {
self.$mpmf(src1, src2, src3)
}

fn $mpmf<N1, N2, N3>(&self, src1: &N1, src2: &N2, src3: &N3) -> Self::Rounded
fn $name<N1, N2, N3>(&self, src1: &N1, src2: &N2, src3: &N3) -> Self::Format
where
N1: Real,
N2: Real,
Expand All @@ -141,4 +114,4 @@ macro_rules! rounded_3ary_impl {
};
}

rounded_3ary_impl!(RoundedFMA, format_fma, fma, mpfr_fma);
rounded_3ary_impl!(RoundedFMA, fma, mpfr_fma);

0 comments on commit 1ccae08

Please sign in to comment.