[pypy-svn] r20884 - in pypy/dist/pypy: rpython rpython/test translator/c/src translator/c/test
mwh at codespeak.net
mwh at codespeak.net
Thu Dec 8 12:35:55 CET 2005
Author: mwh
Date: Thu Dec 8 12:35:54 2005
New Revision: 20884
Modified:
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/rint.py
pypy/dist/pypy/rpython/test/test_rint.py
pypy/dist/pypy/translator/c/src/int.h
pypy/dist/pypy/translator/c/test/test_annotated.py
Log:
(mwh, johahn)
implement truncation of signed long long to signed long
(need to do all this stuff for unsigned variants at some
point too ... fun).
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Thu Dec 8 12:35:54 2005
@@ -4,6 +4,7 @@
from pypy.rpython.memory import lladdress
from pypy.rpython.ootypesystem import ootype
+import sys
import math
import py
@@ -440,6 +441,11 @@
assert type(b) is int
return r_longlong(b)
+ def op_truncate_longlong_to_int(self, b):
+ assert type(b) is r_longlong
+ assert -sys.maxint-1 <= b <= sys.maxint
+ return int(b)
+
def op_int_floordiv_ovf_zer(self, a, b):
assert type(a) is int
assert type(b) is int
Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py (original)
+++ pypy/dist/pypy/rpython/rint.py Thu Dec 8 12:35:54 2005
@@ -46,6 +46,8 @@
return llops.genop('cast_uint_to_int', [v], resulttype=Signed)
if r_from.lowleveltype == Signed and r_to.lowleveltype == SignedLongLong:
return llops.genop('cast_int_to_longlong', [v], resulttype=SignedLongLong)
+ if r_from.lowleveltype == SignedLongLong and r_to.lowleveltype == Signed:
+ return llops.genop('truncate_longlong_to_int', [v], resulttype=Signed)
return NotImplemented
#arithmetic
@@ -281,7 +283,7 @@
def rtype_int(self, hop):
if self.lowleveltype in (Unsigned, UnsignedLongLong):
raise TyperError("use intmask() instead of int(r_uint(...))")
- vlist = hop.inputargs(self)
+ vlist = hop.inputargs(Signed)
return vlist[0]
def rtype_float(_, hop):
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 Dec 8 12:35:54 2005
@@ -115,3 +115,9 @@
res = interpret(g, [1])
assert res == 1
+
+def test_downcast_int():
+ def f(i):
+ return int(i)
+ res = interpret(f, [r_longlong(0)])
+ assert res == 0
Modified: pypy/dist/pypy/translator/c/src/int.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/int.h (original)
+++ pypy/dist/pypy/translator/c/src/int.h Thu Dec 8 12:35:54 2005
@@ -161,6 +161,8 @@
#define OP_CAST_INT_TO_CHAR(x,r,err) r = (char)(x)
#define OP_CAST_PTR_TO_INT(x,r,err) r = (long)(x) /* XXX */
+#define OP_TRUNCATE_LONGLONG_TO_INT(x,r,err) r = (long)(x)
+
#define OP_CAST_UNICHAR_TO_INT(x,r,err) r = (long)((unsigned long)(x)) /*?*/
#define OP_CAST_INT_TO_UNICHAR(x,r,err) r = (unsigned int)(x)
Modified: pypy/dist/pypy/translator/c/test/test_annotated.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_annotated.py (original)
+++ pypy/dist/pypy/translator/c/test/test_annotated.py Thu Dec 8 12:35:54 2005
@@ -5,6 +5,9 @@
from pypy.translator.test import snippet
+from pypy.rpython.rarithmetic import r_ulonglong, r_longlong
+
+
# XXX this tries to make compiling faster for full-scale testing
from pypy.translator.tool import cbuild
cbuild.enable_fast_compilation()
@@ -235,7 +238,6 @@
assert fn(-3) == 42
def test_long_long(self):
- from pypy.rpython.rarithmetic import r_ulonglong, r_longlong
def f(i=r_ulonglong):
return 4*i
fn = self.getcompiled(f, view=False)
@@ -247,7 +249,6 @@
assert gn(sys.maxint) == 4*sys.maxint
def test_specializing_int_functions(self):
- from pypy.rpython.rarithmetic import r_longlong
def f(i):
return i + 1
f._annspecialcase_ = "specialize:argtype0"
@@ -260,3 +261,9 @@
fn = self.getcompiled(g)
assert g(0) == 1
assert g(1) == 1
+
+ def test_downcast_int(self):
+ def f(i=r_longlong):
+ return int(i)
+ fn = self.getcompiled(f)
+ assert f(0) == 0
More information about the Pypy-commit
mailing list