nac3core: fix powi with negative integer power

This commit is contained in:
ychenfo 2022-04-02 03:26:15 +08:00
parent 4f66bdeda9
commit 0059b389ae
1 changed files with 28 additions and 2 deletions

View File

@ -957,10 +957,36 @@ pub fn gen_binop_expr<'ctx, 'a, G: CodeGenerator>(
ctx.module.add_function("llvm.powi.f64.i16", ty, None)
});
let right = ctx.builder.build_int_truncate(right.into_int_value(), i16_t, "r_pow");
ctx.builder
let is_neg = ctx.builder.build_int_compare(
inkwell::IntPredicate::SLT,
right,
i16_t.const_zero(),
"powi_pow_neg",
);
let abs_intr = ctx.module.get_function("llvm.abs.i16").unwrap_or_else(|| {
let ty = i16_t.fn_type(&[i16_t.into(), ctx.ctx.bool_type().into()], false);
ctx.module.add_function("llvm.abs.i16", ty, None)
});
let right = ctx
.builder
.build_call(
abs_intr,
&[right.into(), ctx.ctx.bool_type().const_int(0, false).into()],
"pow_abs",
)
.try_as_basic_value()
.unwrap_left();
let pow = ctx
.builder
.build_call(pow_intr, &[left.into(), right.into()], "f_pow_i")
.try_as_basic_value()
.unwrap_left()
.unwrap_left();
let reciprocal = ctx.builder.build_float_div(
ctx.ctx.f64_type().const_float(1.0),
pow.into_float_value(),
"recip",
);
ctx.builder.build_select(is_neg, reciprocal, pow.into_float_value(), "float_pow_i")
} else {
unimplemented!()
}