Revert removal of round and round64 #352

Merged
sb10q merged 3 commits from issue-149 into master 2023-11-04 13:35:53 +08:00
1 changed files with 2 additions and 38 deletions
Showing only changes of commit e3e874c54c - Show all commits

View File

@ -1300,49 +1300,13 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
&[(float, "x")], &[(float, "x")],
"llvm.sqrt.f64", "llvm.sqrt.f64",
), ),
create_fn_by_codegen( create_fn_by_intrinsic(
primitives, primitives,
&var_map, &var_map,
"rint", "rint",
float, float,
&[(float, "x")], &[(float, "x")],
Box::new(|ctx, _, fun, args, generator| { "llvm.roundeven.f64",
let float = ctx.primitives.float;
let llvm_f64 = ctx.ctx.f64_type();
let x_ty = fun.0.args[0].ty;
let x_val = args[0].1.clone()
.to_basic_value_enum(ctx, generator, x_ty)?;
assert!(ctx.unifier.unioned(x_ty, float));
let intrinsic_fn = ctx.module.get_function("llvm.round.f64").unwrap_or_else(|| {
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
ctx.module.add_function("llvm.round.f64", fn_type, None)
});
// rint(x) == round(x * 0.5) * 2.0
// %0 = fmul f64 %x, 0.5
let x_half = ctx.builder
.build_float_mul(x_val.into_float_value(), llvm_f64.const_float(0.5), "");
// %1 = call f64 @llvm.round.f64(f64 %0)
let round = ctx.builder
.build_call(
intrinsic_fn,
&vec![x_half.into()],
"",
)
.try_as_basic_value()
.left()
.unwrap();
// %2 = fmul f64 %1, 2.0
let val = ctx.builder
.build_float_mul(round.into_float_value(), llvm_f64.const_float(2.0).into(), "rint");
Ok(Some(val.into()))
}),
), ),
create_fn_by_extern( create_fn_by_extern(
primitives, primitives,