[pypy-svn] r77070 - in pypy/branch/fast-forward/pypy: rpython rpython/lltypesystem rpython/test translator/c/src
afa at codespeak.net
afa at codespeak.net
Tue Sep 14 18:10:24 CEST 2010
Author: afa
Date: Tue Sep 14 18:10:23 2010
New Revision: 77070
Modified:
pypy/branch/fast-forward/pypy/rpython/lltypesystem/lloperation.py
pypy/branch/fast-forward/pypy/rpython/lltypesystem/opimpl.py
pypy/branch/fast-forward/pypy/rpython/rfloat.py
pypy/branch/fast-forward/pypy/rpython/test/test_rfloat.py
pypy/branch/fast-forward/pypy/translator/c/src/float.h
Log:
Implement r_float <-> r_ulonglong conversions,
needed by rlib/rstruct/ieee.py at least.
Modified: pypy/branch/fast-forward/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/lltypesystem/lloperation.py (original)
+++ pypy/branch/fast-forward/pypy/rpython/lltypesystem/lloperation.py Tue Sep 14 18:10:23 2010
@@ -347,10 +347,12 @@
'cast_int_to_longlong': LLOp(canfold=True),
'cast_uint_to_int': LLOp(canfold=True),
'cast_uint_to_float': LLOp(canfold=True),
- 'cast_longlong_to_float':LLOp(canfold=True),
+ 'cast_longlong_to_float' :LLOp(canfold=True),
+ 'cast_ulonglong_to_float':LLOp(canfold=True),
'cast_float_to_int': LLOp(canraise=(OverflowError,), tryfold=True),
'cast_float_to_uint': LLOp(canfold=True), # XXX need OverflowError?
- 'cast_float_to_longlong':LLOp(canfold=True),
+ 'cast_float_to_longlong' :LLOp(canfold=True),
+ 'cast_float_to_ulonglong':LLOp(canfold=True),
'truncate_longlong_to_int':LLOp(canfold=True),
'force_cast': LLOp(sideeffects=False), # only for rffi.cast()
Modified: pypy/branch/fast-forward/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/lltypesystem/opimpl.py (original)
+++ pypy/branch/fast-forward/pypy/rpython/lltypesystem/opimpl.py Tue Sep 14 18:10:23 2010
@@ -291,6 +291,13 @@
ui = float(int(i >> 31)) * float(0x80000000)
return ui + li
+def op_cast_ulonglong_to_float(i):
+ assert isinstance(i, r_ulonglong)
+ # take first 32 bits
+ li = float(int(i & r_ulonglong(0xffffffff)))
+ ui = float(int(i >> 32)) * float(0x100000000)
+ return ui + li
+
def op_cast_int_to_char(b):
assert type(b) is int
return chr(b)
@@ -319,6 +326,10 @@
truncated = int((small - high) * r)
return r_longlong_result(high) * 0x100000000 + truncated
+def op_cast_float_to_ulonglong(f):
+ assert type(f) is float
+ return r_ulonglong(r_longlong(f))
+
def op_cast_char_to_int(b):
assert type(b) is str and len(b) == 1
return ord(b)
Modified: pypy/branch/fast-forward/pypy/rpython/rfloat.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/rfloat.py (original)
+++ pypy/branch/fast-forward/pypy/rpython/rfloat.py Tue Sep 14 18:10:23 2010
@@ -1,7 +1,8 @@
from pypy.tool.pairtype import pairtype
from pypy.annotation import model as annmodel
-from pypy.rpython.lltypesystem.lltype import \
- Signed, Unsigned, SignedLongLong, Bool, Float, Void, pyobjectptr
+from pypy.rpython.lltypesystem.lltype import (
+ Signed, Unsigned, SignedLongLong, UnsignedLongLong,
+ Bool, Float, Void, pyobjectptr)
from pypy.rpython.error import TyperError
from pypy.rpython.rmodel import FloatRepr
from pypy.rpython.rmodel import IntegerRepr, BoolRepr
@@ -157,6 +158,9 @@
if r_from.lowleveltype == SignedLongLong and r_to.lowleveltype == Float:
log.debug('explicit cast_longlong_to_float')
return llops.genop('cast_longlong_to_float', [v], resulttype=Float)
+ if r_from.lowleveltype == UnsignedLongLong and r_to.lowleveltype == Float:
+ log.debug('explicit cast_ulonglong_to_float')
+ return llops.genop('cast_ulonglong_to_float', [v], resulttype=Float)
return NotImplemented
class __extend__(pairtype(FloatRepr, IntegerRepr)):
@@ -170,6 +174,9 @@
if r_from.lowleveltype == Float and r_to.lowleveltype == SignedLongLong:
log.debug('explicit cast_float_to_longlong')
return llops.genop('cast_float_to_longlong', [v], resulttype=SignedLongLong)
+ if r_from.lowleveltype == Float and r_to.lowleveltype == UnsignedLongLong:
+ log.debug('explicit cast_float_to_ulonglong')
+ return llops.genop('cast_float_to_ulonglong', [v], resulttype=UnsignedLongLong)
return NotImplemented
class __extend__(pairtype(BoolRepr, FloatRepr)):
Modified: pypy/branch/fast-forward/pypy/rpython/test/test_rfloat.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/test/test_rfloat.py (original)
+++ pypy/branch/fast-forward/pypy/rpython/test/test_rfloat.py Tue Sep 14 18:10:23 2010
@@ -2,8 +2,8 @@
from pypy.translator.translator import TranslationContext
from pypy.rpython.test import snippet
from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
-from pypy.rlib.rarithmetic import r_int, r_uint, r_longlong, r_singlefloat,\
- isnan, isinf
+from pypy.rlib.rarithmetic import (
+ r_int, r_uint, r_longlong, r_ulonglong, r_singlefloat)
from pypy.rlib.objectmodel import compute_hash
class TestSnippet(object):
@@ -114,6 +114,21 @@
res = self.interpret(fn, [-9])
assert self.float_eq(res, 0.5 * ((sys.maxint+1)*2 - 9))
+ def test_to_r_ulonglong(self):
+ def fn(x):
+ return r_ulonglong(x)
+ res = self.interpret(fn, [12.34])
+ assert res == 12
+ bigval = sys.maxint * 1.234
+ res = self.interpret(fn, [bigval])
+ assert long(res) == long(bigval)
+
+ def test_from_r_ulonglong(self):
+ def fn(n):
+ return float(r_ulonglong(n)) / 2
+ res = self.interpret(fn, [41])
+ assert self.float_eq(res, 20.5)
+
def test_r_singlefloat(self):
def fn(x):
y = r_singlefloat(x)
Modified: pypy/branch/fast-forward/pypy/translator/c/src/float.h
==============================================================================
--- pypy/branch/fast-forward/pypy/translator/c/src/float.h (original)
+++ pypy/branch/fast-forward/pypy/translator/c/src/float.h Tue Sep 14 18:10:23 2010
@@ -36,8 +36,10 @@
#define OP_CAST_INT_TO_FLOAT(x,r) r = (double)(x)
#define OP_CAST_UINT_TO_FLOAT(x,r) r = (double)(x)
#define OP_CAST_LONGLONG_TO_FLOAT(x,r) r = (double)(x)
+#define OP_CAST_ULONGLONG_TO_FLOAT(x,r) r = (double)(x)
#define OP_CAST_BOOL_TO_FLOAT(x,r) r = (double)(x)
#ifdef HAVE_LONG_LONG
#define OP_CAST_FLOAT_TO_LONGLONG(x,r) r = (long long)(x)
+#define OP_CAST_FLOAT_TO_ULONGLONG(x,r) r = (unsigned long long)(x)
#endif
More information about the Pypy-commit
mailing list