本文整理汇总了Python中pypy.rlib.rarithmetic.r_singlefloat函数的典型用法代码示例。如果您正苦于以下问题:Python r_singlefloat函数的具体用法?Python r_singlefloat怎么用?Python r_singlefloat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了r_singlefloat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_r_singlefloat_eq
def test_r_singlefloat_eq():
x = r_singlefloat(2.5) # exact number
y = r_singlefloat(2.5)
assert x == y
assert not x != y
assert not x == 2.5
assert x != 2.5
py.test.raises(TypeError, "x>y")
示例2: test_cast
def test_cast(self):
res = cast(SIZE_T, -1)
assert type(res) is r_size_t
assert res == r_size_t(-1)
#
res = cast(lltype.Signed, 42.5)
assert res == 42
res = cast(lltype.SingleFloat, 12.3)
assert res == r_singlefloat(12.3)
res = cast(lltype.SingleFloat, res)
assert res == r_singlefloat(12.3)
res = cast(lltype.Float, r_singlefloat(12.))
assert res == 12.
示例3: test_single_float_args
def test_single_float_args(self):
"""
float sum_xy_float(float x, float y)
{
return x+y;
}
"""
from ctypes import c_float # this is used only to compute the expected result
libfoo = self.get_libfoo()
func = (libfoo, 'sum_xy_float', [types.float, types.float], types.float)
x = r_singlefloat(12.34)
y = r_singlefloat(56.78)
res = self.call(func, [x, y], rffi.FLOAT, jitif=["singlefloats"])
expected = c_float(c_float(12.34).value + c_float(56.78).value).value
assert float(res) == expected
示例4: test_contains_unsupported_variable_type
def test_contains_unsupported_variable_type():
def f(x):
return x
graph = support.getgraph(f, [5])
for sf in [False, True]:
for sll in [False, True]:
for ssf in [False, True]:
assert not contains_unsupported_variable_type(graph, sf,
sll, ssf)
#
graph = support.getgraph(f, [5.5])
for sf in [False, True]:
for sll in [False, True]:
for ssf in [False, True]:
res = contains_unsupported_variable_type(graph, sf, sll, ssf)
assert res is not sf
#
graph = support.getgraph(f, [r_singlefloat(5.5)])
for sf in [False, True]:
for sll in [False, True]:
for ssf in [False, True]:
res = contains_unsupported_variable_type(graph, sf, sll, ssf)
assert res == (not ssf)
#
graph = support.getgraph(f, [r_longlong(5)])
for sf in [False, True]:
for sll in [False, True]:
for ssf in [False, True]:
res = contains_unsupported_variable_type(graph, sf, sll, ssf)
assert res == (sys.maxint == 2147483647 and not sll)
示例5: _singlefloat
def _singlefloat(self, w_ffitype, w_obj):
# a separate function, which can be seen by the jit or not,
# depending on whether singlefloats are supported
from pypy.rlib.rarithmetic import r_singlefloat
floatval = self.space.float_w(w_obj)
singlefloatval = r_singlefloat(floatval)
self.handle_singlefloat(w_ffitype, w_obj, singlefloatval)
示例6: test_annotation_to_lltype
def test_annotation_to_lltype():
from pypy.rlib.rarithmetic import r_uint, r_singlefloat
s_i = SomeInteger()
s_pos = SomeInteger(nonneg=True)
s_1 = SomeInteger(nonneg=True); s_1.const = 1
s_m1 = SomeInteger(nonneg=False); s_m1.const = -1
s_u = SomeInteger(nonneg=True, unsigned=True);
s_u1 = SomeInteger(nonneg=True, unsigned=True);
s_u1.const = r_uint(1)
assert annotation_to_lltype(s_i) == lltype.Signed
assert annotation_to_lltype(s_pos) == lltype.Signed
assert annotation_to_lltype(s_1) == lltype.Signed
assert annotation_to_lltype(s_m1) == lltype.Signed
assert annotation_to_lltype(s_u) == lltype.Unsigned
assert annotation_to_lltype(s_u1) == lltype.Unsigned
assert annotation_to_lltype(SomeBool()) == lltype.Bool
assert annotation_to_lltype(SomeChar()) == lltype.Char
PS = lltype.Ptr(lltype.GcStruct('s'))
s_p = SomePtr(ll_ptrtype=PS)
assert annotation_to_lltype(s_p) == PS
py.test.raises(ValueError, "annotation_to_lltype(si0)")
C = ootype.Instance('C', ROOT, {})
ref = SomeOOInstance(C)
assert annotation_to_lltype(ref) == C
s_singlefloat = SomeSingleFloat()
s_singlefloat.const = r_singlefloat(0.0)
assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
示例7: arg_singlefloat
def arg_singlefloat(self, space, argchain, w_arg):
# a separate function, which can be seen by the jit or not,
# depending on whether singlefloats are supported
from pypy.rlib.rarithmetic import r_singlefloat
fval = space.float_w(w_arg)
sfval = r_singlefloat(fval)
argchain.arg(sfval)
示例8: test_wrap
def test_wrap():
def _is(box1, box2):
return box1.__class__ == box2.__class__ and box1.value == box2.value
p = lltype.malloc(lltype.GcStruct("S"))
po = lltype.cast_opaque_ptr(llmemory.GCREF, p)
assert _is(wrap(None, 42), BoxInt(42))
assert _is(wrap(None, 42.5), boxfloat(42.5))
assert _is(wrap(None, p), BoxPtr(po))
assert _is(wrap(None, 42, in_const_box=True), ConstInt(42))
assert _is(wrap(None, 42.5, in_const_box=True), constfloat(42.5))
assert _is(wrap(None, p, in_const_box=True), ConstPtr(po))
if longlong.supports_longlong:
import sys
from pypy.rlib.rarithmetic import r_longlong, r_ulonglong
value = r_longlong(-sys.maxint * 17)
assert _is(wrap(None, value), BoxFloat(value))
assert _is(wrap(None, value, in_const_box=True), ConstFloat(value))
value_unsigned = r_ulonglong(-sys.maxint * 17)
assert _is(wrap(None, value_unsigned), BoxFloat(value))
sfval = r_singlefloat(42.5)
ival = longlong.singlefloat2int(sfval)
assert _is(wrap(None, sfval), BoxInt(ival))
assert _is(wrap(None, sfval, in_const_box=True), ConstInt(ival))
示例9: detect_floatformat
def detect_floatformat():
from pypy.rpython.lltypesystem import rffi, lltype
buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
packed = rffi.charpsize2str(buf, 8)
if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
double_format = 'IEEE, big-endian'
elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
double_format = 'IEEE, little-endian'
else:
double_format = 'unknown'
lltype.free(buf, flavor='raw')
#
buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
packed = rffi.charpsize2str(buf, 4)
if packed == "\x4b\x7f\x01\x02":
float_format = 'IEEE, big-endian'
elif packed == "\x02\x01\x7f\x4b":
float_format = 'IEEE, little-endian'
else:
float_format = 'unknown'
lltype.free(buf, flavor='raw')
return double_format, float_format
示例10: pack_float
def pack_float(fmtiter):
doubleval = fmtiter.accept_float_arg()
floatval = r_singlefloat(doubleval)
float_buf[0] = floatval
p = rffi.cast(rffi.CCHARP, float_buf)
for i in range(sizeof_float):
fmtiter.result.append(p[i])
示例11: test_specialize_value
def test_specialize_value():
assert specialize_value(lltype.Char, 0x41) == '\x41'
if longlong.supports_longlong:
import sys
value = longlong.r_float_storage(sys.maxint*17)
assert specialize_value(lltype.SignedLongLong, value) == sys.maxint*17
sfval = r_singlefloat(42.5)
ival = longlong.singlefloat2int(sfval)
assert specialize_value(rffi.FLOAT, ival) == sfval
示例12: test_call_with_singlefloats
def test_call_with_singlefloats(self):
cpu = self.cpu
if not cpu.supports_floats or not cpu.supports_singlefloats:
py.test.skip('requires floats and singlefloats')
import random
from pypy.rlib.libffi import types
from pypy.rlib.rarithmetic import r_singlefloat
def func(*args):
res = 0.0
for i, x in enumerate(args):
res += (i + 1.1) * float(x)
return res
F = lltype.Float
S = lltype.SingleFloat
I = lltype.Signed
floats = [random.random() - 0.5 for i in range(8)]
singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(8)]
ints = [random.randrange(-99, 99) for i in range(8)]
for repeat in range(100):
args = []
argvalues = []
argslist = []
local_floats = list(floats)
local_singlefloats = list(singlefloats)
local_ints = list(ints)
for i in range(8):
case = random.randrange(0, 3)
if case == 0:
args.append(F)
arg = local_floats.pop()
argslist.append(boxfloat(arg))
elif case == 1:
args.append(S)
arg = local_singlefloats.pop()
argslist.append(BoxInt(longlong.singlefloat2int(arg)))
else:
args.append(I)
arg = local_ints.pop()
argslist.append(BoxInt(arg))
argvalues.append(arg)
FUNC = self.FuncType(args, F)
FPTR = self.Ptr(FUNC)
func_ptr = llhelper(FPTR, func)
calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
EffectInfo.MOST_GENERAL)
funcbox = self.get_funcbox(cpu, func_ptr)
res = self.execute_operation(rop.CALL,
[funcbox] + argslist,
'float', descr=calldescr)
expected = func(*argvalues)
assert abs(res.getfloat() - expected) < 0.0001
示例13: pack_float
def pack_float(fmtiter):
doubleval = fmtiter.accept_float_arg()
floatval = r_singlefloat(doubleval)
value = longlong2float.singlefloat2uint(floatval)
value = widen(value)
if fmtiter.bigendian:
for i in range_4_unroll:
x = (value >> (8*i)) & 0xff
fmtiter.result.append(chr(x))
else:
for i in range_4_unroll:
fmtiter.result.append(chr(value & 0xff))
value >>= 8
示例14: ctypes2lltype
def ctypes2lltype(T, cobj):
"""Convert the ctypes object 'cobj' to its lltype equivalent.
'T' is the expected lltype type.
"""
if isinstance(T, lltype.Ptr):
if not cobj: # NULL pointer
return lltype.nullptr(T.TO)
if isinstance(T.TO, lltype.Struct):
if T.TO._arrayfld is not None:
raise NotImplementedError("XXX var-sized structs")
container = lltype._struct(T.TO)
struct_use_ctypes_storage(container, cobj.contents)
elif isinstance(T.TO, lltype.Array):
if T.TO._hints.get('nolength', False):
container = _array_of_unknown_length(T.TO)
container._storage = cobj.contents
else:
raise NotImplementedError("array with an explicit length")
elif isinstance(T.TO, lltype.FuncType):
_callable = get_ctypes_trampoline(T.TO, cobj)
return lltype.functionptr(T.TO, getattr(cobj, '__name__', '?'),
_callable=_callable)
elif isinstance(T.TO, lltype.OpaqueType):
container = lltype._opaque(T.TO)
else:
raise NotImplementedError(T)
llobj = lltype._ptr(T, container, solid=True)
elif T is lltype.Char:
llobj = chr(cobj)
elif T is lltype.UniChar:
llobj = unichr(cobj)
elif T is lltype.Signed:
llobj = cobj
elif T is lltype.SingleFloat:
if isinstance(cobj, ctypes.c_float):
cobj = cobj.value
llobj = r_singlefloat(cobj)
else:
from pypy.rpython.lltypesystem import rffi
try:
inttype = rffi.platform.numbertype_to_rclass[T]
except KeyError:
llobj = cobj
else:
llobj = inttype(cobj)
assert lltype.typeOf(llobj) == T
return llobj
示例15: test_struct_fields_singlefloat
def test_struct_fields_singlefloat(self):
POINT = lltype.Struct('POINT',
('x', rffi.FLOAT),
('y', rffi.FLOAT)
)
y_ofs = 4
p = lltype.malloc(POINT, flavor='raw')
p.x = r_singlefloat(123.4)
p.y = r_singlefloat(567.8)
addr = rffi.cast(rffi.VOIDP, p)
assert struct_getfield_singlefloat(types.double, addr, 0) == r_singlefloat(123.4)
assert struct_getfield_singlefloat(types.double, addr, y_ofs) == r_singlefloat(567.8)
#
struct_setfield_singlefloat(types.double, addr, 0, r_singlefloat(321.0))
struct_setfield_singlefloat(types.double, addr, y_ofs, r_singlefloat(876.5))
assert p.x == r_singlefloat(321.0)
assert p.y == r_singlefloat(876.5)
#
lltype.free(p, flavor='raw')