[pypy-commit] pypy int-tag-untag-as-operations: add a more precise .mul operation on integer bounds.

cfbolz noreply at buildbot.pypy.org
Mon Oct 24 11:21:32 CEST 2011


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: int-tag-untag-as-operations
Changeset: r48366:c499ddaefd52
Date: 2011-10-24 08:21 +0200
http://bitbucket.org/pypy/pypy/changeset/c499ddaefd52/

Log:	add a more precise .mul operation on integer bounds.

diff --git a/pypy/jit/metainterp/optimizeopt/intutils.py b/pypy/jit/metainterp/optimizeopt/intutils.py
--- a/pypy/jit/metainterp/optimizeopt/intutils.py
+++ b/pypy/jit/metainterp/optimizeopt/intutils.py
@@ -96,8 +96,32 @@
         return res
 
     def mul(self, value):
-        return self.mul_bound(IntBound(value, value))
-    
+        upper = 0
+        has_upper = False
+        if self.has_upper:
+            try:
+                upper = ovfcheck(self.upper * value)
+            except OverflowError:
+                pass
+            else:
+                has_upper = True
+        lower = 0
+        has_lower = False
+        if self.has_lower:
+            try:
+                lower = ovfcheck(self.lower * value)
+            except OverflowError:
+                pass
+            else:
+                has_lower = True
+        if value < 0:
+            has_upper, has_lower = has_lower, has_upper
+            upper, lower = lower, upper
+        result = IntBound(lower, upper)
+        result.has_lower = has_lower
+        result.has_upper = has_upper
+        return result
+
     def add_bound(self, other):
         res = self.clone()
         if other.has_upper:
diff --git a/pypy/jit/metainterp/optimizeopt/test/test_intutils.py b/pypy/jit/metainterp/optimizeopt/test/test_intutils.py
new file mode 100644
--- /dev/null
+++ b/pypy/jit/metainterp/optimizeopt/test/test_intutils.py
@@ -0,0 +1,29 @@
+from pypy.jit.metainterp.optimizeopt import intutils
+
+# XXX this file should really be filled with tests for all operations!
+
+def test_mul_with_constant():
+    x = intutils.IntBound(0, 100)
+    y = x.mul(2)
+    assert y.has_lower
+    assert y.has_upper
+    assert y.lower == 0
+    assert y.upper == 200
+
+    y = x.mul(-5)
+    assert y.has_lower
+    assert y.has_upper
+    assert y.lower == -500
+    assert y.upper == 0
+
+    x = intutils.IntUpperBound(100)
+    y = x.mul(2)
+    assert not y.has_lower
+    assert y.has_upper
+    assert y.upper == 200
+
+    y = x.mul(-5)
+    assert y.has_lower
+    assert not y.has_upper
+    assert y.lower == -500
+    assert y.upper == 0


More information about the pypy-commit mailing list