Skip to content

Commit

Permalink
clamp ldexp exponent to i16
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 10, 2019
1 parent 3ae01a6 commit 0743ed6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"_ldexp" | "ldexp" | "scalbn" => {
let x = this.read_scalar(args[0])?.to_f64()?;
let exp = this.read_scalar(args[1])?.to_i32()?;
let res = x.scalbn(exp.try_into().unwrap());
// Saturating cast to i16. Even those are outside the valid exponent range to
// `scalbn` below will to its over/underflow handling.
let exp = if exp > i16::max_value() as i32 {
i16::max_value()
} else if exp < i16::min_value() as i32 {
i16::min_value()
} else {
exp.try_into().unwrap()
};
let res = x.scalbn(exp);
this.write_scalar(Scalar::from_f64(res), dest)?;
}

Expand Down

0 comments on commit 0743ed6

Please sign in to comment.