harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r575302 - /harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java
Date Thu, 13 Sep 2007 13:11:48 GMT
Author: tellison
Date: Thu Sep 13 06:11:47 2007
New Revision: 575302

URL: http://svn.apache.org/viewvc?rev=575302&view=rev
Log:
Apply patch HARMONY-4780 ([classlib][math] BigInteger pow speed-up)

Modified:
    harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java

Modified: harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java?rev=575302&r1=575301&r2=575302&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java (original)
+++ harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java Thu
Sep 13 06:11:47 2007
@@ -25,10 +25,6 @@
 
 import org.apache.harmony.math.internal.nls.Messages;
 
-/**
- * @author Intel Middleware Product Division
- * @author Instituto Tecnologico de Cordoba
- */
 public class BigInteger extends Number implements Comparable<BigInteger>,
         Serializable {
 
@@ -679,19 +675,29 @@
         return Multiplication.multiply(this, val);
     }
 
-    /** @ar.org.fitc.spec_ref */
     public BigInteger pow(int exp) {
-        if (exp < 0){
+        if (exp < 0) {
             // math.16=Negative exponent
             throw new ArithmeticException(Messages.getString("math.16")); //$NON-NLS-1$
         }
         if (exp == 0) {
             return ONE;
-        } else if(exp == 1 || equals(ONE) || equals(ZERO)) {
+        } else if (exp == 1 || equals(ONE) || equals(ZERO)) {
             return this;
         }
+
+        // if even take out 2^x factor which we can
+        // calculate by shifting.
+        if (!testBit(0)) {
+            int x = 1;
+            BigInteger factor = BigInteger.ONE.shiftLeft(exp);
+            while (!testBit(x)) {
+                factor = factor.shiftLeft(exp);
+                x++;
+            }
+            return factor.multiply(this.shiftRight(x).pow(exp));
+        }
         return Multiplication.pow(this, exp);
-        
     }
 
     /** @ar.org.fitc.spec_ref */



Mime
View raw message