Skip to content

Commit

Permalink
replace hand-rolled gcd function by PIR gcd opcode
Browse files Browse the repository at this point in the history
This speeds up the spectest run by about 2.4%.
After the release (and bumping the parrot revision) this will require
.loadlib  'obscure_ops'
  • Loading branch information
moritz committed May 19, 2010
1 parent 643442b commit 5ec28d3
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/core/Rat.pm
Expand Up @@ -2,15 +2,6 @@ class Rat is Cool does Real {
has $.numerator;
has $.denominator;

our sub gcd(Int $a is copy, Int $b is copy) {
$a = -$a if ($a < 0);
$b = -$b if ($b < 0);
while $a > 0 && $b > 0 {
($a, $b) = ($b, $a) if ($b > $a);
$a %= $b;
}
return $a + $b;
}

multi method new() {
self.bless(*, :numerator(0), :denominator(1));
Expand All @@ -22,7 +13,7 @@ class Rat is Cool does Real {
$numerator = -$numerator;
$denominator = -$denominator;
}
my $gcd = gcd($numerator, $denominator);
my $gcd = pir::gcd__iii($numerator, $denominator);
$numerator = $numerator div $gcd;
$denominator = $denominator div $gcd;
self.bless(*, :numerator($numerator), :denominator($denominator));
Expand Down Expand Up @@ -64,7 +55,7 @@ class Rat is Cool does Real {
}

multi sub infix:<+>(Rat $a, Rat $b) {
my $gcd = Rat::gcd($a.denominator, $b.denominator);
my $gcd = pir::gcd__iii($a.denominator, $b.denominator);
($a.numerator * ($b.denominator div $gcd) + $b.numerator * ($a.denominator div $gcd))
/ (($a.denominator div $gcd) * $b.denominator);
}
Expand All @@ -78,7 +69,7 @@ multi sub infix:<+>(Int $a, Rat $b) {
}

multi sub infix:<->(Rat $a, Rat $b) {
my $gcd = Rat::gcd($a.denominator, $b.denominator);
my $gcd = pir::gcd__iii($a.denominator, $b.denominator);
($a.numerator * ($b.denominator div $gcd) - $b.numerator * ($a.denominator div $gcd))
/ (($a.denominator div $gcd) * $b.denominator);
}
Expand Down

0 comments on commit 5ec28d3

Please sign in to comment.