From 2e45d7267df9d412a0f25b431142951f88a4bc36 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 17 Sep 2014 16:22:00 +0800 Subject: [PATCH] py2llvm/fractions: fix round() --- artiq/py2llvm/fractions.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/artiq/py2llvm/fractions.py b/artiq/py2llvm/fractions.py index e4c4c2bff..361bb8f79 100644 --- a/artiq/py2llvm/fractions.py +++ b/artiq/py2llvm/fractions.py @@ -129,8 +129,28 @@ class VFraction(VGeneric): else: r = VInt(64) a, b = self._nd(builder) - h_b = builder.ashr(b, lc.Constant.int(lc.Type.int(), 1)) - a = builder.add(a, h_b) + h_b = builder.ashr(b, lc.Constant.int(lc.Type.int(64), 1)) + + function = builder.basic_block.function + add_block = function.append_basic_block("fr_add") + sub_block = function.append_basic_block("fr_sub") + merge_block = function.append_basic_block("fr_merge") + + condition = builder.icmp( + lc.ICMP_SLT, a, lc.Constant.int(lc.Type.int(64), 0)) + builder.cbranch(condition, sub_block, add_block) + + builder.position_at_end(add_block) + a_add = builder.add(a, h_b) + builder.branch(merge_block) + builder.position_at_end(sub_block) + a_sub = builder.sub(a, h_b) + builder.branch(merge_block) + + builder.position_at_end(merge_block) + a = builder.phi(lc.Type.int(64)) + a.add_incoming(a_add, add_block) + a.add_incoming(a_sub, sub_block) r.auto_store(builder, builder.sdiv(a, b)) return r.o_intx(target_bits, builder)