Skip to content
Permalink
Browse files

Streamline Rat == Rat, Rat == Int, Int == Rat

- make Rat == Int / Int == Rat up to 15x faster
- make Rat == Rat upto 4x faster
- fixes Inf.Rat == 1/3 and 1/3 == Inf.Rat  R#2578
- use nqp ops instead of accessor methods
  • Loading branch information...
lizmat committed Jan 2, 2019
1 parent afccc2c commit c11e7e6bcd3b5463a586642066e76b4086722cc2
Showing with 32 additions and 13 deletions.
  1. +32 −13 src/core/Rat.pm6
@@ -219,22 +219,41 @@ multi sub infix:<**>(Rational:D \a, Int:D \b) {
RAKUDO_INTERNAL_DIVIDE_NUMBERS_NO_NORMALIZE $de, $nu, a, b)))
}

multi sub infix:<==>(Rational:D \a, Rational:D \b) {
multi sub infix:<==>(Rational:D \a, Rational:D \b --> Bool:D) {
nqp::hllbool(
nqp::isfalse(a.denominator) || nqp::isfalse(b.denominator)
?? nqp::iseq_I(a.numerator, b.numerator)
&& nqp::istrue(a.numerator) # NaN != NaN
!! nqp::iseq_I(
nqp::mul_I(a.numerator, b.denominator, Int),
nqp::mul_I(b.numerator, a.denominator, Int)))
}
multi sub infix:<==>(Rational:D \a, Int:D \b) {
nqp::iseq_I(
(my \anum := nqp::getattr(nqp::decont(a),Rat,'$!numerator')),
nqp::getattr(nqp::decont(b),Rat,'$!numerator')
) && nqp::iseq_I(
(my \adenom := nqp::getattr(nqp::decont(a),Rat,'$!denominator')),
nqp::getattr(nqp::decont(b),Rat,'$!denominator')
) && ( # num/denom both same
nqp::istrue(anum) # 1/X, Inf == Inf also true
|| nqp::istrue(adenom) # 0/1, NaN == NaN becomes false
)
)
}
multi sub infix:<==>(Rational:D \a, Int:D \b --> Bool:D) {
nqp::hllbool(
nqp::iseq_I(a.numerator, nqp::decont(b)) && nqp::iseq_I(a.denominator, 1))
}
multi sub infix:<==>(Int:D \a, Rational:D \b) {
nqp::iseq_I(
nqp::getattr(nqp::decont(a),Rat,'$!denominator'),
1
) && nqp::iseq_I(
nqp::getattr(nqp::decont(a),Rat,'$!numerator'),
nqp::decont(b)
)
)
}
multi sub infix:<==>(Int:D \a, Rational:D \b --> Bool:D) {
nqp::hllbool(
nqp::iseq_I(nqp::decont(a), b.numerator) && nqp::iseq_I(b.denominator, 1))
nqp::iseq_I(
nqp::getattr(nqp::decont(b),Rat,'$!denominator'),
1
) && nqp::iseq_I(
nqp::decont(a),
nqp::getattr(nqp::decont(b),Rat,'$!numerator')
)
)
}
multi sub infix:<===>(Rational:D \a, Rational:D \b --> Bool:D) {
nqp::hllbool(

0 comments on commit c11e7e6

Please sign in to comment.
You can’t perform that action at this time.