def test_singlefloat(self): buf = struct.pack('@ff', 12.3, 45.6) size = struct.calcsize('@f') x = self.str_storage_getitem(lltype.SingleFloat, buf, 0) assert x == r_singlefloat(12.3) x = self.str_storage_getitem(lltype.SingleFloat, buf, size) assert x == r_singlefloat(45.6)
def test_simple_call_singlefloat_args(self, **kwds): kwds.setdefault('supports_singlefloats', True) kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1) kwds['expected_call_release_gil_i'] = 0 self._run([types.float] * 2, types.double, [r_singlefloat(10.5), r_singlefloat(31.5)], -4.5, **kwds)
def test_float_array_at(monkeypatch): space, interp, image, reader = read_image("Squeak4.3.image") prim_res = [] st_res = [] w_FloatArray = space.smalltalk_at("FloatArray") w_ary = W_WordsObject(space, w_FloatArray, 1) from rpython.rlib.longlong2float import singlefloat2uint, uint2singlefloat from rpython.rlib.rarithmetic import r_singlefloat w_ary.setword(0, singlefloat2uint(r_singlefloat(1.5))) prim_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)])) prim_res.append(interp.perform(w_ary, "at:put:", w_arguments=[space.w(1), space.w(12.5)])) prim_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)])) assert [space.unwrap_float(w) for w in prim_res] == [1.5, 12.5, 12.5] from rsqueakvm.primitives.control import ExternalPlugins from rsqueakvm.plugins.float_array_plugin import FloatArrayPlugin for p in ExternalPlugins: if p is FloatArrayPlugin: monkeypatch.delitem(p.primitives, "primitiveAt") monkeypatch.delitem(p.primitives, "primitiveAtPut") break try: w_ary.setword(0, singlefloat2uint(r_singlefloat(1.5))) st_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)])) st_res.append(interp.perform(w_ary, "at:put:", w_arguments=[space.w(1), space.w(12.5)])) st_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)])) assert [space.unwrap_float(w) for w in st_res] == [1.5, 12.5, 12.5] finally: monkeypatch.undo()
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")
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.
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
def change_current_fraction(self, hash, new_fraction): """Change the value stored for 'hash' to be the given 'new_fraction', which should be a float equal to or slightly lower than 1.0. """ p_entry = self.timetable[self._get_index(hash)] subhash = self._get_subhash(hash) # find in 'n' the index that will be overwritten: the first within # range(5) that contains either the right subhash, or a null time # (or, if there isn't any, then just n == 4 will do). n = 0 while n < 4 and (p_entry.subhashes[n] != subhash and float(p_entry.times[n]) != 0.0): n += 1 # move one step to the right all elements [n - 1, n - 2, ..., 0], # (this overwrites the old item at index 'n') while n > 0: n -= 1 p_entry.subhashes[n + 1] = p_entry.subhashes[n] p_entry.times[n + 1] = p_entry.times[n] # insert the new hash at index 0. This is a good approximation, # because change_current_fraction() should be used for # new_fraction == value close to 1.0. p_entry.subhashes[0] = rffi.cast(rffi.USHORT, subhash) p_entry.times[0] = r_singlefloat(new_fraction)
def test_annotation_to_lltype(): 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 si0 = SomeInstance(DummyClassDef(), True) with py.test.raises(ValueError): annotation_to_lltype(si0) s_singlefloat = SomeSingleFloat() s_singlefloat.const = r_singlefloat(0.0) assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
def detect_floatformat(): from rpython.rtyper.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
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)
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 rpython.rlib.rarithmetic import r_singlefloat floatval = self.space.float_w(w_obj) singlefloatval = r_singlefloat(floatval) self.handle_singlefloat(w_ffitype, w_obj, singlefloatval)
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 rpython.rlib.rarithmetic import r_singlefloat floatval = self.space.float_w(w_obj, allow_conversion=False) singlefloatval = r_singlefloat(floatval) self.handle_singlefloat(w_ffitype, w_obj, singlefloatval)
def primitiveAtPut(interp, s_frame, words, index0, w_float): value = interp.space.unwrap_float(w_float) try: words[index0] = r_uint(singlefloat2uint(r_singlefloat(value))) except IndexError: raise PrimitiveFailedError return w_float
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)
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 rpython.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(20)] singlefloats = [ r_singlefloat(random.random() - 0.5) for i in range(20) ] ints = [random.randrange(-99, 99) for i in range(20)] for repeat in range(100): args = [] argvalues = [] argslist = [] local_floats = list(floats) local_singlefloats = list(singlefloats) local_ints = list(ints) for i in range(random.randrange(4, 20)): case = random.randrange(0, 6) if case & 1: boxme = InputArgInt else: boxme = ConstInt if case < 2: args.append(F) arg = arg1 = local_floats.pop() if case & 1: boxme = boxfloat else: boxme = constfloat elif case < 4: args.append(S) arg = local_singlefloats.pop() arg1 = longlong.singlefloat2int(arg) else: args.append(I) arg = arg1 = local_ints.pop() argslist.append(boxme(arg1)) 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_F, [funcbox] + argslist, 'float', descr=calldescr) expected = func(*argvalues) res = longlong.getrealfloat(res) assert abs(res - expected) < 0.0001
def __init__(self, space, default): self.valid_default = False try: fval = float(rfloat.rstring_to_float(default)) self.valid_default = True except Exception: fval = float(0.) self.default = rffi.cast(rffi.FLOAT, r_singlefloat(fval))
def test_single_float_args(self): """ RPY_EXPORTED 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
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 rpython.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(20)] singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(20)] ints = [random.randrange(-99, 99) for i in range(20)] for repeat in range(100): args = [] argvalues = [] argslist = [] local_floats = list(floats) local_singlefloats = list(singlefloats) local_ints = list(ints) for i in range(random.randrange(4, 20)): case = random.randrange(0, 6) if case & 1: boxme = InputArgInt else: boxme = ConstInt if case < 2: args.append(F) arg = arg1 = local_floats.pop() if case & 1: boxme = boxfloat else: boxme = constfloat elif case < 4: args.append(S) arg = local_singlefloats.pop() arg1 = longlong.singlefloat2int(arg) else: args.append(I) arg = arg1 = local_ints.pop() argslist.append(boxme(arg1)) 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_F, [funcbox] + argslist, 'float', descr=calldescr) expected = func(*argvalues) res = longlong.getrealfloat(res) assert abs(res - expected) < 0.0001
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
def test_gc_load_indexed(self): buf = struct.pack('dfi', 123.456, 123.456, 0x12345678) val = self.gc_load_from_string(rffi.DOUBLE, buf, 0) assert val == 123.456 # val = self.gc_load_from_string(rffi.FLOAT, buf, 8) assert val == r_singlefloat(123.456) # val = self.gc_load_from_string(rffi.INT, buf, 12) assert val == 0x12345678
def c_call_f(space, cppmethod, cppobject, nargs, cargs): args = [ _Arg(h=cppmethod), _Arg(h=cppobject), _Arg(l=nargs), _Arg(vp=cargs) ] return rffi.cast( rffi.FLOAT, r_singlefloat(space.float_w(call_capi(space, 'call_f', args))))
def pack_float(pack_obj, fmtdesc, count): for _ in xrange(count): value = pack_obj.space.float_w(pack_obj.pop_arg()) floatval = r_singlefloat(value) value = longlong2float.singlefloat2uint(floatval) value = widen(value) for i in range(fmtdesc.size): pack_obj.result.append(chr(value & 0xff)) value >>= 8
def pack_float(fmtiter): doubleval = fmtiter.accept_float_arg() floatval = r_singlefloat(doubleval) if std.pack_fastpath(rffi.FLOAT)(fmtiter, floatval): return # slow path value = longlong2float.singlefloat2uint(floatval) value = widen(value) value = intmask(value) pack_float_to_buffer(fmtiter.wbuf, fmtiter.pos, value, 4, fmtiter.bigendian) fmtiter.advance(4)
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
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
def test_call_stubs_single_float(): from rpython.rlib.longlong2float import uint2singlefloat, singlefloat2uint from rpython.rlib.rarithmetic import r_singlefloat, intmask # c0 = GcCache(False) ARGS = [lltype.SingleFloat, lltype.SingleFloat, lltype.SingleFloat] RES = lltype.SingleFloat def f(a, b, c): a = float(a) b = float(b) c = float(c) x = a - (b / c) return r_singlefloat(x) fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f) descr2 = get_call_descr(c0, ARGS, RES) a = intmask(singlefloat2uint(r_singlefloat(-10.0))) b = intmask(singlefloat2uint(r_singlefloat(3.0))) c = intmask(singlefloat2uint(r_singlefloat(2.0))) res = descr2.call_stub_i(rffi.cast(lltype.Signed, fnptr), [a, b, c], [], []) assert float(uint2singlefloat(rffi.r_uint(res))) == -11.5
def _tick_slowpath(self, p_entry, subhash): if p_entry.subhashes[1] == subhash: n = self._swap(p_entry, 0) elif p_entry.subhashes[2] == subhash: n = self._swap(p_entry, 1) elif p_entry.subhashes[3] == subhash: n = self._swap(p_entry, 2) elif p_entry.subhashes[4] == subhash: n = self._swap(p_entry, 3) else: n = 4 while n > 0 and float(p_entry.times[n - 1]) == 0.0: n -= 1 p_entry.subhashes[n] = rffi.cast(rffi.USHORT, subhash) p_entry.times[n] = r_singlefloat(0.0) return n
def tick(self, hash, increment): p_entry = self.timetable[self._get_index(hash)] subhash = self._get_subhash(hash) # if p_entry.subhashes[0] == subhash: n = 0 else: n = self._tick_slowpath(p_entry, subhash) # counter = float(p_entry.times[n]) + increment if counter < 1.0: p_entry.times[n] = r_singlefloat(counter) return False else: # when the bound is reached, we immediately reset the value to 0.0 self.reset(hash) return True
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')
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')
def test_wrap(): def InputArgInt(a): i = IntFrontendOp(0) i.setint(a) return i def InputArgFloat(a): i = FloatFrontendOp(0) i.setfloatstorage(a) return i def InputArgRef(a): i = RefFrontendOp(0) i.setref_base(a) return i def boxfloat(x): return InputArgFloat(longlong.getfloatstorage(x)) def _is(box1, box2): return (box1.__class__ == box2.__class__ and box1.getvalue() == box2.getvalue()) p = lltype.malloc(lltype.GcStruct('S')) po = lltype.cast_opaque_ptr(llmemory.GCREF, p) assert _is(wrap(None, 42), InputArgInt(42)) assert _is(wrap(None, 42.5), boxfloat(42.5)) assert _is(wrap(None, p), InputArgRef(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 rpython.rlib.rarithmetic import r_longlong, r_ulonglong value = r_longlong(-sys.maxint*17) assert _is(wrap(None, value), InputArgFloat(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), InputArgFloat(value)) sfval = r_singlefloat(42.5) ival = longlong.singlefloat2int(sfval) assert _is(wrap(None, sfval), InputArgInt(ival)) assert _is(wrap(None, sfval, in_const_box=True), ConstInt(ival))
def test_annotation_to_lltype(): from rpython.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)") s_singlefloat = SomeSingleFloat() s_singlefloat.const = r_singlefloat(0.0) assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
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 rpython.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))
def c_call_f(space, cppmethod, cppobject, nargs, cargs): args = [_Arg(h=cppmethod), _Arg(h=cppobject), _Arg(l=nargs), _Arg(vp=cargs)] return rffi.cast(rffi.FLOAT, r_singlefloat(space.float_w(call_capi(space, 'call_f', args))))
def f(x): a = r_singlefloat(x) b = r_singlefloat(x+1) return a == b
def test_r_singlefloat(): x = r_singlefloat(2.5) # exact number assert float(x) == 2.5 x = r_singlefloat(2.1) # approximate number, bits are lost assert float(x) != 2.1 assert abs(float(x) - 2.1) < 1E-6
def jit_ffi_call_impl_singlefloat(cif_description, func_addr, exchange_buffer): jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer) return r_singlefloat(-1.0)
def __init__(self, space, default): if default: fval = float(rfloat.rstring_to_float(default)) else: fval = float(0.) self.default = r_singlefloat(fval)
def test_compiled_single(): fn2 = compile(fnsingle, [float]) for x in enum_floats(): res = fn2(x) assert repr(res) == repr(float(r_singlefloat(x)))
from rpython.rtyper import rtyper from rpython.rtyper.rmodel import inputconst from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat from rpython.rlib.debug import ll_assert from rpython.rtyper.llannotation import lltype_to_annotation from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator from rpython.tool.sourcetools import func_with_new_name PrimitiveErrorValue = { lltype.Signed: -1, lltype.Unsigned: r_uint(-1), lltype.SignedLongLong: r_longlong(-1), lltype.UnsignedLongLong: r_ulonglong(-1), lltype.Float: -1.0, lltype.SingleFloat: r_singlefloat(-1.0), lltype.LongFloat: r_longfloat(-1.0), lltype.Char: chr(255), lltype.UniChar: unichr(0xFFFF), # XXX is this always right? lltype.Bool: True, llmemory.Address: llmemory.NULL, lltype.Void: None } for TYPE in rffi.NUMBER_TYPES: PrimitiveErrorValue[TYPE] = lltype.cast_primitive(TYPE, -1) del TYPE def error_value(T): if isinstance(T, lltype.Primitive):
def test_float_and_double(self): space = self.space self.check(app_types.float, space.wrap(12.34), r_singlefloat(12.34)) self.check(app_types.double, space.wrap(12.34), 12.34)
def fnsingle(f1): sf1 = r_singlefloat(f1) ii = singlefloat2uint(sf1) sf2 = uint2singlefloat(ii) f2 = float(sf2) return f2
def test_simple_call_singlefloat(self, **kwds): kwds.setdefault('supports_singlefloats', True) self._run([types.float] * 2, types.float, [r_singlefloat(10.5), r_singlefloat(31.5)], r_singlefloat(-4.5), **kwds)
def _unwrap_object(self, space, w_obj): return r_singlefloat(space.float_w(w_obj))
def test_int_as_singlefloat(): for x in enum_floats(): res = fnsingle(x) assert repr(res) == repr(float(r_singlefloat(x)))