[pypy-svn] r77101 - in pypy/trunk/pypy: jit/tl module/__builtin__ module/__builtin__/test module/pypyjit/test

hakanardo at codespeak.net hakanardo at codespeak.net
Thu Sep 16 07:27:48 CEST 2010


Author: hakanardo
Date: Thu Sep 16 07:27:46 2010
New Revision: 77101

Modified:
   pypy/trunk/pypy/jit/tl/pypyjit_demo.py
   pypy/trunk/pypy/module/__builtin__/functional.py
   pypy/trunk/pypy/module/__builtin__/test/test_minmax.py
   pypy/trunk/pypy/module/pypyjit/test/test_pypy_c.py
Log:
Allow jit to unroll calls to max() and min() with more than one argument.

Modified: pypy/trunk/pypy/jit/tl/pypyjit_demo.py
==============================================================================
--- pypy/trunk/pypy/jit/tl/pypyjit_demo.py	(original)
+++ pypy/trunk/pypy/jit/tl/pypyjit_demo.py	Thu Sep 16 07:27:46 2010
@@ -39,16 +39,24 @@
 
 try:
     from array import array
+
+    def coords(w,h):
+        y = 0
+        while y < h:
+            x = 0
+            while x < w:
+                yield x,y
+                x += 1
+            y += 1
+
     def f(img):
-        i=0
         sa=0
-        while i < img.__len__():
-            sa+=img[i]
-            i+=1
+        for x, y in coords(4,4):
+            sa += x * y
         return sa
 
-    img=array('h',(1,2,3,4))
-    print f(img)
+    #img=array('h',(1,2,3,4))
+    print f(3)
 except Exception, e:
     print "Exception: ", type(e)
     print e

Modified: pypy/trunk/pypy/module/__builtin__/functional.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/functional.py	(original)
+++ pypy/trunk/pypy/module/__builtin__/functional.py	Thu Sep 16 07:27:46 2010
@@ -13,6 +13,7 @@
 from pypy.rlib.objectmodel import specialize
 from pypy.module.__builtin__.app_functional import range as app_range
 from inspect import getsource, getfile
+from pypy.rlib.jit import unroll_safe
 
 """
 Implementation of the common integer case of range. Instead of handling
@@ -96,12 +97,31 @@
     return W_RangeListObject(start, step, howmany)
 
 
+ at unroll_safe
 @specialize.arg(2)
 def min_max(space, args, implementation_of):
     if implementation_of == "max":
         compare = space.gt
     else:
         compare = space.lt
+
+    args_w = args.arguments_w
+    if len(args_w) > 1 and not args.keywords: # Unrollable case
+        w_max_item = None
+        for w_item in args_w:
+            if w_max_item is None or \
+                   space.is_true(compare(w_item, w_max_item)):
+                w_max_item = w_item
+        return w_max_item
+    else:
+        return min_max_loop(space, args, implementation_of)
+
+ at specialize.arg(2)
+def min_max_loop(space, args, implementation_of):
+    if implementation_of == "max":
+        compare = space.gt
+    else:
+        compare = space.lt
     args_w = args.arguments_w
     if len(args_w) > 1:
         w_sequence = space.newtuple(args_w)

Modified: pypy/trunk/pypy/module/__builtin__/test/test_minmax.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/test/test_minmax.py	(original)
+++ pypy/trunk/pypy/module/__builtin__/test/test_minmax.py	Thu Sep 16 07:27:46 2010
@@ -51,3 +51,37 @@
 
    def test_max_empty(self):
       raises(ValueError, max, [])
+
+class AppTestMaxTuple:
+
+   def test_max_usual(self):
+      assert max((1, 2, 3)) == 3
+
+   def test_max_floats(self):
+      assert max((0.1, 2.7, 14.7)) == 14.7
+
+   def test_max_chars(self):
+      assert max(('a', 'b', 'c')) == 'c'
+
+   def test_max_strings(self):
+      assert max(('aaa', 'bbb', 'c')) == 'c'
+
+   def test_max_mixed(self):
+      assert max(('1', 2, 3, 'aa')) == 'aa'
+
+class AppTestMinList:
+
+   def test_min_usual(self):
+      assert min([1, 2, 3]) == 1
+
+   def test_min_floats(self):
+      assert min([0.1, 2.7, 14.7]) == 0.1
+
+   def test_min_chars(self):
+      assert min(['a', 'b', 'c']) == 'a'
+
+   def test_min_strings(self):
+      assert min(['aaa', 'bbb', 'c']) == 'aaa'
+
+   def test_min_mixed(self):
+      assert min(['1', 2, 3, 'aa']) == 2

Modified: pypy/trunk/pypy/module/pypyjit/test/test_pypy_c.py
==============================================================================
--- pypy/trunk/pypy/module/pypyjit/test/test_pypy_c.py	(original)
+++ pypy/trunk/pypy/module/pypyjit/test/test_pypy_c.py	Thu Sep 16 07:27:46 2010
@@ -762,6 +762,8 @@
                 else:
                     n = 215
 
+                print
+                print 'Test:', e1, e2, n, res
                 self.run_source('''
                 class tst:
                     pass
@@ -779,6 +781,25 @@
                     return sa
                 '''%(e1, e2), n, ([], res))
 
+    def test_boolrewrite_ptr_single(self):
+        self.run_source('''
+            class tst:
+                pass
+            def main():
+                a = tst()
+                b = tst()
+                c = tst()
+                sa = 0
+                for i in range(1000):
+                    if a == b: sa += 1
+                    else: sa += 2
+                    if a != b: sa += 10000
+                    else: sa += 20000
+                    if i > 750: a = b
+                return sa
+            ''', 215, ([], 12481752))
+        assert False
+
     def test_array_sum(self):
         for tc, maxops in zip('bhilBHILfd', (38,) * 6 + (40, 40, 41, 38)):
             res = 19352859
@@ -1059,7 +1080,16 @@
 
         ''', 170, ([], 1239690.0))
 
-        
+    def test_min_max(self):
+        self.run_source('''
+        def main():
+            i=0
+            sa=0
+            while i < 2000: 
+                sa+=min(max(i, 3000), 4000)
+                i+=1
+            return sa
+        ''', 51, ([], 2000*3000))
 
     # test_circular
 



More information about the Pypy-commit mailing list