[pypy-commit] pypy bounds-int-add-or: add and test next_power2 utility

squeaky noreply at buildbot.pypy.org
Mon Feb 17 23:58:56 CET 2014


Author: Squeaky <squeaky_pl at gmx.com>
Branch: bounds-int-add-or
Changeset: r69188:5eca705b5884
Date: 2014-02-17 14:51 +0100
http://bitbucket.org/pypy/pypy/changeset/5eca705b5884/

Log:	add and test next_power2 utility

diff --git a/rpython/jit/metainterp/optimizeopt/intbounds.py b/rpython/jit/metainterp/optimizeopt/intbounds.py
--- a/rpython/jit/metainterp/optimizeopt/intbounds.py
+++ b/rpython/jit/metainterp/optimizeopt/intbounds.py
@@ -23,6 +23,17 @@
         return (1 << ((byte_size << 3) - 1)) - 1
 
 
+def next_power2(val):
+    """Calculate next power of 2 greater than val.
+
+       Danger: this can overflow, use only when val is sufficiently
+       lower than symbolic.WORD"""
+    power = 1
+    while power < val + 1:
+        power <<= 1
+    return power
+
+
 class OptIntBounds(Optimization):
     """Keeps track of the bounds placed on integers by guards and remove
        redundant guards"""
@@ -82,6 +93,8 @@
             val = v1.box.getint()
             if val >= 0:
                 r.intbound.intersect(IntBound(0, val))
+        elif v1.intbound.lower >= 0 and v2.intbound.lower >= 0:
+            pass
 
     def optimize_INT_SUB(self, op):
         v1 = self.getvalue(op.getarg(0))
diff --git a/rpython/jit/metainterp/optimizeopt/test/test_intbounds.py b/rpython/jit/metainterp/optimizeopt/test/test_intbounds.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/metainterp/optimizeopt/test/test_intbounds.py
@@ -0,0 +1,10 @@
+from rpython.jit.metainterp.optimizeopt.intbounds import next_power2
+
+
+def test_next_power2():
+    assert next_power2(0) == 1
+    assert next_power2(1) == 2
+    assert next_power2(7) == 8
+    assert next_power2(256) == 512
+    assert next_power2(255) == 256
+    assert next_power2(80) == 128


More information about the pypy-commit mailing list