[pypy-commit] pypy default: Special-case for "complex ** 2" too.

arigo noreply at buildbot.pypy.org
Sat Jan 21 18:28:42 CET 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r51594:76e23aa1ef63
Date: 2012-01-21 18:28 +0100
http://bitbucket.org/pypy/pypy/changeset/76e23aa1ef63/

Log:	Special-case for "complex ** 2" too.

diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -8,6 +8,7 @@
 from pypy.rlib.rbigint import rbigint
 from pypy.rlib.rfloat import (
     formatd, DTSF_STR_PRECISION, isinf, isnan, copysign)
+from pypy.rlib import jit
 
 import math
 
@@ -129,10 +130,10 @@
             ir = len * math.sin(phase)
         return W_ComplexObject(rr, ir)
 
-    def pow_int(self, n):
-        if n > 100 or n < -100:
-            return self.pow(W_ComplexObject(1.0 * n, 0.0))
-        elif n > 0:
+    def pow_small_int(self, n):
+        if n >= 0:
+            if jit.isconstant(n) and n == 2:
+                return self.mul(self)
             return self.pow_positive_int(n)
         else:
             return w_one.div(self.pow_positive_int(-n))
@@ -217,10 +218,10 @@
 def pow__Complex_Complex_ANY(space, w_complex, w_exponent, thirdArg):
     if not space.is_w(thirdArg, space.w_None):
         raise OperationError(space.w_ValueError, space.wrap('complex modulo'))
-    int_exponent = int(w_exponent.realval)
     try:
-        if w_exponent.imagval == 0.0 and w_exponent.realval == int_exponent:
-            w_p = w_complex.pow_int(int_exponent)
+        r = w_exponent.realval
+        if w_exponent.imagval == 0.0 and -100.0 <= r <= 100.0 and r == int(r):
+            w_p = w_complex.pow_small_int(int(r))
         else:
             w_p = w_complex.pow(w_exponent)
     except ZeroDivisionError:
diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -71,7 +71,7 @@
         assert _powu((0.0,1.0),2) == (-1.0,0.0)
 
         def _powi((r1, i1), n):
-            w_res = W_ComplexObject(r1, i1).pow_int(n)
+            w_res = W_ComplexObject(r1, i1).pow_small_int(n)
             return w_res.realval, w_res.imagval
         assert _powi((0.0,2.0),0) == (1.0,0.0)
         assert _powi((0.0,0.0),2) == (0.0,0.0)
@@ -213,6 +213,7 @@
         assert a ** 105 == a ** 105
         assert a ** -105 == a ** -105
         assert a ** -30 == a ** -30
+        assert a ** 2 == a * a
 
         assert 0.0j ** 0 == 1
 


More information about the pypy-commit mailing list