Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly speedup exponentiation by Ints #4891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Perl6/Optimizer.nqp
Expand Up @@ -3335,6 +3335,16 @@ class Perl6::Optimizer {
&& self.optimize-post-pre-inc-dec-ops($op) -> $qast {
return $qast;
}

# Re-write `$foo ** 2` into `$foo * $foo`. Literals will be constant folded and
# we can't do it for non-VARs, since side effects could be duplicated (e.g.,
# `my $a = 3; say ($a += 3) ** 2; say $a` would give `89␤9` instead of `36␤6`).
if $!level >= 2 && ($op.name eq '&infix:<**>' || $op.name eq '&postfix:<ⁿ>') && $!symbols.is_from_core($op.name) &&
nqp::istype($op[0], QAST::Var) && $op[1].has_compile_time_value &&
nqp::istype((my $p := $op[1].compile_time_value), $!symbols.find_in_setting("Int")) && $p == 2 {
return QAST::Op.new(:op($op.op), :name('&infix:<*>'), $op[0], $op[0]);
}

# If it's an onlystar proto, we have a couple of options.
# The first is that we may be able to work out what to
# call at compile time. Failing that, we can at least inline
Expand Down
28 changes: 17 additions & 11 deletions src/core.c/Int.pm6
Expand Up @@ -379,18 +379,24 @@ multi sub infix:<%%>(int $a, int $b --> Bool:D) {
nqp::hllbool(nqp::iseq_i(nqp::mod_i($a, $b), 0))
}

my constant UINT64_UPPER = nqp::pow_I(2, 64, Num, Int);

multi sub infix:<**>(Int:D $a, Int:D $b --> Real:D) {
my $power := nqp::pow_I($a,nqp::if($b >= 0,$b,-$b),Num,Int);
# when a**b is too big nqp::pow_I returns Inf
nqp::istype($power, Num)
?? Failure.new(
$b >= 0 ?? X::Numeric::Overflow.new !! X::Numeric::Underflow.new
)
!! $b >= 0
?? $power
!! ($power := 1 / $power) == 0 && $a != 0
?? Failure.new(X::Numeric::Underflow.new)
!! $power
my $power;
if nqp::isge_I($b, 0) {
$power := nqp::pow_I($a, $b, Num, Int);
# when a**b is too big nqp::pow_I returns Inf
nqp::istype($power, Int)
?? $power
!! Failure.new(X::Numeric::Overflow.new)
}
else {
$power := nqp::pow_I($a, nqp::neg_I($b, Int), Num, Int);
# when a**b is too big nqp::pow_I returns Inf
nqp::istype($power, Num) || (nqp::isge_I($power, UINT64_UPPER) && nqp::isne_I($a, 0))
?? Failure.new(X::Numeric::Underflow.new)
!! 1 / $power
}
}

multi sub infix:<**>(int $a, int $b --> int) {
Expand Down
2 changes: 0 additions & 2 deletions src/core.c/Rat.pm6
Expand Up @@ -31,8 +31,6 @@ my class Rat is Cool does Rational[Int, Int] {
}
}

my constant UINT64_UPPER = nqp::pow_I(2, 64, Num, Int);

my class FatRat is Cool does Rational[Int, Int] {
method FatRat(FatRat:D:) { self }
method Rat(FatRat:D:) {
Expand Down