compiler: in codegen for delay(), round fp instead of truncating.

Consider delay(8*us). It results in the following computation...
  >>> 8*1e-06/1e-09
  7999.999999999999
with the result promptly getting truncated to 7999.

Fixes #706.
This commit is contained in:
whitequark 2017-04-21 17:36:44 +00:00
parent a820ae98cf
commit ed2b10c5aa
2 changed files with 19 additions and 1 deletions

View File

@ -1717,7 +1717,7 @@ class ARTIQIRGenerator(algorithm.Visitor):
if len(node.args) == 1 and len(node.keywords) == 0:
arg = self.visit(node.args[0])
arg_mu_float = self.append(ir.Arith(ast.Div(loc=None), arg, self.ref_period))
arg_mu = self.append(ir.Coerce(arg_mu_float, builtins.TInt64()))
arg_mu = self.append(ir.Builtin("round", [arg_mu_float], builtins.TInt64()))
return self.append(ir.Builtin("delay_mu", [arg_mu], builtins.TNone()))
else:
assert False

View File

@ -314,6 +314,19 @@ class Handover(EnvExperiment):
self.k("t2")
class Rounding(EnvExperiment):
def build(self):
self.setattr_device("core")
@kernel
def run(self):
self.core.reset()
t1 = now_mu()
delay(8*us)
t2 = now_mu()
self.set_dataset("delta", t2 - t1)
class DummyException(Exception):
pass
@ -434,6 +447,11 @@ class CoredeviceTest(ExperimentCase):
self.assertEqual(self.dataset_mgr.get("t1") + 1234,
self.dataset_mgr.get("t2"))
def test_rounding(self):
self.execute(Rounding)
dt = self.dataset_mgr.get("delta")
self.assertEqual(dt, 8000)
class RPCTiming(EnvExperiment):
def build(self, repeats=100):