Skip to content

Commit

Permalink
Only handle BinOp::Mul in codegen_i128::maybe_codegen_mul_checked
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Jan 9, 2025
1 parent ec96e02 commit 8fd8b2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
39 changes: 16 additions & 23 deletions src/codegen_i128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ pub(crate) fn maybe_codegen<'tcx>(
}
}

pub(crate) fn maybe_codegen_checked<'tcx>(
pub(crate) fn maybe_codegen_mul_checked<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
bin_op: BinOp,
lhs: CValue<'tcx>,
rhs: CValue<'tcx>,
) -> Option<CValue<'tcx>> {
Expand All @@ -78,25 +77,19 @@ pub(crate) fn maybe_codegen_checked<'tcx>(

let is_signed = type_sign(lhs.layout().ty);

match bin_op {
BinOp::Add | BinOp::Sub => None,
BinOp::Mul => {
let out_ty = Ty::new_tup(fx.tcx, &[lhs.layout().ty, fx.tcx.types.bool]);
let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
let param_types = vec![
AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
AbiParam::new(types::I128),
AbiParam::new(types::I128),
];
let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)];
fx.lib_call(
if is_signed { "__rust_i128_mulo" } else { "__rust_u128_mulo" },
param_types,
vec![],
&args,
);
Some(out_place.to_cvalue(fx))
}
_ => bug!("binop {:?} on checked int/uint lhs: {:?} rhs: {:?}", bin_op, lhs, rhs),
}
let out_ty = Ty::new_tup(fx.tcx, &[lhs.layout().ty, fx.tcx.types.bool]);
let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
let param_types = vec![
AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
AbiParam::new(types::I128),
AbiParam::new(types::I128),
];
let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)];
fx.lib_call(
if is_signed { "__rust_i128_mulo" } else { "__rust_u128_mulo" },
param_types,
vec![],
&args,
);
Some(out_place.to_cvalue(fx))
}
8 changes: 4 additions & 4 deletions src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
let lhs = in_lhs.load_scalar(fx);
let rhs = in_rhs.load_scalar(fx);

if let Some(res) = crate::codegen_i128::maybe_codegen_checked(fx, bin_op, in_lhs, in_rhs) {
return res;
}

let signed = type_sign(in_lhs.layout().ty);

let (res, has_overflow) = match bin_op {
Expand Down Expand Up @@ -236,6 +232,10 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
(val, has_overflow)
}
BinOp::Mul => {
if let Some(res) = crate::codegen_i128::maybe_codegen_mul_checked(fx, in_lhs, in_rhs) {
return res;
}

let ty = fx.bcx.func.dfg.value_type(lhs);
match ty {
types::I8 | types::I16 | types::I32 if !signed => {
Expand Down

0 comments on commit 8fd8b2d

Please sign in to comment.