Permalink
Browse files

Replace expmod with a call to BigInteger.ModPow.

  • Loading branch information...
1 parent 55f5b8a commit f357a5aa10e79376259da92c1dcdbbb67ac55219 @colomon colomon committed Sep 24, 2012
Showing with 19 additions and 14 deletions.
  1. +17 −0 lib/Builtins.cs
  2. +2 −14 lib/CORE.setting
View
@@ -1088,6 +1088,23 @@ public partial class Builtins {
return MakeInt(BigInteger.GreatestCommonDivisor(PromoteToBigInt(r1, n1), PromoteToBigInt(r2, n2)));
}
+ static readonly Func<Constants,Variable,Variable,Variable,Variable> expmod_d = expmod;
+ [ImplicitConsts] public static Variable expmod(Constants c, Variable a1, Variable a2, Variable a3) {
+ P6any o1 = a1.Fetch();
+ P6any o2 = a2.Fetch();
+ P6any o3 = a3.Fetch();
+ if (!o1.mo.is_any || !o2.mo.is_any || !o3.mo.is_any)
+ return HandleSpecial3(c, a1,a2,a3,o1,o2,o3, expmod_d);
+ int r1, r2, r3;
+ P6any n1 = GetNumber(a1, o1, out r1);
+ P6any n2 = GetNumber(a2, o2, out r2);
+ P6any n3 = GetNumber(a3, o3, out r3);
+
+ return MakeInt(BigInteger.ModPow(PromoteToBigInt(r1, n1),
+ PromoteToBigInt(r2, n2),
+ PromoteToBigInt(r3, n3)));
+ }
+
[TrueGlobal] static IForeignInterpreter p5_interpreter;
public static Variable eval_perl5(Variable v) {
P6any o1 = v.Fetch();
View
@@ -845,19 +845,7 @@ my class Int does Integral {
push @res, '-' if self < 0;
join '', @res.reverse;
}
- method expmod($power, $mod) {
- # Routine by TimToady; should be replaced with BigInteger.ModPow.
- my $x = self;
- my $y = $power.Int;
- my $z = $mod.Int;
-
- my $result = 1;
- repeat while $y div= 2 {
- ($result *= $x) %= $z if $y % 2;
- ($x *= $x) %= $z;
- }
- $result;
- }
+ method expmod($power, $mod) { expmod(self, $power, $mod) }
method is-prime($tries = 100) { is-prime(self, $tries) }
}
my class Rat does Real {
@@ -3871,7 +3859,7 @@ sub hypot($a, $b) is pure {
my $r = $min / $max;
$max * sqrt(1 + $r * $r);
}
-sub expmod($exp, $base, $mod) { $exp.Int.expmod($base, $mod) }
+sub expmod($exp, $power, $mod) is pure { Q:CgOp { (expmod {$exp.Int} {$power.Int} {$mod.Int}) } }
sub is-prime($candidate, $tries = 100) {
# Miller-Rabin via TimToady
my Int $n = $candidate.Int;

0 comments on commit f357a5a

Please sign in to comment.