[pypy-svn] r13734 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Thu Jun 23 19:41:45 CEST 2005


Author: arigo
Date: Thu Jun 23 19:41:41 2005
New Revision: 13734

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/rpython/test/test_rint.py
Log:
Unsigned arithmetic: basic support and bug fixes in the code that was already
there but not tested.


Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Thu Jun 23 19:41:41 2005
@@ -3,6 +3,7 @@
 from pypy.translator.translator import Translator
 from pypy.tool.sourcetools import compile2
 from pypy.objspace.flow.model import Constant, Variable, last_exception
+from pypy.rpython.rarithmetic import intmask, r_uint
 import py
 
 log = py.log.Producer('llinterp')
@@ -251,22 +252,34 @@
         assert type(b) is str and len(b) == 1
         return ord(b)
 
+    def op_cast_int_to_uint(self, b):
+        assert type(b) is int
+        return r_uint(b)
+
+    def op_cast_uint_to_int(self, b):
+        assert type(b) is r_uint
+        return intmask(b)
+
 # __________________________________________________________
 # primitive operations
 from pypy.objspace.flow.operation import FunctionByName
 opimpls = FunctionByName.copy()
 opimpls['is_true'] = bool
 
-for typ in (float, int):
+for typ in (float, int, r_uint):
     typname = typ.__name__
+    if typ is r_uint:
+        opnameprefix = 'uint'
+    else:
+        opnameprefix = typname
     optup = ('add', 'sub', 'mul', 'div', 'mod', 'gt', 'lt', 'ge', 'ne', 'le', 'eq', 'and_', 'or_')
-    if typ is int:
+    if typ in (int, r_uint):
         optup += 'truediv', 'floordiv'
     for opname in optup:
         assert opname in opimpls
         pureopname = opname.rstrip('_')
         exec py.code.Source("""
-            def %(typname)s_%(pureopname)s(x, y):
+            def %(opnameprefix)s_%(pureopname)s(x, y):
                 assert isinstance(x, %(typname)s)
                 assert isinstance(y, %(typname)s)
                 func = opimpls[%(opname)r]
@@ -275,7 +288,7 @@
     for opname in 'is_true', 'neg':
         assert opname in opimpls
         exec py.code.Source("""
-            def %(typname)s_%(opname)s(x):
+            def %(opnameprefix)s_%(opname)s(x):
                 assert isinstance(x, %(typname)s)
                 func = opimpls[%(opname)r]
                 return func(x)

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Thu Jun 23 19:41:41 2005
@@ -159,7 +159,7 @@
 
 def intmask(n):
     if isinstance(n, int):
-        return n
+        return int(n)   # possibly bool->int
     if isinstance(n, r_uint):
         n = long(n)
     n &= LONG_MASK

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Thu Jun 23 19:41:41 2005
@@ -2,7 +2,6 @@
 from pypy.annotation import model as annmodel
 from pypy.rpython import lltype
 from pypy.rpython import rarithmetic
-from pypy.rpython.lltype import Void, Signed, Float, Ptr, RuntimeTypeInfo
 from pypy.rpython.rtyper import TyperError
 from pypy.rpython.rrange import rtype_builtin_range
 from pypy.rpython.rmodel import Repr, TyperError
@@ -33,7 +32,7 @@
 
 
 class BuiltinFunctionRepr(Repr):
-    lowleveltype = Void
+    lowleveltype = lltype.Void
 
     def __init__(self, builtinfunc):
         self.builtinfunc = builtinfunc
@@ -102,7 +101,11 @@
 #def rtype_builtin_range(hop): see rrange.py
 
 def rtype_intmask(hop):
-    vlist = hop.inputargs(Signed)
+    vlist = hop.inputargs(lltype.Signed)
+    return vlist[0]
+
+def rtype_r_uint(hop):
+    vlist = hop.inputargs(lltype.Unsigned)
     return vlist[0]
 
 
@@ -119,11 +122,11 @@
 def rtype_malloc(hop):
     assert hop.args_s[0].is_constant()
     if hop.nb_args == 1:
-        vlist = hop.inputargs(Void)
+        vlist = hop.inputargs(lltype.Void)
         return hop.genop('malloc', vlist,
                          resulttype = hop.r_result.lowleveltype)
     else:
-        vlist = hop.inputargs(Void, Signed)
+        vlist = hop.inputargs(lltype.Void, lltype.Signed)
         return hop.genop('malloc_varsize', vlist,
                          resulttype = hop.r_result.lowleveltype)
 
@@ -133,7 +136,7 @@
 def rtype_cast_pointer(hop):
     assert hop.args_s[0].is_constant()
     assert isinstance(hop.args_r[1], rptr.PtrRepr)
-    v_type, v_input = hop.inputargs(Void, hop.args_r[1])
+    v_type, v_input = hop.inputargs(lltype.Void, hop.args_r[1])
     return hop.genop('cast_pointer', [v_input],    # v_type implicit in r_result
                      resulttype = hop.r_result.lowleveltype)
 
@@ -141,7 +144,7 @@
     assert isinstance(hop.args_r[0], rptr.PtrRepr)
     vlist = hop.inputargs(hop.args_r[0])
     return hop.genop('runtime_type_info', vlist,
-                     resulttype = rptr.PtrRepr(Ptr(RuntimeTypeInfo)))
+                 resulttype = rptr.PtrRepr(lltype.Ptr(lltype.RuntimeTypeInfo)))
 
 
 BUILTIN_TYPER[lltype.malloc] = rtype_malloc
