[pypy-svn] r54171 - in pypy/dist/pypy/objspace/std: . test

fijal at codespeak.net fijal at codespeak.net
Mon Apr 28 12:18:22 CEST 2008


Author: fijal
Date: Mon Apr 28 12:18:20 2008
New Revision: 54171

Modified:
   pypy/dist/pypy/objspace/std/floatobject.py
   pypy/dist/pypy/objspace/std/intobject.py
   pypy/dist/pypy/objspace/std/test/test_floatobject.py
Log:
* Shorten a bit comparison function implementation (by metaprogramming)
* Implement comparisons between floats and ints, both way, not sure
  what to do with int -> float comparison where we loose precision.

  This speeds up pybench quite a bit (there is a benchmark for that).
  (from 3.5x times slower than cpython on int/float comparison to roughly
  equal)


Modified: pypy/dist/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/floatobject.py	(original)
+++ pypy/dist/pypy/objspace/std/floatobject.py	Mon Apr 28 12:18:20 2008
@@ -87,35 +87,47 @@
     s = formatd("%.12g", x)
     return space.wrap(should_not_look_like_an_int(s))
 
-def lt__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i < j )
-
-def le__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i <= j )
-
-def eq__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i == j )
-
-def ne__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i != j )
-
-def gt__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i > j )
-
-def ge__Float_Float(space, w_float1, w_float2):
-    i = w_float1.floatval
-    j = w_float2.floatval
-    return space.newbool( i >= j )
+
+def declare_new_float_comparison(opname):
+    import operator
+    from pypy.tool.sourcetools import func_with_new_name
+    op = getattr(operator, opname)
+    def f(space, w_int1, w_int2):
+        i = w_int1.floatval
+        j = w_int2.floatval
+        return space.newbool(op(i, j))
+    name = opname + "__Float_Float"
+    return func_with_new_name(f, name), name
+
+def declare_new_int_float_comparison(opname):
+    import operator
+    from pypy.tool.sourcetools import func_with_new_name
+    op = getattr(operator, opname)
+    def f(space, w_int1, w_float2):
+        i = w_int1.intval
+        j = w_float2.floatval
+        return space.newbool(op(float(i), j))
+    name = opname + "__Int_Float"
+    return func_with_new_name(f, name), name
+
+def declare_new_float_int_comparison(opname):
+    import operator
+    from pypy.tool.sourcetools import func_with_new_name
+    op = getattr(operator, opname)
+    def f(space, w_float1, w_int2):
+        i = w_float1.floatval
+        j = w_int2.intval
+        return space.newbool(op(i, float(j)))
+    name = opname + "__Float_Int"
+    return func_with_new_name(f, name), name
+
+for op in ['lt', 'le', 'eq', 'ne', 'gt', 'ge']:
+    func, name = declare_new_float_comparison(op)
+    globals()[name] = func
+    func, name = declare_new_int_float_comparison(op)
+    globals()[name] = func
+    func, name = declare_new_float_int_comparison(op)
+    globals()[name] = func
 
 # for overflowing comparisons between longs and floats
 # XXX we might have to worry (later) about eq__Float_Int, for the case

Modified: pypy/dist/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/intobject.py	(original)
+++ pypy/dist/pypy/objspace/std/intobject.py	Mon Apr 28 12:18:20 2008
@@ -50,35 +50,20 @@
 
 str__Int = repr__Int
 
-def lt__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i < j )
-
-def le__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i <= j )
-
-def eq__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i == j )
-
-def ne__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i != j )
-
-def gt__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i > j )
-
-def ge__Int_Int(space, w_int1, w_int2):
-    i = w_int1.intval
-    j = w_int2.intval
-    return space.newbool( i >= j )
+def declare_new_int_comparison(opname):
+    import operator
+    from pypy.tool.sourcetools import func_with_new_name
+    op = getattr(operator, opname)
+    def f(space, w_int1, w_int2):
+        i = w_int1.intval
+        j = w_int2.intval
+        return space.newbool(op(i, j))
+    name = opname + "__Int_Int"
+    return func_with_new_name(f, name), name
+
+for op in ['lt', 'le', 'eq', 'ne', 'gt', 'ge']:
+    func, name = declare_new_int_comparison(op)
+    globals()[name] = func
 
 def hash__Int(space, w_int1):
     # unlike CPython, we don't special-case the value -1 in most of our

Modified: pypy/dist/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_floatobject.py	Mon Apr 28 12:18:20 2008
@@ -157,6 +157,16 @@
         assert 13.01 > 13L
         assert 13.0 >= 13L
         assert 13.01 >= 13L
+        assert 12.0 == 12
+        assert 12.1 != 12
+        assert infinite != 123456789
+        assert 12.9 < 13
+        assert -infinite < -13
+        assert 12.9 <= 13
+        assert 13.0 <= 13
+        assert 13.01 > 13
+        assert 13.0 >= 13
+        assert 13.01 >= 13
         assert infinite > verylonglong
         assert infinite >= verylonglong
         assert 1234.56 < verylonglong
@@ -177,3 +187,8 @@
         assert verylonglong <= infinite
         assert verylonglong > 1234.56
         assert verylonglong >= 1234.56
+        assert 13 >= 12.9
+        assert 13 >= 13.0
+        assert 13 < 13.01
+        assert 13 <= 13.0
+        assert 13 <= 13.01



More information about the Pypy-commit mailing list