Skip to content

Commit

Permalink
Use the upstream exact_div implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 24, 2019
1 parent 2e17933 commit 2152ca7
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_f64(res), dest)?;
}

"exact_div" => {
// Performs an exact division, resulting in undefined behavior where
// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
let a = this.read_immediate(args[0])?;
let b = this.read_immediate(args[1])?;
// check x % y != 0
if this.overflowing_binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
// Check if `b` is -1, which is the "min_value / -1" case.
let minus1 = Scalar::from_int(-1, dest.layout.size);
return Err(if b.to_scalar().unwrap() == minus1 {
err_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented")
} else {
err_ub_format!("exact_div: {:?} cannot be divided by {:?} without remainder", *a, *b)
}.into());
}
this.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?;
},
"exact_div" => this.exact_div(
this.read_immediate(args[0])?,
this.read_immediate(args[1])?,
dest,
)?,

"forget" => {}

Expand Down

0 comments on commit 2152ca7

Please sign in to comment.