[pypy-commit] pypy default: fix(?) small ints by doing more aggressive copying and by implementing some

cfbolz noreply at buildbot.pypy.org
Wed Oct 26 14:19:05 CEST 2011


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r48471:f060ae084c2e
Date: 2011-10-26 14:08 +0200
http://bitbucket.org/pypy/pypy/changeset/f060ae084c2e/

Log:	fix(?) small ints by doing more aggressive copying and by
	implementing some operations that don't need overflow checking
	directly.

diff --git a/pypy/objspace/std/smallintobject.py b/pypy/objspace/std/smallintobject.py
--- a/pypy/objspace/std/smallintobject.py
+++ b/pypy/objspace/std/smallintobject.py
@@ -12,6 +12,7 @@
 from pypy.rlib.rbigint import rbigint
 from pypy.rlib.rarithmetic import r_uint
 from pypy.tool.sourcetools import func_with_new_name
+from pypy.objspace.std.inttype import wrapint
 
 class W_SmallIntObject(W_Object, UnboxedValue):
     __slots__ = 'intval'
@@ -48,14 +49,36 @@
 def delegate_SmallInt2Complex(space, w_small):
     return space.newcomplex(float(w_small.intval), 0.0)
 
+def add__SmallInt_SmallInt(space, w_a, w_b):
+    return wrapint(space, w_a.intval + w_b.intval) # cannot overflow
+
+def sub__SmallInt_SmallInt(space, w_a, w_b):
+    return wrapint(space, w_a.intval - w_b.intval) # cannot overflow
+
+def floordiv__SmallInt_SmallInt(space, w_a, w_b):
+    return wrapint(space, w_a.intval // w_b.intval) # cannot overflow
+
+div__SmallInt_SmallInt = floordiv__SmallInt_SmallInt
+
+def mod__SmallInt_SmallInt(space, w_a, w_b):
+    return wrapint(space, w_a.intval % w_b.intval) # cannot overflow
+
+def divmod__SmallInt_SmallInt(space, w_a, w_b):
+    w = wrapint(space, w_a.intval // w_b.intval) # cannot overflow
+    z = wrapint(space, w_a.intval % w_b.intval)
+    return space.newtuple([w, z])
+
 def copy_multimethods(ns):
     """Copy integer multimethods for small int."""
     for name, func in intobject.__dict__.iteritems():
         if "__Int" in name:
             new_name = name.replace("Int", "SmallInt")
-            # Copy the function, so the annotator specializes it for
-            # W_SmallIntObject.
-            ns[new_name] = func_with_new_name(func, new_name)
+            if new_name not in ns:
+                # Copy the function, so the annotator specializes it for
+                # W_SmallIntObject.
+                ns[new_name] = func = func_with_new_name(func, new_name, globals=ns)
+        else:
+            ns[name] = func
     ns["get_integer"] = ns["pos__SmallInt"] = ns["int__SmallInt"]
     ns["get_negint"] = ns["neg__SmallInt"]
 
diff --git a/pypy/tool/sourcetools.py b/pypy/tool/sourcetools.py
--- a/pypy/tool/sourcetools.py
+++ b/pypy/tool/sourcetools.py
@@ -216,9 +216,11 @@
 
 # ____________________________________________________________
 
-def func_with_new_name(func, newname):
+def func_with_new_name(func, newname, globals=None):
     """Make a renamed copy of a function."""
-    f = new.function(func.func_code, func.func_globals,
+    if globals is None:
+        globals = func.func_globals
+    f = new.function(func.func_code, globals,
                         newname, func.func_defaults,
                         func.func_closure)
     if func.func_dict:


More information about the pypy-commit mailing list