@@ -151,6 +154,7 @@
 BUILTIN_TYPER[lltype.getRuntimeTypeInfo] = rtype_const_result
 BUILTIN_TYPER[lltype.runtime_type_info] = rtype_runtime_type_info
 BUILTIN_TYPER[rarithmetic.intmask] = rtype_intmask
+BUILTIN_TYPER[rarithmetic.r_uint] = rtype_r_uint
 
 import time
 
@@ -164,9 +168,9 @@
 import math
 
 def rtype_math_exp(hop):
-    vlist = hop.inputargs(Float)
+    vlist = hop.inputargs(lltype.Float)
     # XXX need PyFPE_START_PROTECT/PyFPE_END_PROTECT/Py_SET_ERRNO_ON_MATH_ERROR
-    return hop.llops.gencapicall('exp', vlist, resulttype=Float,
+    return hop.llops.gencapicall('exp', vlist, resulttype=lltype.Float,
                                  includes=["math.h"])   # XXX clean up needed
 
 BUILTIN_TYPER[math.exp] = rtype_math_exp

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Thu Jun 23 19:41:41 2005
@@ -6,6 +6,7 @@
 from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr, CharRepr, \
      inputconst
 from pypy.rpython.robject import PyObjRepr, pyobj_repr
+from pypy.rpython.rarithmetic import intmask, r_uint
 
 
 debug = False
@@ -27,10 +28,10 @@
 class __extend__(pairtype(IntegerRepr, IntegerRepr)):
 
     def convert_from_to((r_from, r_to), v, llops):
-        if r_from.lowleveltype == Unsigned and r_to.lowleveltype == Signed:
+        if r_from.lowleveltype == Signed and r_to.lowleveltype == Unsigned:
             if debug: print 'explicit cast_int_to_uint'
             return llops.genop('cast_int_to_uint', [v], resulttype=Unsigned)
-        if r_from.lowleveltype == Signed and r_to.lowleveltype == Unsigned:
+        if r_from.lowleveltype == Unsigned and r_to.lowleveltype == Signed:
             if debug: print 'explicit cast_uint_to_int'
             return llops.genop('cast_uint_to_int', [v], resulttype=Signed)
         return v
@@ -189,9 +190,13 @@
 class __extend__(IntegerRepr):
 
     def convert_const(self, value):
-        if not isinstance(value, int):   # can be bool
+        if not isinstance(value, (int, r_uint)):   # can be bool
             raise TyperError("not an integer: %r" % (value,))
-        return int(value)
+        if self.lowleveltype == Signed:
+            return intmask(value)
+        if self.lowleveltype == Unsigned:
+            return r_uint(value)
+        raise NotImplementedError
 
     def rtype_float(_, hop):
         vlist = hop.inputargs(Float)

Modified: pypy/dist/pypy/rpython/test/test_rint.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rint.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rint.py	Thu Jun 23 19:41:41 2005
@@ -3,6 +3,7 @@
 from pypy.annotation import model as annmodel
 from pypy.rpython.test import snippet
 from pypy.rpython.test.test_llinterp import interpret, make_interpreter
+from pypy.rpython.rarithmetic import r_uint
 
 
 class TestSnippet(object):
@@ -79,3 +80,16 @@
     res = ev_fun(-123)
     assert ''.join(res.chars) == '-0x7b'
     
+def test_unsigned():
+    def dummy(i):
+        i = r_uint(i)
+        j = r_uint(12)
+        return i < j
+
+    ev_fun = make_interpreter(dummy, [0])
+
+    res = ev_fun(0)
+    assert res is True
+
+    res = ev_fun(-1)
+    assert res is False    # -1 ==> 0xffffffff



More information about the Pypy-commit mailing list