def test_float_field(self): if not self.cpu.supports_floats: py.test.skip('requires floats') t_box, T_box, _ = self.alloc_instance(self.TFloat) floatdescr = self.cpu.fielddescrof(self.SFloat, 'float') self.execute_operation(rop.SETFIELD_GC, [t_box, boxfloat(3.4)], 'void', descr=floatdescr) res = self.execute_operation(rop.GETFIELD_GC_F, [t_box], 'float', descr=floatdescr) assert longlong.getrealfloat(res) == 3.4 # self.execute_operation(rop.SETFIELD_GC, [t_box, constfloat(-3.6)], 'void', descr=floatdescr) res = self.execute_operation(rop.GETFIELD_GC_F, [t_box], 'float', descr=floatdescr) assert longlong.getrealfloat(res) == -3.6
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_shadowstack_collecting_call_float(self): cpu = self.cpu def float_return(i, f): # mark frame for write barrier frame = rffi.cast(lltype.Ptr(JITFRAME), i) frame.hdr |= 1 return 1.2 + f FUNC = lltype.FuncType([lltype.Signed, lltype.Float], lltype.Float) fptr = llhelper(lltype.Ptr(FUNC), float_return) calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL) loop = self.parse(""" [f0] i = force_token() f1 = call(ConstClass(fptr), i, f0, descr=calldescr) finish(f1, descr=finaldescr) """, namespace={'fptr': fptr, 'calldescr': calldescr, 'finaldescr': BasicFinalDescr(1)}) token = JitCellToken() cpu.gc_ll_descr.init_nursery(20) cpu.setup_once() cpu.compile_loop(loop.inputargs, loop.operations, token) arg = longlong.getfloatstorage(2.3) frame = cpu.execute_token(token, arg) ofs = cpu.get_baseofs_of_frame_field() f = cpu.read_float_at_mem(frame, ofs) f = longlong.getrealfloat(f) assert f == 2.3 + 1.2
def test_shadowstack_collecting_call_float(self): cpu = self.cpu def float_return(i, f): # mark frame for write barrier frame = rffi.cast(lltype.Ptr(JITFRAME), i) frame.hdr |= 1 return 1.2 + f FUNC = lltype.FuncType([lltype.Signed, lltype.Float], lltype.Float) fptr = llhelper(lltype.Ptr(FUNC), float_return) calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL) loop = self.parse(""" [f0] i = force_token() f1 = call_f(ConstClass(fptr), i, f0, descr=calldescr) finish(f1, descr=finaldescr) """, namespace={'fptr': fptr, 'calldescr': calldescr, 'finaldescr': BasicFinalDescr(1)}) token = JitCellToken() cpu.gc_ll_descr.init_nursery(20) cpu.setup_once() cpu.compile_loop(loop.inputargs, loop.operations, token) arg = longlong.getfloatstorage(2.3) frame = cpu.execute_token(token, arg) ofs = cpu.get_baseofs_of_frame_field() f = cpu.read_float_at_mem(frame, ofs) f = longlong.getrealfloat(f) assert f == 2.3 + 1.2
def test_vstm_vldm_combined(self): n = 14 source_container = lltype.malloc(lltype.Array(longlong.FLOATSTORAGE, hints={'nolength': True}), n, flavor='raw') target_container = lltype.malloc(lltype.Array(longlong.FLOATSTORAGE, hints={'nolength': True}), n, flavor='raw') for x in range(n): source_container[x] = longlong.getfloatstorage( float("%d.%d" % (x, x))) self.a.gen_func_prolog() self.a.mc.gen_load_int(r.ip.value, rffi.cast(lltype.Signed, source_container)) self.a.mc.VLDM(r.ip.value, [x for x in range(n)]) self.a.mc.gen_load_int(r.ip.value, rffi.cast(lltype.Signed, target_container)) self.a.mc.VSTM(r.ip.value, [x for x in range(n)]) self.a.gen_func_epilog() run_asm(self.a) for d in range(n): res = longlong.getrealfloat(target_container[0]) == float("%d.%d" % (d, d)) lltype.free(source_container, flavor='raw') lltype.free(target_container, flavor='raw')
def test_float_ops(): cpu = FakeCPU() for opnum, boxargs, rettype, retvalue in get_float_tests(cpu): res = _execute_arglist(cpu, None, opnum, boxargs) if rettype == 'float': res = longlong.getrealfloat(res) assert res == retvalue
def cast_from_floatstorage(TYPE, x): assert isinstance(x, longlong.r_float_storage) if TYPE is lltype.Float: return longlong.getrealfloat(x) if longlong.is_longlong(TYPE): return rffi.cast(TYPE, x) raise TypeError(TYPE)
def test_call_aligned_with_spilled_values(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): local_floats = list(floats) local_ints = list(ints) args = [] spills = [] funcargs = [] float_count = 0 int_count = 0 for i in range(8): if case & (1<<i): args.append('f%d' % float_count) spills.append('force_spill(f%d)' % float_count) float_count += 1 funcargs.append(F) else: args.append('i%d' % int_count) spills.append('force_spill(i%d)' % int_count) int_count += 1 funcargs.append(I) arguments = ', '.join(args) spill_ops = '\n'.join(spills) FUNC = self.FuncType(funcargs, 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) ops = '[%s]\n' % arguments ops += '%s\n' % spill_ops ops += 'f99 = call_f(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments ops += 'i99 = same_as_i(0)\n' ops += 'guard_true(i99) [f99, %s]\n' % arguments ops += 'finish()\n' loop = parse(ops, namespace=locals()) looptoken = JitCellToken() self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) deadframe = self.cpu.execute_token(looptoken, *argvals) x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0)) assert abs(x - expected_result) < 0.0001
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_call_aligned_with_spilled_values(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): local_floats = list(floats) local_ints = list(ints) args = [] spills = [] funcargs = [] float_count = 0 int_count = 0 for i in range(8): if case & (1 << i): args.append('f%d' % float_count) spills.append('force_spill(f%d)' % float_count) float_count += 1 funcargs.append(F) else: args.append('i%d' % int_count) spills.append('force_spill(i%d)' % int_count) int_count += 1 funcargs.append(I) arguments = ', '.join(args) spill_ops = '\n'.join(spills) FUNC = self.FuncType(funcargs, 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) ops = '[%s]\n' % arguments ops += '%s\n' % spill_ops ops += 'f99 = call(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments ops += 'i99 = same_as(0)\n' ops += 'guard_true(i99) [f99, %s]\n' % arguments ops += 'finish()\n' loop = parse(ops, namespace=locals()) looptoken = JitCellToken() self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) deadframe = self.cpu.execute_token(looptoken, *argvals) x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0)) assert abs(x - expected_result) < 0.0001
def test_label_float_in_reg_and_on_stack(self): targettoken = TargetToken() ops = """ [i0, f3] i2 = same_as_i(i0) # but forced to be in a register force_spill(i2) force_spill(f3) f4 = float_add(f3, 5.0) label(f3, f4, descr=targettoken) force_spill(f3) f5 = same_as_f(f3) # but forced to be in a register finish(f5) """ faildescr = BasicFailDescr(2) loop = parse(ops, self.cpu, namespace=locals()) looptoken = JitCellToken() info = self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) ops2 = """ [i0, f1] i1 = same_as_i(i0) f2 = same_as_f(f1) f3 = float_add(f1, 10.0) force_spill(f3) force_spill(i1) f4 = float_add(f3, f1) jump(f3, f4, descr=targettoken) """ loop2 = parse(ops2, self.cpu, namespace=locals()) looptoken2 = JitCellToken() info = self.cpu.compile_loop(loop2.inputargs, loop2.operations, looptoken2) deadframe = self.cpu.execute_token(looptoken, -9, longlong.getfloatstorage(-13.5)) res = longlong.getrealfloat(self.cpu.get_float_value(deadframe, 0)) assert res == -13.5 # deadframe = self.cpu.execute_token(looptoken2, -9, longlong.getfloatstorage(-13.5)) res = longlong.getrealfloat(self.cpu.get_float_value(deadframe, 0)) assert res == -3.5
def test_execute_varargs(): cpu = FakeCPU() descr = FakeCallDescr() argboxes = [InputArgInt(99999), InputArgInt(321), constfloat(2.25), ConstInt(123), InputArgRef(), boxfloat(5.5)] box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, descr) assert longlong.getrealfloat(box) == 42.5 assert cpu.fakecalled == (99999, [321, 123], [ConstPtr.value], [longlong.getfloatstorage(2.25), longlong.getfloatstorage(5.5)], descr)
def interp_operations(self, f, args, **kwds): # get the JitCodes for the function f _get_jitcodes(self, self.CPUClass, f, args, **kwds) # try to run it with blackhole.py result1 = _run_with_blackhole(self, args) # try to run it with pyjitpl.py result2 = _run_with_pyjitpl(self, args) assert result1 == result2 or isnan(result1) and isnan(result2) # try to run it by running the code compiled just before result3 = _run_with_machine_code(self, args) assert result1 == result3 or result3 == NotImplemented or isnan(result1) and isnan(result3) # if longlong.supports_longlong and isinstance(result1, longlong.r_float_storage): result1 = longlong.getrealfloat(result1) return result1
def test_vldm(self): n = 14 container = lltype.malloc(lltype.Array(longlong.FLOATSTORAGE, hints={'nolength': True}), n, flavor='raw') for x in range(n): container[x] = longlong.getfloatstorage(float("%d.%d" % (x,x))) self.a.gen_func_prolog() self.a.mc.gen_load_int(r.ip.value, rffi.cast(lltype.Signed, container)) self.a.mc.VLDM(r.ip.value, [x for x in range(n)]) for x in range(1, n): self.a.mc.VADD(0, 0, x) self.a.mc.VSTR(r.d0.value, r.ip.value) self.a.gen_func_epilog() res = run_asm(self.a) assert longlong.getrealfloat(container[0]) == sum([float("%d.%d" % (d,d)) for d in range(n)]) lltype.free(container, flavor='raw')
def interp_operations(self, f, args, **kwds): # get the JitCodes for the function f stats = _get_jitcodes(self, self.CPUClass, f, args, **kwds) # try to run it with blackhole.py result1 = _run_with_blackhole(self, args) # try to run it with pyjitpl.py result2 = _run_with_pyjitpl(self, args, stats) assert result1 == result2 or isnan(result1) and isnan(result2) # try to run it by running the code compiled just before df, result3 = _run_with_machine_code(self, args) self._lastframe = df assert result1 == result3 or result3 == NotImplemented or isnan(result1) and isnan(result3) # if (longlong.supports_longlong and isinstance(result1, longlong.r_float_storage)): result1 = longlong.getrealfloat(result1) return result1
def test_call_stubs_2(): c0 = GcCache(False) ARRAY = lltype.GcArray(lltype.Signed) ARGS = [lltype.Float, lltype.Ptr(ARRAY)] RES = lltype.Float def f2(a, b): return float(b[0]) + a fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f2) descr2 = get_call_descr(c0, ARGS, RES) a = lltype.malloc(ARRAY, 3) opaquea = lltype.cast_opaque_ptr(llmemory.GCREF, a) a[0] = 1 res = descr2.call_stub_f(rffi.cast(lltype.Signed, fnptr), [], [opaquea], [longlong.getfloatstorage(3.5)]) assert longlong.getrealfloat(res) == 4.5
def test_execute_varargs(): cpu = FakeCPU() descr = FakeCallDescr() argboxes = [ InputArgInt(99999), InputArgInt(321), constfloat(2.25), ConstInt(123), InputArgRef(), boxfloat(5.5) ] box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, descr) assert longlong.getrealfloat(box) == 42.5 assert cpu.fakecalled == (99999, [321, 123], [ConstPtr.value], [ longlong.getfloatstorage(2.25), longlong.getfloatstorage(5.5) ], descr)
def test_vstm_vldm_combined(self): n = 14 source_container = lltype.malloc(lltype.Array(longlong.FLOATSTORAGE, hints={'nolength': True}), n, flavor='raw') target_container = lltype.malloc(lltype.Array(longlong.FLOATSTORAGE, hints={'nolength': True}), n, flavor='raw') for x in range(n): source_container[x] = longlong.getfloatstorage(float("%d.%d" % (x,x))) self.a.gen_func_prolog() self.a.mc.gen_load_int(r.ip.value, rffi.cast(lltype.Signed, source_container)) self.a.mc.VLDM(r.ip.value, [x for x in range(n)]) self.a.mc.gen_load_int(r.ip.value, rffi.cast(lltype.Signed, target_container)) self.a.mc.VSTM(r.ip.value, [x for x in range(n)]) self.a.gen_func_epilog() run_asm(self.a) for d in range(n): res = longlong.getrealfloat(target_container[0]) == float("%d.%d" % (d,d)) lltype.free(source_container, flavor='raw') lltype.free(target_container, flavor='raw')
def test_call_aligned_with_args_on_the_stack(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): result = 0.0 args = [] argslist = [] local_floats = list(floats) local_ints = list(ints) for i in range(8): if case & (1<<i): args.append(F) arg = local_floats.pop() result += arg argslist.append(boxfloat(arg)) else: args.append(I) arg = local_ints.pop() result += arg argslist.append(InputArgInt(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) res = longlong.getrealfloat(res) assert abs(res - result) < 0.0001
def specialize_value(TYPE, x): """'x' must be a Signed, a GCREF or a FLOATSTORAGE. This function casts it to a more specialized type, like Char or Ptr(..). """ INPUT = lltype.typeOf(x) if INPUT is lltype.Signed: if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'raw': # non-gc pointer return rffi.cast(TYPE, x) elif TYPE is lltype.SingleFloat: return longlong.int2singlefloat(x) else: return lltype.cast_primitive(TYPE, x) elif INPUT is longlong.FLOATSTORAGE: if longlong.is_longlong(TYPE): return rffi.cast(TYPE, x) assert TYPE is lltype.Float return longlong.getrealfloat(x) else: return lltype.cast_opaque_ptr(TYPE, x)
def test_call_aligned_with_args_on_the_stack(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') def func(*args): return float(sum(args)) F = lltype.Float I = lltype.Signed floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 13, -42, 1111, 95, 1] for case in range(256): result = 0.0 args = [] argslist = [] local_floats = list(floats) local_ints = list(ints) for i in range(8): if case & (1 << i): args.append(F) arg = local_floats.pop() result += arg argslist.append(boxfloat(arg)) else: args.append(I) arg = local_ints.pop() result += arg argslist.append(InputArgInt(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) res = longlong.getrealfloat(res) assert abs(res - result) < 0.0001
def test_execute_nonspec(): cpu = FakeCPU() descr = FakeDescr() # cases with a descr # arity == -1 argboxes = [InputArgInt(321), ConstInt(123)] box = _execute_arglist(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, FakeCallDescr()) assert longlong.getrealfloat(box) == 42.5 # arity == 0 box = _execute_arglist(cpu, None, rop.NEW, [], descr) assert box.fakeargs == ('new', descr) # arity == 1 box1 = InputArgRef() box = _execute_arglist(cpu, None, rop.ARRAYLEN_GC, [box1], descr) assert box == 55 # arity == 2 box2 = boxfloat(222.2) fielddescr = FakeFieldDescr() _execute_arglist(cpu, None, rop.SETFIELD_GC, [box1, box2], fielddescr) assert cpu.fakesetfield == (box1.getref_base(), box2.getfloatstorage(), fielddescr) # arity == 3 box3 = InputArgInt(33) arraydescr = FakeArrayDescr() _execute_arglist(cpu, None, rop.SETARRAYITEM_GC, [box1, box3, box2], arraydescr) assert cpu.fakesetarrayitem == (box1.getref_base(), box3.getint(), box2.getfloatstorage(), arraydescr) # cases without descr # arity == 1 box = _execute_arglist(cpu, None, rop.INT_INVERT, [box3]) assert box == ~33 # arity == 2 box = _execute_arglist(cpu, None, rop.INT_LSHIFT, [box3, InputArgInt(3)]) assert box == 33 << 3 # arity == 3 _execute_arglist(cpu, None, rop.STRSETITEM, [box1, InputArgInt(3), box3]) assert cpu.fakestrsetitem == (box1.getref_base(), 3, box3.getint())
def test_call_alignment_call_assembler(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') fdescr3 = BasicFinalDescr(3) fdescr4 = BasicFinalDescr(4) def assembler_helper(failindex, virtualizable): assert 0, 'should not be called, but was with failindex (%d)' % failindex return 13 FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF], lltype.Signed)) class FakeJitDriverSD: index_of_virtualizable = -1 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper) assembler_helper_adr = llmemory.cast_ptr_to_adr( _assembler_helper_ptr) floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 42, -42, 1111, 95, 1] for case in range(256): float_count = 0 int_count = 0 args = [] called_ops = '' total_index = -1 for i in range(8): if case & (1<<i): args.append('f%d' % float_count) else: args.append('i%d' % int_count) called_ops += 'f%d = cast_int_to_float(i%d)\n' % ( float_count, int_count) int_count += 1 if total_index == -1: total_index = float_count float_count += 1 else: called_ops += 'f%d = float_add(f%d, f%d)\n' % ( float_count + 1, total_index, float_count) total_index = float_count + 1 float_count += 2 arguments = ', '.join(args) called_ops = '[%s]\n' % arguments + called_ops called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index # compile called loop called_loop = parse(called_ops, namespace=locals()) called_looptoken = JitCellToken() called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD() done_descr = called_loop.operations[-1].getdescr() self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) deadframe = cpu.execute_token(called_looptoken, *argvals) assert cpu.get_latest_descr(deadframe) == fdescr3 t = longlong.getrealfloat(cpu.get_float_value(deadframe, 0)) assert abs(t - expected_result) < 0.0001 ARGS = [] RES = lltype.Float for x in args: if x[0] == 'f': ARGS.append(lltype.Float) else: ARGS.append(lltype.Signed) FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof( lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES, EffectInfo.MOST_GENERAL) ops = ''' [%s] f99 = call_assembler_f(%s, descr=called_looptoken) guard_not_forced()[] finish(f99, descr=fdescr4) ''' % (arguments, arguments) loop = parse(ops, namespace=locals()) # we want to take the fast path self.cpu.done_with_this_frame_descr_float = done_descr try: othertoken = JitCellToken() self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken) # prepare call to called_loop argvals, _ = self._prepare_args(args, floats, ints) deadframe = cpu.execute_token(othertoken, *argvals) x = longlong.getrealfloat( cpu.get_float_value(deadframe, 0)) assert cpu.get_latest_descr(deadframe) == fdescr4 assert abs(x - expected_result) < 0.0001 finally: del self.cpu.done_with_this_frame_descr_float
def getfloat(v): return longlong.getrealfloat(getfloatstorage(v))
def test_functions(): xll = longlong.getfloatstorage(3.5) assert longlong.getrealfloat(xll) == 3.5 assert is_valid_int(longlong.gethash(xll))
def getfloat(self): return longlong.getrealfloat(self.getfloatstorage())
def getfloat(self): return longlong.getrealfloat(self.value)
def __repr__(self): floatvalue = longlong.getrealfloat(self.aslonglong) return '<FloatImmedLoc(%s)>' % (floatvalue,)
def getfloats(self, end): return [ longlong.getrealfloat( self.cpu.get_float_value(self.deadframe, index)) for index in range(0, end) ]
def getfloats(self, end): return [longlong.getrealfloat( self.cpu.get_float_value(self.deadframe, index)) for index in range(0, end)]
def test_call_alignment_call_assembler(self): cpu = self.cpu if not cpu.supports_floats: py.test.skip('requires floats') fdescr3 = BasicFinalDescr(3) fdescr4 = BasicFinalDescr(4) def assembler_helper(failindex, virtualizable): assert 0, 'should not be called, but was with failindex (%d)' % failindex return 13 FUNCPTR = lltype.Ptr( lltype.FuncType([lltype.Signed, llmemory.GCREF], lltype.Signed)) class FakeJitDriverSD: index_of_virtualizable = -1 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper) assembler_helper_adr = llmemory.cast_ptr_to_adr( _assembler_helper_ptr) floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56] ints = [7, 11, 23, 42, -42, 1111, 95, 1] for case in range(256): float_count = 0 int_count = 0 args = [] called_ops = '' total_index = -1 for i in range(8): if case & (1 << i): args.append('f%d' % float_count) else: args.append('i%d' % int_count) called_ops += 'f%d = cast_int_to_float(i%d)\n' % ( float_count, int_count) int_count += 1 if total_index == -1: total_index = float_count float_count += 1 else: called_ops += 'f%d = float_add(f%d, f%d)\n' % ( float_count + 1, total_index, float_count) total_index = float_count + 1 float_count += 2 arguments = ', '.join(args) called_ops = '[%s]\n' % arguments + called_ops called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index # compile called loop called_loop = parse(called_ops, namespace=locals()) called_looptoken = JitCellToken() called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD() done_descr = called_loop.operations[-1].getdescr() self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken) argvals, expected_result = self._prepare_args(args, floats, ints) deadframe = cpu.execute_token(called_looptoken, *argvals) assert cpu.get_latest_descr(deadframe) == fdescr3 t = longlong.getrealfloat(cpu.get_float_value(deadframe, 0)) assert abs(t - expected_result) < 0.0001 ARGS = [] RES = lltype.Float for x in args: if x[0] == 'f': ARGS.append(lltype.Float) else: ARGS.append(lltype.Signed) FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof( lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES, EffectInfo.MOST_GENERAL) ops = ''' [%s] f99 = call_assembler(%s, descr=called_looptoken) guard_not_forced()[] finish(f99, descr=fdescr4) ''' % (arguments, arguments) loop = parse(ops, namespace=locals()) # we want to take the fast path self.cpu.done_with_this_frame_descr_float = done_descr try: othertoken = JitCellToken() self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken) # prepare call to called_loop argvals, _ = self._prepare_args(args, floats, ints) deadframe = cpu.execute_token(othertoken, *argvals) x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0)) assert cpu.get_latest_descr(deadframe) == fdescr4 assert abs(x - expected_result) < 0.0001 finally: del self.cpu.done_with_this_frame_descr_float