Author: leo
Date: Sat Feb 18 17:12:54 2006
New Revision: 11659

Modified:
   trunk/src/ops/experimental.ops
Log:
implement modular pow algorithm for pow_n_n_i

Modified: trunk/src/ops/experimental.ops
==============================================================================
--- trunk/src/ops/experimental.ops      (original)
+++ trunk/src/ops/experimental.ops      Sat Feb 18 17:12:54 2006
@@ -282,6 +282,41 @@ op debug_brk() {
     goto NEXT();
 }
 
+=item B<pow>(out NUM, in NUM, in INT)
+
+Set $1 to $2 raised to the power $3.
+
+=cut
+
+inline op pow(out NUM, in NUM, in INT) :base_core {
+    FLOATVAL n2 = $2;
+    FLOATVAL res = 1.0;
+    INTVAL   e  = $3;
+    int s = 1;
+    if (e != 0) {
+       if (e < 0) {
+           s = -1;
+           e = -e;
+       }
+    }
+    while (e) {
+       if (e & 1) {
+           res *= n2;
+           --e;
+       }
+       else {
+           n2 *= n2;
+           e >>= 1;
+       }
+    }
+    if (s < 0) {
+       res = 1.0/res;
+    }
+    $1 = res;
+    goto NEXT();
+}
+
+
 =back
 
 =head1 COPYRIGHT

Reply via email to