clamp ldexp exponent to i16

This commit is contained in:
Ralf Jung 2019-08-10 11:27:27 +02:00
parent 3ae01a64bc
commit 0743ed631e

View file

@ -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)?;
}